-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlcd.c
More file actions
161 lines (137 loc) · 3.46 KB
/
lcd.c
File metadata and controls
161 lines (137 loc) · 3.46 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
/**
* @file lcd.c
* @brief LCD display management
*
* @author Irving Wang (irvingw@purdue.edu)
*/
#include "lcd.h"
#include <stddef.h>
#include <stdint.h>
#include "amk.h"
#include "nextion.h"
// pages
#include "pages/race.h"
#include "pages/calibration.h"
#include "pages/faults.h"
volatile page_t curr_page = PAGE_PREFLIGHT; // Current page displayed on the LCD
volatile page_t prev_page = PAGE_PREFLIGHT; // Previous page displayed on the LCD
// Page handlers array stored in flash
const page_handler_t page_handlers[NUM_PAGES] = { // Order must match page_t enum
[PAGE_RACE] = {
.update = nullptr,
.move_up = nullptr,
.move_down = nullptr,
.select = nullptr,
.telemetry = race_telemetry_update,
.string = RACE_STRING
},
[PAGE_FAULTS] = {
.update = faults_update,
.move_up = faults_move_up,
.move_down = faults_move_down,
.select = faults_select,
.telemetry = faults_telemetry_update,
.string = FAULT_STRING
},
[PAGE_CALIBRATION] = {
.update = nullptr,
.move_up = nullptr,
.move_down = nullptr,
.select = nullptr,
.telemetry = calibration_telemetry_update,
.string = CALIBRATION_STRING
},
[PAGE_AMK] = {
.update = nullptr,
.move_up = nullptr,
.move_down = nullptr,
.select = nullptr,
.telemetry = amk_telemetry_update,
.string = AMK_STRING
}
};
void updatePage();
// Initialize the LCD screen
// Preflight will be shown on power on, then reset to RACE
void LCD_init(uint32_t baud_rate) {
NXT_setBaud(baud_rate);
NXT_setBrightness(100);
// Set page (leave preflight)
curr_page = PAGE_RACE;
updatePage();
}
/**
* @brief Advances to the next selectable page
*/
void advancePage() {
curr_page = curr_page + 1;
if (curr_page >= NUM_PAGES) {
curr_page = 0;
}
updatePage();
}
/**
* @brief Moves to the previous selectable page
*/
void backPage() {
if (curr_page == 0) {
curr_page = NUM_PAGES - 1;
} else {
curr_page = curr_page - 1;
}
updatePage();
}
/**
* @brief Updates LCD display page
*
* Key behaviors:
* - Maintains display of error pages when active
*/
void updatePage() {
// If we do not detect a page update, do nothing
if (curr_page == prev_page) {
return;
}
// Set the page on display
NXT_setPage(page_handlers[curr_page].string);
prev_page = curr_page;
// Call update handler if available
if (page_handlers[curr_page].update != nullptr) {
page_handlers[curr_page].update();
}
}
void moveUp() {
if (curr_page >= NUM_PAGES) {
return;
}
if (page_handlers[curr_page].move_up != nullptr) {
page_handlers[curr_page].move_up();
}
}
void moveDown() {
if (curr_page >= NUM_PAGES) {
return;
}
if (page_handlers[curr_page].move_down != nullptr) {
page_handlers[curr_page].move_down();
}
}
void selectItem() {
if (curr_page >= NUM_PAGES) {
return;
}
if (page_handlers[curr_page].select != nullptr) {
page_handlers[curr_page].select();
}
}
/**
* @brief Updates current telemetry page by calling its handler if available
*/
void updateTelemetryPages() {
if (curr_page >= NUM_PAGES) {
return;
}
if (page_handlers[curr_page].telemetry != nullptr) {
page_handlers[curr_page].telemetry();
}
}