-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.h
207 lines (168 loc) · 3.71 KB
/
lcd.h
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
#ifndef TIMER_LCD_H
#define TIMER_LCD_H
#include "config.h"
unsigned long last_draw = 0;
int8_t active_frame = 0,
prev_frame = -1,
next_frame = -1;
int8_t get_active_frame() {
return active_frame;
}
int8_t get_prev_frame() {
return prev_frame;
}
int8_t get_next_frame() {
return next_frame;
}
void set_active_frame(int8_t frame) {
active_frame = frame;
}
void set_prev_frame(int8_t frame) {
prev_frame = frame;
}
void set_next_frame(int8_t frame) {
next_frame = frame;
}
void set_last_draw() {
last_draw = millis();
}
unsigned long get_last_draw() {
return last_draw;
}
void lcd_print_two_digit(uint8_t num) {
if (num < 10) {
lcd.print('0');
}
lcd.print(num);
}
void lcd_clear_row(uint8_t row, uint8_t col_start, uint8_t cols) {
lcd.setCursor(col_start, row);
uint8_t i;
for (i = 0; i < cols; ++i) {
lcd.print(' ');
}
lcd.setCursor(col_start, row);
}
void lcd_clear_row(uint8_t row, uint8_t cols) {
lcd_clear_row(row, 0, cols);
}
void lcd_clear_row(uint8_t row) {
lcd_clear_row(row, 0, COLS);
}
uint8_t frame_needs_redraw() {
if (active_frame != prev_frame) {
return 1;
}
return 0;
}
void switch_frame(uint8_t frame) {
next_frame = frame;
}
void frame_blink_text(unsigned long* blink_time, uint8_t* state,
const char* text, uint8_t col, uint8_t row) {
/* blink text every 500ms */
if ((millis() - *blink_time) > 500) {
*blink_time = millis();
if (*state == 0) {
*state = 1;
} else if (*state == 1) {
*state = 0;
}
}
lcd.setCursor(col, row);
size_t textlen = strlen(text);
if (*state == 0) {
lcd_clear_row(row);
} else if (*state == 1) {
lcd.print(text);
}
}
void frame_blink_text(unsigned long* blink_time, uint8_t* state,
uint8_t value, uint8_t col, uint8_t row) {
/* blink text every 500ms */
if ((millis() - *blink_time) > 500) {
*blink_time = millis();
if (*state == 0) {
*state = 1;
} else if (*state == 1) {
*state = 0;
}
}
lcd.setCursor(col, row);
if (*state == 0) {
lcd.print(" ");
} else if (*state == 1) {
lcd_print_two_digit(value);
}
}
uint8_t frame_button_handle_enum(uint8_t* value, uint8_t* types, uint8_t types_len,
uint8_t col, uint8_t row, uint8_t* current_state, uint8_t next_state) {
uint8_t selected_index = 0, i;
for (i = 0; i < types_len; ++i) {
if (*value == types[i]) {
selected_index = i;
break;
}
}
int8_t btn = button_handle();
switch (btn) {
case 0:
case 1:
// UP
if (btn == 0) {
if (selected_index == (types_len - 1)) {
selected_index = 0;
} else {
selected_index = selected_index + 1;
}
} else if (btn == 1) {
// DOWN
if (selected_index == 0) {
selected_index = types_len - 1;
} else {
selected_index = selected_index - 1;
}
}
*value = types[selected_index];
break;
case 2:
// SET
*current_state = next_state;
return 1;
break;
}
return 0;
}
uint8_t frame_button_handle(uint8_t* value, uint8_t min, uint8_t max,
uint8_t col, uint8_t row, uint8_t* current_state, uint8_t next_state) {
int8_t btn = button_handle();
switch (btn) {
case 0:
case 1:
// UP
if (btn == 0) {
if (*value == max) {
*value = min;
} else {
(*value)++;
}
} else if (btn == 1) {
// DOWN
if (*value == min) {
*value = max;
} else {
(*value)--;
}
}
break;
case 2:
// SET
*current_state = next_state;
lcd.setCursor(col, row);
lcd_print_two_digit(*value);
return 1;
break;
}
return 0;
}
#endif