forked from penfold42/stuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWS2801_to_2812_josh.ino
executable file
·337 lines (285 loc) · 10.8 KB
/
WS2801_to_2812_josh.ino
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Take spi data intended for ws2801, buffer it, spit out ws2812 protocol
speed is important if you are using hyperion so the SPI data is sent out
using the code at http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
This is on PORTD.4/arduino pint 4
Also uses the UART to control a second string of LEDs (max 4 at the moment)
This just uses the adafruit neopixel library
This is on arduino pin 9
*/
#define ENABLE_UART
#define BAUDRATE 9600 // Serial port speed,
//#define BAUDRATE 115200 // Serial port speed,
#define POLLED_MODE // no interrupts, poll everying
// lots of arduino core stuff will fail tho
//#undef POLLED_MODE // use interrupts
#include "Adafruit_NeoPixel.h"
#define ADA_DATAPIN 9 // Datapin
#define ADA_LEDCOUNT 4 // Number of LEDs used
#define ADA_BRIGHTNESS 50
Adafruit_NeoPixel strip = Adafruit_NeoPixel(ADA_LEDCOUNT, ADA_DATAPIN, NEO_GRB + NEO_KHZ800);
// DANGER! if you change any of the IO pins, you must update this too!
byte spare_pins[] = {0,1,2,3,4,5,6,7,8,9,A0,A1,A2,A3,A4,A5};
char ada_buffer[ADA_LEDCOUNT*3+5]; // "LED" + RGB for each LED + "\n"
byte ada_ptr;
byte update_ada;
#include <SPI.h>
#define MAX_PIXELS 300 // Number of pixels in the string
#define Josh2812_PIXEL_PORT PORTD // Port of the pin the pixels are connected to
#define Josh2812_PIXEL_DDR DDRD // Port of the pin the pixels are connected to
#define Josh2812_PIXEL_BIT 4 // Bit of the pin the pixels are connected to
#define T1H 900 // Width of a 1 bit in ns
#define T1L 600 // Width of a 1 bit in ns
#define T0H 400 // Width of a 0 bit in ns
#define T0L 900 // Width of a 0 bit in ns
#define RES 6000 // Width of the low gap between bits to cause a frame to latch
// Here are some convience defines for using nanoseconds specs to generate actual CPU delays
#define NS_PER_SEC (1000000000L) // Note that this has to be SIGNED since we want to be able to check for negative values of derivatives
#define CYCLES_PER_SEC (F_CPU)
#define NS_PER_CYCLE ( NS_PER_SEC / CYCLES_PER_SEC )
#define NS_TO_CYCLES(n) ( (n) / NS_PER_CYCLE )
#define bufsz 3*MAX_PIXELS
char buf [bufsz];
int pos;
void setup (void)
{
for (int i=0; i<sizeof(spare_pins); i++) {
pinMode(spare_pins[i], INPUT_PULLUP);
}
Serial.begin (BAUDRATE); // debugging
Serial.println ("\n---- started ----");
Serial.print ("FILE: ");
Serial.println (__FILE__);
Serial.print ("BUILT: ");
Serial.print (__DATE__);
Serial.print (" ");
Serial.println (__TIME__);
#ifdef POLLED_MODE
Serial.println ("Polled mode active");
#else
Serial.println ("Int mode active");
#endif
delay(100);
setup_Adafruit();
setup_SPI();
Josh2812_ledsetup();
colourfade_strip();
// WARNING!!! interrupts are DISABLED in polled mode
// this is needed to run at 2 Mbit/sec
#ifdef POLLED_MODE
cli();
#endif
}
unsigned int temp_loopctr = 0;
unsigned int loopctr = 0;
byte c;
byte t;
void loop (void) {
while (!(SPSR & (1<<SPIF))) {
if (loopctr++ > 250) { // 250
#ifdef ENABLE_UART
#ifdef POLLED_MODE
if (UCSR0A & (1<<RXC0)) handle_uart();
#else // interrupts are available
if (Serial.available()>0) handle_uart();
#endif
#endif //ENABLE_UART
loopctr = 0;
if (pos >= 1) { // have we had at least 1 byte?
if (pos%3 == 0) { // check for divisibility by 3, if not we know its corrupt
#ifndef POLLED_MODE
cli(); // disable interrupts while we blat out the string
#endif
for (int i=0; i<pos; i++) {
Josh2812_sendByte(buf[i]);
}
#ifndef POLLED_MODE
sei(); // and re-enable them
#endif
}
Josh2812_show();
pos=0;
}
}
#ifdef ENABLE_UART
if (update_ada) {
update_ada = 0;
strip.show();
#ifdef POLLED_MODE
cli(); // strip.show always re-enabled interrupts
#endif
}
#endif // ENABLE_UART
}
buf[pos++] = SPDR;
if (pos>=bufsz) { pos=bufsz-1; }
loopctr = 0;
}
void handle_uart() {
#ifdef POLLED_MODE
c=UDR0;
UDR0 = c;
#else // interrupts are available
c=Serial.read();
Serial.write(':');
Serial.println(c,HEX);
#endif
ada_buffer[ada_ptr++] = c;
if (ada_ptr >= sizeof ada_buffer) {
ada_ptr--;
}
if (c=='\n') {
ada_ptr = 0;
if (ada_buffer[8] == '\n') {
ada_buffer[8] = 0;
strip.setPixelColor(
16*ascii2hex(ada_buffer[0])+ascii2hex(ada_buffer[1]),
16*ascii2hex(ada_buffer[2])+ascii2hex(ada_buffer[3]),
16*ascii2hex(ada_buffer[4])+ascii2hex(ada_buffer[5]),
16*ascii2hex(ada_buffer[6])+ascii2hex(ada_buffer[7])
);
update_ada = 1;
}
if (ada_buffer[4] == '\n') {
ada_buffer[4] = 0;
strip.setPixelColor(
16*ascii2hex(ada_buffer[0]),
16*ascii2hex(ada_buffer[1]),
16*ascii2hex(ada_buffer[2]),
16*ascii2hex(ada_buffer[3])
);
update_ada = 1;
}
}
}
void Josh2812_sendBit( bool bitVal ) {
if ( bitVal ) { // 0 bit
asm volatile (
"sbi %[port], %[bit] \n\t" // Set the output bit
".rept %[onCycles] \n\t" // Execute NOPs to delay exactly the specified number of cycles
"nop \n\t"
".endr \n\t"
"cbi %[port], %[bit] \n\t" // Clear the output bit
".rept %[offCycles] \n\t" // Execute NOPs to delay exactly the specified number of cycles
"nop \n\t"
".endr \n\t"
::
[port] "I" (_SFR_IO_ADDR(Josh2812_PIXEL_PORT)),
[bit] "I" (Josh2812_PIXEL_BIT),
[onCycles] "I" (NS_TO_CYCLES(T1H) - 2), // 1-bit width less overhead for the actual bit setting, note that this delay could be longer and everything would still work
[offCycles] "I" (NS_TO_CYCLES(T1L) - 2) // Minimum interbit delay. Note that we probably don't need this at all since the loop overhead will be enough, but here for correctness
);
} else { // 1 bit
// **************************************************************************
// This line is really the only tight goldilocks timing in the whole program!
// **************************************************************************
asm volatile (
"sbi %[port], %[bit] \n\t" // Set the output bit
".rept %[onCycles] \n\t" // Now timing actually matters. The 0-bit must be long enough to be detected but not too long or it will be a 1-bit
"nop \n\t" // Execute NOPs to delay exactly the specified number of cycles
".endr \n\t"
"cbi %[port], %[bit] \n\t" // Clear the output bit
".rept %[offCycles] \n\t" // Execute NOPs to delay exactly the specified number of cycles
"nop \n\t"
".endr \n\t"
::
[port] "I" (_SFR_IO_ADDR(Josh2812_PIXEL_PORT)),
[bit] "I" (Josh2812_PIXEL_BIT),
[onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
[offCycles] "I" (NS_TO_CYCLES(T0L) - 2)
);
}
// Note that the inter-bit gap can be as long as you want as long as it doesn't exceed the 5us reset timeout (which is A long time)
// Here I have been generous and not tried to squeeze the gap tight but instead erred on the side of lots of extra time.
// This has thenice side effect of avoid glitches on very long strings becuase
}
void Josh2812_sendByte( unsigned char byte ) {
for( unsigned char bit = 0 ; bit < 8 ; bit++ ) {
Josh2812_sendBit( bitRead( byte , 7 ) ); // Neopixel wants bit in highest-to-lowest order
// so send highest bit (bit #7 in an 8-bit byte since they start at 0)
byte <<= 1; // and then shift left so bit 6 moves into 7, 5 moves into 6, etc
}
}
/*
The following three functions are the public API:
ledSetup() - set up the pin that is connected to the string. Call once at the begining of the program.
sendPixel( r g , b ) - send a single pixel to the string. Call this once for each pixel in a frame.
show() - show the recently sent pixel on the LEDs . Call once per frame.
*/
void Josh2812_ledsetup() {
bitSet( Josh2812_PIXEL_DDR , Josh2812_PIXEL_BIT );
cli();
for (int i=0; i< 2000; i++) Josh2812_sendPixel( 0,0,0 );
sei();
}
void Josh2812_sendPixel( unsigned char r, unsigned char g , unsigned char b ) {
Josh2812_sendByte(g); // Neopixel wants colors in green then red then blue order
Josh2812_sendByte(r);
Josh2812_sendByte(b);
}
// Just wait long enough without sending any bits to cause the pixels to latch and display the last sent frame
void Josh2812_show() {
_delay_us( (RES / 1000UL) + 1); // Round up since the delay must be _at_least_ this long (too short might not work, too long not a problem)
}
void setup_SPI() {
SPI.setClockDivider(SPI_CLOCK_DIV8); // irrelevant really, we go slave mode
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
// turn on SPI in slave mode
SPCR |= bit (SPE);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
}
void colourfade_strip() {
#define SLOW 4
#define FAST 32
int r=0; int g=0; int b=0;
int step = SLOW;
int junk = SPSR;
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (r=0; r<=255; r+=step) fill( r,0,0,0 ); // black to red
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (g=0; g<=255; g+=step) fill( 255,g,0,0 ); // red to yellow
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (r=255; r>=0; r-=step) fill( r,255,0,0 ); // yellow to green
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (b=0; b<=255; b+=step) fill( 0,255,b,0 ); // green to cyan
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (g=255; g>=0; g-=step) fill( 0,g,255,0 ); // cyan to blue
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (r=0; r<=255; r+=step) fill( r,0,255,0 ); // blue to purple
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (g=0; g<=255; g+=step) fill( 255,g,255,0 ); // purple to white
if ((SPSR & (1<<SPIF))) { step = FAST; }
for (g=255; g>=0; g-=(step/2)) fill( g,g,g,0 ); // white to black
if ((SPSR & (1<<SPIF))) { step = FAST; }
fill( 0,0,0,0 ); // force black
}
void setup_Adafruit() {
strip.begin(); // Init LED strand, set all black, then all to startcolor
strip.setBrightness( (255 / 100) * ADA_BRIGHTNESS );
}
void fill (byte _r, byte _g, byte _b, int _pause) {
cli();
for (int i=0; i< 200; i++) Josh2812_sendPixel( _r,_g,_b );
sei();
for ( int i=0; i < ADA_LEDCOUNT; i++ ) { // For each LED
strip.setPixelColor( i, _r,_g,_b ); // .. set the color
}
strip.show();
delay(_pause);
}
byte ascii2hex(char asc) {
if ((asc >= '0') && (asc <= '9')) {
return asc-'0';
}
else if ((asc >= 'a') && (asc <= 'f')) {
return 10+asc-'a';
}
else if ((asc >= 'A') && (asc <= 'F')) {
return 10+asc-'A';
}
else {
return 0;
}
}