2022-05-26 15:11:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-29 18:45:46 +02:00
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2022-05-29 16:13:53 +02:00
|
|
|
|
2022-05-29 18:45:46 +02:00
|
|
|
#include <cstddef>
|
2022-05-29 16:13:53 +02:00
|
|
|
#include <cstdint>
|
2022-05-26 15:11:00 +02:00
|
|
|
|
|
|
|
#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-29 16:13:53 +02:00
|
|
|
template <std::uint16_t Width, std::uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin>
|
2022-05-26 16:03:38 +02:00
|
|
|
class Eink {
|
2022-05-28 17:02:00 +02:00
|
|
|
using word_t = typename Spi::word_t;
|
|
|
|
|
2022-05-29 16:13:53 +02:00
|
|
|
enum class Cmd : std::uint8_t {
|
2022-05-28 16:14:12 +02:00
|
|
|
DRIVER_OUTPUT_CONTROL = 0x01,
|
|
|
|
DEEP_SLEEP_MODE = 0x10,
|
|
|
|
DATA_ENTRY_MODE = 0x11,
|
|
|
|
SW_RESET = 0x12,
|
|
|
|
READ_TEMPERATURE_SENSOR = 0x18,
|
|
|
|
UPDATE_DISPLAY = 0x20,
|
|
|
|
DISPLAY_UPDATE_CONTROL_2 = 0x22,
|
|
|
|
WRITE_RAM_BLACK = 0x24,
|
|
|
|
WRITE_RAM_RED = 0x26,
|
|
|
|
BORDER_WAVEFORM_CONTROL = 0x3C,
|
|
|
|
SET_RAM_X_ADDR_POSITIONS = 0x44,
|
|
|
|
SET_RAM_Y_ADDR_POSITIONS = 0x45,
|
|
|
|
SET_RAM_X_ADDR = 0x4E,
|
|
|
|
SET_RAM_Y_ADDR = 0x4F,
|
2022-05-26 15:53:29 +02:00
|
|
|
};
|
|
|
|
|
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-28 17:02:00 +02:00
|
|
|
static constexpr auto THREE_WIRE_SPI = (DcPin == io::P::NONE ? true : false);
|
|
|
|
static_assert((THREE_WIRE_SPI && sizeof(word_t) > 1) || !THREE_WIRE_SPI,
|
|
|
|
"Three wire SPI requires SPI word size of at least 9 bits");
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-05-28 11:45:42 +02:00
|
|
|
static constexpr auto BLOCK_SIZE = 5;
|
|
|
|
|
2022-05-29 16:13:53 +02:00
|
|
|
enum class Color : std::uint8_t {
|
2022-05-28 11:45:42 +02:00
|
|
|
BLACK = 0b00,
|
|
|
|
WHITE = 0b01,
|
|
|
|
RED = 0b10,
|
|
|
|
ERROR = 0b11,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ImageBlock {
|
|
|
|
public:
|
2022-05-29 16:13:53 +02:00
|
|
|
inline Color &operator[](const std::size_t idx)
|
2022-05-28 11:45:42 +02:00
|
|
|
{
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:13:53 +02:00
|
|
|
inline const Color &operator[](const std::size_t idx) const
|
2022-05-28 11:45:42 +02:00
|
|
|
{
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Color data[BLOCK_SIZE];
|
|
|
|
};
|
|
|
|
|
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-28 16:14:12 +02:00
|
|
|
static void sendCommand(const Cmd command)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
|
|
|
m_dc = false;
|
2022-05-28 17:02:00 +02:00
|
|
|
spiTransfer(static_cast<word_t>(command));
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-28 17:02:00 +02:00
|
|
|
static void sendData(word_t data)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-05-28 17:02:00 +02:00
|
|
|
if constexpr (THREE_WIRE_SPI) {
|
|
|
|
data |= 1 << 8;
|
|
|
|
}
|
|
|
|
|
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-29 18:45:46 +02:00
|
|
|
template <typename RleImage>
|
|
|
|
static void draw(const RleImage &rleImage)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-05-29 18:45:46 +02:00
|
|
|
constexpr auto pgm_load = [](const auto &object) {
|
|
|
|
using object_t = std::remove_cvref_t<decltype(object)>;
|
|
|
|
auto buffer = object_t{};
|
|
|
|
auto rawBuffer = reinterpret_cast<std::byte *>(&buffer);
|
|
|
|
for (auto i = std::size_t{0}; i < sizeof(object_t); ++i) {
|
|
|
|
rawBuffer[i] = static_cast<std::byte>(pgm_read_byte(&reinterpret_cast<const std::byte *>(&object)[i]));
|
|
|
|
}
|
|
|
|
return buffer;
|
2022-05-27 11:37:23 +02:00
|
|
|
};
|
|
|
|
|
2022-05-29 18:45:46 +02:00
|
|
|
constexpr auto sendImageChannel = [pgm_load](const auto command, const auto &image) {
|
2022-05-27 11:37:23 +02:00
|
|
|
sendCommand(command);
|
2022-05-29 18:45:46 +02:00
|
|
|
for (auto j = std::size_t{0}; j < image.size(); ++j) {
|
|
|
|
const auto [count, data] = pgm_load(image[j]);
|
|
|
|
for (auto i = std::uint16_t{0}; i < count; ++i) {
|
|
|
|
if (command == Cmd::WRITE_RAM_BLACK) {
|
|
|
|
sendData(data);
|
|
|
|
} else {
|
|
|
|
sendData(~data);
|
2022-05-27 11:37:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-29 18:45:46 +02:00
|
|
|
sendImageChannel(Cmd::WRITE_RAM_BLACK, std::get<0>(rleImage));
|
|
|
|
sendImageChannel(Cmd::WRITE_RAM_RED, std::get<1>(rleImage));
|
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);
|
2022-05-29 16:13:53 +02:00
|
|
|
for (auto i = std::uint16_t{0}; i < Width * Height / 8; i++) {
|
2022-05-26 16:03:38 +02:00
|
|
|
sendData(0xff);
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::WRITE_RAM_RED);
|
2022-05-29 16:13:53 +02:00
|
|
|
for (auto i = std::uint16_t{0}; i < Width * Height / 8; i++) {
|
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::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-28 17:02:00 +02:00
|
|
|
static void spiTransfer(const word_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
|