83 lines
1.5 KiB
C++
83 lines
1.5 KiB
C++
#ifndef PICOLED_H
|
|
#define PICOLED_H
|
|
|
|
#include "picoled/buffer.h"
|
|
|
|
#include <cstdint>
|
|
|
|
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<uint16_t>(r) + static_cast<uint16_t>(g) + static_cast<uint16_t>(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<pixel>& active_buffer() { return active ? buffer_b : buffer_a; }
|
|
|
|
void update();
|
|
void swap_buffers();
|
|
|
|
protected:
|
|
virtual void update_impl() const = 0;
|
|
|
|
buffer<pixel>& inactive_buffer() { return active ? buffer_a : buffer_b; }
|
|
const buffer<pixel>& inactive_buffer() const { return active ? buffer_a : buffer_b; }
|
|
|
|
private:
|
|
bool active;
|
|
buffer<pixel> buffer_a;
|
|
buffer<pixel> buffer_b;
|
|
};
|
|
|
|
}
|
|
|
|
#endif//PICOLED_H
|