2022-05-26 15:11:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-29 18:45:46 +02:00
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2022-06-02 12:24:49 +02:00
|
|
|
#include <utility>
|
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
|
|
|
|
2022-06-01 20:16:09 +02:00
|
|
|
#include "eink_spi.hpp"
|
2022-06-02 21:37:34 +02:00
|
|
|
#include "otp.hpp"
|
2022-06-01 20:16:09 +02:00
|
|
|
|
2022-05-26 15:11:00 +02:00
|
|
|
#include "../clock.hpp"
|
2022-06-02 21:37:34 +02:00
|
|
|
#include "../flash/flash.hpp"
|
2022-05-26 15:11:00 +02:00
|
|
|
#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-06-01 20:16:09 +02:00
|
|
|
template <std::uint16_t Width, std::uint16_t Height, typename Spi, io::P RstPin, 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-06-03 11:07:49 +02:00
|
|
|
struct original_lut_tag {
|
|
|
|
};
|
|
|
|
|
2022-06-01 20:16:09 +02:00
|
|
|
static io::Pin<RstPin> m_rst;
|
|
|
|
static io::Pin<BusyPin> m_busy;
|
|
|
|
|
|
|
|
public:
|
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,
|
2022-06-02 21:37:34 +02:00
|
|
|
READ_RAM = 0x27,
|
|
|
|
LOAD_OTP_TO_RAM = 0x31,
|
2022-06-03 11:07:49 +02:00
|
|
|
WRITE_LUT = 0x32,
|
2022-05-28 16:14:12 +02:00
|
|
|
BORDER_WAVEFORM_CONTROL = 0x3C,
|
2022-06-02 21:37:34 +02:00
|
|
|
READ_RAM_CHANNEL = 0x41,
|
2022-05-28 16:14:12 +02:00
|
|
|
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-06-02 12:24:49 +02:00
|
|
|
enum class Color : std::uint8_t {
|
2022-06-02 21:37:34 +02:00
|
|
|
BLACK = 0x00,
|
|
|
|
RED = 0x01,
|
|
|
|
WHITE = 0x02,
|
2022-06-02 12:24:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class RamDirection : std::uint8_t {
|
|
|
|
DECREMENT = 0,
|
|
|
|
INCREMENT = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class FastestMovingIndex : std::uint8_t {
|
|
|
|
X = 0,
|
|
|
|
Y = 1,
|
|
|
|
};
|
|
|
|
|
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);
|
2022-06-01 20:16:09 +02:00
|
|
|
m_busy.pullup(false);
|
|
|
|
m_busy.dir(io::Dir::IN);
|
2022-05-26 15:11:00 +02:00
|
|
|
Spi::init();
|
|
|
|
|
2022-05-26 16:03:38 +02:00
|
|
|
reset();
|
2022-06-02 12:24:49 +02:00
|
|
|
softReset();
|
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-06-02 12:24:49 +02:00
|
|
|
setDataEntryMode(RamDirection::DECREMENT, RamDirection::INCREMENT, FastestMovingIndex::X);
|
2022-05-26 15:11:00 +02:00
|
|
|
|
2022-06-02 12:24:49 +02:00
|
|
|
setRamRange({Width / 8 - 1, 0}, {0, Height - 1});
|
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-06-02 12:24:49 +02:00
|
|
|
setRamXPos(Width / 8 - 1);
|
|
|
|
setRamYPos(0);
|
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
|
|
|
{
|
2022-06-01 20:16:09 +02:00
|
|
|
Spi::select(true);
|
|
|
|
Spi::write(static_cast<word_t>(command), true);
|
|
|
|
Spi::select(false);
|
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-06-01 20:16:09 +02:00
|
|
|
Spi::select(true);
|
|
|
|
Spi::write(data, false);
|
|
|
|
Spi::select(false);
|
|
|
|
}
|
2022-05-28 17:02:00 +02:00
|
|
|
|
2022-06-01 20:16:09 +02:00
|
|
|
static word_t readData()
|
|
|
|
{
|
|
|
|
Spi::select(true);
|
|
|
|
const auto res = Spi::read();
|
|
|
|
Spi::select(false);
|
|
|
|
return res;
|
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
|
|
|
{
|
2022-06-01 20:16:09 +02:00
|
|
|
while (m_busy) {
|
2022-05-26 15:11:00 +02:00
|
|
|
_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-06-02 12:24:49 +02:00
|
|
|
waitUntilIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void softReset()
|
|
|
|
{
|
|
|
|
sendCommand(Cmd::SW_RESET);
|
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
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-06-03 11:07:49 +02:00
|
|
|
draw(rleImage, original_lut_tag{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RleImage, typename Lut = original_lut_tag>
|
|
|
|
static void draw(const RleImage &rleImage, const Lut &lut)
|
|
|
|
{
|
|
|
|
constexpr auto USE_ORIGINAL_LUT = std::is_same_v<Lut, original_lut_tag>;
|
|
|
|
if constexpr (!USE_ORIGINAL_LUT) {
|
|
|
|
writeLut(lut);
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:49 +02:00
|
|
|
constexpr auto sendImageChannel = [](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) {
|
2022-06-02 21:37:34 +02:00
|
|
|
const auto [count, data] = flash::load(image[j]);
|
2022-05-29 18:45:46 +02:00
|
|
|
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);
|
2022-06-03 11:07:49 +02:00
|
|
|
sendData(USE_ORIGINAL_LUT ? 0xF7 : 0xC7);
|
2022-05-26 16:03:38 +02:00
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
2022-05-26 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:49 +02:00
|
|
|
static void clear(const Color color = Color::WHITE)
|
2022-05-26 15:11:00 +02:00
|
|
|
{
|
2022-06-02 12:24:49 +02:00
|
|
|
constexpr auto getFillData = [](const auto &color) -> std::pair<std::uint8_t, std::uint8_t> {
|
|
|
|
switch (color) {
|
|
|
|
case Color::BLACK:
|
|
|
|
return {0x00, 0x00};
|
|
|
|
case Color::RED:
|
|
|
|
return {0xFF, 0xFF};
|
2022-06-02 21:37:34 +02:00
|
|
|
case Color::WHITE:
|
|
|
|
return {0xFF, 0x00};
|
2022-06-02 12:24:49 +02:00
|
|
|
}
|
|
|
|
return {0xFF, 0x00};
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto fillData = getFillData(color);
|
|
|
|
|
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-06-02 12:24:49 +02:00
|
|
|
sendData(fillData.first);
|
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-06-02 12:24:49 +02:00
|
|
|
sendData(fillData.second);
|
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-06-03 11:07:49 +02:00
|
|
|
static void writeLut(const Waveform &lut)
|
|
|
|
{
|
|
|
|
const auto flashLutPtr = reinterpret_cast<const std::byte *>(&lut);
|
|
|
|
|
|
|
|
sendCommand(Cmd::WRITE_LUT);
|
|
|
|
for (auto i = std::size_t{0}; i < sizeof(lut); ++i) {
|
|
|
|
const auto lutByte = flash::load(flashLutPtr[i]);
|
|
|
|
sendData(static_cast<word_t>(lutByte));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 20:16:09 +02:00
|
|
|
static void autoPatternFill()
|
|
|
|
{
|
|
|
|
constexpr auto RED_PATTERN_FILL_CMD = static_cast<Cmd>(0x46);
|
|
|
|
constexpr auto BLACK_PATTERN_FILL_CMD = static_cast<Cmd>(0x47);
|
|
|
|
|
|
|
|
constexpr auto RED_PATTERN = 0b0'001'0'001;
|
|
|
|
constexpr auto BLACK_PATTERN = 0b1'000'0'000;
|
|
|
|
|
|
|
|
sendCommand(RED_PATTERN_FILL_CMD);
|
|
|
|
sendData(RED_PATTERN);
|
|
|
|
waitUntilIdle();
|
|
|
|
|
|
|
|
sendCommand(BLACK_PATTERN_FILL_CMD);
|
|
|
|
sendData(BLACK_PATTERN);
|
|
|
|
waitUntilIdle();
|
|
|
|
|
|
|
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
|
|
|
sendData(0xF7);
|
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:49 +02:00
|
|
|
static void setDataEntryMode(const RamDirection &xDir = RamDirection::INCREMENT,
|
|
|
|
const RamDirection &yDir = RamDirection::INCREMENT,
|
|
|
|
const FastestMovingIndex &fastestMovingIndex = FastestMovingIndex::X)
|
|
|
|
{
|
|
|
|
auto setting = static_cast<std::uint8_t>(xDir) << 0;
|
|
|
|
setting |= static_cast<std::uint8_t>(yDir) << 1;
|
|
|
|
setting |= static_cast<std::uint8_t>(fastestMovingIndex) << 2;
|
|
|
|
|
|
|
|
sendCommand(Cmd::DATA_ENTRY_MODE);
|
|
|
|
sendData(setting);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setRamRange(const std::pair<std::uint8_t, std::uint8_t> &xrange,
|
|
|
|
const std::pair<std::uint16_t, std::uint16_t> &yrange)
|
|
|
|
{
|
|
|
|
sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
|
|
|
|
sendData(xrange.first & 0b00111111);
|
|
|
|
sendData(xrange.second & 0b00111111);
|
|
|
|
|
|
|
|
sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
|
|
|
|
sendData(yrange.first & 0xFF);
|
|
|
|
sendData((yrange.first >> 8) & 0b1);
|
|
|
|
sendData(yrange.second & 0xFF);
|
|
|
|
sendData((yrange.second >> 8) & 0b1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setRamXPos(const std::uint8_t pos)
|
|
|
|
{
|
|
|
|
sendCommand(Cmd::SET_RAM_X_ADDR);
|
|
|
|
sendData(pos & 0b00111111);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setRamYPos(const std::uint16_t pos)
|
|
|
|
{
|
|
|
|
sendCommand(Cmd::SET_RAM_Y_ADDR);
|
|
|
|
sendData(pos & 0xFF);
|
|
|
|
sendData((pos >> 8) & 0b1);
|
|
|
|
}
|
|
|
|
|
2022-06-02 21:37:34 +02:00
|
|
|
template <typename PrintFn>
|
|
|
|
static void dumpOTP(PrintFn &&printFn)
|
|
|
|
{
|
|
|
|
constexpr auto byteWidth = Width / 8;
|
|
|
|
constexpr auto ramHeight = Height + 46;
|
|
|
|
|
|
|
|
constexpr auto xRamRange = std::pair<std::uint8_t, std::uint8_t>{0, byteWidth - 1};
|
|
|
|
constexpr auto yRamRange = std::pair<std::uint16_t, std::uint16_t>{0, ramHeight - 1};
|
|
|
|
|
|
|
|
setDataEntryMode(RamDirection::INCREMENT, RamDirection::INCREMENT, FastestMovingIndex::X);
|
|
|
|
setRamRange(xRamRange, yRamRange);
|
|
|
|
setRamXPos(0);
|
|
|
|
setRamYPos(0);
|
|
|
|
|
|
|
|
sendCommand(Cmd::WRITE_RAM_BLACK);
|
|
|
|
for (auto i = std::uint16_t{0}; i < byteWidth * ramHeight; i++) {
|
|
|
|
sendData(0xFF);
|
|
|
|
}
|
|
|
|
sendCommand(Cmd::WRITE_RAM_RED);
|
|
|
|
for (auto i = std::uint16_t{0}; i < byteWidth * ramHeight; i++) {
|
|
|
|
sendData(0x00);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
|
|
|
sendData(0xF7);
|
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
|
|
|
sendData(0x80);
|
|
|
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
|
|
|
waitUntilIdle();
|
|
|
|
|
|
|
|
sendCommand(Cmd::LOAD_OTP_TO_RAM);
|
|
|
|
waitUntilIdle();
|
|
|
|
|
|
|
|
setRamXPos(0);
|
|
|
|
setRamYPos(0);
|
|
|
|
|
|
|
|
sendCommand(Cmd::READ_RAM_CHANNEL);
|
|
|
|
sendData(static_cast<word_t>(Color::BLACK));
|
|
|
|
|
|
|
|
sendCommand(Cmd::READ_RAM);
|
|
|
|
readData(); // First byte must be discarded
|
|
|
|
|
|
|
|
for (auto i = std::size_t{0}; i < sizeof(OTP); ++i) {
|
|
|
|
printFn(readData());
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
} // namespace eink
|