Implement multi-channel run length encoded images in flash memory

This commit is contained in:
BlackMark 2022-05-29 18:45:46 +02:00
parent 75046d1c1a
commit 83a064ddc1

View File

@ -1,7 +1,9 @@
#pragma once
#include <utility>
#include <tuple>
#include <type_traits>
#include <cstddef>
#include <cstdint>
#include <avr/pgmspace.h>
@ -146,41 +148,35 @@ class Eink {
_delay_ms(200);
}
template <typename Image>
static void draw(const Image &image)
template <typename RleImage>
static void draw(const RleImage &rleImage)
{
constexpr auto lookup = [](std::uint8_t bits) {
auto block = ImageBlock{};
util::for_constexpr(
[&](const auto idx) {
block[idx.value] = static_cast<Color>(bits % 3);
bits /= 3;
},
std::make_index_sequence<BLOCK_SIZE>{});
return block;
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;
};
constexpr auto sendImageChannel = [lookup](const auto command, const auto image) {
constexpr auto sendImageChannel = [pgm_load](const auto command, const auto &image) {
sendCommand(command);
auto buffer = std::uint8_t{0};
auto bufferPos = std::uint8_t{0};
for (auto i = std::uint16_t{0}; i < Width * Height / BLOCK_SIZE; i++) {
const auto block = lookup(pgm_read_byte(&image[i]));
for (auto p = std::uint8_t{0}; p < BLOCK_SIZE; ++p) {
const auto pixel = std::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;
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);
}
}
}
};
sendImageChannel(Cmd::WRITE_RAM_BLACK, image.data());
sendImageChannel(Cmd::WRITE_RAM_RED, image.data());
sendImageChannel(Cmd::WRITE_RAM_BLACK, std::get<0>(rleImage));
sendImageChannel(Cmd::WRITE_RAM_RED, std::get<1>(rleImage));
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
sendData(0xF7);