-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRGBMatrixPanel.cpp
More file actions
159 lines (123 loc) · 4.11 KB
/
RGBMatrixPanel.cpp
File metadata and controls
159 lines (123 loc) · 4.11 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
#include "RGBMatrixPanel.h"
volatile SemaphoreHandle_t RGBMatrixPanel::timerSemaphore;
RGBMatrixPanel* RGBMatrixPanel ::singleton;
void IRAM_ATTR RGBMatrixPanel::onTimer() {
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL_ISR(&timerMux);
singleton->draw();
portEXIT_CRITICAL_ISR(&timerMux);
xSemaphoreGiveFromISR(timerSemaphore, NULL);
}
void RGBMatrixPanel::begin() {
singleton = this;
pinMode(pinR1, OUTPUT);
pinMode(pinG1, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinR2, OUTPUT);
pinMode(pinG2, OUTPUT);
pinMode(pinB2, OUTPUT);
pinMode(pinLAT, OUTPUT);
pinMode(pinCLK, OUTPUT);
pinMode(pinOE, OUTPUT);
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
digitalWrite(pinE, LOW);
digitalWrite(pinLAT, LOW);
digitalWrite(pinCLK, LOW);
digitalWrite(pinOE, HIGH);
timerSemaphore = xSemaphoreCreateBinary();
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 60, true);
timerAlarmEnable(timer);
}
void RGBMatrixPanel::stop() {
if (timer) {
timerDetachInterrupt(timer);
timerEnd(timer);
}
}
uint16_t RGBMatrixPanel::colorHSV(long hue, uint8_t sat, uint8_t val) {
uint8_t r, g, b, lo;
uint16_t s1, v1;
// Hue ( 0 - 1535 )
hue %= 1536;
if (hue < 0) hue += 1536;
lo = hue & 255; // Low byte = primary/secondary color mix
switch (hue >> 8) { // High byte = sextant of colorwheel
case 0 : r = 255 ; g = lo ; b = 0 ; break; // R to Y
case 1 : r = 255 - lo; g = 255 ; b = 0 ; break; // Y to G
case 2 : r = 0 ; g = 255 ; b = lo ; break; // G to C
case 3 : r = 0 ; g = 255 - lo; b = 255 ; break; // C to B
case 4 : r = lo ; g = 0 ; b = 255 ; break; // B to M
default: r = 255 ; g = 0 ; b = 255 - lo; break; // M to R
}
s1 = sat + 1;
r = 255 - (((255 - r) * s1) >> 8);
g = 255 - (((255 - g) * s1) >> 8);
b = 255 - (((255 - b) * s1) >> 8);
v1 = val + 1;
r = (r * v1) >> 11;
g = (g * v1) >> 11;
b = (b * v1) >> 11;
return color555(r, g, b);
}
void RGBMatrixPanel::drawPixel(int16_t x, int16_t y, uint16_t color) {
//if (x < 0 || x >= 128 || y < 0 || y >= 32) return;
if (x < 0 || x >= panelwidth || y < 0 || y >= panelHeight) return;
/*
if (y < 32) {
x = x + 64;
y = y + 32;
}
if (y >= 32) {
x = x;
y = y - 32;
}
*/
int16_t idx = x + y * panelwidth;
drawBuffer()[idx] = color;
}
uint16_t RGBMatrixPanel::getPixel(int16_t x, int16_t y) {
if (x < 0 || x >= panelwidth || y < 0 || y >= panelHeight) 0;
int16_t idx = x + y * panelwidth;
return drawBuffer()[idx];
}
void IRAM_ATTR RGBMatrixPanel::draw() {
static byte cnt = 30;
static byte y = 15;
static uint32_t out = 0;
y = (y + 1) % scanDevider;
if (y == 0)
cnt = (cnt + 1) % 31; // 31 must be skipped
byte cmp = (cnt >> 4) | ((cnt >> 2) & 0x2) | (cnt & 0x4) | ((cnt << 2) & 0x8) | ((cnt << 4) & 0x10);
for (int x = 0; x < screenWidth; x++) {
bool r1, b1, g1, r2, g2, b2;
uint16_t c = matrixbuff[x + y * screenWidth];
r1 = (c & 0x1f) > cmp;
g1 = ((c >> 5) & 0x1f) > cmp;
b1 = ((c >> 10) & 0x1f) > cmp;
c = matrixbuff[x + (y + scanDevider) * screenWidth];
r2 = (c & 0x1f) > cmp;
g2 = ((c >> 5) & 0x1f) > cmp;
b2 = ((c >> 10) & 0x1f) > cmp;
REG_WRITE(GPIO_OUT_REG, out |
((uint32_t)r1 << pinR1) |
((uint32_t)g1 << pinG1) |
((uint32_t)b1 << pinB1) |
((uint32_t)r2 << pinR2) |
((uint32_t)g2 << pinG2) |
((uint32_t)b2 << pinB2));
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << pinCLK));
}
//REG_WRITE(GPIO_OUT_W1TC_REG, (1 << pinCLK));
REG_WRITE(GPIO_OUT1_W1TS_REG, (1 << (pinOE - 32)) | (1 << (pinLAT - 32)));
out = ((y & 1) << pinA) | ((y & 2) << (pinB - 1)) |
((y & 4) << (pinC - 2)) | ((y & 8) << (pinD - 3)) | ((y & 16) << (pinE - 4));
REG_WRITE(GPIO_OUT_REG, out);
for (int x = 0; x < 8; x++) NOP(); // to wait latch and row switch
REG_WRITE(GPIO_OUT1_W1TC_REG, (1 << (pinOE - 32)) | (1 << (pinLAT - 32)));
}