Compare commits

...

3 Commits

162
eink.hpp
View File

@ -7,143 +7,155 @@
#include "../clock.hpp"
#include "../io/io.hpp"
template <typename Spi>
class Epd {
static constexpr auto width = uint16_t{200};
static constexpr auto height = uint16_t{200};
namespace eink {
io::Pin<io::P::C1> m_rst;
io::Pin<io::P::C0> m_dc;
io::Pin<io::P::C2> m_bsy;
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};
static constexpr auto DATA_ENTRY_MODE = uint8_t{0x11};
static constexpr auto SET_RAM_X_ADDR_POSITIONS = uint8_t{0x44};
static constexpr auto SET_RAM_Y_ADDR_POSITIONS = uint8_t{0x45};
static constexpr auto BORDER_WAVEFORM_CONTROL = uint8_t{0x3C};
static constexpr auto READ_TEMPERATURE_SENSOR = uint8_t{0x18};
static constexpr auto SET_RAM_X_ADDR = uint8_t{0x4E};
static constexpr auto SET_RAM_Y_ADDR = uint8_t{0x4F};
static constexpr auto WRITE_RAM_BLACK = uint8_t{0x24};
static constexpr auto WRITE_RAM_RED = uint8_t{0x26};
static constexpr auto DISPLAY_UPDATE_CONTROL_2 = uint8_t{0x22};
static constexpr auto UPDATE_DISPLAY = uint8_t{0x20};
static constexpr auto DEEP_SLEEP_MODE = uint8_t{0x10};
};
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();
/* EPD hardware init start */
Reset();
reset();
WaitUntilIdle();
SendCommand(0x12); // SWRESET
WaitUntilIdle();
waitUntilIdle();
sendCommand(Cmd::SW_RESET);
waitUntilIdle();
SendCommand(0x01); // Driver output control
SendData(0xC7);
SendData(0x00);
SendData(0x01);
sendCommand(Cmd::DRIVER_OUTPUT_CONTROL);
sendData(0xC7);
sendData(0x00);
sendData(0x01);
SendCommand(0x11); // data entry mode
SendData(0x01);
sendCommand(Cmd::DATA_ENTRY_MODE);
sendData(0x01);
SendCommand(0x44); // set Ram-X address start/end position
SendData(0x00);
SendData(0x18); // 0x18-->(24+1)*8=200
sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
sendData(0x00);
sendData((Width / 8) - 1);
SendCommand(0x45); // set Ram-Y address start/end position
SendData(0xC7); // 0xC7-->(199+1)=200
SendData(0x00);
SendData(0x00);
SendData(0x00);
sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
sendData(Height - 1);
sendData(0x00);
sendData(0x00);
sendData(0x00);
SendCommand(0x3C); // BorderWavefrom
SendData(0x05);
sendCommand(Cmd::BORDER_WAVEFORM_CONTROL);
sendData(0x05);
SendCommand(0x18); // Read built-in temperature sensor
SendData(0x80);
sendCommand(Cmd::READ_TEMPERATURE_SENSOR);
sendData(0x80);
SendCommand(0x4E); // set RAM x address count to 0;
SendData(0x00);
SendCommand(0x4F); // set RAM y address count to 0X199;
SendData(0xC7);
SendData(0x00);
WaitUntilIdle();
sendCommand(Cmd::SET_RAM_X_ADDR);
sendData(0x00);
/* EPD hardware init end */
sendCommand(Cmd::SET_RAM_Y_ADDR);
sendData(Height - 1);
sendData(0x00);
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(0x24);
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(0x26);
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(0x22);
SendData(0xF7);
SendCommand(0x20);
WaitUntilIdle();
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
sendData(0xF7);
sendCommand(Cmd::UPDATE_DISPLAY);
waitUntilIdle();
}
void DisplayClear()
static void clear()
{
SendCommand(0x24);
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(0x26);
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(0x22);
SendData(0xF7);
SendCommand(0x20);
WaitUntilIdle();
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
sendData(0xF7);
sendCommand(Cmd::UPDATE_DISPLAY);
waitUntilIdle();
}
void Sleep()
static void sleep()
{
SendCommand(0x10); // power setting
SendData(0x01); // gate switch to external
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