2022-05-26 15:11:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
|
|
|
#include "../clock.hpp"
|
|
|
|
#include "../io/io.hpp"
|
2022-05-27 11:37:23 +02:00
|
|
|
#include "../util/util.hpp"
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
namespace eink {
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
template <uint16_t Width, uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin>
|
|
|
|
class Eink {
|
2022-05-26 15:53:29 +02:00
|
|
|
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};
|
|
|
|
};
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static io::Pin<RstPin> m_rst;
|
|
|
|
static io::Pin<DcPin> m_dc;
|
|
|
|
static io::Pin<BusyPin> m_bsy;
|
2022-05-26 15:11:00 +02:00
|
|
|
|
|
|
|
public:
|
2022-05-26 16:03:38 +02:00
|
|
|
static void init()
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
m_rst.dir(io::Dir::OUT);
|
|
|
|
m_dc.dir(io::Dir::OUT);
|
|
|
|
m_bsy.dir(io::Dir::IN);
|
|
|
|
Spi::init();
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
reset();
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
waitUntilIdle();
|
|
|
|
sendCommand(Cmd::SW_RESET);
|
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::DRIVER_OUTPUT_CONTROL);
|
|
|
|
sendData(0xC7);
|
|
|
|
sendData(0x00);
|
|
|
|
sendData(0x01);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::DATA_ENTRY_MODE);
|
2022-05-26 19:33:49 +02:00
|
|
|
sendData(0x02);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
|
2022-05-26 19:33:49 +02:00
|
|
|
sendData(Width / 8 - 1);
|
2022-05-26 16:03:38 +02:00
|
|
|
sendData(0x00);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
|
|
|
|
sendData(0x00);
|
|
|
|
sendData(0x00);
|
2022-05-26 19:33:49 +02:00
|
|
|
sendData(Height - 1);
|
2022-05-26 16:03:38 +02:00
|
|
|
sendData(0x00);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::BORDER_WAVEFORM_CONTROL);
|
|
|
|
sendData(0x05);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::READ_TEMPERATURE_SENSOR);
|
|
|
|
sendData(0x80);
|
2022-05-26 15:53:29 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::SET_RAM_X_ADDR);
|
2022-05-26 19:33:49 +02:00
|
|
|
sendData(Width / 8 - 1);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::SET_RAM_Y_ADDR);
|
2022-05-26 19:33:49 +02:00
|
|
|
sendData(0x00);
|
2022-05-26 16:03:38 +02:00
|
|
|
sendData(0x00);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void sendCommand(const uint8_t command)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
m_dc = false;
|
2022-05-26 16:03:38 +02:00
|
|
|
spiTransfer(command);
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void sendData(const uint8_t data)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
m_dc = true;
|
2022-05-26 16:03:38 +02:00
|
|
|
spiTransfer(data);
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void waitUntilIdle()
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
while (m_bsy) {
|
|
|
|
_delay_ms(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void reset()
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
m_rst = true;
|
|
|
|
_delay_ms(200);
|
2022-05-26 16:03:38 +02:00
|
|
|
m_rst = false;
|
2022-05-26 15:11:00 +02:00
|
|
|
_delay_ms(10);
|
|
|
|
m_rst = true;
|
|
|
|
_delay_ms(200);
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:39:51 +02:00
|
|
|
template <typename Image>
|
|
|
|
static void draw(const Image &image)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-05-27 11:37:23 +02:00
|
|
|
constexpr auto BLOCK_SIZE = 5;
|
|
|
|
|
|
|
|
enum class Color : uint8_t {
|
|
|
|
BLACK = 0b00,
|
|
|
|
WHITE = 0b01,
|
|
|
|
RED = 0b10,
|
|
|
|
ERROR = 0b11,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Block {
|
|
|
|
public:
|
|
|
|
inline Color &operator[](const size_t idx)
|
|
|
|
{
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const Color &operator[](const size_t idx) const
|
|
|
|
{
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Color data[BLOCK_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr auto lookup = [](uint8_t bits) {
|
|
|
|
auto block = Block{};
|
|
|
|
for_constexpr(
|
|
|
|
[&](const auto idx) {
|
|
|
|
block[idx.value] = static_cast<Color>(bits % 3);
|
|
|
|
bits /= 3;
|
|
|
|
},
|
|
|
|
util::make_index_sequence<BLOCK_SIZE>{});
|
|
|
|
return block;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr auto sendImageChannel = [lookup](const auto command, const auto image) {
|
|
|
|
sendCommand(command);
|
|
|
|
auto buffer = uint8_t{0};
|
|
|
|
auto bufferPos = uint8_t{0};
|
|
|
|
for (auto i = uint16_t{0}; i < Width * Height / BLOCK_SIZE; i++) {
|
|
|
|
const auto block = lookup(pgm_read_byte(&image[i]));
|
|
|
|
for (auto p = uint8_t{0}; p < BLOCK_SIZE; ++p) {
|
|
|
|
const auto pixel = uint8_t{(command == Cmd::WRITE_RAM_BLACK) ? (block[p] != Color::BLACK)
|
|
|
|
: (block[p] == Color::RED)};
|
|
|
|
buffer |= pixel << (7 - bufferPos++);
|
|
|
|
if (bufferPos == 8) {
|
|
|
|
sendData(buffer);
|
|
|
|
buffer = 0;
|
|
|
|
bufferPos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-27 20:39:51 +02:00
|
|
|
sendImageChannel(Cmd::WRITE_RAM_BLACK, image.data());
|
|
|
|
sendImageChannel(Cmd::WRITE_RAM_RED, image.data());
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
|
|
|
sendData(0xF7);
|
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void clear()
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::WRITE_RAM_BLACK);
|
|
|
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
|
|
|
sendData(0xff);
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::WRITE_RAM_RED);
|
|
|
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
|
|
|
sendData(0x00);
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
|
|
|
sendData(0xF7);
|
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void sleep()
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::DEEP_SLEEP_MODE);
|
|
|
|
sendData(0x01);
|
2022-05-26 15:11:00 +02:00
|
|
|
_delay_ms(100);
|
|
|
|
}
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
static void spiTransfer(const uint8_t data)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
Spi::select(true);
|
|
|
|
Spi::transfer(data);
|
|
|
|
Spi::select(false);
|
|
|
|
}
|
|
|
|
};
|
2022-05-26 16:03:38 +02:00
|
|
|
|
|
|
|
} // namespace eink
|