-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGB16BuffAdaptor.h
96 lines (83 loc) · 3.09 KB
/
RGB16BuffAdaptor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include "RGB16DisplayAdaptor.h"
class RGB16BuffAdaptor : public RGB16DisplayAdaptor {
public:
/*
* The display adapter implementation writing pixels to in memory bitmap.
* Use it whenever you need to prepare some portion of the displayed image
* before writing it to the physical display.
*/
RGB16BuffAdaptor(
// We assume buffer has column order of pixels
uint16_t* buff, uint16_t w, uint16_t h,
// The following parameter may be used to specify transparent color.
// Writes of such color does not modify buffer content.
int32_t transparent = -1
)
: m_buff(buff)
, m_w(w), m_h(h)
, m_transparent(transparent) {}
virtual uint16_t width() const { return m_w; }
virtual uint16_t height() const { return m_h; }
virtual void init(uint16_t fill_colour = RGB16Black) {
uint32_t const pixels = m_w * m_h;
for (uint32_t i = 0; i < pixels; ++i)
m_buff[i] = fill_colour;
}
virtual void enable(bool on) {};
virtual void set_brightness(uint8_t val) {}
void put_pixel_(uint16_t x, uint16_t y, uint16_t colour) {
if (colour != m_transparent && x < m_w && y < m_h)
m_buff[(uint32_t)m_h * x + y] = colour; // Assume column order
}
virtual void put_pixel(uint16_t x, uint16_t y, uint16_t colour) {
put_pixel_(x, y, colour);
}
virtual void fill_rect(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t colour) {
for (uint16_t x = x0; x <= x1; ++x)
for (uint16_t y = y0; y <= y1; ++y)
put_pixel_(x, y, colour);
}
/* Setup rectangular writing area */
virtual void write_begin(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, bool col_order) {
m_wr_x0 = x0; m_wr_y0 = y0;
m_wr_x1 = x1; m_wr_y1 = y1;
m_col_order = col_order;
}
/* Write pixels */
virtual void write_pixels(uint16_t const* pix_buff, int len, int pgm = false) {
if (m_col_order)
for (uint16_t x = m_wr_x0; x <= m_wr_x1; ++x)
for (uint16_t y = m_wr_y0; y <= m_wr_y1 && len > 0; ++y, --len, ++pix_buff)
put_pixel_(x, y, pgm ? pgm_read_word(pix_buff) : *pix_buff);
else
for (uint16_t y = m_wr_y0; y <= m_wr_y1; ++y)
for (uint16_t x = m_wr_x0; x <= m_wr_x1 && len > 0; ++x, --len, ++pix_buff)
put_pixel_(x, y, pgm ? pgm_read_word(pix_buff) : *pix_buff);
}
/* Write pixels bitmap */
virtual void write_pixels_bm(uint8_t const* pix_bm, int len, uint16_t colours[2], int pgm = false) {
int i = 0;
if (m_col_order)
for (uint16_t x = m_wr_x0; x <= m_wr_x1; ++x)
for (uint16_t y = m_wr_y0; y <= m_wr_y1 && i < len; ++y, ++i) {
uint8_t const byte = pgm ? pgm_read_byte(pix_bm + i/8) : pix_bm[i/8];
uint16_t const colour = colours[1 & (byte >> (i%8))];
put_pixel_(x, y, colour);
}
else
for (uint16_t y = m_wr_y0; y <= m_wr_y1; ++y)
for (uint16_t x = m_wr_x0; x <= m_wr_x1 && i < len; ++x, ++i) {
uint8_t const byte = pgm ? pgm_read_byte(pix_bm + i/8) : pix_bm[i/8];
uint16_t const colour = colours[1 & (byte >> (i%8))];
put_pixel_(x, y, colour);
}
}
virtual void write_end() {}
private:
uint16_t* m_buff;
uint16_t m_w, m_h;
int32_t m_transparent;
uint16_t m_wr_x0, m_wr_y0, m_wr_x1, m_wr_y1;
bool m_col_order;
};