-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws2812_demo.c
233 lines (197 loc) · 6.32 KB
/
ws2812_demo.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
#include <stdint.h>
#include "stm8s.h"
#include "stm8_init.h"
#include "ws2812b.h"
#define LED_COUNT 32
volatile uint8_t ledbuffer[LED_COUNT * 3];
int32_t abs( int32_t x ) {
return x < 0 ? -x: x;
}
int8_t abs8( int8_t x ) {
return x < 0 ? -x: x;
}
void set_hsv( uint8_t* leds, uint8_t index, unsigned char hue, unsigned char sat, unsigned char val ) {
int32_t h = 6LL * (int32_t)hue;
int32_t c = (int32_t)val * (int32_t)sat;
int32_t x = c * ( 255LL - abs( h % ( 2LL << 8LL ) - 255LL ) );
c >>= 8;
h >>= 8;
x >>= 16;
int32_t m = val - c;
uint8_t r, g, b;
switch ( h ) {
case 6:
case 0: r = c + m; g = x + m; b = m; break;
case 1: r = x + m; g = c + m; b = m; break;
case 2: r = m; g = c + m; b = x + m; break;
case 3: r = m; g = x + m; b = c + m; break;
case 4: r = x + m; g = m; b = c + m; break;
case 5: r = c + m; g = m; b = x + m; break;
}
leds[ 3 * index + 0 ] = g;
leds[ 3 * index + 1 ] = r;
leds[ 3 * index + 2 ] = b;
}
void set_rgb( uint8_t* leds, uint8_t index, uint8_t r, uint8_t g, uint8_t b ) {
leds[ 3 * index + 0 ] = g;
leds[ 3 * index + 1 ] = r;
leds[ 3 * index + 2 ] = b;
}
void rainbow( unsigned int progress, uint8_t brightness ) {
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
set_hsv( ledbuffer, i, progress + 2 * i, 255, brightness );
}
}
void red( unsigned int progress, uint8_t brightness ) {
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
uint8_t br = ((((progress>>3)&1)^(i&1)) ? 0.2 : 1) * brightness;
set_rgb( ledbuffer, i, br, 0, 0 );
}
}
void green( unsigned int progress, uint8_t brightness ) {
progress = progress;
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
uint8_t br = ((((progress>>3)&1)^(i&1)) ? 0.2 : 1) * brightness;
set_rgb( ledbuffer, i, 0, br, 0 );
}
}
void blue( unsigned int progress, uint8_t brightness ) {
progress = progress;
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
uint8_t br = ((((progress>>3)&1)^(i&1)) ? 0.2 : 1) * brightness;
set_rgb( ledbuffer, i, 0, 0, br );
}
}
void white( unsigned int progress, uint8_t brightness ) {
progress = progress;
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
set_rgb( ledbuffer, i, brightness, brightness, brightness );
}
}
void black( unsigned int progress, uint8_t brightness ) {
(void)brightness;
progress = progress;
for ( uint8_t i = 0; i != LED_COUNT; i++ ) {
set_rgb( ledbuffer, i, 0, 0, 0 );
}
}
void (*modes[]) ( unsigned int, uint8_t ) = {
red,
green,
blue,
};
uint16_t times[3]; // change when changing modes
void EEWriteU8(uint16_t address, uint8_t value)
{
// Check if the EEPROM is write-protected. If it is then unlock the EEPROM.
if ((FLASH_IAPSR & FLASH_IAPSR_DUL) == 0) {
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
}
// Write the data to the EEPROM.
(*(uint8_t *) (0x4000 + address)) = value;
// Now write protect the EEPROM.
FLASH_IAPSR &= ~FLASH_IAPSR_DUL;
}
void EEReadArray(uint16_t address, uint8_t * dest, uint8_t count)
{
uint8_t i = 0;
// Write the data to the EEPROM.
uint8_t *EEAddress = (uint8_t *) (0x4000 + address); // EEPROM base address.
for (; i < count; i++) {
dest[i] = *EEAddress;
EEAddress++;
}
}
void EEWriteU16(uint16_t address, uint16_t value) {
EEWriteU8(address, value >> 8);
EEWriteU8(address+1, value & 0xFF);
}
uint16_t EEReadU16(uint16_t address) {
uint8_t *EEAddress = (uint8_t *) (0x4000 + address); // EEPROM base address.
return (*EEAddress << 8) + *(EEAddress+1);
}
#define EEPROM_TIME_START 0
#define EEPROM_TIME_STEP 16
int modeCount = sizeof(modes) / sizeof(modes[0]);
uint8_t bright[] = {
8, 11, 17, 26, 38, 56, 84, 124, 184, 255, 0
};
const int brightCount = sizeof(bright) / sizeof(bright[0]);
int main(void)
{
sysclock_init(0);
ws_init();
ws_reset();
ws_clrarray(&ledbuffer[0], LED_COUNT);
PA_CR1 |= (1 << PA1) | (1 << PA2);
delay_ms( 10 );
if ( !(PA_IDR & (1 << PA1)) && !(PA_IDR & (1 << PA2)) ) {
// reset times in EEPROM
for(uint8_t i = 0; i < modeCount; ++i)
EEWriteU16(EEPROM_TIME_START + i*EEPROM_TIME_STEP, 0);
for (uint8_t i = 0; i < 4; i++) {
white( 0, 11 );
ws_showarray(ledbuffer, LED_COUNT);
delay_ms( 300 );
black( 0, 11 );
ws_showarray(ledbuffer, LED_COUNT);
delay_ms( 300 );
}
}
// initialize times
for(uint8_t i = 0; i < modeCount; ++i)
times[i] = EEReadU16(EEPROM_TIME_START + i*EEPROM_TIME_STEP);
unsigned int progress = 0;
uint8_t brightness = 0;
// Set brightness
while( (PA_IDR & (1 << PA1) ) ) {
if ( !(PA_IDR & (1 << PA2) ) ) {
brightness++;
if ( brightness == brightCount )
brightness = 0;
delay_ms( 300 );
}
progress++;
ws_showarray(ledbuffer, LED_COUNT);
red( progress, bright[brightness] );
ws_showarray(ledbuffer, LED_COUNT);
delay_ms( 10 );
}
delay_ms( 300 );
// Brightness fixed, Wait for first group
while( (PA_IDR & (1 << PA1)) && (PA_IDR & (1 << PA2)) ) {
progress++;
rainbow( progress, bright[brightness >= 2 ? brightness-2 : 0] );
ws_showarray(ledbuffer, LED_COUNT);
delay_ms( 10 );
}
delay_ms( 300 );
uint8_t mode = 0; // red
uint8_t sec_counter = 0;
uint8_t failsave_counter = 0;
// Run
while ( 1 ) {
if ( !(PA_IDR & (1 << PA1) ) || !(PA_IDR & (1 << PA2) ) ) {
EEWriteU16(EEPROM_TIME_START + mode*EEPROM_TIME_STEP, times[mode]);
mode++;
if ( mode == modeCount )
mode = 0;
delay_ms( 300 );
}
progress++;
modes[ mode ]( progress, bright[brightness] );
ws_showarray(ledbuffer, LED_COUNT);
delay_ms( 10 );
sec_counter++;
if (sec_counter >= 63) { // 1 second
sec_counter = 0;
times[mode]++;
failsave_counter++;
if (failsave_counter == 10) { // write data each 10 s
failsave_counter = 0;
EEWriteU16(EEPROM_TIME_START + mode*EEPROM_TIME_STEP, times[mode]);
}
}
}
}