forked from Expatria-Technologies/grblhal-rgb-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2812.c
249 lines (202 loc) · 8.05 KB
/
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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright (C) Sienci Labs Inc.
This file is part of the SuperLongBoard family of products.
This source describes Open Hardware and is licensed under the "CERN-OHL-S v2"
You may redistribute and modify this source and make products using
it under the terms of the CERN-OHL-S v2 (https://ohwr.org/cern_ohl_s_v2.t).
This source is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS FOR A
PARTICULAR PURPOSE. Please see the CERN-OHL-S v2 for applicable conditions.
As per CERN-OHL-S v2 section 4, should You produce hardware based on this
source, You must maintain the Source Location clearly visible on the external
case of the CNC Controller or other product you make using this source.
You should have received a copy of the CERN-OHL-S v2 license with this source.
If not, see <https://ohwr.org/project/cernohl/wikis/Documents/CERN-OHL-version-2>.
Contact for information regarding this program and its license
can be sent through [email protected] or mailed to the main office
of Sienci Labs Inc. in Waterloo, Ontario, Canada.
*/
/*
#include "WS2812.h"
#include "PixelArray.h"
#define WS2812_BUF 150
#define NUM_COLORS 6
#define NUM_LEDS_PER_COLOR 10
PixelArray px(WS2812_BUF);
// See the program page for information on the timing numbers
// The given numbers are for the K64F
WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
int main()
{
ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
// set up the colours we want to draw with
int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
// for each of the colours (j) write out 10 of them
// the pixels are written at the colour*10, plus the colour position
// all modulus 60 so it wraps around
for (int i = 0; i < WS2812_BUF; i++) {
px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
}
// now all the colours are computed, add a fade effect using intensity scaling
// compute and write the II value for each pixel
for (int j=0; j<WS2812_BUF; j++) {
// px.SetI(pixel position, II value)
px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
}
// Now the buffer is written, rotate it
// by writing it out with an increasing offset
while (1) {
for (int z=WS2812_BUF; z >= 0 ; z--) {
ws.write_offsets(px.getBuf(),z,z,z);
wait(0.075);
}
}
}
*/
#include <stdint.h>
#include "driver.h"
#include "grbl/hal.h"
#include "ws2812.h"
#define FRAME_SIZE 24
#define OFF 0
#define GLOBAL 1
#define PER_PIXEL 2
void WS2812_setDelays(WS2812* ws2812, int zeroHigh, int zeroLow, int oneHigh, int oneLow, int latch) {
ws2812->zeroHigh = zeroHigh;
ws2812->zeroLow = zeroLow;
ws2812->oneHigh = oneHigh;
ws2812->oneLow = oneLow;
ws2812->latch = latch;
}
void WS2812_loadBuf(WS2812* ws2812, int* buf, int r_offset, int g_offset, int b_offset) {
for (int i = 0; i < ws2812->size; i++) {
int color = 0;
color |= ((buf[(i + g_offset) % ws2812->size] & 0x0000FF00));
color |= ((buf[(i + r_offset) % ws2812->size] & 0x00FF0000));
color |= (buf[(i + b_offset) % ws2812->size] & 0x000000FF);
color |= (buf[i] & 0xFF000000);
unsigned char agrb[4] = {0x0, 0x0, 0x0, 0x0};
unsigned char sf;
agrb[0] = (color & 0x0000FF00) >> 8;
agrb[1] = (color & 0x00FF0000) >> 16;
agrb[2] = color & 0x000000FF;
agrb[3] = (color & 0xFF000000) >> 24;
if (ws2812->use_II == GLOBAL) {
sf = ws2812->II;
} else if (ws2812->use_II == PER_PIXEL) {
sf = agrb[3];
} else {
sf = 0xFF;
}
for (int clr = 0; clr < 3; clr++) {
agrb[clr] = ((agrb[clr] * sf) >> 8);
for (int j = 0; j < 8; j++) {
if (((agrb[clr] << j) & 0x80) == 0x80) {
ws2812->transmitBuf[(i * FRAME_SIZE) + (clr * 8) + j] = 1;
} else {
ws2812->transmitBuf[(i * FRAME_SIZE) + (clr * 8) + j] = 0;
}
}
}
}
}
void WS2812_write(WS2812* ws2812, int* buf) {
WS2812_write_offsets(ws2812, buf, 0, 0, 0);
}
void WS2812_write_offsets(WS2812* ws2812, int* buf, int r_offset, int g_offset, int b_offset) {
int i, j, k;
WS2812_loadBuf(ws2812, buf, r_offset, g_offset, b_offset);
// Entering timing critical section, so disabling interrupts
// Assuming __disable_irq() and __enable_irq() are custom functions
// that disable and enable interrupts respectively
//__disable_irq();
for (i = 0; i < ws2812->size; i++) {
for (k = 0; k<FRAME_SIZE; k++){
if ((ws2812->transmitBuf[i]>>k)&0x01) {
//need to set the output high
//*(ws2812->gpo) = 1;
hal.port.digital_out(ws2812->gpo, true);
for (j = 0; j < ws2812->oneHigh; j++) {
__ASM volatile ("nop");
}
//need to set the output low
//*(ws2812->gpo) = 0;
hal.port.digital_out(ws2812->gpo, false);
for (j = 0; j < ws2812->oneLow; j++) {
__ASM volatile ("nop");
}
} else {
//need to set the output high
//*(ws2812->gpo) = 1;
hal.port.digital_out(ws2812->gpo, true);
for (j = 0; j < ws2812->zeroHigh; j++) {
__ASM volatile ("nop");
}
//need to set the output low
//*(ws2812->gpo) = 0;
hal.port.digital_out(ws2812->gpo, false);
for (j = 0; j < ws2812->zeroLow; j++) {
__ASM volatile ("nop");
}
}
}
}
// Exiting timing critical section, so enabling interrupts
//__enable_irq();
for (j = 0; j < ws2812->latch; j++) {
__ASM volatile ("nop");
}
}
void WS2812_write_simple(WS2812* ws2812, int color) {
int i, j, k;
// Entering timing critical section, so disabling interrupts
// Assuming __disable_irq() and __enable_irq() are custom functions
// that disable and enable interrupts respectively
__disable_irq();
for (i = 0; i < ws2812->size; i++) {
for (k = FRAME_SIZE-1; k>=0; k--){
if ((color>>k)&0x01) {
//need to set the output high
//*(ws2812->gpo) = 1;
hal.port.digital_out(ws2812->gpo, true);
for (j = 0; j < ws2812->oneHigh; j++) {
__ASM volatile ("nop");
}
//need to set the output low
//*(ws2812->gpo) = 0;
hal.port.digital_out(ws2812->gpo, false);
for (j = 0; j < ws2812->oneLow; j++) {
__ASM volatile ("nop");
}
} else {
//need to set the output high
//*(ws2812->gpo) = 1;
hal.port.digital_out(ws2812->gpo, true);
//for (j = 0; j < ws2812->zeroHigh; j++) {
// __ASM volatile ("nop");
//}
//need to set the output low
//*(ws2812->gpo) = 0;
hal.port.digital_out(ws2812->gpo, false);
for (j = 0; j < ws2812->zeroLow; j++) {
__ASM volatile ("nop");
}
}
}
}
// Exiting timing critical section, so enabling interrupts
__enable_irq();
for (j = 0; j < ws2812->latch; j++) {
__ASM volatile ("nop");
}
}
void WS2812_useII(WS2812* ws2812, int bc) {
if (bc > OFF) {
ws2812->use_II = bc;
} else {
ws2812->use_II = OFF;
}
}
void WS2812_setII(WS2812* ws2812, uint8_t II) {
ws2812->II = II;
}