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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#include "gui.h"
static struct guiNode_t *firstGUI = NULL;
uint16_t x, y; // global touchpad coordinates
static uint8_t addElement(struct guiNode_t *newNode) {
struct guiNode_t *new;
if(firstGUI == NULL) {
firstGUI = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(firstGUI == NULL)
return 0;
*firstGUI = *newNode;
firstGUI->next = NULL;
} else {
new = firstGUI;
while(new->next != NULL)
new = new->next;
new->next = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(new->next == NULL)
return 0;
new = new->next;
*new = *newNode;
new->next = NULL;
}
return 1;
}
static uint8_t deleteElement(char *label) {
struct guiNode_t *pointer, *pointer1;
if(firstGUI != NULL) {
if(strcmp(firstGUI->label, label) == 0) {
pointer = firstGUI->next;
chHeapFree(firstGUI);
firstGUI = pointer;
} else {
pointer = firstGUI;
while(pointer->next != NULL) {
pointer1 = pointer->next;
if(strcmp(firstGUI->label, label) == 0) {
pointer->next = pointer1->next;
chHeapFree(pointer1);
break;
}
pointer = pointer1;
}
}
return 1; // successful
}
return 0; // not successful
}
void guiPrintElements(BaseSequentialStream *chp) {
struct guiNode_t *pointer = firstGUI;
chprintf(chp, "\r\n\nguiNodes:\r\n\n");
while(pointer != NULL) {
chprintf(chp, "x0: %d\r\n", pointer->x0);
chprintf(chp, "y0: %d\r\n", pointer->y0);
chprintf(chp, "x1: %d\r\n", pointer->x1);
chprintf(chp, "y1: %d\r\n", pointer->y1);
chprintf(chp, "label: %s\r\n", pointer->label);
chprintf(chp, "active: %d\r\n", *(pointer->active));
chprintf(chp, "state: %d\r\n", *(pointer->state));
chprintf(chp, "*next: 0x%x\r\n", pointer->next);
chprintf(chp, "\r\n\n");
pointer = pointer->next;
}
}
static inline void buttonUpdate(struct guiNode_t *node) {
if(x >= node->x0 && x <= node->x1 && y >= node->y0 && y <= node->y1) {
*(node->state) = 1;
} else {
*(node->state) = 0;
}
}
static inline void sliderUpdate(struct guiNode_t *node) {
uint16_t length;
if(node->orientation == horizontal)
length = node->x1 - node->x0;
else if(node->orientation == vertical)
length = node->y1 - node->y0;
if(node->mode == modePassive) {
node->percentage = *(node->state);
} else if(node->mode == modeActive) {
if(x >= node->x0 && x <= node->x1 && y >= node->y0 && y <= node->y1) {
if(node->orientation == horizontal) {
node->percentage = (((x - node->x0) * 100) / length);
} else if(node->orientation == vertical) {
node->percentage = (((y - node->y0) * 100) / length);
}
}
*(node->state) = node->percentage;
}
// a bit of safety here
if(node->percentage > 100)
node->percentage = 100;
if(node->percentage < 0)
node->percentage = 0;
if(node->orientation == horizontal) {
node->value = ((((node->x1)-(node->x0)) * node->percentage) / 100);
if(node->oldValue > node->value || node->value == 0)
lcdFillArea(node->x0+1, node->y0+1, node->x1, node->y1, node->bkColor);
else
lcdDrawRect(node->x0+1, node->y0+1, node->x0+node->value, node->y1, filled, node->valueColor);
} else if(node->orientation == vertical) {
node->value = ((((node->y1)-(node->y0)) * node->percentage) / 100);
if(node->oldValue > node->value || node->value == 0)
lcdFillArea(node->x0+1, node->y0+1, node->x1, node->y1, node->bkColor);
else
lcdDrawRect(node->x0+1, node->y0+1, node->x1, node->y0+node->value, filled, node->valueColor);
}
node->oldValue = node->value;
}
static inline void wheelUpdate(struct guiNode_t *node) {
(void)node;
}
static inline void keymatrixUpdate(struct guiNode_t *node) {
(void)node;
}
static void guiThread(const uint16_t interval) {
struct guiNode_t *node;
chRegSetThreadName("GUI");
while(TRUE) {
for(node = firstGUI; node; node = node->next) {
// check if GUI element is set active
if(*(node->active) == active) {
x = tpReadX();
y = tpReadY();
switch(node->type) {
case button:
buttonUpdate(node);
break;
case slider:
sliderUpdate(node);
break;
case wheel:
wheelUpdate(node);
break;
case keymatrix:
keymatrixUpdate(node);
break;
}
}
}
chThdSleepMilliseconds(interval);
}
}
Thread *guiInit(uint16_t interval, tprio_t priority) {
Thread *tp = NULL;
tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(512), priority, guiThread, interval);
return tp;
}
uint8_t guiDeleteElement(char *label) {
return deleteElement(label);
}
uint8_t guiDrawButton(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, char *str, font_t font, uint16_t fontColor, uint16_t buttonColor, uint16_t shadow, char *label, uint8_t *active, uint8_t *state) {
struct guiNode_t *newNode;
uint16_t i;
newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(newNode == NULL)
return 0;
newNode->type = button;
newNode->label = label;
newNode->x0 = x0;
newNode->y0 = y0;
newNode->x1 = x1;
newNode->y1 = y1;
newNode->shadow = shadow;
newNode->active = active;
newNode->state = state;
if(addElement(newNode) != 1)
return 0;
lcdDrawRectString(x0, y0, x1, y1, str, font, fontColor, buttonColor);
if(shadow != 0) {
for(i = 0; i < shadow; i++) {
lcdDrawLine(x0+shadow, y1+i, x1+shadow-1, y1+i, Black);
lcdDrawLine(x1+i, y0+shadow, x1+i, y1+shadow-1, Black);
}
}
chHeapFree(newNode);
return 1;
}
uint8_t guiDrawSlider(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t orientation, uint8_t mode, uint16_t frameColor, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value) {
struct guiNode_t *newNode;
newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(newNode == NULL)
return 0;
newNode->type = slider;
newNode->label = label;
newNode->x0 = x0;
newNode->y0 = y0;
newNode->x1 = x1;
newNode->y1 = y1;
newNode->mode = mode;
newNode->bkColor = bkColor;
newNode->valueColor = valueColor;
newNode->state = value;
newNode->active = active;
newNode->orientation = orientation;
newNode->percentage = 0;
if(addElement(newNode) != 1)
return 0;
(void)bkColor;
(void)valueColor;
// lcdDraw functions
lcdDrawRect(x0, y0, x1, y1, frame, frameColor);
chHeapFree(newNode);
return 1;
}
uint8_t guiDrawWheel(uint16_t x0, uint16_t y0, uint16_t radius1, uint16_t radius2, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value) {
struct guiNode_t *newNode;
newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(newNode == NULL)
return 0;
newNode->type = wheel;
newNode->label = label;
newNode->x0 = x0;
newNode->y0 = y0;
newNode->r1 = radius1;
newNode->r2 = radius2;
newNode->active = active;
newNode->state = value;
if(addElement(newNode) != 1)
return 0;
(void)bkColor;
(void)valueColor;
// lcdDraw functions
chHeapFree(newNode);
return 1;
}
uint8_t guiDrawKeymatrix(uint16_t x0, uint16_t y0, uint16_t size, uint16_t space, uint16_t shadow, uint16_t buttonColor, uint16_t fontColor, font_t font, char *label, uint8_t *active, uint8_t *value) {
struct guiNode_t *newNode;
newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(newNode == NULL)
return 0;
newNode->type = keymatrix;
newNode->label = label;
newNode->x0 = x0;
newNode->y0 = y0;
newNode->shadow = shadow;
newNode->active = active;
newNode->state = value;
if(addElement(newNode) != 1)
return 0;
// lcdDraw functions
chHeapFree(newNode);
return 1;
}
|