// intellectual property is bullshit bgdc

#ifndef SPI_HPP
#define SPI_HPP

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

// Generic SPI interface class. Generally assumes a non interrupt driven style
// interface.
class spi
{
public:
	virtual ~spi() = default;

	// Transfer one byte, returning the data received.
	virtual uint8_t transfer(uint8_t data) = 0;

	// Transfer a packet. Does not manipulate chip selects.
	//
	// data_out: data to send
	// data_in:  buffer to receive response. May equal data_out; may be null
	// length:   number of bytes to send
	//
	// Implementer: override only if you have a faster approach.
	virtual void transfer_block(
		uint8_t const * data_out,
		uint8_t       * data_in,
		size_t          length
	)
	{
		if (data_in)
		{
			for (size_t i = 0; i < length; i++)
				data_in[i] = transfer(data_out[i]);
		}
		else
		{
			for (size_t i = 0; i < length; i++)
				transfer(data_out[i]);
		}
	}
};

#endif // !defined(SPI_HPP)
