-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoGigaLvglDisplay.ino
More file actions
545 lines (489 loc) · 15.9 KB
/
ArduinoGigaLvglDisplay.ino
File metadata and controls
545 lines (489 loc) · 15.9 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
* @file ArduinoGigaLvglDisplay.ino
* @author Jadon Jung (jadonjung3@gmail.com)
* @brief Arduino Giga LVGL Display For Robot Car
* @version 1.0
* @date 2026-02-22
*
* @copyright Copyright (c) 2026
*
*/
/**
* @defgroup Imports All the imports needed
* @brief Includes all necessary libraries and headers for LVGL and Arduino functionality.
*
*/
#include <Arduino_GigaDisplayTouch.h>
// #include <lv_api_map_v8.h>
#include <lv_api_map_v9_0.h>
#include <lv_api_map_v9_1.h>
#include <lv_api_map_v9_2.h>
#include <lv_api_map_v9_3.h>
#include <lv_conf_internal.h>
#include <lv_conf_kconfig.h>
#include <lv_init.h>
#include <lvgl_private.h>
#include <lvgl.h>
#include <Arduino_constants.h>
#include <motor_constants.h>
#include <msg_constants.h>
#include <other_constants.h>
#include "lvgl.h"
#include "Arduino_GigaDisplayTouch.h"
#include "Arduino_H7_Video.h"
#include <vector>
/** @} */ // end of Imports
/**
* @defgroup Motor_Pins Motor control pin definitions
* @brief Initialize motor control pins
*
*/
enum MotorPins
{
ENB_B = 13,
IN4_B = 12,
IN3_B = 11,
IN2_B = 10,
IN1_B = 9,
ENA_B = 8,
ENB_A = 7,
IN4_A = 6,
IN3_A = 5,
IN2_A = 4,
IN1_A = 3,
ENA_A = 2
};
/** @} */ // end of Motor_Pins
// ===== Shared label + vector logic =====
/**
* @brief Applies the selected movement mode and direction.
* This function creates a temporary message label on the screen
* indicating the applied mode and direction. It also appends the
* direction to the other_constants::program vector for later execution.
* @param mode_name mode of movement
* @param direction direction of movement
*/
static void apply_mode(const char *mode_name, const char *direction)
{
if (msg_constants::msg_label != NULL)
{
lv_obj_del(msg_constants::msg_label);
msg_constants::msg_label = NULL;
}
msg_constants::msg_label = lv_label_create(lv_scr_act());
char buffer[64];
snprintf(buffer, sizeof(buffer), "Applied %s Mode: %s.", mode_name, direction);
lv_label_set_text(msg_constants::msg_label, buffer);
other_constants::program.push_back(direction);
lv_obj_set_style_text_font(msg_constants::msg_label, &lv_font_montserrat_24, 0);
lv_obj_align(msg_constants::msg_label, LV_ALIGN_BOTTOM_MID, 0, -20);
msg_constants::msg_start_time = millis();
}
// ===== Run Program (special) =====
/**
* @brief Handles the "Run Program" button event.
*
* This callback is triggered when the user presses the **Prgm Car** button
* in the button matrix. It displays a temporary "Applied program!" message
* on the screen, prints all stored program steps to the serial monitor,
* clears the program list, and records the timestamp so the message can
* be removed later.
*
* The function also deletes any previous message label before creating
* a new one.
*
* @param e Pointer to the LVGL event descriptor. Contains information
* about the event and the object that triggered it, although
* this callback does not use it directly.
*/
static void btn_Run_Program_event_cb(lv_event_t *e)
{
if (msg_constants::msg_label != NULL)
{
lv_obj_del(msg_constants::msg_label);
msg_constants::msg_label = NULL;
}
msg_constants::msg_label = lv_label_create(lv_scr_act());
lv_label_set_text(msg_constants::msg_label, "Applied program!");
// Print program to Serial Monitor (for debugging)
for (auto p : other_constants::program)
{
Serial.println(p);
}
Serial.println();
delay(500);
other_constants::program.clear();
lv_obj_set_style_text_font(msg_constants::msg_label, &lv_font_montserrat_24, 0);
lv_obj_align(msg_constants::msg_label, LV_ALIGN_BOTTOM_MID, 0, -20);
msg_constants::msg_start_time = millis();
}
// ======== CALLBACK DECLARATIONS ========
static void main_btnm_event_cb(lv_event_t *e);
static void test_btnm_event_cb(lv_event_t *e);
static void slider_event_cb(lv_event_t *e);
static void toggle_fwdrev_cb(lv_event_t *e);
// ======== MAIN BUTTON CALLBACK ========
/**
* @brief Handles all main directional and mode selection button events.
*
* This callback is triggered whenever a button in the primary button matrix
* is pressed. It reads the button's text label and maps it to a specific
* movement mode (Straight, Sideways, Diagonal, Pivot, Pivot Sideways,
* Rotate). The corresponding mode and direction string are then forwarded
* to apply_mode().
*
* If the selected button is "Prgm Car", this function instead triggers the
* special program execution callback btn_Run_Program_event_cb().
*
* @param e Pointer to the LVGL event descriptor containing details about
* the button press event (e.g., target object).
*/
static void main_btnm_event_cb(lv_event_t *e)
{
lv_obj_t *btnm = (lv_obj_t *)lv_event_get_target(e);
const char *dir_txt = lv_btnmatrix_get_btn_text(
btnm, lv_btnmatrix_get_selected_btn(btnm));
if (dir_txt == NULL)
return;
if (strcmp(dir_txt, "Forward") == 0 || strcmp(dir_txt, "Backward") == 0)
{
apply_mode("Straight", dir_txt);
}
else if (strcmp(dir_txt, "Left") == 0 || strcmp(dir_txt, "Right") == 0)
{
apply_mode("Sideways", dir_txt);
}
else if (strcmp(dir_txt, "45") == 0 || strcmp(dir_txt, "135") == 0 || strcmp(dir_txt, "225") == 0 || strcmp(dir_txt, "315") == 0)
{
apply_mode("Diagonal", dir_txt);
}
else if (strcmp(dir_txt, "Right Fwd") == 0 || strcmp(dir_txt, "Right Bwd") == 0 || strcmp(dir_txt, "Left Fwd") == 0 || strcmp(dir_txt, "Left Bwd") == 0)
{
apply_mode("Pivot", dir_txt);
}
else if (strcmp(dir_txt, "Front Right") == 0 || strcmp(dir_txt, "Front Left") == 0 || strcmp(dir_txt, "Rear Right") == 0 || strcmp(dir_txt, "Rear Left") == 0)
{
apply_mode("Pivot Sideways", dir_txt);
}
else if (strcmp(dir_txt, "CCW") == 0 || strcmp(dir_txt, "CW") == 0)
{
apply_mode("Rotate", dir_txt);
}
else if (strcmp(dir_txt, "Prgm Car") == 0)
{
btn_Run_Program_event_cb(e);
}
}
// ======== test CONTROL BUTTON CALLBACK ========
/**
* @brief Handles individual test control button events.
*
* Triggered when a test control button (FL, FR, RL, RR) is pressed.
* This callback identifies which test was selected and calls the
* appropriate test movement function using the current global
* speed and direction values.
*
* @param e Pointer to the LVGL event descriptor. Used to determine
* which button in the test control matrix was pressed.
*/
static void test_btnm_event_cb(lv_event_t *e)
{
lv_obj_t *btnm = (lv_obj_t *)lv_event_get_target(e);
const char *txt = lv_btnmatrix_get_btn_text(
btnm, lv_btnmatrix_get_selected_btn(btnm));
if (txt == NULL)
return;
if (strcmp(txt, "FL") == 0)
{
Serial.println("Front Left test Move");
FL_move(motor_constants::motor_speed * motor_constants::motor_direction);
}
else if (strcmp(txt, "FR") == 0)
{
Serial.println("Front Right test Move");
FR_move(motor_constants::motor_speed * motor_constants::motor_direction);
}
else if (strcmp(txt, "RL") == 0)
{
Serial.println("Rear Left test Move");
RL_move(motor_constants::motor_speed * motor_constants::motor_direction);
}
else if (strcmp(txt, "RR") == 0)
{
Serial.println("Rear Right test Move");
RR_move(motor_constants::motor_speed * motor_constants::motor_direction);
}
delay(500);
}
// ======== FWD/REV TOGGLE CALLBACK ========
/**
* @brief Handles toggling between Forward and Reverse.
*
* When the toggle button is pressed, this callback swaps the
* label between "Fwd" and "Rev" and updates the global direction
* variable accordingly:
* - "Fwd" → direction = -1
* - "Rev" → direction = 1
*
* A message is printed to the serial monitor showing the new mode
* and numeric direction.
*
* @param e Pointer to the LVGL event descriptor. Provides access
* to the toggle button object.
*/
static void toggle_fwdrev_cb(lv_event_t *e)
{
lv_obj_t *btn = (lv_obj_t *)lv_event_get_target(e);
lv_obj_t *label = lv_obj_get_child(btn, 0);
const char *txt = lv_label_get_text(label);
// If txt is "Fwd", change to "Rev" and assign motor_direction 1 else change to "Fwd" and assign motor_direction -1
if (strcmp(txt, "Fwd") == 0)
{
lv_label_set_text(label, "Rev");
motor_constants::motor_direction = 1;
}
else
{
lv_label_set_text(label, "Fwd");
motor_constants::motor_direction = -1;
}
Serial.print("Mode: ");
Serial.println(lv_label_get_text(label));
Serial.print("Direction set to ");
Serial.println(motor_constants::motor_direction);
}
// ======== SLIDER CALLBACK ========
/**
* @brief Updates the global speed variable when a slider is moved.
*
* This callback is triggered whenever the user interacts with a
* speed slider. It reads the slider's current value and assigns it
* to the global speed variable.
*
* If user data is attached to the event (typically a string naming
* the slider), that label is printed to the serial monitor for clarity.
*
* @param e Pointer to the LVGL event descriptor. Used to access the
* slider object and optional user data.
*/
static void slider_event_cb(lv_event_t *e)
{
lv_obj_t *slider = (lv_obj_t *)lv_event_get_target(e);
motor_speed = lv_slider_get_value(slider);
// Identify which slider this is (optional, if you name them)
const char *name = (const char *)lv_event_get_user_data(e);
Serial.print(name);
Serial.print(" speed set to ");
Serial.println(motor_constants::motor_speed);
}
// ======== UI CREATION FUNCTION ========
/**
* @brief Set the btnm bg colors object
*
* @param btnm button map object
* @param normal color at normal state
* @param pressed color at pressed state
*/
void set_btnm_bg_colors(lv_obj_t *btnm, uint32_t normal, uint32_t pressed)
{
lv_obj_set_style_bg_color(btnm, lv_color_hex(normal), LV_PART_ITEMS);
lv_obj_set_style_bg_opa(btnm, LV_OPA_COVER, LV_PART_ITEMS);
lv_obj_set_style_bg_color(btnm, lv_color_hex(pressed), LV_PART_ITEMS | LV_STATE_PRESSED);
lv_obj_set_style_bg_opa(btnm, LV_OPA_COVER, LV_PART_ITEMS | LV_STATE_PRESSED);
}
// ===== Main Button Matrix =====
/**
* @brief Create the main button matrix object
* @param screen_buttons parent screen object
* Runs the btnm_event_cb on button press
*/
void create_main_button_matrix(lv_obj_t *screen_buttons)
{
static const char *btnm_map[] = {
"Forward", "Backward", "Left", "Right", "\n",
"45", "135", "225", "315", "\n",
"Right Fwd", "Right Bwd", "Left Fwd", "Left Bwd", "\n",
"Front Right", "Front Left", "Rear Right", "Rear Left", "\n",
"CCW", "CW", "Prgm Car", ""};
lv_obj_t *btnm = lv_btnmatrix_create(screen_buttons);
lv_btnmatrix_set_map(btnm, btnm_map);
lv_obj_set_size(btnm, 460, 300);
lv_obj_align(btnm, LV_ALIGN_TOP_LEFT, 10, 0);
set_btnm_bg_colors(btnm, 0x008800, 0xe74c3c);
lv_obj_set_style_text_color(btnm, lv_color_hex(0xffffff), LV_PART_ITEMS);
lv_obj_add_event_cb(btnm, main_btnm_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}
// ===== Test Button Matrix =====
void create_test_button_matrix(lv_obj_t *screen_buttons)
{
static const char *test_btnm_map[] = {"FL", "FR", "\n", "RL", "RR", ""};
lv_obj_t *test_btnm = lv_btnmatrix_create(screen_buttons);
lv_btnmatrix_set_map(test_btnm, test_btnm_map);
lv_obj_set_size(test_btnm, 200, 120);
lv_obj_align(test_btnm, LV_ALIGN_BOTTOM_LEFT, 20, -20);
set_btnm_bg_colors(test_btnm, 0x5555ff, 0xe74c3c);
lv_obj_add_event_cb(test_btnm, test_btnm_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}
/**
* @brief Create a speed sliders object
*
* @param parent parent screen object
* Creates 4 sliders for FR, FL, RR, RL with labels and value displays
*/
// ===== Speed Sliders =====
void create_speed_sliders(lv_obj_t *parent)
{
const char *labels[] = {"FR", "FL", "RR", "RL"};
int y_positions[] = {20, 60, 100, 140};
for (int i = 0; i < 4; i++)
{
// Create label
lv_obj_t *label = lv_label_create(parent);
lv_label_set_text(label, labels[i]);
lv_obj_align(label, LV_ALIGN_TOP_RIGHT, -230, y_positions[i]);
// Create slider
lv_obj_t *slider = lv_slider_create(parent);
lv_slider_set_range(slider, 0, 255);
lv_obj_set_size(slider, 150, 20);
lv_obj_align_to(slider, label, LV_ALIGN_OUT_RIGHT_MID, 15, 0);
// Create value label
lv_obj_t *value_label = lv_label_create(parent);
char buf[4];
snprintf(buf, sizeof(buf), "%d", lv_slider_get_value(slider));
lv_label_set_text(value_label, buf);
lv_obj_align_to(value_label, slider, LV_ALIGN_OUT_RIGHT_MID, 15, 0);
// Add event callback that updates the value label
lv_obj_add_event_cb(
slider, [](lv_event_t *e)
{
lv_obj_t* s = (lv_obj_t*)lv_event_get_target(e);
lv_obj_t* val_lbl = (lv_obj_t*)lv_event_get_user_data(e);
char buf[4]; // Buffer to hold the string representation of the slider value (3 digits + null terminator)
snprintf(buf, sizeof(buf), "%d", lv_slider_get_value(s));
lv_label_set_text(val_lbl, buf); },
LV_EVENT_VALUE_CHANGED, value_label);
// Add slider event callback to update speed
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, (void *)labels[i]);
}
}
/**
* @brief Create the main UI
*
*/
void create_ui()
{
other_constants::screen_buttons = lv_obj_create(NULL);
create_main_button_matrix(other_constants::screen_buttons);
create_test_button_matrix(other_constants::screen_buttons);
// Call the new function to create speed sliders
create_speed_sliders(other_constants::screen_buttons);
// ===== Fwd/Rev Toggle Button =====
lv_obj_t *toggle_FwdRev = lv_btn_create(other_constants::screen_buttons);
lv_obj_set_size(toggle_FwdRev, 80, 40);
lv_obj_align(toggle_FwdRev, LV_ALIGN_TOP_RIGHT, -20, 180);
lv_obj_t *toggle_label = lv_label_create(toggle_FwdRev);
lv_label_set_text(toggle_label, "Fwd");
lv_obj_center(toggle_label);
// Set direction
lv_obj_add_event_cb(toggle_FwdRev, toggle_fwdrev_cb, LV_EVENT_CLICKED, NULL);
}
// ===== test movement functions =====
/**
* @brief Moves the front-right (FR) motor at the specified speed.
*
* @param speed Speed of movement (-255 to 255)
*/
void FR_move(int speed)
{
if (speed >= 0)
{
digitalWrite(MotorPins::IN1_B, HIGH);
digitalWrite(MotorPins::IN2_B, LOW);
analogWrite(MotorPins::ENA_B, speed);
}
else
{
digitalWrite(MotorPins::IN1_B, LOW);
digitalWrite(MotorPins::IN2_B, HIGH);
analogWrite(MotorPins::ENA_B, -speed);
}
}
/**
* @brief Moves the front-left (FL) motor at the specified speed.
*
* @param speed Speed of movement (-255 to 255)
*/
void FL_move(int speed)
{
if (speed >= 0)
{
digitalWrite(MotorPins::IN3_B, HIGH);
digitalWrite(MotorPins::IN4_B, LOW);
analogWrite(MotorPins::ENB_B, speed);
}
else
{
digitalWrite(MotorPins::IN3_B, LOW);
digitalWrite(MotorPins::IN4_B, HIGH);
analogWrite(MotorPins::ENB_B, -speed);
}
}
/**
* @brief Moves the rear-right (RR) motor at the specified speed.
*
* @param speed Speed of movement (-255 to 255)
*/
void RR_move(int speed)
{
if (speed >= 0)
{
digitalWrite(MotorPins::IN1_A, HIGH);
digitalWrite(MotorPins::IN2_A, LOW);
analogWrite(MotorPins::ENA_A, speed);
}
else
{
digitalWrite(MotorPins::IN1_A, LOW);
digitalWrite(MotorPins::IN2_A, HIGH);
analogWrite(MotorPins::ENA_A, -speed);
}
}
/**
* @brief Moves the rear-left (RL) motor at the specified speed.
*
* @param speed Speed of movement (-255 to 255)
*/
void RL_move(int speed)
{
if (speed >= 0)
{
digitalWrite(MotorPins::IN3_A, HIGH);
digitalWrite(MotorPins::IN4_A, LOW);
analogWrite(MotorPins::ENB_A, speed);
}
else
{
digitalWrite(MotorPins::IN3_A, LOW);
digitalWrite(MotorPins::IN4_A, HIGH);
analogWrite(MotorPins::ENB_A, -speed);
}
}
// ===== Setup & loop =====
void setup()
{
delay(3000);
Serial.begin(115200);
Arduino_constants::Display.begin();
Arduino_constants::TouchDetector.begin();
create_ui();
lv_scr_load(other_constants::screen_buttons);
}
void loop()
{
lv_timer_handler();
if (msg_label != NULL && (millis() - msg_start_time > msg_duration))
{
lv_obj_del(msg_label);
msg_label = NULL;
}
}