-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLEDPanel.cpp
481 lines (418 loc) · 11.2 KB
/
OLEDPanel.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
/*
||
|| @file OLEDPanel.cpp
|| @version 1.1
|| @author Michael Zimmermann
|| @contact michael.zimmermann.sg@t-online.de
||
|| @description
|| | encapsulates and expands the OLED-library into class 'OLEDPanel'
|| | Original Source written by M.Köhler (https://github.com/Sylaina/oled-display.git)
|| | remark: original source was modified and expanded
|| |
|| | This library includes functionality for button-states (more functionality)
|| |
|| | routines for I2C-OLED-Display 128*64, 1.3"
|| | uses: i2c-files : i2c.c, i2c.h
|| | uses: oled-files : lcd.h, lcd.c
|| | uses: font-files : font.h, font.c
|| |
|| | defines in lcd.h:
|| | displaycontroller #define SH1106
|| | displaymode #define TEXTMODE
|| | I2C-ADDR #define LCD_I2C_ADR 0x78
|| #
||
|| @license
|| | Copyright (c) 2018 Michael Zimmermann <http://www.kruemelsoft.privat.t-online.de>
|| | All rights reserved.
|| |
|| | This program is free software: you can redistribute it and/or modify
|| | it under the terms of the GNU General Public License as published by
|| | the Free Software Foundation, either version 3 of the License, or
|| | (at your option) any later version.
|| |
|| | This program is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|| | GNU General Public License for more details.
|| |
|| | You should have received a copy of the GNU General Public License
|| | along with this program. If not, see <http://www.gnu.org/licenses/>.
|| #
||
*/
#include "OLEDPanel.h"
#include <stdio.h>
#include <string.h>
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
extern uint8_t I2C_ErrorCode;
OLEDPanel::OLEDPanel() {
m_ui8KeyAddr = 0;
m_bCursorOn = false;
m_bBlinken1Hz = false;
m_ulpreviousMillis = 0;
m_ui8KeyAddr = 0;
cursorPos.x = 0;
cursorPos.y = 0;
}
uint16_t OLEDPanel::detect_i2c(uint8_t ui8_keyAddr)
{
m_ui8KeyAddr = ui8_keyAddr;
// OLED-Display:
i2c_init();
i2c_start(LCD_I2C_ADR << 1);
if(I2C_ErrorCode)
return I2C_ErrorCode;
if (m_ui8KeyAddr)
{
// PCF8574 for Buttons:
i2c_init();
i2c_start(m_ui8KeyAddr);
return I2C_ErrorCode << 8;
}
return 0;
}
void OLEDPanel::begin(uint8_t /*cols*/, uint8_t /*rows*/)
{
lcd_init(LCD_DISP_ON); // init lcd and turn on
initButtons();
}
void OLEDPanel::initButtons()
{
// init Buttons:
if(m_ui8KeyAddr)
{
i2c_init();
i2c_start(m_ui8KeyAddr);
i2c_byte(0b11111111); // all inputs
i2c_stop();
// After setting up the button, setup the Bounce instances :
debouncer_OK.attach(m_ui8KeyAddr, 0, DEBOUNCE_TIME);
debouncer_Right.attach(m_ui8KeyAddr, 1, DEBOUNCE_TIME);
debouncer_Down.attach(m_ui8KeyAddr, 2, DEBOUNCE_TIME);
debouncer_Up.attach(m_ui8KeyAddr, 3, DEBOUNCE_TIME);
debouncer_Left.attach(m_ui8KeyAddr, 4, DEBOUNCE_TIME);
debouncer_F1.attach(m_ui8KeyAddr, 5, DEBOUNCE_TIME);
debouncer_F2.attach(m_ui8KeyAddr, 6, DEBOUNCE_TIME);
debouncer_F3.attach(m_ui8KeyAddr, 7, DEBOUNCE_TIME);
}
}
void OLEDPanel::updateDebounce()
{
if (m_ui8KeyAddr)
{
// Update the Bounce instances :
debouncer_OK.update();
debouncer_Right.update();
debouncer_Down.update();
debouncer_Up.update();
debouncer_Left.update();
debouncer_F1.update();
debouncer_F2.update();
debouncer_F3.update();
}
}
void OLEDPanel::setKeyAddr(uint8_t ui8_keyAddr, bool bInit)
{
m_ui8KeyAddr = ui8_keyAddr;
if(bInit)
initButtons();
}
void OLEDPanel::clear()
{
lcd_clrscr();
}
void OLEDPanel::clearLine(uint8_t y)
{
clearToEOL(0, y);
}
void OLEDPanel::clearToEOL(uint8_t x, uint8_t y)
{
clear(x, y, COUNT_OF_CHARS - x);
}
// clear iCount of chars starting at x, y
void OLEDPanel::clear(uint8_t x, uint8_t y, uint8_t iCount)
{
if((x + iCount) > COUNT_OF_CHARS || y > (COUNT_OF_LINES - 1))
return;
lcd_gotoxy(x, y);
register uint8_t iSize(6 * iCount);
uint8_t clearLine[iSize];
memset(clearLine, 0x00, iSize);
lcd_data(clearLine, sizeof(clearLine));
lcd_gotoxy(x/* + iCount*/, y);
}
void OLEDPanel::setCharMode(bool bDouble, bool bInvert, bool bUnderline)
{
uint8_t uCharMode(NORMALSIZE);
if (bDouble)
uCharMode = DOUBLESIZE;
if (bUnderline)
uCharMode |= UNDERLINE;
if (bInvert)
uCharMode |= INVERT;
lcd_charMode(uCharMode);
}
void OLEDPanel::noCursor()
{
m_bCursorOn = false;
clearToEOL(0, cursorPos.y + 1);
}
void OLEDPanel::cursor(uint8_t x, uint8_t y)
{
m_bCursorOn = true;
cursorPos.x = x;
cursorPos.y = y;
clearToEOL(0, cursorPos.y + 1);
lcd_gotoxy(cursorPos.x, cursorPos.y + 1);
lcd_putc(0xAF); // (Macron = 'Overline')
lcd_gotoxy(cursorPos.x, cursorPos.y);
}
//needs to be called cyclic e.g. to generate 1-Hz-puls
void OLEDPanel::refresh()
{
if (millis() - m_ulpreviousMillis > 500)
{
m_ulpreviousMillis = millis(); // keep actual timevalue
// change status:
m_bBlinken1Hz = !m_bBlinken1Hz;
}
}
void OLEDPanel::setCursor(uint8_t x, uint8_t y)
{
lcd_gotoxy(x, y);
}
size_t OLEDPanel::print(const __FlashStringHelper *pText)
{
// count chars to display
PGM_P p1(reinterpret_cast<PGM_P>(pText));
register uint8_t c;
register uint8_t iCount(0);
while ((c = pgm_read_byte(p1++)))
{
lcd_putc((unsigned char)(c));
++iCount;
}
return iCount;
}
size_t OLEDPanel::print(const char *pText)
{
// count chars to display
register uint8_t iCount(0);
while (*pText)
{
lcd_putc((unsigned char)(*pText++));
++iCount;
}
return iCount;
}
size_t OLEDPanel::print(const String& s)
{
// count chars to display
register uint8_t iCount(0);
for (uint16_t i = 0; i < s.length(); i++)
{
lcd_putc((unsigned char)(s[i]));
++iCount;
}
return iCount;
}
size_t OLEDPanel::print(char ch)
{
lcd_putc((unsigned char)(ch));
return 1;
}
size_t OLEDPanel::print(uint8_t x, uint8_t y, char ch)
{
lcd_gotoxy(x, y);
lcd_putc((unsigned char)(ch));
return 1;
}
#define BUF_SIZE 33
size_t OLEDPanel::print(uint8_t ui8Value, int iType)
{
char buf[BUF_SIZE]; // Assumes 8-bit chars plus zero byte.
return print(intToAscii((char*)&(buf), BUF_SIZE, (unsigned long)ui8Value, iType));
}
size_t OLEDPanel::print(uint16_t ui16Value, int iType)
{
char buf[BUF_SIZE]; // Assumes 8-bit chars plus zero byte.
return print(intToAscii((char*)&(buf), BUF_SIZE, (unsigned long)ui16Value, iType));
}
size_t OLEDPanel::print(unsigned long ulValue, int iType)
{
char buf[BUF_SIZE]; // Assumes 8-bit chars plus zero byte.
return print(intToAscii((char*)&(buf), BUF_SIZE, ulValue, iType));
}
// output text centered in line y
size_t OLEDPanel::printc(uint8_t y, const __FlashStringHelper *pText)
{
if(setStartPositionForCenterText(y, countChar(pText)))
// output text to display
return print(pText);
return 0;
}
// output text centered in line y
size_t OLEDPanel::printc(uint8_t y, const char *pText)
{
if(setStartPositionForCenterText(y, countChar(pText)))
// output text to display
return print(pText);
return 0;
}
// output number
// x!=255 && y!=255 : use cursorposition x, y
// x==255 && y!=255 : center in line y
size_t OLEDPanel::printc(uint8_t x, uint8_t y, unsigned long ulValue, int iType)
{
char buf[BUF_SIZE]; // Assumes 8-bit chars plus zero byte.
char *pText(intToAscii((char*)&(buf), BUF_SIZE, ulValue, iType));
if(y != 255)
{
if(x == 255)
{
if(!setStartPositionForCenterText(y, countChar(pText)))
return 0;
}
else
lcd_gotoxy(x, y);
}
// output text to display
return print(pText);
}
// output text rightaligned in line y
size_t OLEDPanel::printr(uint8_t y, uint8_t iMaxChar, const __FlashStringHelper *pText)
{
if (setStartPositionForRightText(y, iMaxChar, countChar(pText)))
// output text to display
return print(pText);
return 0;
}
// bit is set for each button pressed
uint8_t OLEDPanel::readButtons()
{
register uint8_t iRetValue(0xff);
if(m_ui8KeyAddr)
{
i2c_start(m_ui8KeyAddr + 1);
iRetValue = i2c_readNAck();
i2c_stop();
}
return ~iRetValue; // since buttons are switch to GND, we invert the state
}
void OLEDPanel::printOuterFrame()
{
// upper frameline
lcd_gotoxy(0, 0);
i2c_start(LCD_I2C_ADR << 1);
i2c_byte(0x40); // 0x00 for command, 0x40 for data
i2c_byte(0xFF);
for (uint8_t i = 1; i < (DISPLAY_WIDTH - 1); i++)
i2c_byte(0x01);
i2c_byte(0xFF);
i2c_stop();
// border lines
for (uint8_t i = 1; i < (COUNT_OF_LINES - 1); i++)
{
// left border
lcd_gotoxy(0, i);
i2c_start(LCD_I2C_ADR << 1);
i2c_byte(0x40); // 0x00 for command, 0x40 for data
i2c_byte(0xFF);
i2c_stop();
// right border
lcd_gotoxy(COUNT_OF_CHARS - 1, i);
i2c_start(LCD_I2C_ADR << 1);
i2c_byte(0x40); // 0x00 for command, 0x40 for data
for (uint8_t j = 0; j <= CHAR_WIDTH; j++)
i2c_byte(0x00);
i2c_byte(0xFF);
i2c_stop();
}
// lower frameline
lcd_gotoxy(0, COUNT_OF_LINES - 1);
i2c_start(LCD_I2C_ADR << 1);
i2c_byte(0x40); // 0x00 for command, 0x40 for data
i2c_byte(0xFF);
for (uint8_t i = 1; i < (DISPLAY_WIDTH - 1); i++)
i2c_byte(0x80);
i2c_byte(0xFF);
i2c_stop();
}
//=== static functions ========================================================
char* OLEDPanel::intToAscii(char *buf, uint8_t len, unsigned long n, uint8_t base)
{
char *str(&buf[len-1]);
*str = '\0';
// prevent crash if called with base == 1
if (base < BIN)
base = DEC;
do
{
char c(n % base);
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
--len;
} while(n && (len != 0));
return str;
}
//=== protected functions =====================================================
uint8_t OLEDPanel::countChar(const char *ps)
{
// count chars to display
register uint8_t iCount(0);
while (*ps++)
++iCount;
return iCount;
}
uint8_t OLEDPanel::countChar(const __FlashStringHelper *pText)
{
// count chars to display
PGM_P ps(reinterpret_cast<PGM_P>(pText));
register uint8_t iCount(0);
while (pgm_read_byte(ps++))
++iCount;
return iCount;
}
// calculate startposition for centered text
bool OLEDPanel::setStartPositionForCenterText(uint8_t y, uint8_t iCount)
{
if (y > (COUNT_OF_LINES - 1))
return false; // out of display
// TODO ZIM: not shure why "lcd_gotoxy(0, y)" is neccessary
lcd_gotoxy(0, y);
// calculate startposition for text
uint8_t x((DISPLAY_WIDTH - (iCount * CHAR_WIDTH)) / 2);
// the following 'lcd_command' is similar to 'lcd_gotoxy')
#if defined SSD1306
uint8_t commandSequence[] = {(uint8_t)(0xb0+y), 0x21, x, 0x7f};
#elif defined SH1106
uint8_t commandSequence[] = {(uint8_t)(0xb0+(y & 0x0f)), 0x21, (uint8_t)(0x00+((2+x) & (0x0f))), (uint8_t)(0x10+( ((2+x) & (0xf0)) >> 4 )), 0x7f};
#endif
lcd_command(commandSequence, sizeof(commandSequence));
return true;
}
bool OLEDPanel::setStartPositionForRightText(uint8_t y, uint8_t iMaxChar, uint8_t iCount)
{
if (y > (COUNT_OF_LINES - 1))
return false; // out of display
// TODO ZIM: not shure why "lcd_gotoxy(0, y)" is neccessary
lcd_gotoxy(0, y);
if (iMaxChar && (iMaxChar < iCount))
iCount = iMaxChar;
// calculate startposition for text
uint8_t x((DISPLAY_WIDTH - (iCount * CHAR_WIDTH)));
// the following 'lcd_command' is similar to 'lcd_gotoxy')
#if defined SSD1306
uint8_t commandSequence[] = { (uint8_t)(0xb0 + y), 0x21, x, 0x7f };
#elif defined SH1106
uint8_t commandSequence[] = { (uint8_t)(0xb0 + (y & 0x0f)), 0x21, (uint8_t)(0x00 + ((2 + x) & (0x0f))), (uint8_t)(0x10 + (((2 + x) & (0xf0)) >> 4)), 0x7f };
#endif
lcd_command(commandSequence, sizeof(commandSequence));
return true;
}