eink/eink.hpp

162 lines
3.5 KiB
C++

#pragma once
#include <stdint.h>
#include <avr/pgmspace.h>
#include "../clock.hpp"
#include "../io/io.hpp"
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};
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:
static void init()
{
m_rst.dir(io::Dir::OUT);
m_dc.dir(io::Dir::OUT);
m_bsy.dir(io::Dir::IN);
Spi::init();
reset();
waitUntilIdle();
sendCommand(Cmd::SW_RESET);
waitUntilIdle();
sendCommand(Cmd::DRIVER_OUTPUT_CONTROL);
sendData(0xC7);
sendData(0x00);
sendData(0x01);
sendCommand(Cmd::DATA_ENTRY_MODE);
sendData(0x02);
sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
sendData(Width / 8 - 1);
sendData(0x00);
sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
sendData(0x00);
sendData(0x00);
sendData(Height - 1);
sendData(0x00);
sendCommand(Cmd::BORDER_WAVEFORM_CONTROL);
sendData(0x05);
sendCommand(Cmd::READ_TEMPERATURE_SENSOR);
sendData(0x80);
sendCommand(Cmd::SET_RAM_X_ADDR);
sendData(Width / 8 - 1);
sendCommand(Cmd::SET_RAM_Y_ADDR);
sendData(0x00);
sendData(0x00);
waitUntilIdle();
}
static void sendCommand(const uint8_t command)
{
m_dc = false;
spiTransfer(command);
}
static void sendData(const uint8_t data)
{
m_dc = true;
spiTransfer(data);
}
static void waitUntilIdle()
{
while (m_bsy) {
_delay_ms(100);
}
}
static void reset()
{
m_rst = true;
_delay_ms(200);
m_rst = false;
_delay_ms(10);
m_rst = true;
_delay_ms(200);
}
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(&blackFrame[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();
}
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_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();
}
static void sleep()
{
sendCommand(Cmd::DEEP_SLEEP_MODE);
sendData(0x01);
_delay_ms(100);
}
static void spiTransfer(const uint8_t data)
{
Spi::select(true);
Spi::transfer(data);
Spi::select(false);
}
};
} // namespace eink