Refactor interface to be fully static

This commit is contained in:
BlackMark 2022-05-26 16:03:38 +02:00
parent 612f6a9fbc
commit 2cb38b9fdc

144
eink.hpp
View File

@ -7,11 +7,10 @@
#include "../clock.hpp" #include "../clock.hpp"
#include "../io/io.hpp" #include "../io/io.hpp"
template <typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin> namespace eink {
class Epd {
static constexpr auto width = uint16_t{200};
static constexpr auto height = uint16_t{200};
template <uint16_t Width, uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin>
class Eink {
struct Cmd { struct Cmd {
static constexpr auto SW_RESET = uint8_t{0x12}; static constexpr auto SW_RESET = uint8_t{0x12};
static constexpr auto DRIVER_OUTPUT_CONTROL = uint8_t{0x01}; static constexpr auto DRIVER_OUTPUT_CONTROL = uint8_t{0x01};
@ -29,137 +28,134 @@ class Epd {
static constexpr auto DEEP_SLEEP_MODE = uint8_t{0x10}; static constexpr auto DEEP_SLEEP_MODE = uint8_t{0x10};
}; };
io::Pin<RstPin> m_rst; static io::Pin<RstPin> m_rst;
io::Pin<DcPin> m_dc; static io::Pin<DcPin> m_dc;
io::Pin<BusyPin> m_bsy; static io::Pin<BusyPin> m_bsy;
public: public:
Epd() = default; static void init()
~Epd() = default;
int Init()
{ {
m_rst.dir(io::Dir::OUT); m_rst.dir(io::Dir::OUT);
m_dc.dir(io::Dir::OUT); m_dc.dir(io::Dir::OUT);
m_bsy.dir(io::Dir::IN); m_bsy.dir(io::Dir::IN);
Spi::init(); Spi::init();
Reset(); reset();
WaitUntilIdle(); waitUntilIdle();
SendCommand(Cmd::SW_RESET); sendCommand(Cmd::SW_RESET);
WaitUntilIdle(); waitUntilIdle();
SendCommand(Cmd::DRIVER_OUTPUT_CONTROL); sendCommand(Cmd::DRIVER_OUTPUT_CONTROL);
SendData(0xC7); sendData(0xC7);
SendData(0x00); sendData(0x00);
SendData(0x01); sendData(0x01);
SendCommand(Cmd::DATA_ENTRY_MODE); sendCommand(Cmd::DATA_ENTRY_MODE);
SendData(0x01); sendData(0x01);
SendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS); sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
SendData(0x00); sendData(0x00);
SendData((width / 8) - 1); sendData((Width / 8) - 1);
SendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS); sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
SendData(height - 1); sendData(Height - 1);
SendData(0x00); sendData(0x00);
SendData(0x00); sendData(0x00);
SendData(0x00); sendData(0x00);
SendCommand(Cmd::BORDER_WAVEFORM_CONTROL); sendCommand(Cmd::BORDER_WAVEFORM_CONTROL);
SendData(0x05); sendData(0x05);
SendCommand(Cmd::READ_TEMPERATURE_SENSOR); sendCommand(Cmd::READ_TEMPERATURE_SENSOR);
SendData(0x80); sendData(0x80);
SendCommand(Cmd::SET_RAM_X_ADDR); sendCommand(Cmd::SET_RAM_X_ADDR);
SendData(0x00); sendData(0x00);
SendCommand(Cmd::SET_RAM_Y_ADDR); sendCommand(Cmd::SET_RAM_Y_ADDR);
SendData(height - 1); sendData(Height - 1);
SendData(0x00); sendData(0x00);
WaitUntilIdle(); waitUntilIdle();
return 0;
} }
void SendCommand(unsigned char command) static void sendCommand(const uint8_t command)
{ {
m_dc = false; m_dc = false;
SpiTransfer(command); spiTransfer(command);
} }
void SendData(unsigned char data) static void sendData(const uint8_t data)
{ {
m_dc = true; m_dc = true;
SpiTransfer(data); spiTransfer(data);
} }
void WaitUntilIdle() static void waitUntilIdle()
{ {
while (m_bsy) { while (m_bsy) {
_delay_ms(100); _delay_ms(100);
} }
} }
void Reset() static void reset()
{ {
m_rst = true; m_rst = true;
_delay_ms(200); _delay_ms(200);
m_rst = false; // module reset m_rst = false;
_delay_ms(10); _delay_ms(10);
m_rst = true; m_rst = true;
_delay_ms(200); _delay_ms(200);
} }
void DisplayFrame(const unsigned char *frame_buffer_black, const unsigned char *frame_buffer_red) static void draw(const uint8_t *blackFrame, const uint8_t *redFrame)
{ {
SendCommand(Cmd::WRITE_RAM_BLACK); sendCommand(Cmd::WRITE_RAM_BLACK);
for (auto i = uint16_t{0}; i < width * height / 8; i++) { for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
SendData(pgm_read_byte(&frame_buffer_black[i])); sendData(pgm_read_byte(&blackFrame[i]));
} }
SendCommand(Cmd::WRITE_RAM_RED); sendCommand(Cmd::WRITE_RAM_RED);
for (auto i = uint16_t{0}; i < width * height / 8; i++) { for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
SendData(~pgm_read_byte(&frame_buffer_red[i])); sendData(~pgm_read_byte(&redFrame[i]));
} }
SendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2); sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
SendData(0xF7); sendData(0xF7);
SendCommand(Cmd::UPDATE_DISPLAY); sendCommand(Cmd::UPDATE_DISPLAY);
WaitUntilIdle(); waitUntilIdle();
} }
void DisplayClear() static void clear()
{ {
SendCommand(Cmd::WRITE_RAM_BLACK); sendCommand(Cmd::WRITE_RAM_BLACK);
for (auto i = uint16_t{0}; i < width * height / 8; i++) { for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
SendData(0xff); sendData(0xff);
} }
SendCommand(Cmd::WRITE_RAM_RED); sendCommand(Cmd::WRITE_RAM_RED);
for (auto i = uint16_t{0}; i < width * height / 8; i++) { for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
SendData(0x00); sendData(0x00);
} }
SendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2); sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
SendData(0xF7); sendData(0xF7);
SendCommand(Cmd::UPDATE_DISPLAY); sendCommand(Cmd::UPDATE_DISPLAY);
WaitUntilIdle(); waitUntilIdle();
} }
void Sleep() static void sleep()
{ {
SendCommand(Cmd::DEEP_SLEEP_MODE); sendCommand(Cmd::DEEP_SLEEP_MODE);
SendData(0x01); sendData(0x01);
_delay_ms(100); _delay_ms(100);
} }
void SpiTransfer(unsigned char data) static void spiTransfer(const uint8_t data)
{ {
Spi::select(true); Spi::select(true);
Spi::transfer(data); Spi::transfer(data);
Spi::select(false); Spi::select(false);
} }
}; };
} // namespace eink