-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneopixel.c
More file actions
92 lines (76 loc) · 2.15 KB
/
neopixel.c
File metadata and controls
92 lines (76 loc) · 2.15 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
#ifndef __NEOPIXEL_INC
#define __NEOPIXEL_INC
#include <stdlib.h>
#include "ws2818b.pio.h"
// Definição de pixel GRB
struct pixel_t {
uint8_t G, R, B; // Três valores de 8-bits compõem um pixel.
};
typedef struct pixel_t pixel_t;
typedef pixel_t npLED_t; // Mudança de nome de "struct pixel_t" para "npLED_t" por clareza.
// Declaração do buffer de pixels que formam a matriz.
static npLED_t *leds;
static uint led_count;
// Variáveis para uso da máquina PIO.
static PIO np_pio;
static uint np_sm;
/**
* Inicializa a máquina PIO para controle da matriz de LEDs.
*/
void npInit(uint pin, uint amount) {
led_count = amount;
leds = (npLED_t *)calloc(led_count, sizeof(npLED_t));
// Cria programa PIO.
uint offset = pio_add_program(pio0, &ws2818b_program);
np_pio = pio0;
// Toma posse de uma máquina PIO.
np_sm = pio_claim_unused_sm(np_pio, false);
if (np_sm < 0) {
np_pio = pio1;
np_sm = pio_claim_unused_sm(np_pio, true); // Se nenhuma máquina estiver livre, panic!
}
// Inicia programa na máquina PIO obtida.
ws2818b_program_init(np_pio, np_sm, offset, pin, 800000.f);
// Limpa buffer de pixels.
for (uint i = 0; i < led_count; ++i) {
leds[i].R = 0;
leds[i].G = 0;
leds[i].B = 0;
}
}
/**
* Atribui uma cor RGB a um LED.
*/
void npSetLED(const uint index, const uint8_t r, const uint8_t g, const uint8_t b) {
leds[index].R = r;
leds[index].G = g;
leds[index].B = b;
}
/**
* Atribui uma cor RGB a um LED específico no buffer de LEDs.
*/
void npSetPixelColor(uint index, uint8_t r, uint8_t g, uint8_t b) {
leds[index].R = r;
leds[index].G = g;
leds[index].B = b;
}
/**
* Limpa o buffer de pixels.
*/
void npClear() {
for (uint i = 0; i < led_count; ++i)
npSetLED(i, 0, 0, 0);
}
/**
* Escreve os dados do buffer nos LEDs.
*/
void npWrite() {
// Escreve cada dado de 8-bits dos pixels em sequência no buffer da máquina PIO.
for (uint i = 0; i < led_count; ++i) {
pio_sm_put_blocking(np_pio, np_sm, leds[i].G);
pio_sm_put_blocking(np_pio, np_sm, leds[i].R);
pio_sm_put_blocking(np_pio, np_sm, leds[i].B);
}
sleep_us(100); // Espera 100us, sinal de RESET do datasheet.
}
#endif