-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.cpp
More file actions
379 lines (326 loc) · 11.1 KB
/
ui.cpp
File metadata and controls
379 lines (326 loc) · 11.1 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "ui.h"
#include "keyboard.h"
#include "geometry.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_HX8357.h>
#include <Adafruit_TSC2007.h>
#define TFT_RST -1
#ifdef ESP8266
#define STMPE_CS 16
#define TFT_CS 0
#define TFT_DC 15
#define SD_CS 2
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32C6)
#define STMPE_CS 6
#define TFT_CS 7
#define TFT_DC 8
#define SD_CS 5
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3)
#define STMPE_CS 32
#define TFT_CS 15
#define TFT_DC 33
#define SD_CS 14
#elif defined(TEENSYDUINO)
#define TFT_DC 10
#define TFT_CS 4
#define STMPE_CS 3
#define SD_CS 8
#elif defined(ARDUINO_STM32_FEATHER)
#define TFT_DC PB4
#define TFT_CS PA15
#define STMPE_CS PC7
#define SD_CS PC5
#elif defined(ARDUINO_NRF52832_FEATHER) /* BSP 0.6.5 and higher! */
#define TFT_DC 11
#define TFT_CS 31
#define STMPE_CS 30
#define SD_CS 27
#elif defined(ARDUINO_MAX32620FTHR) || defined(ARDUINO_MAX32630FTHR)
#define TFT_DC P5_4
#define TFT_CS P5_3
#define STMPE_CS P3_3
#define SD_CS P3_2
#else
// Anything else, defaults!
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
#endif
// Init screen on hardware SPI, HX8357D type:
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
#if defined(_ADAFRUIT_STMPE610H_)
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#elif defined(_ADAFRUIT_TSC2007_H)
// If you're using the TSC2007 there is no CS pin needed, so instead its an IRQ!
#define TSC_IRQ STMPE_CS
Adafruit_TSC2007 ts = Adafruit_TSC2007(); // newer rev 2 touch contoller
#else
#error("You must have either STMPE or TSC2007 headers included!")
#endif
// This is calibration data for the raw touch data to the screen coordinates
// For STMPE811/STMPE610
#define STMPE_TS_MINX 3800
#define STMPE_TS_MAXX 100
#define STMPE_TS_MINY 100
#define STMPE_TS_MAXY 3750
// For TSC2007
#define TSC_TS_MINX 300
#define TSC_TS_MAXX 3800
#define TSC_TS_MINY 185
#define TSC_TS_MAXY 3700
#define TSC_IRQ STMPE_CS
// Size of the color selection boxes and the paintbrush size
#define PENRADIUS 3
#define TEXT_COLOR HX8357_RED
typedef struct {
Box_2D obj;
uint16_t background_color;
Translation_2D location;
} UI_Element;
typedef struct {
const Keyboard::Keyboard* board;
Translation_2D location;
} UI_Keyboard;
UI_Element send_button;
UI_Element text_area;
UI_Element history_area;
UI_Keyboard regular = {
.board = &Keyboard::regular,
.location = { .x = 10, .y = KEY_WIDTH * 12 }
};
UI_Keyboard numeric = {
.board = &Keyboard::numeric,
.location = { .x = 10, .y = KEY_WIDTH * 12 }
};
UI_Keyboard* current_keyboard = ®ular;
// At textSize(2) the screen fits 26 characters per line
uint16_t text_2_width;
uint16_t text_2_height;
ChatMessage current_msg = { "Me", { 0 } };
int text_cursor = 0;
void clear_area(Adafruit_HX8357* tft, UI_Element* area) {
tft->fillRect(area->location.x, area->location.y, area->obj.w, area->obj.h, area->background_color);
}
void draw_send(Adafruit_HX8357* tft, UI_Element* send) {
tft->fillRect(send->location.x, send->location.y, send->obj.w, send->obj.h, send->background_color);
tft->drawRect(send->location.x, send->location.y, send->obj.w, send->obj.h, KEY_BORDER_COLOR);
tft->setTextSize(KEY_TEXT_SIZE);
tft->setCursor(send->location.x + send->obj.w / 3, send->location.y + send->obj.h / 8);
tft->print("SEND");
}
void draw_text_area_cursor(Adafruit_HX8357* tft, int cursor, uint color) {
int row = cursor / LINE_WIDTH;
int col = cursor - row * LINE_WIDTH;
tft->drawFastVLine(
text_2_width * col,
text_area.location.y + text_2_height * row,
text_2_height,
color
);
}
// paints over the given index with the background color
void delete_text_buffer_index(Adafruit_HX8357* tft, int index) {
int16_t dirty_row = index / LINE_WIDTH;
int16_t dirty_col = index - dirty_row * LINE_WIDTH;
Serial.printf("Computed bounds for %d (%d, %d): (%d, %d, %d, %d)\n", index, dirty_col, dirty_row, dirty_col * text_2_width, dirty_row * text_2_height, text_2_width, text_2_height);
tft->fillRect(
dirty_col * text_2_width,
text_area.location.y + dirty_row * text_2_height,
text_2_width,
text_2_height,
text_area.background_color
);
}
void draw_keyboard(Adafruit_HX8357* tft, UI_Keyboard* board) {
Serial.printf("Drawing %d keys:\n", board->board->total_keys);
tft->setTextColor(TEXT_COLOR);
tft->setTextSize(KEY_TEXT_SIZE);
for (int i = 0; i < board->board->total_keys; i++) {
Keyboard::Key k = board->board->keys[i];
Serial.printf("-> %c (%d, %d)\n", k.key, k.col, k.row);
TS_Point local_key_p = key_location(&k);
TS_Point key_p = translate(&local_key_p, &board->location);
tft->fillRect(key_p.x, key_p.y, KEY_WIDTH * k.u, KEY_WIDTH, KEY_COLOR);
tft->drawRect(key_p.x, key_p.y, KEY_WIDTH * k.u, KEY_WIDTH, KEY_BORDER_COLOR);
tft->setCursor(key_p.x + (KEY_WIDTH * k.u / 4), key_p.y + (KEY_WIDTH / 8));
tft->printf("%c", k.key);
}
Serial.println("Key drawing complete!");
}
#define CHAT_HISTORY_SIZE 6
ChatMessage history[CHAT_HISTORY_SIZE] = { 0 };
uint used_history_entries = 0;
uint next_history = 0;
void copy_chat(ChatMessage* target, const ChatMessage* source) {
memcpy(target, source, sizeof(ChatMessage));
}
void record_chat(ChatMessage* msg) {
copy_chat(history + next_history, msg);
next_history = (next_history + 1) % CHAT_HISTORY_SIZE;
used_history_entries = min(CHAT_HISTORY_SIZE, used_history_entries + 1);
}
// we will assign the calibration values on init
int16_t min_x, max_x, min_y, max_y;
void setup_ui(const char* this_participant) {
Serial.println("Initializing UI");
strncpy(current_msg.author, this_participant, LINE_WIDTH - 1);
#if defined(_ADAFRUIT_STMPE610H_)
if (!ts.begin()) {
Serial.println("Couldn't start STMPE touchscreen controller");
while (1) delay(100);
}
min_x = STMPE_TS_MINX;
max_x = STMPE_TS_MAXX;
min_y = STMPE_TS_MINY;
max_y = STMPE_TS_MAXY;
#else
if (!ts.begin(0x48, &Wire)) {
Serial.println("Couldn't start TSC2007 touchscreen controller");
while (1) delay(100);
}
min_x = TSC_TS_MINX;
max_x = TSC_TS_MAXX;
min_y = TSC_TS_MINY;
max_y = TSC_TS_MAXY;
pinMode(TSC_IRQ, INPUT);
#endif
Serial.println("Touchscreen started");
tft.begin();
tft.fillScreen(HX8357_BLACK);
// find single character bounding box
int16_t x;
int16_t y;
tft.setTextSize(2);
tft.getTextBounds("r", 0, 0, &x, &y, &text_2_width, &text_2_height);
Serial.printf("Found character bounding box: (%d, %d)\n", text_2_width, text_2_height);
// landscape with USB port at bottom
tft.setRotation(2);
text_area = {
.obj = {.x = 0, .y = 0, .w = tft.width(), .h = TEXT_BUFFER_LINES * text_2_height },
.background_color = HX8357_BLUE,
.location = {.x = 0, .y = current_keyboard->location.y - TEXT_BUFFER_LINES * text_2_height }
};
send_button = {
.obj = {.x = 0, .y = 0, .w = tft.width(), .h = KEY_WIDTH},
.background_color = KEY_COLOR,
.location = {.x = 0, .y = text_area.location.y - KEY_WIDTH}
};
history_area = {
.obj = {.x = 0, .y = 0, .w = tft.width(), .h = send_button.location.y},
.background_color = HX8357_GREEN,
.location = {.x = 0, .y = 0}
};
clear_area(&tft, &text_area);
draw_keyboard(&tft, current_keyboard);
draw_send(&tft, &send_button);
clear_area(&tft, &history_area);
}
#define DEBOUNCE_DELAY_MS 200
unsigned long last_keypress_ms = 0;
void update_chat_history() {
clear_area(&tft, &history_area);
int display_entry = CHAT_HISTORY_SIZE - 1;
for (int msg = 0; msg < used_history_entries; msg++) {
int msg_offset = next_history - 1 - msg;
int msg_index = 0 <= msg_offset ? msg_offset : CHAT_HISTORY_SIZE + msg_offset;
Serial.printf("Printing msg %d, index %d\n", msg, msg_index);
tft.setCursor(0, text_2_height * display_entry * 3);
tft.println(history[msg_index].author);
tft.println(history[msg_index].message);
display_entry--;
}
}
bool update_ui(ChatMessage* out) {
#if defined(TSC_IRQ)
if (digitalRead(TSC_IRQ)) {
// IRQ pin is high, nothing to read!
return false;
}
#endif
TS_Point p = ts.getPoint();
Serial.print("X = ");
Serial.print(p.x);
Serial.print("\tY = ");
Serial.print(p.y);
Serial.print("\tPressure = ");
Serial.print(p.z);
if (((p.x == 0) && (p.y == 0)) || (p.z < 10)) {
Serial.printf("Rejected point (%d, %d, %d)\n", p.x, p.y, p.z);
return false; // no pressure, no touch
}
/* When the user presses the screen, it generates a series of presses
* at varying pressure levels. Ideally we need to reduce these to one
* discrete screen press for each logical press.
*/
unsigned long current_time = millis();
if (current_time < last_keypress_ms + DEBOUNCE_DELAY_MS) {
// reject this keypress as a duplicate
Serial.println("Rejecting duplicate keypress");
return false;
} else {
// accept this keypress, record keypress time
last_keypress_ms = current_time;
}
// Scale from ~0->4000 to tft.width using the calibration #'s
// These values get confusing because .height() and .width() account for the
// screen rotation but .getPoint() does not.
int scaled_x = map(p.x, max_x, min_x, 0, tft.width());
int scaled_y = map(p.y, max_y, min_y, 0, tft.height());
p.x = scaled_x;
p.y = scaled_y;
Serial.print(" -> ");
Serial.print(p.x);
Serial.print(", ");
Serial.println(p.y);
TS_Point keyboard_point = inverse_translate(&p, ¤t_keyboard->location);
const Keyboard::Key* k = Keyboard::check_keypress(&keyboard_point, current_keyboard->board);
int dirty = -1;
if (k != NULL) {
Serial.printf("Pushed key %c\n", k->key);
if (k->key == '<') {
// backspace
text_cursor = max(0, text_cursor - 1);
dirty = text_cursor;
} else if (k->key == '#' && current_keyboard == ®ular) {
current_keyboard = &numeric;
draw_keyboard(&tft, current_keyboard);
} else if (k->key == 'A' && current_keyboard == &numeric) {
current_keyboard = ®ular;
draw_keyboard(&tft, current_keyboard);
} else {
current_msg.message[text_cursor] = k->key;
text_cursor = min(TEXT_BUFFER_SIZE - 1, text_cursor + 1);
}
}
TS_Point translated_p = inverse_translate(&p, &send_button.location);
bool send_pushed = box_intersect(&translated_p, &send_button.obj);
bool should_send_msg = send_pushed && text_cursor > 0;
if (should_send_msg) {
current_msg.message[text_cursor] = 0;
record_chat(¤t_msg);
copy_chat(out, ¤t_msg);
text_cursor = 0;
}
if (((p.y - PENRADIUS) > 0) && ((p.y + PENRADIUS) < tft.height())) {
tft.fillCircle(p.x, p.y, PENRADIUS, HX8357_BLUE);
}
current_msg.message[text_cursor] = 0;
if (dirty != -1) {
delete_text_buffer_index(&tft, dirty);
draw_text_area_cursor(&tft, dirty + 1, text_area.background_color);
}
if (should_send_msg) {
clear_area(&tft, &text_area);
}
tft.setTextSize(2);
tft.setCursor(0, text_area.location.y);
tft.print(current_msg.message);
// clear old cursor and draw new one
draw_text_area_cursor(&tft, max(0, text_cursor - 1), text_area.background_color);
draw_text_area_cursor(&tft, text_cursor, TEXT_COLOR);
if (should_send_msg) {
update_chat_history();
}
return should_send_msg;
}