-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemt_interface.h
476 lines (405 loc) · 16.1 KB
/
gemt_interface.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
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
#ifndef GEMT_INTERFACE_H
#define GEMT_INTERFACE_H
#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EncoderButton.h>
#include <Fonts/Org_01.h>
#include "HardwareSerial.h"
/*========================================================================
TODO:
- Info screen
- Integrate hardware tests
- Create seperate .h and .cpp
========================================================================*/
//========================================================================
// Global Definitions
//========================================================================
#define displayRowLimit 8;
#define displayColLimit 21;
/* Use of volatile for variables that:
- Memormy mapped registers
- global varchanged by interrupt
- global var accessed by multithreading
https://barrgroup.com/embedded-systems/how-to/c-volatile-keyword
*/
// Anon namespace better than static
namespace
{
volatile uint8_t ebState = 0; // Current state (Position) of the encoder. Max by uint8 is 255
volatile bool clicked = false; // Updated on encoder "click" case, must reset after use
uint8_t clickedItemNumber = 0;
}
//========================================================================
// Initializers
//========================================================================
// Note: If using jumper wires make sure pins are well spaced out.
// rotary encoder is super noisy and registers false clicks among other issues
enum encoderSWPins
{
pinA = 19 , // CLK
pinB = 2, // DT
pinSW = 38 // SW
};
enum oledDisplayPins
{
screenWidth = 128,
screenHeight = 64,
screenAddress = 0x3C, // i2c Address
screenReset = -1 // -1 since sharing Arduino reset pin
};
// Display init
Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire, screenReset);
// EncoderSW init
EncoderButton eb1(pinA, pinB, pinSW);
//========================================================================
// Menu Functions (WIP)
//========================================================================
typedef void (*func)(void);
// Menu screen template
typedef struct Menu
{
unsigned short int choice;
const char* menuTextPtr;
func selectionAction; // Function pointer to Menu selection action
// At the moment only takes void functions...
// Still have not figured out how to set parameters in a general way
/* Possible CPP solution
// store the result of a call to std::bind
<void()> selectionAction = NULL;
//std::bind(print_num, 31337); // To init you would do
*/
// Method styling in C https://www.cs.uaf.edu/courses/cs301/2014-fall/notes/methods/index.html
// Ellipses ref https://www.lemoda.net/c/function-pointer-ellipsis/
} Menu;
// Main Menu Options
Menu MainMenu[] =
{
{1, "9G Servo Test"}, // Action - Update Menu, Servo
{2, "ESR Test"}, // Action - Test
{3, "nRF24 Test"}, // Action - Test
{4, "L298N Test"}, // Action - Test
{5, "Ultrasonic Test"}, // Action - Test
};
size_t mainMenuLen = sizeof(MainMenu) / sizeof(MainMenu[0]);
// Submenu for servo
Menu ServoMenu[] =
{
{1, "Manual Servo Test"},
{2, "Auto Servo Test"},
{3, "Back"}, // Action - Update Menu, Main
};
size_t servoMenuLen = sizeof(ServoMenu) / sizeof(ServoMenu[0]);
// Setup a pointer to change which menu is displayed. Start in Main
Menu* CurrentMenuPtr = MainMenu;
size_t* currentMenuLenPtr = mainMenuLen;
// Logo Bitmap Init
const uint8_t logo_bmp [] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0x86, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x80, 0x00, 0x3e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x77, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x38, 0x01, 0xc3, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x0e, 0x07, 0x81, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0x87, 0x03, 0x8e, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x00, 0xf8, 0x00, 0x70, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x30, 0x00, 0x38, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x00, 0x3f, 0xfc, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
0x00, 0x01, 0xc0, 0x00, 0x00, 0x1c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
0x00, 0x03, 0x80, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00,
0x00, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00,
0x00, 0x0e, 0x00, 0x00, 0x00, 0x1c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00,
0x00, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00,
0x00, 0x38, 0x00, 0x00, 0x00, 0x1c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00,
0x00, 0x78, 0x00, 0x00, 0x00, 0x1c, 0x0c, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0x87, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x7f, 0xfc, 0x1f, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0xc0,
0x00, 0x7f, 0xff, 0xe0, 0x7f, 0xff, 0xfc, 0x7f, 0xfc, 0x3f, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0x00,
0x00, 0x1f, 0xff, 0xf0, 0x3f, 0xff, 0xfc, 0x3f, 0xfc, 0x3f, 0xff, 0xf0, 0x3f, 0xff, 0xfc, 0x00,
0x00, 0x07, 0xff, 0xfc, 0x1f, 0xff, 0xfc, 0x3f, 0xfc, 0x3f, 0xff, 0xe0, 0x7f, 0xff, 0xf0, 0x00,
0x00, 0x03, 0xff, 0xfe, 0x0f, 0xff, 0xfc, 0x3f, 0xfc, 0x3f, 0xff, 0xc0, 0xff, 0xff, 0xc0, 0x00,
0x00, 0x00, 0xff, 0xff, 0x07, 0xff, 0xfe, 0x3f, 0xfc, 0x7f, 0xff, 0x81, 0xff, 0xff, 0x80, 0x00,
0x00, 0x00, 0x3f, 0xff, 0x83, 0xff, 0xfe, 0x3f, 0xf8, 0x7f, 0xff, 0x03, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xff, 0xc1, 0xff, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x07, 0xff, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xe0, 0xff, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x0f, 0xff, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x01, 0xff, 0xf0, 0x7f, 0xff, 0x1f, 0xf8, 0xff, 0xfc, 0x1f, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7f, 0xf8, 0x3f, 0xff, 0x1f, 0xf8, 0xff, 0xf8, 0x3f, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x1f, 0xff, 0x1f, 0xf0, 0xff, 0xf0, 0x7f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xfe, 0x0f, 0xff, 0x8f, 0xf0, 0xff, 0xe0, 0xff, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xff, 0x07, 0xff, 0x8f, 0xf1, 0xff, 0xc1, 0xff, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x83, 0xff, 0x8f, 0xf1, 0xff, 0x83, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7f, 0xe1, 0xff, 0x8f, 0xf1, 0xff, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x7f, 0xcf, 0xe3, 0xfe, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x3f, 0xc7, 0xe3, 0xfc, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x1f, 0xc7, 0xe3, 0xf8, 0x7f, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x0f, 0xe7, 0xe3, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x07, 0xe7, 0xe7, 0xe1, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x83, 0xe7, 0xc7, 0xc3, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc1, 0xe3, 0xc7, 0x87, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0xf3, 0xc7, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x73, 0xce, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x33, 0xce, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x13, 0x8c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0b, 0x88, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
//========================================================================
// Encoder Handlers (Interrupt functions)
//========================================================================
// On click, the global selection variable gets updated with
// value of where it was selcted
void onEb1Clicked(EncoderButton& eb)
{
// Set selection value to current state
clicked = true;
clickedItemNumber = CurrentMenuPtr[ebState].choice;
// DEBUG - Delete in actua\l proram as Serial printing slows down interrupts
//Serial.println("CLICKED!");
}
// A function to handle the 'encoder' event
void onEb1Encoder(EncoderButton& eb)
{
/*
// Filter latge spikes from noise
if(eb.increment() > 4)
{
eb.resetPosition(eb.position()); // Reset back to startin pos
}
*/
// Reset if encoder goes past active Menu limit
if (abs(eb.position()) >= currentMenuLenPtr)
{
eb.resetPosition(0);
}
ebState = abs(eb.position());
// DEBUG - Delete in actual proram as Serial printing slows down interrupts
//Serial.println(ebState);
}
//========================================================================
// Helper functions
//========================================================================
// Function to quickly call actions required to prep screen for printing
void displayPrep(void)
{
display.clearDisplay();
display.setCursor(0, 0);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
}
// Create function for clarity
void resetClicked(void)
{
clicked = 0;
}
//========================================================================
// Selection Action Functions
//========================================================================
void goToServoMenu(void)
{
CurrentMenuPtr = ServoMenu;
currentMenuLenPtr = servoMenuLen;
// Start at top of page
ebState = 0;
}
void goToMainMenu(void)
{
CurrentMenuPtr = MainMenu;
currentMenuLenPtr = mainMenuLen;
// Start at top of page
ebState = 0;
}
// Confirm options
namespace
{
const char *confirmOptions[3] = {"OK", "|", "Back"};
const size_t confirmOptionLen = sizeof(confirmOptions) / sizeof(confirmOptions[0]);
}
// Reusable function for printing the confirm options (ok and back)
// To be included within Info screen and test result screen
// Highlights either OK or Back. Must be included within a WHILE loop
void printConfirmOptions(void)
{
for (size_t i = 0; i < confirmOptionLen; i++)
{
// Highlight line if user is hovering over it
// Don't highlight the bar though
if (ebState == i && i != 1)
{
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
}
else
{
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
}
display.print(confirmOptions[i]);
}
}
// Info screen with auto info determination based on "clickedItemNumber" and CurrentMenuPtr
// Prints out pins to connect to for test
void infoScreen(void)
{
eb1.update();
displayPrep();
// Naive way of identifying whinc menu we are in
if (CurrentMenuPtr[0].menuTextPtr == "9G Servo Test")
{
switch(clickedItemNumber)
{
case 2: // esr
{
}
case 3: // nRF
{
}
case 4: // L298H
{
}
case 5: // Ultrasonic
{
}
}
}
else if (CurrentMenuPtr[0].menuTextPtr == "Manual Servo Test")
{
switch(clickedItemNumber)
{
case 1: // Manual
{
}
case 2: // Auto
{
}
}
}
}
// Function to set selection action when button pressed
// Currently only accepts void argument functions!
void setSelectionActions(void)
{
MainMenu[0].selectionAction = goToServoMenu;
//...
ServoMenu[2].selectionAction = goToMainMenu;
//...
}
//========================================================================
// Screen Display Functions
//========================================================================
// Bootup fucnction for display
void startInterface(void)
{
if(!display.begin(SSD1306_SWITCHCAPVCC, screenAddress)) {
display.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
//Link the event(s) to your function
eb1.setClickHandler(onEb1Clicked);
eb1.setEncoderHandler(onEb1Encoder);
// Create mapping to selection action fuunctions
setSelectionActions();
// Display logo for 2 sec
display.clearDisplay();
display.drawBitmap(0, 0, logo_bmp, screenWidth, screenHeight - 5, WHITE); // -5, bc getting some weird stuff at bottom of screen
display.display();
delay(2000);
display.clearDisplay();
display.display();
}
// Display driver code: Outputs current menu items line by-line,
// Highlights and selects based on encoder-button readings
void displayMenuDriver(void)
{
// Condition for executing users selections based on 'clicked' bool
if (clicked)
{
clickedItemNumber = CurrentMenuPtr[ebState].choice;
resetClicked(); // Reset before proceeding to function
CurrentMenuPtr[ebState].selectionAction();
}
//Display the previous Menu state
else
{
char buffer[50]; // init buffer of 50 bytes to hold expected string size
// Setup
eb1.update();
displayPrep();
// Printing header line
display.println("Select module test:");
// Display all current Menu options
for (size_t i = 0; i < (currentMenuLenPtr); ++i)
{
// Highlight line if user is hovering over it
if (ebState == i)
{
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
}
else
{
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
}
// Print out in int and Text format
sprintf(buffer, "%d. %s", CurrentMenuPtr[i].choice, CurrentMenuPtr[i].menuTextPtr);
display.println(buffer);
}
display.display();
}
}
//========================================================================
// Functions for debugging
//========================================================================
void dummyInfo(void)
{
eb1.update();
displayPrep();
display.println("Test Information:");
display.println();
display.println("This is a demo of an info screen"); display.println("for a test.");
display.println();
printConfirmOptions();
display.display();
}
// Demo of test screen functionality
void dummyTest(void)
{
while(clicked == 0)
{
dummyInfo();
delay(3);
}
resetClicked();
while(clicked == 0)
{
eb1.update();
displayPrep();
display.println("This is a screen for the actual test");
display.println();
printConfirmOptions();
display.display();
delay(3);
}
resetClicked();
}
//========================================================================
// Stuff to implement later...
//========================================================================
/*
// Changes Menu pointer to point to selected Menu screen
void menuUpdate(Menu NewMenu[], size_t newMenuLength, Menu* OldMenuPtr, size_t* oldMenuLengthPtr)
{
OldMenuPtr = NewMenu;
oldMenuLengthPtr = newMenuLength;
}
*/
#endif