// intellectual property is bullshit bgdc

#ifndef SERIAL_HPP
#define SERIAL_HPP

#include <stdbool.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>

// Generic serial interface class.
class serial
{
public:
	// Receive one byte. Returns EOF immediately if none is available. May
	// return one of this class's additional error values (FERR, PERR,
	// BUF_OVERFLOW). If the serial peripheral is configured to work with
	// larger units than a byte, the return value may exceed 255. If it is
	// necessary to manually clear the Receive Complete flag, the
	// implementation should do this here.
	virtual int recv() = 0;

	// Transmit one byte (or larger chunk, as above). Undefined behavior
	// for send(EOF).
	virtual void send(int b) = 0;

	// Set whether the Receive Complete interrupt is enabled
	virtual void rxcie(bool en) { (void) en; }

	// Set whether the Transmit Complete interrupt is enabled
	virtual void txcie(bool en) { (void) en; }

	// Set whether the Data Register Empty interrupt is enabled
	virtual void dreie(bool en) { (void) en; }

	virtual ~serial() = default;

	// Transmit a block of data.
	void send(uint8_t const * data, size_t len)
	{
		for (size_t i = 0; i < len; i++)
			send((int)(uint8_t) data[i]);
	}

	// Transmit a string.
	void send_str(char const * s)
	{
		for (size_t i = 0; s[i]; i++)
			send((int)(uint8_t) s[i]);
	}

	static constexpr int FERR = -2;
	static constexpr int PERR = -3;
	static constexpr int BUF_OVERFLOW = -4;
};

#endif // !defined(SERIAL_HPP)
