Add dithering
This commit is contained in:
parent
1c9fc51d01
commit
8115ae1412
@ -2,26 +2,25 @@
|
|||||||
#define PICOLED_H
|
#define PICOLED_H
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
namespace picoled
|
namespace picoled
|
||||||
{
|
{
|
||||||
|
|
||||||
class pixel
|
class pixel
|
||||||
{
|
{
|
||||||
|
friend class oled;
|
||||||
public:
|
public:
|
||||||
pixel() :
|
pixel() :
|
||||||
pixel(false)
|
pixel(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
pixel(bool on) :
|
pixel(uint8_t value) :
|
||||||
on(on)
|
value(value)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool is_on() const { return on; }
|
|
||||||
void invert() { on = !on; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool on;
|
uint8_t value;
|
||||||
};
|
};
|
||||||
|
|
||||||
class oled
|
class oled
|
||||||
@ -44,9 +43,13 @@ public:
|
|||||||
size_t get_width() const { return width; }
|
size_t get_width() const { return width; }
|
||||||
size_t get_height() const { return height; }
|
size_t get_height() const { return height; }
|
||||||
|
|
||||||
virtual void update() const = 0;
|
void update() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
virtual void update_impl() const = 0;
|
||||||
|
|
||||||
|
bool is_on(int x, int y) const;
|
||||||
|
|
||||||
size_t width;
|
size_t width;
|
||||||
size_t height;
|
size_t height;
|
||||||
|
|
||||||
|
@ -16,7 +16,8 @@ public:
|
|||||||
ssd1306(i2c_inst* i2c, uint8_t address);
|
ssd1306(i2c_inst* i2c, uint8_t address);
|
||||||
virtual ~ssd1306();
|
virtual ~ssd1306();
|
||||||
|
|
||||||
void update() const override;
|
protected:
|
||||||
|
void update_impl() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void command(uint8_t cmd) const;
|
void command(uint8_t cmd) const;
|
||||||
|
@ -77,7 +77,7 @@ ssd1306::~ssd1306()
|
|||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ssd1306::update() const
|
void ssd1306::update_impl() const
|
||||||
{
|
{
|
||||||
// clear display to zero
|
// clear display to zero
|
||||||
memset(&buffer[1], 0, width * height * sizeof(*buffer));
|
memset(&buffer[1], 0, width * height * sizeof(*buffer));
|
||||||
@ -86,7 +86,7 @@ void ssd1306::update() const
|
|||||||
{
|
{
|
||||||
for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
if (!(*this)(x, y).is_on()) { continue; }
|
if (!is_on(x, y)) { continue; }
|
||||||
|
|
||||||
const int index = (width - x - 1) + (y / 8) * width;
|
const int index = (width - x - 1) + (y / 8) * width;
|
||||||
const uint8_t bits = 1 << (y & 7);
|
const uint8_t bits = 1 << (y & 7);
|
||||||
|
@ -1,5 +1,14 @@
|
|||||||
#include "picoled.h"
|
#include "picoled.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
picoled::pixel DUMMY_PIXEL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
namespace picoled
|
namespace picoled
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -14,18 +23,29 @@ oled::~oled()
|
|||||||
delete[] pixels;
|
delete[] pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pixel& oled::operator()(int x, int y)
|
pixel& oled::operator()(int x, int y)
|
||||||
{
|
{
|
||||||
static pixel dummy;
|
if (x < 0 || x >= width || y < 0 || y >= height) { return DUMMY_PIXEL; }
|
||||||
if (x < 0 || x >= width || y < 0 || y >= height) { return dummy; }
|
|
||||||
return pixels[x + y * width];
|
return pixels[x + y * width];
|
||||||
}
|
}
|
||||||
|
|
||||||
const pixel& oled::operator()(int x, int y) const
|
const pixel& oled::operator()(int x, int y) const
|
||||||
{
|
{
|
||||||
|
if (x < 0 || x >= width || y < 0 || y >= height) { return DUMMY_PIXEL; }
|
||||||
return pixels[x + y * width];
|
return pixels[x + y * width];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void oled::update() const
|
||||||
|
{
|
||||||
|
update_impl();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool oled::is_on(int x, int y) const
|
||||||
|
{
|
||||||
|
return (*this)(x, y).value > rand() % 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
void oled::clear()
|
void oled::clear()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < width * height; i++)
|
for (int i = 0; i < width * height; i++)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user