-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2812B.cpp
154 lines (140 loc) · 3.69 KB
/
ws2812B.cpp
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
#include "ws2812B.h"
#define RGB_PIO_SET GPIOB->BSRR = GPIO_BSRR_BS13
#define RGB_PIO_CLEAR GPIOB->BSRR = GPIO_BSRR_BR13
//T1H 800 ns => 57 : 43
#define WAIT_T1H; __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
//T1L 450 ns => 32 : 24
#define WAIT_T1L; __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
//T0H 400 ns => 28 : 21
#define WAIT_T0H; __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
//T0L 850 ns => 61 : 46
#define WAIT_T0L; __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
#define PLAY_ONE; RGB_PIO_SET; WAIT_T1H; RGB_PIO_CLEAR; WAIT_T1L;
#define PLAY_ZERO; RGB_PIO_SET; WAIT_T0H; RGB_PIO_CLEAR; WAIT_T0L;
ws2812B::ws2812B(PinName pio):rgbout(pio)
{
//reset
rgbout = 0;
wait_us(50);
}
void wait_corse_nop(uint32_t nb_nop)
{
for(uint32_t i=0;i<nb_nop;i++)
{
__NOP();
}
}
void wait_RESET()
{
wait_corse_nop(4);
}
void ws2812B::set(uint8_t R,uint8_t G,uint8_t B)
{
uint32_t GRB;//Order of WS2812B is G7...G0...R...B...
GRB = G;
GRB <<= 8;
GRB += R;
GRB <<= 8;
GRB += B;
__disable_irq();
//---- BYTE 3 ----
if(GRB & 0x800000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x400000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x200000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x100000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x080000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x040000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x020000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x010000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
//---- BYTE 2 ----
if(GRB & 0x8000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x4000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x2000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x1000)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x0800)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x0400)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x0200)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x0100)
{PLAY_ONE;}
else
{PLAY_ZERO;}
//---- BYTE 1 ----
if(GRB & 0x80)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x40)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x20)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x10)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x08)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x04)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x02)
{PLAY_ONE;}
else
{PLAY_ZERO;}
if(GRB & 0x01)
{PLAY_ONE;}
else
{PLAY_ZERO;}
__enable_irq();
}