-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFX.cpp
235 lines (199 loc) · 5.1 KB
/
GFX.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "GFX.hpp"
namespace {
inline static void swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
};
/**
* Create GFX instantion
*
* @param DevAddr device i2c address.
* @param Size screen size (W128xH64 or W128xH32)
* @param i2c i2c instance
*/
GFX::GFX(uint16_t const DevAddr, size Size, i2c_inst_t * i2c) : SSD1306(DevAddr, Size, i2c) {};
/**
* @brief Draw one char.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param chr char to be written
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawChar(int x, int y, char chr, colors color)
{
if(chr > 0x7E) return; // chr > '~'
for(uint8_t i=0; i < this->font[1]; i++ )
{
uint8_t line = (uint8_t)(this->font)[(chr-0x20) * (this->font)[1] + i + 2];
for(int8_t j=0; j<this->font[0]; j++, line >>= 1)
{
if(line & 1)
{
this->drawPixel(x+i, y+j, color);
}
}
}
}
/**
* @brief Draw string.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param str string to be written
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawString(int x, int y, std::string str, colors color)
{
int x_tmp = x;
while(str.length())
{
this->drawChar(x_tmp, y, str.front(), color);
x_tmp += ((uint8_t)font[1]) + 1;
str.erase(str.begin());
}
}
/**
* @brief Draw empty rectangle.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param w width of the rectangle
* @param h height of the rectangle
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawRectangle(int x, int y, uint16_t w, uint16_t h, colors color)
{
this->drawHorizontalLine(x, y, w, color);
this->drawHorizontalLine(x, y+h-1, w, color);
this->drawVerticalLine(x, y, h, color);
this->drawVerticalLine(x+w-1, y, h, color);
}
/**
* @brief Draw filled rectangle.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param w width of the rectangle
* @param h height of the rectangle
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawFillRectangle(int x, int y, uint16_t w, uint16_t h, colors color)
{
for (int i=x; i<x+w; i++) {
this->drawVerticalLine(i, y, h, color);
}
}
/**
* @brief Draw progress bar.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param w width of the rectangle
* @param h height of the rectangle
* @param progress progress (0, 100)
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawProgressBar(int x, int y, uint16_t w, uint16_t h, uint8_t progress, colors color)
{
this->drawRectangle(x, y, w, h, color);
this->drawFillRectangle(x, y, (uint8_t)((w*progress)/100), h, color);
}
/**
* @brief Draw vertical line.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param h height of the line
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawVerticalLine(int x, int y, int h, colors color)
{
this->drawLine(x, y, x, y+h-1, color);
}
/**
* @brief Draw horizontal line.
*
* @param x position from the left edge (0, MAX WIDTH)
* @param y position from the top edge (0, MAX HEIGHT)
* @param w width of the line
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawHorizontalLine(int x, int y, int w, colors color)
{
this->drawLine(x, y, x+w-1, y, color);
}
/**
* @brief Draw straight line.
*
* @param x_start position of the first point from the left edge (0, MAX WIDTH)
* @param y_start position of the first point from the top edge (0, MAX HEIGHT)
* @param x_end position of the second point from the left edge (0, MAX WIDTH)
* @param y_end position of the second point from the top edge (0, MAX HEIGHT)
* @param color colors::BLACK, colors::WHITE or colors::INVERSE
*/
void GFX::drawLine(int x_start, int y_start, int x_end, int y_end, colors color)
{
int16_t steep = abs(y_end - y_start) > abs(x_end - x_start);
if (steep)
{
swap(x_start, y_start);
swap(x_end, y_end);
}
if (x_start > x_end)
{
swap(x_start, x_end);
swap(y_start, y_end);
}
int16_t dx = x_end - x_start;
int16_t dy = abs(y_end - y_start);
int16_t err = dx / 2;
int16_t ystep;
if (y_start < y_end)
{
ystep = 1;
}
else
{
ystep = -1;
}
while (x_start <= x_end)
{
if (steep)
{
this->drawPixel(y_start, x_start, color);
}
else
{
this->drawPixel(x_start, y_start, color);
}
x_start++;
err -= dy;
if (err < 0)
{
y_start += ystep;
err += dx;
}
}
}
/**
* @brief Set your own font
*
* @param font Pointer to array with your font
*/
void GFX::setFont(const uint8_t* font)
{
this->font = font;
}
/**
* @brief Get pointer to font array
*
* @return Pointer to array with the currently used font
*/
const uint8_t* GFX::getFont()
{
return font;
}