Make use of C++ standard library

This commit is contained in:
BlackMark 2022-05-29 16:13:53 +02:00
parent 2e417a0f3a
commit 75046d1c1a

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <stdint.h> #include <utility>
#include <cstdint>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
@ -10,11 +12,11 @@
namespace eink { namespace eink {
template <uint16_t Width, uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin> template <std::uint16_t Width, std::uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin>
class Eink { class Eink {
using word_t = typename Spi::word_t; using word_t = typename Spi::word_t;
enum class Cmd : uint8_t { enum class Cmd : std::uint8_t {
DRIVER_OUTPUT_CONTROL = 0x01, DRIVER_OUTPUT_CONTROL = 0x01,
DEEP_SLEEP_MODE = 0x10, DEEP_SLEEP_MODE = 0x10,
DATA_ENTRY_MODE = 0x11, DATA_ENTRY_MODE = 0x11,
@ -40,7 +42,7 @@ class Eink {
static constexpr auto BLOCK_SIZE = 5; static constexpr auto BLOCK_SIZE = 5;
enum class Color : uint8_t { enum class Color : std::uint8_t {
BLACK = 0b00, BLACK = 0b00,
WHITE = 0b01, WHITE = 0b01,
RED = 0b10, RED = 0b10,
@ -49,12 +51,12 @@ class Eink {
class ImageBlock { class ImageBlock {
public: public:
inline Color &operator[](const size_t idx) inline Color &operator[](const std::size_t idx)
{ {
return data[idx]; return data[idx];
} }
inline const Color &operator[](const size_t idx) const inline const Color &operator[](const std::size_t idx) const
{ {
return data[idx]; return data[idx];
} }
@ -147,26 +149,26 @@ class Eink {
template <typename Image> template <typename Image>
static void draw(const Image &image) static void draw(const Image &image)
{ {
constexpr auto lookup = [](uint8_t bits) { constexpr auto lookup = [](std::uint8_t bits) {
auto block = ImageBlock{}; auto block = ImageBlock{};
for_constexpr( util::for_constexpr(
[&](const auto idx) { [&](const auto idx) {
block[idx.value] = static_cast<Color>(bits % 3); block[idx.value] = static_cast<Color>(bits % 3);
bits /= 3; bits /= 3;
}, },
util::make_index_sequence<BLOCK_SIZE>{}); std::make_index_sequence<BLOCK_SIZE>{});
return block; return block;
}; };
constexpr auto sendImageChannel = [lookup](const auto command, const auto image) { constexpr auto sendImageChannel = [lookup](const auto command, const auto image) {
sendCommand(command); sendCommand(command);
auto buffer = uint8_t{0}; auto buffer = std::uint8_t{0};
auto bufferPos = uint8_t{0}; auto bufferPos = std::uint8_t{0};
for (auto i = uint16_t{0}; i < Width * Height / BLOCK_SIZE; i++) { for (auto i = std::uint16_t{0}; i < Width * Height / BLOCK_SIZE; i++) {
const auto block = lookup(pgm_read_byte(&image[i])); const auto block = lookup(pgm_read_byte(&image[i]));
for (auto p = uint8_t{0}; p < BLOCK_SIZE; ++p) { for (auto p = std::uint8_t{0}; p < BLOCK_SIZE; ++p) {
const auto pixel = uint8_t{(command == Cmd::WRITE_RAM_BLACK) ? (block[p] != Color::BLACK) const auto pixel = std::uint8_t{(command == Cmd::WRITE_RAM_BLACK) ? (block[p] != Color::BLACK)
: (block[p] == Color::RED)}; : (block[p] == Color::RED)};
buffer |= pixel << (7 - bufferPos++); buffer |= pixel << (7 - bufferPos++);
if (bufferPos == 8) { if (bufferPos == 8) {
sendData(buffer); sendData(buffer);
@ -189,11 +191,11 @@ class Eink {
static void clear() static void clear()
{ {
sendCommand(Cmd::WRITE_RAM_BLACK); sendCommand(Cmd::WRITE_RAM_BLACK);
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) { for (auto i = std::uint16_t{0}; i < Width * Height / 8; i++) {
sendData(0xff); sendData(0xff);
} }
sendCommand(Cmd::WRITE_RAM_RED); sendCommand(Cmd::WRITE_RAM_RED);
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) { for (auto i = std::uint16_t{0}; i < Width * Height / 8; i++) {
sendData(0x00); sendData(0x00);
} }