-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
262 lines (210 loc) · 7.89 KB
/
display.c
File metadata and controls
262 lines (210 loc) · 7.89 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//------------------------------------------------------------------------------
// This is Open source software. You can place this code on your site, but don't
// forget a link to my YouTube-channel: https://www.youtube.com/channel/UChButpZaL5kUUl_zTyIDFkQ
// Это программное обеспечение распространяется свободно. Вы можете размещать
// его на вашем сайте, но не забудьте указать ссылку на мой YouTube-канал
// "Электроника в объектике" https://www.youtube.com/channel/UChButpZaL5kUUl_zTyIDFkQ
// Автор: Надыршин Руслан / Nadyrshin Ruslan
//------------------------------------------------------------------------------
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "display.h"
#include "fonts/font.h"
#include "st7920.h"
uint8_t sFrameBuf[(DISPLAY_Width * DISPLAY_Height) / 8];
// инициализирует 1-цветый дисплей
void display_Init()
{
ST7920_Init(DISPLAY_Width, DISPLAY_Height);
}
// заполняет буфер кадра значением FillValue
void display_FillScreenbuff (uint8_t value)
{
memset (sFrameBuf, value, sizeof(sFrameBuf));
}
// обновляет состояние индикаторов в соответствии с буфером кадра disp1color_buff
void display_UpdateFromBuff (void)
{
ST7920_DisplayFullUpdate (sFrameBuf, sizeof(sFrameBuf));
}
// выводит на дисплей форматированную строку
uint8_t display_printf (int16_t X, int16_t Y, uint8_t FontID, const char *args, ...)
{
char StrBuff[60];
va_list ap;
va_start(ap, args);
char len = vsnprintf (StrBuff, sizeof(StrBuff), args, ap);
va_end(ap);
display_DrawString (X, Y, FontID, (uint8_t *) StrBuff);
return len;
}
// устанавливает состояние 1 пикселя дисплея
void display_DrawPixel (int16_t x, int16_t y, uint8_t value)
{
// Проверяем, находится ли точка в поле отрисовки дисплея
if ((x >= DISPLAY_Width) || (y >= DISPLAY_Height) || (x < 0) || (y < 0))
return;
uint16_t byteIdx = y >> 3;
uint8_t bitIdx = y - (byteIdx << 3); // Высота относительно строки байт (0<=Y<=7)
byteIdx *= DISPLAY_Width;
byteIdx += x;
if (value)
sFrameBuf[byteIdx] |= (1 << bitIdx);
else
sFrameBuf[byteIdx] &= ~(1 << bitIdx);
}
// рисует прямую линию в буфере кадра дисплея
void display_Line (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t val)
{
const int16_t deltaX = abs (x2 - x1);
const int16_t deltaY = abs (y2 - y1);
const int16_t signX = x1 < x2 ? 1 : -1;
const int16_t signY = y1 < y2 ? 1 : -1;
int16_t error = deltaX - deltaY;
display_DrawPixel (x2, y2, val);
while (x1 != x2 || y1 != y2)
{
display_DrawPixel (x1, y1, val);
const int16_t error2 = error * 2;
if (error2 > -deltaY)
{
error -= deltaY;
x1 += signX;
}
if (error2 < deltaX)
{
error += deltaX;
y1 += signY;
}
}
}
void display_FillRectangle (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t fill)
{
for(uint8_t i = y1; i <= y2; ++i)
{
display_Line(x1, i, x2, i, fill);
}
}
// рисует прямоугольник в буфере кадра дисплея
void display_DrawRectangle (int16_t x1, int16_t y1, int16_t x2, int16_t y2)
{
display_DrawLine (x1, y1, x1, y2);
display_DrawLine (x2, y1, x2, y2);
display_DrawLine (x1, y1, x2, y1);
display_DrawLine (x1, y2, x2, y2);
}
// рисует окружность в буфере кадра дисплея. x0 и y0 - координаты центра окружности
void display_DrawCircle (int16_t x0, int16_t y0, int16_t radius)
{
int x = 0;
int y = radius;
int delta = 1 - 2 * radius;
int error = 0;
while (y >= 0)
{
display_DrawPixel (x0 + x, y0 + y, 1);
display_DrawPixel (x0 + x, y0 - y, 1);
display_DrawPixel (x0 - x, y0 + y, 1);
display_DrawPixel (x0 - x, y0 - y, 1);
error = 2 * (delta + y) - 1;
if (delta < 0 && error <= 0)
{
++x;
delta += 2 * x + 1;
continue;
}
error = 2 * (delta - x) - 1;
if (delta > 0 && error > 0)
{
--y;
delta += 1 - 2 * y;
continue;
}
++x;
delta += 2 * (x - y);
--y;
}
}
// вывода символа Char на дисплей. Возвращает ширину выведенного символа
uint8_t display_DrawChar (int16_t x, int16_t y, uint8_t fontId, uint8_t c)
{
// Указатель на подтабличку конкретного символа шрифта
uint8_t *charTable = font_GetFontStruct (fontId, c);
uint8_t charWidth = charTable[0]; // Ширина символа
uint8_t charHeight = charTable[1]; // Высота символа
charTable += 2; //skip width and height
if (fontId == FONTID_6X8M)
{
for (uint8_t row = 0; row < charHeight; ++row)
{
uint8_t chRow = charTable[row];
uint8_t mask = 0b10000000;
for (uint8_t col = 0; col < charWidth; ++col)
{
display_DrawPixel (x + col, y, chRow & mask);
mask >>= 1;
}
++y;
}
}
return charWidth;
}
uint8_t display_DrawInvertedChar (int16_t x, int16_t y, uint8_t fontId, uint8_t c)
{
// Указатель на подтабличку конкретного символа шрифта
uint8_t *charTable = font_GetFontStruct (fontId, c);
uint8_t charWidth = charTable[0]; // Ширина символа
uint8_t charHeight = charTable[1]; // Высота символа
charTable += 2; //skip width and height
if (fontId == FONTID_6X8M)
{
for (uint8_t row = 0; row < charHeight; ++row)
{
uint8_t chRow = ~(charTable[row]);
uint8_t mask = 0b10000000;
for (uint8_t col = 0; col < charWidth; ++col)
{
display_DrawPixel (x + col, y, chRow & mask);
mask >>= 1;
}
++y;
}
}
return charWidth;
}
// вывода текста из строки Str на дисплей
void display_DrawString (int16_t x, int16_t y, uint8_t fontId, const uint8_t* s)
{
uint8_t strHeight = 8; // Высота символов в пикселях для перехода на следующую строку
bool inverted = false;
// Вывод строки
for(int16_t i=x; *s; ++s)
{
switch (*s)
{
case '\n': // Переход на следующую строку
y += strHeight;
break;
case '\r': // Переход в начало строки
i = x;
break;
case '\b':
inverted = !inverted;
break;
default: // Отображаемый символ
if(inverted)
{
i += display_DrawInvertedChar (i, y, fontId, *s);
}
else
{
i += display_DrawChar (i, y, fontId, *s);
}
strHeight = font_GetCharHeight (font_GetFontStruct (fontId, *s));
break;
}
}
}