-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_matrix_64x32.c
219 lines (195 loc) · 5.98 KB
/
led_matrix_64x32.c
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
// LED matrix implementation for 64x32 panels
// These panels are naturally programed in "landscape" mode
// but (IMO) the LED rain effect looks better when they are rotated.
// Thus the actual driver works in landscape mode and
// the interface is in portrait mode.
#include "led_matrix.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/mutex.h"
#include <string.h>
#if LED_MATRIX_WIDTH != 32
#error led_matrix_64x32 requires LED_MATRIX_WIDTH be set to 32
#endif
#if LED_MATRIX_HEIGHT != 64
#error led_matrix_64x32 requires LED_MATRIX_HEIGHT be set to 64
#endif
// The driver works in landscape mode.
#define LED_COLUMNS 64
#define LED_ROWS 32
// GPIO for address seclection line. Sometimes called
// A or HA. B, C, and D will follow this sequentially.
// e.g. if GPIO_HA = 9, then GPIO_HB = 10, GPIO_HC = 11, GPIO_HD = 12
#define ADDRESS_COUNT 16 // some displays set this to 8, if you only have A, B, C
#define GPIO_HA 9
// outputs for red and green rows
#define GPIO_R1 16
#define GPIO_G1 19
#define GPIO_B1 17
#define GPIO_R2 18
#define GPIO_G2 20
#define GPIO_B2 8
// other lines
#define GPIO_CLK 13
#define GPIO_LAT 15 // Sometimes called STB
#define GPIO_OE 14
// pixels are 0x00RRGGBB but each one is shifted << 8 before
// comparing to gamma.
#define BRIGHTNESS_STEP 0x1800
#define BRIGHTNESS_GAMMA 91
#define GPIO_ALL_PINKS_MASK ( \
((ADDRESS_COUNT - 1) << GPIO_HA) | \
(1 << GPIO_R1) | \
(1 << GPIO_G1) | \
(1 << GPIO_B1) | \
(1 << GPIO_R2) | \
(1 << GPIO_G2) | \
(1 << GPIO_B2) | \
(1 << GPIO_CLK) | \
(1 << GPIO_LAT) | \
(1 << GPIO_OE))
static struct LEDMatrixFrame {
mutex_t mut;
uint8_t running; // used to stop rendering
uint32_t value; // current value for on/off comparisons
uint32_t step;
uint32_t frame_a[LED_ROWS * LED_COLUMNS];
uint32_t frame_b[LED_ROWS * LED_COLUMNS];
uint32_t* active_frame;
uint32_t* draw_frame;
} led;
static inline void set_rgb(
uint16_t value,
uint32_t pixel,
uint8_t red_pin,
uint8_t green_pin,
uint8_t blue_pin) {
// Note, I was using gpio_put_masked but switching to
// gpio_put improved FPS from 1788 to 1863.
gpio_put(blue_pin, (value <= (pixel & 0xFF)));
pixel >>= 8;
gpio_put(green_pin, (value <= (pixel & 0xFF)));
pixel >>= 8;
gpio_put(red_pin, (value <= pixel));
}
static inline void wait_ns(void) {
__asm volatile ("nop\n");
}
static inline void wait_us(void) {
sleep_us(1);
}
static void program_rows(
uint32_t address,
uint16_t value,
const uint32_t* row0,
const uint32_t* row1) {
for (int column = 0; column < LED_COLUMNS; ++column) {
const uint16_t idx = LED_COLUMNS - column - 1;
set_rgb(value, row0[idx], GPIO_R1, GPIO_G1, GPIO_B1);
set_rgb(value, row1[idx], GPIO_R2, GPIO_G2, GPIO_B2);
// strobe the clock
wait_ns();
gpio_put(GPIO_CLK, 1);
wait_ns();
gpio_put(GPIO_CLK, 0);
}
// latch the data
//wait_ns();
gpio_put(GPIO_OE, 1);
//wait_ns();
gpio_put(GPIO_LAT, 1);
wait_ns();
gpio_put(GPIO_LAT, 0);
//wait_ns();
// disable, write address, reenable
// Note: This originally used gpio_put_masked but perf
// improved from 1863FPS to 1974FPS after changing to gpio_put
gpio_put(GPIO_HA, address & 1);
address >>= 1;
gpio_put(GPIO_HA + 1, address & 1);
address >>= 1;
gpio_put(GPIO_HA + 2, address & 1);
address >>= 1;
gpio_put(GPIO_HA + 3, address & 1);
wait_us(); // a slightly longer wait is needed to avoid ghosting
gpio_put(GPIO_OE, 0);
//wait_ns();
}
void led_matrix_frame_update() {
led.value += led.step;
led.step = (led.step * BRIGHTNESS_GAMMA / 100);
if (led.value > 0xFFFF) {
led.value = BRIGHTNESS_STEP;
led.step = BRIGHTNESS_STEP;
}
for (uint16_t address = 0; address < ADDRESS_COUNT; ++address) {
// address zero, row zero is the bottom row if the panel. Thus 0 -> (LED_ROWS - 1)
const uint32_t* row0 = led.active_frame + (LED_ROWS - 1 - address) * LED_COLUMNS;
// address zero, row one is the middle of the pannel. This 0 -> (ADDRESS_COUNT - 1)
const uint32_t* row1 = led.active_frame + (ADDRESS_COUNT - 1 - address) * LED_COLUMNS;
program_rows(address, (uint16_t)led.value >> 8, row0, row1);
}
}
static void start_core1(void) {
while (1) {
mutex_enter_blocking(&led.mut);
if (!led.running) {
mutex_exit(&led.mut);
return;
}
led_matrix_frame_update();
mutex_exit(&led.mut);
}
}
void led_matrix_init() {
gpio_init_mask(GPIO_ALL_PINKS_MASK);
gpio_set_dir_out_masked(GPIO_ALL_PINKS_MASK);
gpio_put(GPIO_OE, 1); // disable by default
led.value = 0;
led.step = BRIGHTNESS_STEP;
memset(led.frame_a, 80, sizeof(led.frame_a));
memset(led.frame_b, 0, sizeof(led.frame_b));
led.active_frame = led.frame_a;
led.draw_frame = led.frame_b;
mutex_init(&led.mut);
}
void led_matrix_render(uint32_t* data) {
// Rotate date from portrait mode to the landscape mode
// that the driver needs.
//
// Also the data format is 0xIIRRGGBB and the
// driver expected 0x00RRGGBB. The logic below
// converts from the first format to the second
// one.
for (uint16_t y=0; y<LED_MATRIX_HEIGHT; ++y) {
for (uint16_t x=0; x<LED_MATRIX_WIDTH; ++x) {
const uint32_t pixel_in = data[y * LED_MATRIX_WIDTH + x];
const uint32_t br = pixel_in >> 24;
const uint32_t r = (pixel_in >> 16) & 0xFF;
const uint32_t b = (pixel_in >> 8) & 0xFF;
const uint32_t g = pixel_in & 0xFF;
const uint32_t pixel_out =
((r * br / 0xFF) << 16) |
((b * br / 0xFF) << 8) |
(g * br / 0xFF);
led.draw_frame[(LED_ROWS - x - 1) * LED_COLUMNS + (LED_COLUMNS - y - 1)] = pixel_out;
}
}
// swap the frames
mutex_enter_blocking(&led.mut);
uint32_t* tmp_frame = led.active_frame;
led.active_frame = led.draw_frame;
led.draw_frame = tmp_frame;
mutex_exit(&led.mut);
if (!led.running) {
led.running = 1;
multicore_launch_core1(start_core1);
}
}
void led_matrix_stop(void) {
mutex_enter_blocking(&led.mut);
led.running = 0;
mutex_exit(&led.mut);
sleep_ms(20);
multicore_reset_core1();
}