-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRSharpDisplay.cpp
More file actions
455 lines (364 loc) · 11.6 KB
/
LRSharpDisplay.cpp
File metadata and controls
455 lines (364 loc) · 11.6 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
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
//
// Lucky Resistor's Sharp Display Driver 8x8 Pixel
// ---------------------------------------------------------------------------
// (c)2015 by Lucky Resistor. See LICENSE for details.
//
// 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 2 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, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "LRSharpDisplay.h"
namespace lr {
// An anonymous namespace to prevent name conflicts.
namespace {
// Use a number of global variables for time critical parts
// The following variables are used for the communication.
static uint8_t gChipSelectPin;
static uint8_t gClockPin;
static volatile uint8_t *gClockPort;
static uint8_t gClockMask;
static uint8_t gDataPin;
static volatile uint8_t *gDataPort;
static uint8_t gDataMask;
// This constants are bitmasks for the commands
static const uint8_t CMD_WRITE = 0x80;
static const uint8_t CMD_CLEAR = 0x20;
static const uint8_t CMD_VCOM = 0x40;
// This flag is used to toggle the required VCOM bit.
static uint8_t gVComBit;
// The array with the 12x12 screen.
static const uint8_t gScreenHeight = 12;
static const uint8_t gScreenWidth = 12;
static uint8_t gScreenCharacters[gScreenHeight*gScreenWidth];
static const uint8_t gScreenRowRequiresUpdateSize = gScreenHeight/8+1;
static uint8_t gScreenRowRequiresUpdate[gScreenRowRequiresUpdateSize];
// The height of a single character.
static const uint8_t gCharacterHeight = 8;
// A empty font, to prevent any crashes if no font is set.
static const uint8_t gNoFont[8] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0};
// The current font for the display
static const uint8_t *gTextFont;
// The current text flags
static const uint8_t gTextFlagInverse = 0x80;
static const uint8_t gTextCharacterMask = 0x7f;
static uint8_t gTextFlags;
// The cursor position
static uint8_t gCursorX;
static uint8_t gCursorY;
// Set the clock low
inline static void setClockLow()
{
*gClockPort &= ~gClockMask;
}
// Set the clock high
inline static void setClockHigh()
{
*gClockPort |= gClockMask;
}
// Set the data low
inline static void setDataLow()
{
*gDataPort &= ~gDataMask;
}
// Set the data high
inline static void setDataHigh()
{
*gDataPort |= gDataMask;
}
// This method sends a single byte MSB first to the display
static void sendByteMSB(uint8_t byte)
{
for (uint8_t i = 0; i < 8; ++i) {
setClockLow();
if (byte & 0x80) {
setDataHigh();
} else {
setDataLow();
}
setClockHigh();
byte <<= 1;
}
setClockLow();
}
// This method sends a single byte LSB first to the display
static void sendByteLSB(uint8_t byte)
{
for (uint8_t i = 0; i < 8; ++i) {
setClockLow();
if (byte & 0x01) {
setDataHigh();
} else {
setDataLow();
}
setClockHigh();
byte >>= 1;
}
setClockLow();
}
// This method toggles the VCOM bit
inline static void toggleVComBit()
{
gVComBit ^= CMD_VCOM;
}
// This method calculates a pointer to a character on the screen
inline uint8_t* getCharacterPosition(uint8_t row, uint8_t column)
{
return &gScreenCharacters[(row*gScreenWidth)+column];
}
// Clear the whole screen.
inline void clearScreen()
{
memset(gScreenCharacters, 0x00, gScreenWidth*gScreenHeight);
}
// Check if a given row is maked for updates
inline bool isRowMarkedForUpdate(uint8_t row)
{
return ((gScreenRowRequiresUpdate[row>>3] & (1<<(row&7))) != 0);
}
// Mark a given row for an update
inline void markRowForUpdate(uint8_t row)
{
gScreenRowRequiresUpdate[row>>3] |= (1<<(row&7));
}
// Mark the whole screen for an update.
inline void markScreenForUpdate()
{
memset(gScreenRowRequiresUpdate, 0xff, gScreenRowRequiresUpdateSize);
}
// Remove any row update requests from the screen.
inline void clearScreenUpdate()
{
memset(gScreenRowRequiresUpdate, 0x00, gScreenRowRequiresUpdateSize);
}
}
SharpDisplay::SharpDisplay(uint8_t chipSelectPin, uint8_t clockPin, uint8_t dataPin)
{
// Prepare the values for the SPI communication
gChipSelectPin = chipSelectPin;
gClockPin = clockPin;
gClockPort = portOutputRegister(digitalPinToPort(clockPin));
gClockMask = digitalPinToBitMask(clockPin);
gDataPin = dataPin;
gDataPort = portOutputRegister(digitalPinToPort(dataPin));
gDataMask = digitalPinToBitMask(dataPin);
// Initialize the screen memory
clearScreen();
clearScreenUpdate();
gTextFlags = 0;
gTextFont = gNoFont;
gCursorX = 0;
gCursorY = 0;
}
SharpDisplay::~SharpDisplay()
{
}
void SharpDisplay::begin()
{
// Prepare all communication ports
digitalWrite(gChipSelectPin, HIGH);
digitalWrite(gClockPin, LOW);
digitalWrite(gDataPin, HIGH);
pinMode(gChipSelectPin, OUTPUT);
pinMode(gClockPin, OUTPUT);
pinMode(gDataPin, OUTPUT);
// Set the VCOM bit
gVComBit = CMD_VCOM;
// Clear the display.
clear();
}
void SharpDisplay::setFont(const uint8_t *fontData)
{
gTextFont = fontData;
markScreenForUpdate();
}
void SharpDisplay::clear()
{
// Send the clear command.
digitalWrite(gChipSelectPin, HIGH);
sendByteMSB(CMD_CLEAR|gVComBit);
sendByteMSB(0);
toggleVComBit();
digitalWrite(gChipSelectPin, LOW);
// Clear the screen.
clearScreen();
clearScreenUpdate();
gCursorX = 0;
gCursorY = 0;
}
void SharpDisplay::refresh()
{
// Send the write command.
digitalWrite(gChipSelectPin, HIGH);
sendByteMSB(CMD_WRITE|gVComBit);
toggleVComBit();
// Update all rows which need a refresh.
for (uint8_t row = 0; row < gScreenHeight; ++row) {
if (isRowMarkedForUpdate(row)) {
// Draw the row pixel row by pixel row
for (uint8_t pixelRow = 0; pixelRow < gCharacterHeight; ++pixelRow) {
sendByteLSB(row*gCharacterHeight+pixelRow+1);
for (uint8_t column = 0; column < gScreenWidth; ++column) {
const uint8_t screenData = *(getCharacterPosition(row, column));
const uint8_t characterIndex = (screenData & gTextCharacterMask);
uint16_t characterStart = characterIndex;
characterStart *= gCharacterHeight;
characterStart += pixelRow;
uint8_t pixelMask = pgm_read_byte(gTextFont+characterStart);
if ((screenData & gTextFlagInverse) != 0) { // Inverse character?
pixelMask = ~pixelMask;
}
sendByteMSB(pixelMask);
}
sendByteLSB(0x00);
}
}
}
sendByteLSB(0x00);
digitalWrite(gChipSelectPin, LOW);
clearScreenUpdate();
}
void SharpDisplay::setTextInverse(bool enable)
{
if (enable) {
gTextFlags |= gTextFlagInverse;
} else {
gTextFlags &= ~gTextFlagInverse;
}
}
void SharpDisplay::setCharacter(uint8_t row, uint8_t column, uint8_t character)
{
if (row < gScreenWidth && column < gScreenHeight) {
uint8_t* const cp = getCharacterPosition(row, column);
*cp = ((static_cast<uint8_t>(character)-0x20) & gTextCharacterMask) | gTextFlags;
markRowForUpdate(row);
}
}
char SharpDisplay::getCharacter(uint8_t row, uint8_t column)
{
if (row < gScreenWidth && column < gScreenHeight) {
const uint8_t* const cp = getCharacterPosition(row, column);
return (*cp & gTextCharacterMask) + 0x20;
} else {
return 0;
}
}
void SharpDisplay::setLineText(uint8_t row, const String &text)
{
if (row < gScreenHeight) {
const uint8_t length = text.length();
for (uint8_t column = 0; column < gScreenWidth; ++column) {
if (column < length) {
setCharacter(row, column, text.charAt(column));
} else {
setCharacter(row, column, ' ');
}
}
markRowForUpdate(row);
}
}
void SharpDisplay::setLineInverted(uint8_t row, bool inverted)
{
if (row < gScreenHeight) {
for (uint8_t column = 0; column < gScreenWidth; ++column) {
uint8_t *c = getCharacterPosition(row, column);
if (inverted) {
*c |= gTextFlagInverse;
} else {
*c &= ~gTextFlagInverse;
}
}
markRowForUpdate(row);
}
}
void SharpDisplay::setCursorPosition(uint8_t row, uint8_t column)
{
// Check the bounds.
if (row > gScreenHeight) {
row = gScreenHeight;
}
if (column > gScreenWidth) {
column = gScreenWidth;
}
gCursorY = row;
gCursorX = column;
// If the cursor is below the last row, only X position 0 is valid.
if (gCursorY == gScreenHeight) {
gCursorX = 0;
}
}
void SharpDisplay::getCursorPosition(uint8_t &row, uint8_t &column)
{
row = gCursorY;
column = gCursorX;
}
void SharpDisplay::writeCharacter(uint8_t c)
{
if (c == '\n') { // Add a line break
gCursorX = 0;
if (gCursorY < gScreenHeight) {
++gCursorY;
} else {
// Cursor is at the bottom. Scroll is required.
scrollScreen(ScrollUp);
}
} else if (c >= 0x20) { // Ignore any other control characters.
if (gCursorX == gScreenWidth) {
gCursorX = 0;
if (gCursorY < gScreenHeight) {
++gCursorY;
}
}
if (gCursorY == gScreenHeight) {
scrollScreen(ScrollUp);
--gCursorY;
}
setCharacter(gCursorY, gCursorX, c);
++gCursorX;
}
}
void SharpDisplay::writeText(const String &text)
{
const unsigned int length = text.length();
for (unsigned int i = 0; i < length; ++i) {
writeCharacter(text.charAt(i));
}
}
void SharpDisplay::scrollScreen(ScrollDirection direction)
{
switch (direction) {
case ScrollUp:
memmove(getCharacterPosition(0, 0), getCharacterPosition(1, 0), gScreenWidth*(gScreenHeight-1));
memset(getCharacterPosition(gScreenHeight-1, 0), 0, gScreenWidth);
break;
case ScrollDown:
memmove(getCharacterPosition(1, 0), getCharacterPosition(0, 0), gScreenWidth*(gScreenHeight-1));
memset(getCharacterPosition(0, 0), 0, gScreenWidth);
break;
case ScrollLeft:
memmove(getCharacterPosition(0, 0), getCharacterPosition(0, 1), (gScreenWidth*gScreenHeight)-1);
for (uint8_t row = 0; row < gScreenHeight; ++row) {
uint8_t *c = getCharacterPosition(row, gScreenWidth-1);
*c = 0;
}
break;
case ScrollRight:
memmove(getCharacterPosition(0, 1), getCharacterPosition(0, 0), (gScreenWidth*gScreenHeight)-1);
for (uint8_t row = 0; row < gScreenHeight; ++row) {
uint8_t *c = getCharacterPosition(row, 0);
*c = 0;
}
break;
}
markScreenForUpdate();
}
}