-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEDMatrix.cpp
158 lines (137 loc) · 4.14 KB
/
LEDMatrix.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
/*
* LEDMatrix.cpp
*
* Created on: November 11, 2018
* Author: Vishal
*/
#include "io.hpp"
#include "string.h"
#include <stdint.h>
#include "LEDMatrix.hpp"
#include "inttypes.h"
void LEDMatrix:: init(LEDMatrixDisplayPincon &pincon)
{
//Address lines
A = new GPIO(pincon.A);
B = new GPIO(pincon.B);
C = new GPIO(pincon.C);
D = new GPIO(pincon.D);
E = new GPIO(pincon.E);
//Control Signals
latch = new GPIO(pincon.latch);
oe = new GPIO(pincon.oe);
clk = new GPIO(pincon.clk);
//Data lines
r1 = new GPIO(pincon.r1);
g1 = new GPIO(pincon.g1);
b1 = new GPIO(pincon.b1);
r2 = new GPIO(pincon.r2);
g2 = new GPIO(pincon.g2);
b2 = new GPIO(pincon.b2);
//Configure all the pins as Output
A->setAsOutput(); B->setAsOutput(); C->setAsOutput();
D->setAsOutput(); E->setAsOutput();
latch->setAsOutput(); oe->setAsOutput(); clk->setAsOutput();
r1->setAsOutput(); g1->setAsOutput(); b1->setAsOutput();
r2->setAsOutput(); g2->setAsOutput(); b2->setAsOutput();
//set default states
A->setLow(); B->setLow(); C->setLow();
D->setLow(); E->setLow();
r1->setLow(); g1->setLow(); b1->setLow();
r2->setLow(); g2->setLow(); b2->setLow();
disableLatch(); disableDisplay(); clk->setLow();
clearFrameBuffer();
}
void LEDMatrix::enableDisplay()
{
oe->setLow();
}
void LEDMatrix::disableDisplay()
{
oe->setHigh();
}
void LEDMatrix::enableLatch()
{
latch->setHigh();
}
void LEDMatrix::disableLatch()
{
latch->setLow();
}
void LEDMatrix::selectRow(int row)
{
(row & 0x01) ? A->setHigh() : A->setLow();
(row & 0x02) ? B->setHigh() : B->setLow();
(row & 0x04) ? C->setHigh() : C->setLow();
(row & 0x08) ? D->setHigh() : D->setLow();
(row & 0x10) ? E->setHigh() : E->setLow();
}
void LEDMatrix::clearPixel(int row, int column)
{
uint64_t pixel = ~((uint64_t)1 << column);
farmeBuffer[row][RedPlane] &= pixel;
farmeBuffer[row][GreenPlane] &= pixel;
farmeBuffer[row][BluePlane] &= pixel;
}
void LEDMatrix::setPixel(int row, int col, Color color)
{
uint64_t pixel = ((uint64_t)1 << col);
if(color & 0x01) {
farmeBuffer[row][BluePlane] |= pixel;
}
if(color & 0x02) {
farmeBuffer[row][GreenPlane] |= pixel;
}
if(color & 0x04) {
farmeBuffer[row][RedPlane] |= pixel;
}
}
void LEDMatrix::setRowData(int row, Color color, uint64_t data)
{
if(color & 0x01) {
farmeBuffer[row][BluePlane] = data;
}
if(color & 0x02) {
farmeBuffer[row][GreenPlane] = data;
}
if(color & 0x04) {
farmeBuffer[row][RedPlane] = data;
}
}
void LEDMatrix::setRowDataRaw(int row, ColorPlane plane, uint64_t data)
{
farmeBuffer[row][plane] = data;
}
void LEDMatrix::clearFrameBuffer()
{
memset(farmeBuffer, 0, sizeof(farmeBuffer));
}
void LEDMatrix::fillFrameBuffer(uint64_t data, Color color)
{
for(int i = 0; i < 64; i++) {
setRowData(i, color, data);
}
}
void LEDMatrix::updateDisplay()
{
for(int i = 0; i < 32; i++) {
disableDisplay();
disableLatch();
selectRow(i); //will select i and i + 32 rows at same time
for(int j = 63; j >= 0; j--) { //shift data with MSB getting shifted-in first
((farmeBuffer[i][RedPlane] >> j) & 1) ? r1->setHigh() : r1->setLow();
((farmeBuffer[i][GreenPlane] >> j) & 1) ? g1->setHigh() : g1->setLow();
((farmeBuffer[i][BluePlane] >> j) & 1) ? b1->setHigh() : b1->setLow();
((farmeBuffer[i + 32][RedPlane] >> j) & 1) ? r2->setHigh() : r2->setLow();
((farmeBuffer[i + 32][GreenPlane] >> j) & 1) ? g2->setHigh() : g2->setLow();
((farmeBuffer[i + 32][BluePlane] >> j) & 1) ? b2->setHigh() : b2->setLow();
clk->setHigh(); clk->setLow();//shift in all 3 color bits at once for top half/bottom half registers
enableLatch(); disableLatch();
}
//at this point, all 3 shift registers should be filled with corresponding row data in frameBuffer
enableLatch(); //push shift register contents down to output registers
enableDisplay();
delay_us(80);
}
disableDisplay();
}