-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFX.cpp
133 lines (101 loc) · 2.88 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
#include "GFX.hpp"
GFX::GFX(uint16_t const DevAddr, uint8_t const width, uint8_t const height, i2c_inst_t * i2c, uint8_t resetPin) : SSD1309(DevAddr, width, height, i2c, resetPin) {};
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 & size == 1)
{
this->drawPixel(x+i, y+j, color);
}
}
}
}
void GFX::drawCharArray(int x, int y, char* str, colors color)
{
int x_tmp = x;
char znak;
znak = *str;
while(*str++)
{
this->drawChar(x_tmp, y, znak, color);
x_tmp += ((uint8_t)font[1] * size) + 1;
znak = *str;
}
}
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] * size) + 1;
str.erase(str.begin());
}
}
void GFX::drawRectangle(int x, int y, uint16_t w, uint16_t h, colors color)
{
this->drawFastHLine(x, y, w, color);
this->drawFastHLine(x, y+h-1, w, color);
this->drawFastVLine(x, y, h, color);
this->drawFastVLine(x+w-1, y, h, color);
}
void GFX::drawFillRectangle(int x, int y, uint16_t w, uint16_t h, colors color)
{
for (int i=x; i<x+w; i++) {
this->drawFastVLine(i, y, h, color);
}
}
void GFX::drawProgressBar(int x, int y, uint16_t w, uint16_t h, uint8_t progress, colors color)
{
if(progress<0) progress = 0;
if(progress>100) progress = 100;
this->drawRectangle(x, y, w, h, color);
this->drawFillRectangle(x, y, (uint8_t)((w*progress)/100), h, color);
}
void GFX::drawFastVLine(int x_start, int y_start, int h, colors color)
{
this->writeLine(x_start, y_start, x_start, y_start+h-1, color);
}
void GFX::drawFastHLine(int x_start, int y_start, int w, colors color)
{
this->writeLine(x_start, y_start, x_start+w-1, y_start, color);
}
void GFX::writeLine(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_int(x_start, y_start);
_swap_int(x_end, y_end);
}
if (x_start > x_end) {
_swap_int(x_start, x_end);
_swap_int(y_start, y_end);
}
int16_t dx, dy;
dx = x_end - x_start;
dy = abs(y_end - y_start);
int16_t err = dx / 2;
int16_t ystep;
if (y_start < y_end) {
ystep = 1;
} else {
ystep = -1;
}
for (; x_start<=x_end; x_start++) {
if (steep) {
this->drawPixel(y_start, x_start, color);
} else {
this->drawPixel(x_start, y_start, color);
}
err -= dy;
if (err < 0) {
y_start += ystep;
err += dx;
}
}
}