-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterface.c
More file actions
313 lines (253 loc) · 7.14 KB
/
Interface.c
File metadata and controls
313 lines (253 loc) · 7.14 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
// Interface.c
// Created by Robert Leonard on 2019-07-04.
//
#include <stddef.h>
#include <avr/interrupt.h>
#include "Interface.h"
#include "lcd/ViewController.h"
#include "encoder/Encoder.h"
#include "hotwire/Hotwire.h"
#include "feedback/Buzzer.h"
#include "lcd/LCDControl.h"
#include "pid/PID.h"
#include "lcd/lcd.h"
#define SWITCH_HOLD_TIME 150000
// EFFECTS: handles the main menu
uint8_t Interface_main_hotwire_off() {
VCObject.x = 0;
VCObject.y = 0;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
VCObject.NEXT_VC = &VC_main_menu;
while (!Encoder_switch_is_low());
while (1) {
if (Encoder_rotary_read() == ROTATE_RIGHT) {
VCObject.y = 1;
VCObject.direction = LCD_INVERTED_RIGHT_ARROW;
VC_set_cursor();
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
VCObject.y = 0;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
VC_set_cursor();
}
if (Encoder_switch_is_pressed()) {
// Start the hotwire if the button is held
if (Encoder_switch_is_held(SWITCH_HOLD_TIME)) {
Hotwire_start();
Buzzer_play(6, 50);
VC_hotwire_running();
return VIEW_MAIN_RUNNING;
}
// If the button wasn't held, then either edit the PWM or go to settings
if (VCObject.y) {
VC_set_cursor_blink();
while (!Encoder_switch_is_high()) {
if (Encoder_rotary_read() == ROTATE_RIGHT) {
Hotwire_add_power_setting(0.1);
VC_main_menu();
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
Hotwire_add_power_setting(-0.1);
VC_main_menu();
}
}
VC_stop_cursor_blink();
} else {
return VIEW_SETTINGS_MAIN;
}
}
}
}
// EFFECTS: handles the main menu when the hotwire is on
uint8_t Interface_main_hotwire_on() {
VCObject.x = 0;
VCObject.y = 0;
VCObject.direction = LCD_NO_CURSOR;
VCObject.NEXT_VC = &VC_hotwire_running;
while (1) {
if (Encoder_switch_is_pressed()) {
Hotwire_stop();
Buzzer_play(0, 40);
VC_main_menu();
return VIEW_MAIN_STOPPED;
}
if (Encoder_rotary_read() == ROTATE_RIGHT) {
Hotwire_add_power_setting(0.1);
VC_hotwire_running();
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
Hotwire_add_power_setting(-0.1);
VC_hotwire_running();
}
}
}
// EFFECTS: handles the main settings page
uint8_t Interface_settings_main() {
VCObject.x = 0;
VCObject.y = 0;
VCObject.direction = LCD_NO_CURSOR;
VCObject.NEXT_VC = NULL;
uint8_t setting_number = 0;
VC_settings_main(setting_number);
while (1) {
if (Encoder_switch_is_pressed()) {
switch (setting_number) {
case 0:
VC_main_menu();
return VIEW_MAIN_STOPPED;
case 1:
return VIEW_SETTINGS_LCD;
case 2:
return VIEW_SETTINGS_PID;
case 3:
VC_main_menu();
return VIEW_MAIN_STOPPED;
}
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
if (setting_number > 0) setting_number--;
if (!setting_number) VCObject.direction = LCD_INVERTED_LEFT_ARROW;
VC_settings_main(setting_number);
}
if (Encoder_rotary_read() == ROTATE_RIGHT) {
if (setting_number < NUM_SETTINGS) setting_number++;
if (setting_number) VCObject.direction = LCD_INVERTED_RIGHT_ARROW;
VC_settings_main(setting_number);
}
}
}
// EFFECTS: handles the LCD settings page
uint8_t Interface_settings_lcd() {
VCObject.x = 0;
VCObject.y = 0;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
VCObject.NEXT_VC = NULL;
VC_settings_lcd();
while (1) {
// Handle moving the LCD selection cursor and direction arrow
if (Encoder_rotary_read() == ROTATE_LEFT) {
if (VCObject.x == 12 && VCObject.y == 1) {
VCObject.y--;
} else if (VCObject.x == 12 && VCObject.y == 0) {
VCObject.x -= 12;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
}
VC_set_cursor();
while (Encoder_rotary_read() != ROTATE_NONE);
} else if (Encoder_rotary_read() == ROTATE_RIGHT) {
if (VCObject.x == 0 && VCObject.y == 0) {
VCObject.x += 12;
VCObject.direction = LCD_INVERTED_RIGHT_ARROW;
} else if (VCObject.x == 12 && VCObject.y == 0) {
VCObject.y++;
}
VC_set_cursor();
while (Encoder_rotary_read() != ROTATE_NONE);
}
// Handle the encoder button press
if (Encoder_switch_is_pressed()) {
// Back button pressed
if (VCObject.x == 0 && VCObject.y == 0) {
return VIEW_SETTINGS_MAIN;
}
// Wait for encoder to be released
while (!Encoder_switch_is_released());
// Start blinking the cursor to tell which value is being edited
VC_set_cursor_blink();
// Edit the LCD values until the encoder button is pressed
while (!Encoder_switch_is_high()) {
if (Encoder_rotary_read() == ROTATE_RIGHT) {
if (VCObject.y) LCDControl_increment_contrast();
else LCDControl_increment_backlight();
VC_settings_lcd();
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
if (VCObject.y) LCDControl_decrement_contrast();
else LCDControl_decrement_backlight();
VC_settings_lcd();
}
}
// Stop blinking after editing
VC_stop_cursor_blink();
}
}
}
// EFFECTS: handles the PID settings page
uint8_t Interface_settings_pid() {
VCObject.x = 0;
VCObject.y = 0;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
VCObject.NEXT_VC = NULL;
char setting = 0;
VC_settings_pid();
while (1) {
// Handle moving the LCD selection cursor and direction arrow
if (Encoder_rotary_read() == ROTATE_LEFT) {
if (VCObject.x == 1 && VCObject.y == 1) {
VCObject.x = 8;
VCObject.y = 0;
setting = 'i';
} else if (VCObject.x == 8 && VCObject.y == 0) {
VCObject.x = 1;
setting = 'p';
} else if (VCObject.x == 1 && VCObject.y == 0) {
VCObject.x = 0;
VCObject.direction = LCD_INVERTED_LEFT_ARROW;
}
VC_set_cursor();
while (Encoder_rotary_read() != ROTATE_NONE);
} else if (Encoder_rotary_read() == ROTATE_RIGHT) {
if (VCObject.x == 0 && VCObject.y == 0) {
VCObject.x = 1;
VCObject.direction = LCD_INVERTED_RIGHT_ARROW;
setting = 'p';
} else if (VCObject.x == 1 && VCObject.y == 0) {
VCObject.x = 8;
setting = 'i';
} else if (VCObject.x == 8 && VCObject.y == 0) {
VCObject.x = 1;
VCObject.y = 1;
setting = 'd';
}
VC_set_cursor();
while (Encoder_rotary_read() != ROTATE_NONE);
}
// Handle the encoder button press
if (Encoder_switch_is_pressed()) {
// Back button pressed
if (VCObject.x == 0 && VCObject.y == 0) {
PID_save_gains();
return VIEW_SETTINGS_MAIN;
}
// Wait for encoder to be released
while (!Encoder_switch_is_released());
// Start blinking the cursor to tell which value is being edited
VC_set_cursor_blink();
// Edit the LCD values until the encoder button is pressed
while (!Encoder_switch_is_high()) {
if (Encoder_rotary_read() == ROTATE_RIGHT) {
if (VCObject.x == 1 && VCObject.y == 0) {
PID_add_p(0.1);
} else if (VCObject.x == 8 && VCObject.y == 0) {
PID_add_i(0.1);
} else if (VCObject.x == 1 && VCObject.y == 1) {
PID_add_d(0.1);
}
VC_settings_pid();
}
if (Encoder_rotary_read() == ROTATE_LEFT) {
if (setting == 'p') {
PID_add_p(-0.1);
} else if (setting == 'i') {
PID_add_i(-0.1);
} else if (setting == 'd') {
PID_add_d(-0.1);
}
VC_settings_pid();
}
}
// Stop blinking after editing
VC_stop_cursor_blink();
}
}
}