// Filename : output_writer.cpp
//
// This file was initially generated by SpaceStudio but is meant to be modified
// by the user.
#include "output_writer.h"
#include "spacecomp_pre_cpp.h"

#ifdef SPACE_ARCHITECTURE_IMPLEMENTATION
#include <string>
#include <iostream>
#endif

output_writer::output_writer(SPACECOMP_CTOR_PARAMS(output_writer, INDEX))
	: abstract_module(SPACECOMP_MBASE_ARGS(output_writer, INDEX))
	, SPACECOMP_START_ILIST(output_writer, INDEX)
	// Other initializers may go here
{
	SPACECOMP_THREAD(output_writer, INDEX, thread);
	request_stack(16 * 1024);
}

void output_writer::thread(SPACECOMP_THREAD_PARAMS(output_writer, INDEX, thread)) {
	spacecomp_thread_initialize();

	while (1) {
		const bool initializing = spacecomp_thread_loop_start();
		if (initializing) {}
		send_to_uart(read_fifo());
	}
}

uint8_t output_writer::read_fifo() {
	uint32_t data;
	ModuleRead(CONTROLLER0_ID, SPACE_BLOCKING, data);
    return (uint8_t)data;
}

void output_writer::send_to_uart(uint8_t data) {
#ifdef SPACE_ARCHITECTURE_IMPLEMENTATION
	char* strLong =  (char*)&data;
	std::cout << *strLong << std::flush;
#else
	SpacePrint("%c", data);
#endif
}

#include "spacecomp_post_cpp.h"
