-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_spi_led.c
108 lines (77 loc) · 1.57 KB
/
user_spi_led.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
/*
* user.c
*
* Created on: Nov 22, 2011
* Author: cede
*/
#include <stdlib.h>
#include "delay.c"
#include "spi.c"
#define BLACK 0x80
#define WHITE 0xff
#define STATE_COLOR 0x1
#define STATE_BLACK 0x2
#define LEDS 32 * 20
u8 * pixels;
u8 state;
void writezeros(u16 n) {
while(n--)
SPI_write(0);
}
void color() {
u16 i;
for(i=0; i < LEDS; i++) {
if (i % 2 == 0) {
pixels[3*i] = WHITE;
pixels[3*i + 1] = BLACK;
pixels[3*i + 2] = BLACK;
} else {
pixels[3*i] = BLACK;
pixels[3*i + 1] = BLACK;
pixels[3*i + 2] = WHITE;
}
}
}
void black() {
memset(pixels,BLACK, LEDS * 3);
}
void setup() {
pinmode(13,OUTPUT);
/*all pixels black*/
pixels = (u8 *) malloc( LEDS * 3);
black();
state = STATE_COLOR;
SPI_init();
/*10mhz - faster is no working*/
SPI_clock(GetSystemClock() / SPI_PBCLOCK_DIV8);
SPI_mode(SPI_MASTER);
// Issue initial latch to 'wake up' strip (latch length varies w/numLEDs)
writezeros(3 * ((LEDS + 63) / 64));
}
void show() {
u8 data;
u16 i;
for(i=0; i < 3* LEDS; i++) {
data = pixels[i];
SPI_write(data);
}
// to 'latch' the data, we send just zeros
//writezeros(3*numLEDs*2);
//writezeros(4);
// 20111028 pburgess: correct latch length varies --
// three bytes per 64 LEDs.
writezeros(3 * ((LEDS + 63) / 64));
/*tune this*/
Delayms(3);
}
void loop() {
if (state == STATE_COLOR ) {
color();
state = STATE_BLACK;
} else {
black();
state = STATE_COLOR;
}
show();
//Delayms(20);
}