-
Notifications
You must be signed in to change notification settings - Fork 7
/
MatrixDisplay.cpp
571 lines (472 loc) · 15.3 KB
/
MatrixDisplay.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
MatrixDisplay Library 2.0
Author: Miles Burton, www.milesburton.com/
Need a 16x24 display? Check out www.mnethardware.co.uk
Copyright (c) 2010 Miles Burton All Rights Reserved
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) // compiled as ATMega644
//Atmega644 Version of fastWrite - for pins 0-15
#define fWriteA(_pin_, _state_) ( _pin_ < 8 ? (_state_ ? PORTB |= 1 << _pin_ : \
PORTB &= ~(1 << _pin_ )) : (_state_ ? PORTD |= 1 << (_pin_ -8) : PORTD &= ~(1 << (_pin_ -8) )))
//Atmega644 Version of fastWrite - for pins 16-31 (Note: PORTA mapping reversed from others)
#define fWriteB(_pin_, _state_) ( _pin_ < 24 ? (_state_ ? PORTC |= 1 << (_pin_ -16) : \
PORTC &= ~(1 << (_pin_ -16))) : (_state_ ? PORTA |= 1 << (31- _pin_) : PORTA &= ~(1 << (31- _pin_) )))
#else // if ATMega328
//Atmega328 Version of fastWrite - for pins 0-13
#define fWriteA(_pin_, _state_) ( _pin_ < 8 ? (_state_ ? PORTD |= 1 << _pin_ : \
PORTD &= ~(1 << _pin_ )) : (_state_ ? PORTB |= 1 << (_pin_ -8) : PORTB &= ~(1 << (_pin_ -8) )))
//Atmega328 Version of fastWrite - for pins 14-19
#define fWriteB(_pin, _state_) (_state_ ? PORTC |= 1 << (_pin - 14) : \
PORTC &= ~(1 << (_pin -14) ))
#endif
// Encode the Y coordinate to a bit #
#define CalcBit(y) (1 << (y > 7 ? y -8 : y))
#include "MatrixDisplay.h"
#define NULL 0
#define BACKBUFFER_SIZE 48
#define DIRTY_BIT 0x80
///////////////////////////////////////////////////////////////////////////////
// CTORS & DTOR
//
// Setup the buffers within the constructor, a little more inflexible but saves pain later on
MatrixDisplay::MatrixDisplay(uint8_t numDisplays, uint8_t clkPin, uint8_t dataPin, bool buildShadow)
: pDisplayBuffers(NULL)
, pDisplayPins(NULL)
, dataPin(dataPin)
, clkPin(clkPin)
, displayCount(numDisplays)
, backBufferSize(sizeof(uint8_t) * BACKBUFFER_SIZE)
{
// allocate RAM buffer for display bits
// 24 columns * 16 rows / 8 bits = 48 bytes
uint8_t sz = displayCount * backBufferSize;
pDisplayBuffers = (uint8_t *)malloc(sz);
memset(pDisplayBuffers, 0, sz);
if(buildShadow)
{
// allocate RAM buffer for display bits
pShadowBuffers = (uint8_t *)malloc(sz);
memset(pShadowBuffers, 0, sz);
}
// allocate a buffer for pin assignments
pDisplayPins = (uint8_t *) malloc( sizeof(uint8_t) * numDisplays );
memset(pDisplayPins, 0, sizeof(uint8_t) * numDisplays);
// set data & clock pin modes
pinMode(dataPin, OUTPUT);
pinMode(clkPin, OUTPUT);
bitBlast(dataPin, 1);
bitBlast(clkPin, 1);
}
// Destructor
MatrixDisplay::~MatrixDisplay()
{
if(pDisplayBuffers)
{
free(pDisplayBuffers);
pDisplayBuffers = NULL;
}
if(pDisplayPins)
{
free(pDisplayPins);
pDisplayPins = NULL;
}
if(pShadowBuffers)
{
free(pShadowBuffers);
pShadowBuffers = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
//
void MatrixDisplay::initDisplay(uint8_t displayNum, uint8_t pin, bool master)
{
// Associate the pin with this display
pDisplayPins[displayNum] = pin;
// init the hardware
pinMode(pin, OUTPUT);
bitBlast(pin, 1); // Disable chip (pull high)
selectDisplay(displayNum);
// Send Precommand
preCommand();
// Take advantage of successive mode and write the options
writeDataBE(8,HT1632_CMD_SYSDIS, true);
//sendOptionCommand(HT1632_CMD_MSTMD);
writeDataBE(8,HT1632_CMD_SYSON,true);
writeDataBE(8,HT1632_CMD_COMS11,true);
writeDataBE(8,HT1632_CMD_LEDON,true);
writeDataBE(8,HT1632_CMD_BLOFF,true);
writeDataBE(8,HT1632_CMD_PWM+15,true);
releaseDisplay(displayNum);
clear(displayNum,true);
}
uint8_t MatrixDisplay::getPixel(uint8_t displayNum, uint8_t x, uint8_t y, bool useShadow)
{
// Encode XY to an appropriate XY address
uint8_t address = xyToIndex(x, y);
// offset to the correct buffer for the display
address += backBufferSize * displayNum;
uint8_t bit = CalcBit(y);
// fetch the value byte from the buffer
address = (backBufferSize * displayNum) + address;
uint8_t value = 0;
if(useShadow)
{
value = pShadowBuffers[address];
}else{
value = pDisplayBuffers[address];
}
return (value & bit) ? 1 : 0;
}
void MatrixDisplay::setPixel(uint8_t displayNum, uint8_t x, uint8_t y, uint8_t value, bool paint, bool useShadow)
{
// calculate a pointer into the display buffer (6 bit offset)
uint8_t address = xyToIndex(x, y);
uint8_t dispAddress = address;
// offset to the correct buffer for the display
address += backBufferSize * displayNum;
uint8_t bit = CalcBit(y);
// ...and apply the value
if(value)
{
if(useShadow)
{
pShadowBuffers[address] |= bit;
}else{
pDisplayBuffers[address] |= bit;
}
}
else
{
if(useShadow)
{
pShadowBuffers[address] &= ~bit;
}else{
pDisplayBuffers[address] &= ~bit;
}
}
if(useShadow) return;
if(!paint)
{
// flag the display as dirty
//pDisplayPins[displayNum] |= DIRTY_BIT;
}else{
uint8_t dispAddress = displayXYToIndex(x, y);
uint8_t value = pDisplayBuffers[address];
if(((y >> 2)&1)) // Devide y by 4. Work out whether it's odd or even. 8 pixels packed into 1 byte. 16 pixels are in two bytes. We need to figure out whether to shift the buffer
{
value = pDisplayBuffers[address] >> 4;
}
writeNibbles(displayNum, dispAddress, &value, 1);
}
}
void MatrixDisplay::dumpByte(uint8_t aByte)
{
Serial.println("Byte value");
for(int8_t k=7; k>=0; --k)
{
Serial.print((aByte >> k) & 1, DEC);
}
Serial.print("\n\n");
}
// Write the backbuffer out to all displays (which have the dirty bit set!)
void MatrixDisplay::syncDisplays()
{
uint8_t bufferOffset = 0;
uint8_t value = 0;
for(int8_t dispNum=0; dispNum < displayCount; ++dispNum)
{
// if(pDisplayPins[dispNum] & DIRTY_BIT == 0) continue;
bufferOffset = backBufferSize * dispNum;
// flip the "dirty" bit
// pDisplayPins[dispNum] ^= DIRTY_BIT;
selectDisplay(dispNum);
writeDataBE(3, HT1632_ID_WR); // Send "write to display" command
writeDataBE(7, 0); // Send initial address (aka 0)
// Operating in progressive addressing mode
for(int8_t addr = 0; addr < backBufferSize; ++addr) // Thought i'd simplify this a touch
{
value = pDisplayBuffers[addr + bufferOffset];
writeDataLE(8, value);
}
releaseDisplay(dispNum);
}
}
void MatrixDisplay::writeNibbles(uint8_t displayNum, uint8_t addr, uint8_t* data, uint8_t nybbleCount)
{
selectDisplay(displayNum); // Select chip
writeDataBE(3, HT1632_ID_WR); // send ID: WRITE to RAM
writeDataBE(7,addr); // Send address
for(int8_t i = 0; i < nybbleCount; ++i) writeDataLE(4,data[i]); // send multiples of 4 bits of data
releaseDisplay(displayNum); // done
}
void MatrixDisplay::clear(uint8_t displayNum, bool paint, bool useShadow)
{
// clear the display's backbuffer
if(useShadow)
{
memset( pShadowBuffers + (backBufferSize * displayNum),
0,
backBufferSize
);
}else{
memset( pDisplayBuffers + (backBufferSize * displayNum),
0,
backBufferSize
);
// Flag dirty bit
// pDisplayPins[displayNum] |= DIRTY_BIT;
}
// Write out change
if(paint && !useShadow) syncDisplays();
}
void MatrixDisplay::clear(bool paint, bool useShadow)
{
if(useShadow)
{
memset(pShadowBuffers,0, backBufferSize*displayCount);
}else{
memset(pDisplayBuffers,0, backBufferSize*displayCount);
}
// Select all displays and clear
if(paint && !useShadow)
{
for(int8_t i=0; i<displayCount; ++i) selectDisplay(i); // Enable all displays
// Use progressive write mode, faster
writeDataBE(3, HT1632_ID_WR); // Send "write to display" command
writeDataBE(7, 0); // Send initial address (aka 0)
for(uint8_t i = 0; i<48; ++i)
{
writeDataLE(8,0); // Write nada
}
for(int8_t i=0; i<displayCount; ++i) releaseDisplay(i); // Disable all displays
}
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
//
inline uint8_t MatrixDisplay::xyToIndex(uint8_t x, uint8_t y)
{
// cap X coordinate at 24th column
x = x > 23 ? 23 : x;
// cap Y coordinate at 16
y &= 0xF;
uint8_t addresss = y > 7 ? 1 : 0; // Work out which panel it's on (top:0, bottom:1)
addresss += x<<1; // Shift x by 1 and add which panel it's on
return addresss;
}
inline uint8_t MatrixDisplay::displayXYToIndex(uint8_t x, uint8_t y)
{
uint8_t addresss = y == 0 ? 0 : (y / 4); // Calculate which quandrant[?] it's in
addresss += x << 2; // Shift x by 2 and add which panel it's on
return addresss;
}
inline void MatrixDisplay::selectDisplay(uint8_t displayNum)
{
// Serial.println(pDisplayPins[displayNum],DEC);
bitBlast(pDisplayPins[displayNum], 0);
//digitalWrite(5,0);
}
inline void MatrixDisplay::releaseDisplay(uint8_t displayNum)
{
// Serial.println(pDisplayPins[displayNum],DEC);
bitBlast(pDisplayPins[displayNum], 1);
//digitalWrite(5,1);
}
void MatrixDisplay::writeCommand(uint8_t displayNum, uint8_t command)
{
selectDisplay(displayNum);
bitBlast(dataPin, 1);
writeDataBE(3, HT1632_ID_CMD); // Write out MSB [3 bits]
writeDataBE(8, command); // Then MSB [7 8 bits]
writeDataBE(1, 0); // 1 bit extra
bitBlast(dataPin, 0);
releaseDisplay(displayNum);
}
// Writes out LSB first
void MatrixDisplay::writeDataLE(int8_t bitCount, uint8_t data)
{
//if(bitCount <= 0 || bitCount > 8) return;
// assumes correct display is selected
for(int8_t i = 0; i < bitCount; ++i)
{
bitBlast(clkPin, 0);
bitBlast(dataPin, (data >> i) & 1);
bitBlast(clkPin, 1);
}
}
// Writes out MSB first
void MatrixDisplay::writeDataBE(int8_t bitCount, uint8_t data, bool useNop)
{
//if(bitCount <= 0 || bitCount > 8) return;
// assumes correct display is selected
for(int8_t i = bitCount - 1; i >= 0; --i)
{
bitBlast(clkPin, 0);
bitBlast(dataPin, (data >> i) & 1);
bitBlast(clkPin, 1);
}
if(useNop)
{
bitBlast(clkPin, 0); //clk = 0 for data ready
_nop();
_nop();
bitBlast(clkPin, 1); //clk = 1 for data write into 1632
}
}
// Writes out MSB first
void MatrixDisplay::preCommand()
{
bitBlast(clkPin, 0);
bitBlast(dataPin, 1);
_nop();
bitBlast(clkPin, 1);
_nop();
_nop();
bitBlast(clkPin, 0);
bitBlast(dataPin, 0);
_nop();
bitBlast(clkPin, 1);
_nop();
_nop();
bitBlast(clkPin, 0);
bitBlast(dataPin, 0);
_nop();
bitBlast(clkPin, 1);
_nop();
_nop();
}
void MatrixDisplay::bitBlast(uint8_t pin, uint8_t data)
{
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
switch (pin) {
case 0: PORTE &= ~0x01;
PORTE |= (data & 0x01);
break;
case 1: PORTE &= ~0x02;
PORTE |= (data & 0x01) << 1;
break;
case 2: PORTE &= ~0x10;
PORTE |= (data & 0x01) << 4;
break;
case 3: PORTE &= ~0x20;
PORTE |= (data & 0x01) << 5;
break;
case 4: PORTG &= ~0x20;
PORTG |= (data & 0x01) << 5;
break;
case 5: PORTE &= ~0x08;
PORTE |= (data & 0x01) << 3;
break;
case 6: PORTH &= ~0x08;
PORTH |= (data & 0x01) << 3;
break;
case 7: PORTH &= ~0x10;
PORTH |= (data & 0x01) << 4;
break;
case 8: PORTH &= ~0x20;
PORTH |= (data & 0x01) << 5;
break;
case 9: PORTH &= ~0x40;
PORTH |= (data & 0x01) << 6;
break;
case 10: PORTB &= ~0x10;
PORTB |= (data & 0x01) << 4;
break;
case 11: PORTB &= ~0x20;
PORTB |= (data & 0x01) << 5;
break;
case 12: PORTB &= ~0x40;
PORTB |= (data & 0x01) << 6;
break;
case 13: PORTB &= ~0x80;
PORTB |= (data & 0x01) << 7;
break;
case 14: PORTJ &= ~0x02;
PORTJ |= (data & 0x01) << 1;
break;
case 15: PORTJ &= ~0x01;
PORTJ |= (data & 0x01);
break;
case 16: PORTH &= ~0x02;
PORTH |= (data & 0x01) << 1;
break;
case 17: PORTH &= ~0x01;
PORTH |= (data & 0x01);
break;
case 18: PORTD &= ~0x08;
PORTD |= (data & 0x01) << 3;
break;
case 19: PORTD &= ~0x04;
PORTD |= (data & 0x01) << 2;
break;
case 20: PORTD &= ~0x02;
PORTD |= (data & 0x01) << 1;
break;
case 21: PORTD &= ~0x01;
PORTD |= (data & 0x01);
break;
/*
If you want to use the pins 22 - 53 lookup the PORTx in the Arduino Mega pinmap and insert a matching case statement
*/
}
#else
if (pin < 8) {
PORTD &= ~(1 << pin );
PORTD |= (data & 0x01) << pin;
} else if (pin < 14) {
PORTB &= ~(1 << (pin - 8) );
PORTB |= (data & 0x01) << (pin - 8);
} else {
PORTC &= ~(1 << (pin - 14) );
PORTC |= (data & 0x01) << (pin - 14);
}
#endif
}
uint8_t MatrixDisplay::getDisplayCount()
{
return displayCount;
}
uint8_t MatrixDisplay::getDisplayHeight()
{
return 16;
}
uint8_t MatrixDisplay::getDisplayWidth()
{
return 24;
}
// Copy from the display buffer to the shadow buffer (takes a snapshot)
void MatrixDisplay::copyBuffer()
{
if(pShadowBuffers==0) return;
memcpy (pShadowBuffers, pDisplayBuffers, (backBufferSize * displayCount) );
}
void MatrixDisplay::shiftLeft()
{
memcpy ( pDisplayBuffers, pDisplayBuffers+2, (backBufferSize * displayCount));
}
void MatrixDisplay::shiftRight()
{
memcpy ( pDisplayBuffers+2, pDisplayBuffers, (backBufferSize * displayCount)-2);
}
void MatrixDisplay::setBrightness(uint8_t dispNum, uint8_t pwmValue)
{
// Check boundaries
if(pwmValue > 15) pwmValue = 15;
else if(pwmValue < 0) pwmValue = 0;
selectDisplay(dispNum);
preCommand();
writeDataBE(8,HT1632_CMD_PWM+pwmValue,true);
releaseDisplay(dispNum);
}