#ifndef PICOLED_H #define PICOLED_H #include "picoled/buffer.h" #include namespace picoled { class pixel { public: pixel() : pixel(0) {} pixel(uint8_t grayscale) : pixel(grayscale, grayscale, grayscale) {} pixel(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {} uint8_t red() const { return r; } uint8_t green() const { return g; } uint8_t blue() const { return b; } uint8_t grayscale() const { uint16_t unscaled = static_cast(r) + static_cast(g) + static_cast(b); return unscaled / 3; } private: uint8_t r; uint8_t g; uint8_t b; }; class oled { public: oled(size_t width, size_t height); virtual ~oled() = default; oled(const oled&) = delete; oled(oled&&) = delete; oled& operator=(const oled&) = delete; oled& operator=(oled&&) = delete; void clear(); size_t get_width() const { return buffer_a.get_width(); } size_t get_height() const { return buffer_a.get_height(); } virtual void write(int x, int y, const pixel& p) { active_buffer().write(x, y, p); } buffer& active_buffer() { return active ? buffer_b : buffer_a; } void update(); void swap_buffers(); protected: virtual void update_impl() const = 0; buffer& inactive_buffer() { return active ? buffer_a : buffer_b; } const buffer& inactive_buffer() const { return active ? buffer_a : buffer_b; } private: bool active; buffer buffer_a; buffer buffer_b; }; } #endif//PICOLED_H