-
Notifications
You must be signed in to change notification settings - Fork 9
/
ssd1351_nobuffer.inl
188 lines (172 loc) · 4.55 KB
/
ssd1351_nobuffer.inl
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Specific implementations for unbuffered mode.
// This gets included from inside the template definition in ssd1351.h, which is crazy, but it's the only way I know to make this compile.
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void drawPixel(int16_t x, int16_t y, const C &color) {
// Drawing pixels directly to the display requires first setting the correct
// video ram position, from x/y to x+1/y+1.
if(x < 0 || x >= W || y < 0 || y >= H) {
return;
}
SPI.beginTransaction(spi_settings);
setVideoRamPosition(x, y, x+1, y+1);
sendCommandAndContinue(CMD_WRITE_TO_RAM);
pushColor(color, true);
SPI.endTransaction();
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void updateScreen() {
// An unbuffered display doesn't need to update its screen, but we want to
// supply a common interface for all operational modes.
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void fillScreen(const C &color) {
// Instead of drawing each pixel to the screen with the same color, we make
// use of the fact that fillRect is optimized to only incur a single overhead
// for addressing a cordinate on the display
fillRect(0, 0, W, H, color);
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void drawFastVLine(int16_t x, int16_t y, int16_t h, const C &color) {
// Setting the video ram on the display to only contain a single constrained
// column of data allows us to write vertical lines super fast.
// The x/y position is only set to the start, the display then takes care of
// pointing to the next pixel after the first is written.
if((x >= W) || (y >= H)) {
return;
}
if((y + h - 1) >= H) {
h = H - y;
}
SPI.beginTransaction(spi_settings);
setVideoRamPosition(x, y, x, y + h - 1);
sendCommandAndContinue(CMD_WRITE_TO_RAM);
while (h-- > 1) {
pushColor(color);
}
pushColor(color, true);
SPI.endTransaction();
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void drawFastHLine(int16_t x, int16_t y, int16_t w, const C &color) {
// Rudimentary clipping
if((x >= W) || (y >= H)) {
return;
}
if((x + w - 1) >= W) {
w = W - x;
}
SPI.beginTransaction(spi_settings);
setVideoRamPosition(x, y, x + w - 1, y);
sendCommandAndContinue(CMD_WRITE_TO_RAM);
while (w-- > 1) {
pushColor(color);
}
pushColor(color, true);
SPI.endTransaction();
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, const C &color) {
// rudimentary clipping (drawChar w/big text requires this)
if((x >= W) || (y >= H)) {
return;
}
if((x + w - 1) >= W) {
w = W - x;
}
if((y + h - 1) >= H) {
h = H - y;
}
SPI.beginTransaction(spi_settings);
setVideoRamPosition(x, y, x + w - 1, y + h - 1);
sendCommandAndContinue(CMD_WRITE_TO_RAM);
for(y = h; y > 0; --y) {
for(x = w; x > 1; --x) {
pushColor(color);
}
pushColor(color, true);
// At the end of every row, end the transaction to give other SPI devices a chance to communicate.
SPI.endTransaction();
// Start a new transaction, unless this is the last row
if (y) {
SPI.beginTransaction(spi_settings);
}
}
}
MEMBER_REQUIRES(std::is_same<B, NoBuffer>::value)
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, const C &color) {
// Bresenham's algorithm - thx wikpedia
if (y0 == y1) {
if (x1 > x0) {
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else if (x1 < x0) {
drawFastHLine(x1, y0, x0 - x1 + 1, color);
} else {
drawPixel(x0, y0, color);
}
return;
} else if (x0 == x1) {
if (y1 > y0) {
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else {
drawFastVLine(x0, y1, y0 - y1 + 1, color);
}
return;
}
bool steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
int16_t xbegin = x0;
if (steep) {
for (; x0<=x1; x0++) {
err -= dy;
if (err < 0) {
int16_t len = x0 - xbegin;
if (len) {
drawFastVLine(y0, xbegin, len + 1, color);
} else {
drawPixel(y0, x0, color);
}
xbegin = x0 + 1;
y0 += ystep;
err += dx;
}
}
if (x0 > xbegin + 1) {
drawFastVLine(y0, xbegin, x0 - xbegin, color);
}
} else {
for (; x0<=x1; x0++) {
err -= dy;
if (err < 0) {
int16_t len = x0 - xbegin;
if (len) {
drawFastHLine(xbegin, y0, len + 1, color);
} else {
drawPixel(x0, y0, color);
}
xbegin = x0 + 1;
y0 += ystep;
err += dx;
}
}
if (x0 > xbegin + 1) {
drawFastHLine(xbegin, y0, x0 - xbegin, color);
}
}
}