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