forked from mockendon/opengradesim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLEDDisplay.cpp
495 lines (438 loc) · 11.6 KB
/
OLEDDisplay.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "OLEDDisplay.h"
#include "Defines.h"
#ifdef USING_SERIAL
extern HardwareSerial Serial;
#endif
#define OLED_RESET -1 // No reset pin on cheap OLED display
Adafruit_SSD1306 *OLED;
char lineBuffer[NUM_LCD_ROWS][NUM_LCD_COLS + 1]; // Leave an extra space for terminating null
int lineSize[NUM_LCD_ROWS];
int linePosition[NUM_LCD_ROWS];
boolean cursorActive = false;
uint8_t cursorRow;
uint8_t cursorCol;
uint8_t graph_X;
uint8_t graph_Y;
uint8_t graph_Width;
uint8_t graph_Height;
bool display_Graph = false;
static bool valIsSet = false;
// 4 - button pad
//PushButton btn1 = { 5 };
//PushButton btn2 = { 4 };
//PushButton btn3 = { 7 };
//PushButton btn4 = { 6 };
// 3-button pad
PushButton btn1 = { 4, 1 };
PushButton btn2 = { 5, 2 };
PushButton btn3 = { 6, 3 };
int btnPressed = 0;
// Custom Char Bluetooth Logo
byte customChar[] = {
B00000,
B00110,
B00101,
B10110,
B01100,
B10110,
B00101,
B00110
};
//display.setTextColor(BLACK, WHITE); // 'inverted' text
//Characters are rendered in the ratio of 7:10. Meaning, passing font size 1 will render the text at 7×10 pixels per character, passing 2 will render the text at 14×20 pixels per character and so on.
void initOLED()
{
#ifdef USING_SERIAL
Serial.begin(19200);
#else
OLED = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
if (!OLED->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
//resetSys(F("SSD1306 allocation failed"));
}
// Show initial display buffer contents on the screen --
// the library initializes this with a splash screen (edit the splash.h in the library).
OLED->setRotation(0);
//OLED.display();
//delay(1000); // Pause for 1 seconds
OLED->clearDisplay();
// Show the firmware version
OLED->setTextSize(1);
OLED->setTextColor(SSD1306_WHITE);
OLED->setCursor(5, 10);
OLED->print(F("FW Version: "));
OLED->print(VERSION_NUMBER);
OLED->display();
delay(500); // Pause for version # display
OLED->clearDisplay();
#endif
}
void doCursor() {
if (cursorActive) {
#ifdef USING_SERIAL
Serial.print("Cursor at ");
Serial.print(cursorRow);
Serial.println(cursorCol);
#else
//OLED->cursor();
OLED->setCursor(cursorCol, cursorRow);
#endif
}
else {
#ifdef USING_SERIAL
#else
// OLED->noCursor();
#endif
}
}
void doDisplay() {
// static unsigned long preMil = millis();
// unsigned long curMil = millis();
// if(curMil - preMil >= DISPLAY_DELAY){
#ifdef USING_SERIAL
Serial.print(F("0: "));
Serial.print(lineBuffer[0]);
Serial.println(F("\\"));
Serial.print(F("1: "));
Serial.print(lineBuffer[1]);
Serial.print(F("\\ RAM = "));
Serial.println(freeRam());
Serial.println(F("**"));
delay(100);
#else
OLED->clearDisplay();
for (int i = 0; i < NUM_LCD_ROWS; i++) {
lineBuffer[i][NUM_LCD_COLS] = 0; // Make sure the null is there.
OLED->setCursor(0, linePosition[i]);
OLED->setTextSize(lineSize[i]);
OLED->print(lineBuffer[i]);
}
if (display_Graph) {
OLED->fillRect(graph_X, graph_Y, graph_Width, graph_Height, WHITE);
display_Graph = false;
}
// Update the display
OLED->display();
#endif
//doCursor();
// }
}
bool setOLEDDimmer (bool dimVal) {
OLED->dim(dimVal);
return true;
}
void checkButtons(void) {
if (btn1.isOn()) {
btnPressed = btn1.getId();
if (btn2.isPressed()) {
btnPressed = 4;
}
} else if (btn2.isOn()) {
btnPressed = btn2.getId();
if (btn3.isPressed()) {
btnPressed = 5;
}
} else if (btn3.isOn()) {
btnPressed = btn3.getId();
} else {
btnPressed = 0;
}
//Serial.print("btnPressed: "); Serial.println(btnPressed);
// return btnPressed;
}
bool selectBtnPressed()
{
return (btnPressed == SELECT_BTN) ? true: false;
}
bool upDownBtnPressed()
{
return ((btnPressed == INCLINE_BTN) || (btnPressed == DECLINE_BTN)) ? true: false;
}
bool pressAnyButtonToExit()
{
// Loop control - Runs 3 times minimum. return false.
// run #1: init static vars btnWasPushed and firstTime.
// run #2 - #n: check for any button push. If btn was pushed, set reminder to exit on next run. return false.
// last run: reset firstTime to True ready for next time and return true;
static bool firstTime = true;
static bool btnWasPushed = false;
switch (firstTime)
{
case true:
btnWasPushed = false;
firstTime = false;
break;
case false:
switch (btnWasPushed)
{
case false:
btnWasPushed = btnPressed > 0 ? true : false;
return false;
break;
case true:
firstTime = true;
break;
}
break;
}
return btnWasPushed;
}
int getbtnPressed() {
return btnPressed;
}
void bargraph(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
{
display_Graph = true;
graph_X = x;
graph_Y = y;
graph_Width = w;
graph_Height = h;
}
void displayTextLeft(int row, int rowPos, int startcol, int colwidth, int textsize, const char* message) {
lineSize[row] = textsize;
linePosition[row] = rowPos;
int charIndx = 0;
boolean over = false;
for (int i = startcol; i < startcol + colwidth; i++) {
if (!over) {
char c = message[charIndx];
if (c == 0) {
over = true;
lineBuffer[row][i] = ' ';
} else {
lineBuffer[row][i] = c;
}
} else {
lineBuffer[row][i] = ' '; // fill with spaces to clear anything else on the line.
}
charIndx++;
}
}
void displayTextLeft(int row, int rowPos, int startcol, int colwidth, int textsize, const __FlashStringHelper* message) {
lineSize[row] = textsize;
linePosition[row] = rowPos;
boolean over = false;
PGM_P p = reinterpret_cast<PGM_P>(message);
for (int i = startcol; i < startcol + colwidth; i++) {
if (!over) {
char c = pgm_read_byte(&p[i]);
if (c == 0) {
over = true;
lineBuffer[row][i] = ' ';
}
else {
lineBuffer[row][i] = c;
}
}
else {
lineBuffer[row][i] = ' '; // fill with spaces to clear anything else on the line.
}
}
}
void displayTextRight(int row, int rowPos, int startcol, int colwidth, int textsize, const char* message) {
lineSize[row] = textsize;
linePosition[row] = rowPos;
//Serial.println(message);
int numChars = strlen(message);
int numSpaces = 0;
// limit colwidth if not enough room to the right.
// if (colwidth > NUM_LCD_COLS - startcol) {
// colwidth = NUM_LCD_COLS - startcol;
// }
int charIndx = numChars - 1;
for (int i = startcol; i > startcol - colwidth; i--) {
if (charIndx < 0) {
lineBuffer[row][i] = ' ';
} else {
lineBuffer[row][i] = message[charIndx];
}
charIndx--;
}
}
void displayTextRight(int row, int rowPos, int startcol, int colwidth, int textsize, const __FlashStringHelper* message) {
lineSize[row] = textsize;
linePosition[row] = rowPos;
int numChars = strlen_P((const char*)message);
int numSpaces = 0;
PGM_P p = reinterpret_cast<PGM_P>(message);
if (colwidth > NUM_LCD_COLS - startcol) {
colwidth = NUM_LCD_COLS - startcol;
}
numSpaces = colwidth - numChars;
if (numSpaces < 0) {
numSpaces = 0;
}
for (int i = startcol; i < startcol + colwidth; i++) {
if (i < numSpaces) {
lineBuffer[row][i] = ' ';
}
else {
lineBuffer[row][i] = pgm_read_byte(&p[i - numSpaces]);
}
}
}
void displayLineLeft(int row, int rowPos, int textsize, const char* message) {
//Serial.println(message);
displayTextLeft(row, rowPos, 0, NUM_LCD_COLS, textsize, message);
}
void displayLineLeft(int row, int rowPos, int textsize, const __FlashStringHelper* message) {
displayTextLeft(row, rowPos, 0, NUM_LCD_COLS, textsize, message);
}
void displayLineRight(int row, int rowPos, int textsize, const char* message) {
displayTextRight(row, rowPos, 0, NUM_LCD_COLS, textsize, message);
}
void displayLineRight(int row, int rowPos, int textsize, const __FlashStringHelper* message) {
displayTextRight(row, rowPos, 0, NUM_LCD_COLS, textsize, message);
}
void showCursor(boolean aBool) {
cursorActive = aBool;
}
void setCursor(uint8_t arow, uint8_t acol) {
cursorRow = arow;
cursorCol = acol;
cursorActive = true;
}
// To be used as a timer by other functions on the menu
// Can only have one active at a time.
boolean displayDelay(unsigned int delSeconds) {
static uint8_t state = 0;
static unsigned long startMil = millis();
unsigned long curMil = millis();
switch (state) {
case 0: {
startMil = millis();
state++;
return false;
}
case 1: {
if (curMil - startMil >= delSeconds * 1000ul) {
state = 0;
return true;
} else {
return false;
}
}
} // end switch (state)
return false;
}
void displayNumber(int val, const __FlashStringHelper* txt)
{
char buf[20];
sprintf_P(buf, PSTR(" %d%s"), val, F(txt));
//sprintf_P(buf, fmtStr*, val, btnPressed);
displayLineLeft(1, 12, 2, buf);
displayLineLeft(2, 24, 1, " "); // erase the unused line
}
void displayDouble(double val, const __FlashStringHelper* txt)
{
char buf[20];
sprintf_P(buf, PSTR(" %g%s"), val, "%");
displayLineLeft(1, 12, 2, buf);
displayLineLeft(2, 24, 1, " "); // erase the unused lines
}
int adjustNumber(int val, int origVal, int valMin, int valMax, int increment)
{
//todo: limit to min and max
switch (btnPressed) {
case 1:
val = val - increment;
break;
case 2:
val = val + increment;
break;
case 3:
valIsSet = true;
break;
case 4: // pressing both btn 1 and 2 - restore to last saved weight
val = origVal;
break;
default:
break;
}
if (val < valMin)
val = valMin;
if (val > valMax)
val = valMax;
return val;
}
bool setNumber(int& val, int valMin, int valMax, int increment)
{
// This will loop 3 times minumum.
// run 1: set valIsSet to false.
// run 2 - n: run adjustNumber (one or more times).
// last run: reset firstTime to True and return true to end setNumber.
static int origVal = 0;
static bool firstTime = true;
switch (firstTime)
{
case true:
origVal = val;
valIsSet = false;
firstTime = false;
break;
case false:
switch (valIsSet)
{
case false:
val = adjustNumber( val, origVal, valMin, valMax, increment );
break;
case true:
firstTime = true;
break;
}
break;
}
return firstTime;
}
double adjustDouble(double val, double origVal, double valMin, double valMax, double increment)
{
switch (btnPressed) {
case 1:
val = val - increment;
break;
case 2:
val = val + increment;
break;
case 3:
valIsSet = true;
break;
case 4: // pressing both btn 1 and 2 - restore to last saved weight
val = origVal;
break;
default:
break;
}
if (val < valMin)
val = valMin;
if (val > valMax)
val = valMax;
return val;
}
bool setDouble(double& val, double valMin, double valMax, double increment)
{
// This will loop 3 times minumum.
// run 1: set valIsSet to false.
// run 2 - n: run adjustNumber (one or more times).
// last run: reset firstTime to True and return true to end setNumber.;
static double origVal = 0;
static bool firstTime = true;
switch (firstTime)
{
case true:
origVal = val;
valIsSet = false;
firstTime = false;
break;
case false:
switch (valIsSet)
{
case false:
val = adjustDouble( val, origVal, valMin, valMax, increment );
break;
case true:
firstTime = true;
break;
}
break;
}
return firstTime;
}