-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpiuio_ws2812.c
60 lines (47 loc) · 1.34 KB
/
piuio_ws2812.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
#include "piuio_config.h"
#ifdef ENABLE_WS2812_SUPPORT
#include "piuio_ws2812.h"
#include "piuio_ws2812_helpers.h"
#include "ws2812.pio.h"
#include "pico/multicore.h"
mutex_t mutex;
semaphore_t sem;
static struct lampArray* lamp;
void ws2812_update() {
// Write lamp.data to WS2812Bs
put_pixel(lamp->l1_halo ? ws2812_color[0] : urgb_u32(0, 0, 0));
put_pixel(lamp->l2_halo ? ws2812_color[1] : urgb_u32(0, 0, 0));
for (int i = 0; i < 4; i++)
put_pixel(lamp->bass_light ? ws2812_color[2] : urgb_u32(0, 0, 0));
put_pixel(lamp->r2_halo ? ws2812_color[3] : urgb_u32(0, 0, 0));
put_pixel(lamp->r1_halo ? ws2812_color[4] : urgb_u32(0, 0, 0));
}
void ws2812_core1() {
while (true) {
ws2812_lock_mtx();
ws2812_update();
ws2812_unlock_mtx();
sleep_ms(5);
}
}
void ws2812_lock_mtx() {
uint32_t owner = 0;
sem_release(&sem);
if (!mutex_try_enter(&mutex, &owner)) {
mutex_enter_blocking(&mutex);
}
}
void ws2812_unlock_mtx() {
mutex_exit(&mutex);
}
void ws2812_init(struct lampArray* l) {
mutex_init(&mutex);
sem_init(&sem, 0, 2);
lamp = l;
PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &ws2812_program);
ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, WS2812_IS_RGBW);
multicore_launch_core1(ws2812_core1);
}
#endif