forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 3
/
apa102.c
69 lines (56 loc) · 1.7 KB
/
apa102.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
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "apa102.pio.h"
#define PIN_CLK 2
#define PIN_DIN 3
#define N_LEDS 150
#define SERIAL_FREQ (5 * 1000 * 1000)
// Global brightness value 0->31
#define BRIGHTNESS 16
void put_start_frame(PIO pio, uint sm) {
pio_sm_put_blocking(pio, sm, 0u);
}
void put_end_frame(PIO pio, uint sm) {
pio_sm_put_blocking(pio, sm, ~0u);
}
void put_rgb888(PIO pio, uint sm, uint8_t r, uint8_t g, uint8_t b) {
pio_sm_put_blocking(pio, sm,
0x7 << 29 | // magic
(BRIGHTNESS & 0x1f) << 24 | // global brightness parameter
(uint32_t) b << 16 |
(uint32_t) g << 8 |
(uint32_t) r << 0
);
}
#define TABLE_SIZE (1 << 8)
uint8_t wave_table[TABLE_SIZE];
int main() {
stdio_init_all();
PIO pio = pio0;
uint sm = 0;
uint offset = pio_add_program(pio, &apa102_mini_program);
apa102_mini_program_init(pio, sm, offset, SERIAL_FREQ, PIN_CLK, PIN_DIN);
for (int i = 0; i < TABLE_SIZE; ++i)
wave_table[i] = powf(sinf(i * M_PI / TABLE_SIZE), 5.f) * 255;
uint t = 0;
while (true) {
put_start_frame(pio, sm);
for (int i = 0; i < N_LEDS; ++i) {
put_rgb888(pio, sm,
wave_table[(i + t) % TABLE_SIZE],
wave_table[(2 * i + 3 * 2) % TABLE_SIZE],
wave_table[(3 * i + 4 * t) % TABLE_SIZE]
);
}
put_end_frame(pio, sm);
sleep_ms(10);
++t;
}
}