-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUltiLCD2_hi_lib.cpp
273 lines (236 loc) · 8.69 KB
/
UltiLCD2_hi_lib.cpp
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
#include <avr/pgmspace.h>
#include <string.h>
#include "Configuration.h"
#ifdef ENABLE_ULTILCD2
#include "UltiLCD2_hi_lib.h"
menuFunc_t currentMenu;
menuFunc_t previousMenu;
menuFunc_t postMenuCheck;
int16_t previousEncoderPos;
uint8_t led_glow = 0;
uint8_t led_glow_dir;
uint8_t minProgress;
const char* lcd_setting_name;
const char* lcd_setting_postfix;
void* lcd_setting_ptr;
uint8_t lcd_setting_type;
int16_t lcd_setting_min;
int16_t lcd_setting_max;
int16_t lcd_setting_start_value;
void lcd_change_to_menu(menuFunc_t nextMenu, int16_t newEncoderPos)
{
minProgress = 0;
led_glow = led_glow_dir = 0;
LED_NORMAL();
lcd_lib_beep();
previousMenu = currentMenu;
previousEncoderPos = lcd_lib_encoder_pos;
currentMenu = nextMenu;
lcd_lib_encoder_pos = newEncoderPos;
}
void lcd_tripple_menu(const char* left, const char* right, const char* bottom)
{
if (lcd_lib_encoder_pos != ENCODER_NO_SELECTION)
{
if (lcd_lib_encoder_pos < 0)
lcd_lib_encoder_pos += 3*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
if (lcd_lib_encoder_pos >= 3*ENCODER_TICKS_PER_MAIN_MENU_ITEM)
lcd_lib_encoder_pos -= 3*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
}
lcd_lib_clear();
lcd_lib_draw_vline(64, 5, 45);
lcd_lib_draw_hline(3, 124, 48);
if (IS_SELECTED_MAIN(0))
{
lcd_lib_draw_box(3+2, 5+2, 64-3-2, 45-2);
lcd_lib_set(3+3, 5+3, 64-3-3, 45-3);
lcd_lib_clear_string_center_atP(33, 22, left);
}else{
lcd_lib_draw_string_center_atP(33, 22, left);
}
if (IS_SELECTED_MAIN(1))
{
lcd_lib_draw_box(64+3+2, 5+2, 125-2, 45-2);
lcd_lib_set(64+3+3, 5+3, 125-3, 45-3);
lcd_lib_clear_string_center_atP(64 + 33, 22, right);
}else{
lcd_lib_draw_string_center_atP(64 + 33, 22, right);
}
if (bottom != NULL)
{
if (IS_SELECTED_MAIN(2))
{
lcd_lib_draw_box(3+2, 49+2, 125-2, 63-2);
lcd_lib_set(3+3, 49+3, 125-3, 63-3);
lcd_lib_clear_string_centerP(53, bottom);
}else{
lcd_lib_draw_string_centerP(53, bottom);
}
}
}
void lcd_basic_screen()
{
lcd_lib_clear();
lcd_lib_draw_hline(3, 124, 48);
}
void lcd_info_screen(menuFunc_t cancelMenu, menuFunc_t callbackOnCancel, const char* cancelButtonText)
{
if (lcd_lib_encoder_pos != ENCODER_NO_SELECTION)
{
if (lcd_lib_encoder_pos < 0)
lcd_lib_encoder_pos += 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
if (lcd_lib_encoder_pos >= 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM)
lcd_lib_encoder_pos -= 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
}
if (lcd_lib_button_pressed && IS_SELECTED_MAIN(0))
{
if (callbackOnCancel) callbackOnCancel();
if (cancelMenu) lcd_change_to_menu(cancelMenu);
}
lcd_basic_screen();
if (!cancelButtonText) cancelButtonText = PSTR("CANCEL");
if (IS_SELECTED_MAIN(0))
{
lcd_lib_draw_box(3+2, 49+2, 125-2, 63-2);
lcd_lib_set(3+3, 49+3, 125-3, 63-3);
lcd_lib_clear_stringP(65 - strlen_P(cancelButtonText) * 3, 53, cancelButtonText);
}else{
lcd_lib_draw_stringP(65 - strlen_P(cancelButtonText) * 3, 53, cancelButtonText);
}
}
void lcd_question_screen(menuFunc_t optionAMenu, menuFunc_t callbackOnA, const char* AButtonText, menuFunc_t optionBMenu, menuFunc_t callbackOnB, const char* BButtonText)
{
if (lcd_lib_encoder_pos != ENCODER_NO_SELECTION)
{
if (lcd_lib_encoder_pos < 0)
lcd_lib_encoder_pos += 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
if (lcd_lib_encoder_pos >= 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM)
lcd_lib_encoder_pos -= 2*ENCODER_TICKS_PER_MAIN_MENU_ITEM;
}
if (lcd_lib_button_pressed)
{
if (IS_SELECTED_MAIN(0))
{
if (callbackOnA) callbackOnA();
if (optionAMenu) lcd_change_to_menu(optionAMenu);
}else if (IS_SELECTED_MAIN(1))
{
if (callbackOnB) callbackOnB();
if (optionBMenu) lcd_change_to_menu(optionBMenu);
}
}
lcd_basic_screen();
if (IS_SELECTED_MAIN(0))
{
lcd_lib_draw_box(3+2, 49+2, 64-2, 63-2);
lcd_lib_set(3+3, 49+3, 64-3, 63-3);
lcd_lib_clear_stringP(35 - strlen_P(AButtonText) * 3, 53, AButtonText);
}else{
lcd_lib_draw_stringP(35 - strlen_P(AButtonText) * 3, 53, AButtonText);
}
if (IS_SELECTED_MAIN(1))
{
lcd_lib_draw_box(64+2, 49+2, 64+60-2, 63-2);
lcd_lib_set(64+3, 49+3, 64+60-3, 63-3);
lcd_lib_clear_stringP(64+31 - strlen_P(BButtonText) * 3, 53, BButtonText);
}else{
lcd_lib_draw_stringP(64+31 - strlen_P(BButtonText) * 3, 53, BButtonText);
}
}
void lcd_progressbar(uint8_t progress)
{
lcd_lib_draw_box(3, 38, 124, 46);
for(uint8_t n=0; n<progress;n++)
{
if (n>120) break;
uint8_t m = (progress-n-1) % 12;
if (m < 5)
lcd_lib_draw_vline(4 + n, 40, 40+m);
else if (m < 10)
lcd_lib_draw_vline(4 + n, 40+m-5, 44);
}
}
void lcd_scroll_menu(const char* menuNameP, int8_t entryCount, entryNameCallback_t entryNameCallback, entryDetailsCallback_t entryDetailsCallback)
{
if (lcd_lib_button_pressed)
return;//Selection possibly changed the menu, so do not update it this cycle.
if (lcd_lib_encoder_pos == ENCODER_NO_SELECTION)
lcd_lib_encoder_pos = 0;
static int16_t viewPos = 0;
if (lcd_lib_encoder_pos < 0) lcd_lib_encoder_pos += entryCount * ENCODER_TICKS_PER_SCROLL_MENU_ITEM;
if (lcd_lib_encoder_pos >= entryCount * ENCODER_TICKS_PER_SCROLL_MENU_ITEM) lcd_lib_encoder_pos -= entryCount * ENCODER_TICKS_PER_SCROLL_MENU_ITEM;
uint8_t selIndex = uint16_t(lcd_lib_encoder_pos/ENCODER_TICKS_PER_SCROLL_MENU_ITEM);
lcd_lib_clear();
int16_t targetViewPos = selIndex * 8 - 15;
int16_t viewDiff = targetViewPos - viewPos;
viewPos += viewDiff / 4;
if (viewDiff > 0) { viewPos ++; led_glow = led_glow_dir = 0; }
if (viewDiff < 0) { viewPos --; led_glow = led_glow_dir = 0; }
uint8_t drawOffset = 10 - (uint16_t(viewPos) % 8);
uint8_t itemOffset = uint16_t(viewPos) / 8;
for(uint8_t n=0; n<6; n++)
{
uint8_t itemIdx = n + itemOffset;
if (itemIdx >= entryCount)
continue;
char* ptr = entryNameCallback(itemIdx);
//ptr[10] = '\0';
ptr[20] = '\0';
if (itemIdx == selIndex)
{
//lcd_lib_set(3, drawOffset+8*n-1, 62, drawOffset+8*n+7);
lcd_lib_set(3, drawOffset+8*n-1, 124, drawOffset+8*n+7);
lcd_lib_clear_string(4, drawOffset+8*n, ptr);
}else{
lcd_lib_draw_string(4, drawOffset+8*n, ptr);
}
}
lcd_lib_set(3, 0, 124, 8);
lcd_lib_clear(3, 47, 124, 63);
lcd_lib_clear(3, 9, 124, 9);
lcd_lib_draw_hline(3, 124, 48);
lcd_lib_clear_string_centerP(1, menuNameP);
entryDetailsCallback(selIndex);
lcd_lib_update_screen();
}
void lcd_menu_edit_setting()
{
if (lcd_lib_encoder_pos < lcd_setting_min)
lcd_lib_encoder_pos = lcd_setting_min;
if (lcd_lib_encoder_pos > lcd_setting_max)
lcd_lib_encoder_pos = lcd_setting_max;
if (lcd_setting_type == 1)
*(uint8_t*)lcd_setting_ptr = lcd_lib_encoder_pos;
else if (lcd_setting_type == 2)
*(uint16_t*)lcd_setting_ptr = lcd_lib_encoder_pos;
else if (lcd_setting_type == 3)
*(float*)lcd_setting_ptr = float(lcd_lib_encoder_pos) / 100.0;
else if (lcd_setting_type == 4)
*(int32_t*)lcd_setting_ptr = lcd_lib_encoder_pos;
else if (lcd_setting_type == 5)
*(uint8_t*)lcd_setting_ptr = lcd_lib_encoder_pos * 255 / 100;
else if (lcd_setting_type == 6)
*(float*)lcd_setting_ptr = float(lcd_lib_encoder_pos) * 60;
else if (lcd_setting_type == 7)
*(float*)lcd_setting_ptr = float(lcd_lib_encoder_pos) * 100;
else if (lcd_setting_type == 8)
*(float*)lcd_setting_ptr = float(lcd_lib_encoder_pos);
lcd_basic_screen();
lcd_lib_draw_string_centerP(20, lcd_setting_name);
char buffer[LCD_MAX_TEXT_LINE_LENGTH + 1]; // Longest string: "Prev: 20000mm/sec^2" = 19 characters + 1
if (lcd_setting_type == 3)
float_to_string(float(lcd_lib_encoder_pos) / 100.0, buffer, lcd_setting_postfix);
else
int_to_string(lcd_lib_encoder_pos, buffer, lcd_setting_postfix);
lcd_lib_draw_string_center(30, buffer);
strcpy_P(buffer, PSTR("Prev: "));
if (lcd_setting_type == 3)
float_to_string(float(lcd_setting_start_value) / 100.0, buffer + 6, lcd_setting_postfix);
else
int_to_string(lcd_setting_start_value, buffer + 6, lcd_setting_postfix);
lcd_lib_draw_string_center(53, buffer);
lcd_lib_update_screen();
if (lcd_lib_button_pressed)
lcd_change_to_menu(previousMenu, previousEncoderPos);
}
#endif//ENABLE_ULTILCD2