-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfnordlicht.c
364 lines (280 loc) · 9.69 KB
/
fnordlicht.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* vim:fdm=marker ts=4 et ai
* {{{
* fnordlicht firmware next generation
*
* for additional information please
* see http://koeln.ccc.de/prozesse/running/fnordlicht
*
* (c) by Alexander Neumann <[email protected]>
* Lars Noschinski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
}}} */
/* includes */
#include "config.h"
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "common.h"
#include "fnordlicht.h"
#include "pwm.h"
#include "uart.h"
#include "i2c.h"
#if RC5_DECODER
#include "rc5.h"
#endif
#if STATIC_SCRIPTS
/* include static scripts */
#include "static_scripts.h"
#include "testscript.h"
#endif
/* structs */
volatile struct global_t global = {{0, 0}};
/* prototypes */
void (*jump_to_bootloader)(void) = (void *)0xc00;
static inline void init_output(void);
#if SERIAL_UART
static inline void check_serial_input(uint8_t data);
#endif
/** init output channels */
void init_output(void) { /* {{{ */
/* set all channels high -> leds off */
PORTB = _BV(PB0) | _BV(PB1) | _BV(PB2);
/* configure PB0-PB2 as outputs */
DDRB = _BV(PB0) | _BV(PB1) | _BV(PB2);
}
/* }}} */
#if SERIAL_UART
/*für das Einordnen der Befehle*/
uint8_t checkcommand(uint8_t data) {
/*Sonderbefehle (erwarten keine weiteren Werte*/
static const uint8_t extracommandlist[] = {'i','p',0};
/*Farbsetzbefehle*/
static const uint8_t colorcommandlist[] = {'r', 'g', 'b', 0};
/*weitere Farbbefehle*/
static const uint8_t otherextracommandlist[] = {'f', 0};
const uint8_t *b1 = extracommandlist;
const uint8_t *b2 = colorcommandlist;
const uint8_t *b3 = otherextracommandlist;
while(*b1){
if(*b1 == data)
return 1;
b1++;
}
while(*b2){
if(*b2 == data)
return 2;
b2++;
}
while(*b3){
if(*b3 == data)
return 3;
b3++;
}
return 0;
}
/** process serial data received by uart */
void check_serial_input(uint8_t data)
/* {{{ */ {
static uint8_t expectdata = 0;
static uint8_t command = 0; //letzter Befehl
static uint8_t currentstate = 0;
static uint8_t fade_speed = 0; //wenn größer 0 wird gefadet, anstatt die Farbe direkt zu setzten
currentstate = checkcommand(data);//Befehlszuordnung
if(!expectdata){
if (currentstate == 1) {//Sonderbefehle
switch(data) {
case 'i': jump_to_bootloader(); break;
case 'p': jump_to_bootloader(); break;
}
} else if (currentstate > 1) {//0 wird ausgelassen
/*für Befehle die noch Werte brauchen*/
command = data;
UDR0 = command;
expectdata = 1;
}
}
else {
if (checkcommand(command) == 2) {//Farbsetz-Befehle
static uint8_t color = 0;
switch(command) {
case 'r': color = 0; break;
case 'g': color = 1; break;
case 'b': color = 2; break;
}
if(fade_speed){
/* wenn zu den übergebenen farbcode gefadet werden soll */
global_pwm.channels[color].speed_h = HIGH(fade_speed);
global_pwm.channels[color].speed_l = LOW(fade_speed);
} else {
/*wenn die übergebene Farbe sofort gesetzt werden soll */
global_pwm.channels[color].brightness=data;
}
global_pwm.channels[color].target_brightness=data;
} else if (checkcommand(command) == 3) {//weitere Farbsetz-Befehle
switch(command) {
case 'f': fade_speed = data; break;
}
}
expectdata = 0;
UDR0 = 's';
}
} /* }}} */
#endif
/** main function
*/
int main(void) {
init_output();
init_pwm();
#if SERIAL_UART
init_uart();
#endif
#if RC5_DECODER
init_rc5();
#endif
#if I2C
init_i2c();
#endif
global_pwm.channels[0].brightness = 50;
global_pwm.channels[0].target_brightness = 50;
#if STATIC_SCRIPTS
init_script_threads();
#if RS485_CTRL == 0
/* start the example scripts */
script_threads[0].handler.execute = &memory_handler_flash;
script_threads[0].handler.position = (uint16_t) &colorchange_red;
script_threads[0].flags.disabled = 0;
//script_threads[1].handler.execute = &memory_handler_flash;
//script_threads[1].handler.position = (uint16_t) &testscript_flash2;
//script_threads[1].flags.disabled = 0;
//
//script_threads[2].handler.execute = &memory_handler_eeprom;
//script_threads[2].handler.position = (uint16_t) &testscript_eeprom;
//script_threads[2].flags.disabled = 0;
//script_threads[0].handler.execute = &memory_handler_flash;
//script_threads[0].handler.position = (uint16_t) &blinken;
//script_threads[0].flags.disabled = 0;
#endif
#endif
#if I2C_MASTER
i2c_global.send_messages[0].command.size = 4;
i2c_global.send_messages[0].command.code = COMMAND_SET_COLOR;
i2c_global.send_messages[0].command.set_color_parameters.colors[0] = 0x10;
i2c_global.send_messages[0].command.set_color_parameters.colors[1] = 0x10;
i2c_global.send_messages[0].command.set_color_parameters.colors[2] = 0x10;
i2c_global.send_messages_count = 1;
#endif
#if RS485_CTRL
/* init command bus */
UCSR0A = _BV(MPCM0); /* enable multi-processor communication mode */
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01); /* 9 bit frame size */
#define UART_UBRR 8 /* 115200 baud at 16mhz */
UBRR0H = HIGH(UART_UBRR);
UBRR0L = LOW(UART_UBRR);
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(UCSZ02); /* enable receiver and transmitter */
#endif
/* enable interrupts globally */
sei();
while (1) {
/* after the last pwm timeslot, rebuild the timeslot table */
if (global.flags.last_pulse) {
global.flags.last_pulse = 0;
update_pwm_timeslots();
}
/* at the beginning of each pwm cycle, call the fading engine and
* execute all script threads */
if (global.flags.new_cycle) {
global.flags.new_cycle = 0;
update_brightness();
#if STATIC_SCRIPTS
execute_script_threads();
#endif
continue;
}
#if SERIAL_UART
/* check if we received something via uart */
if (fifo_fill(&global_uart.rx_fifo) > 0) {
check_serial_input(fifo_load(&global_uart.rx_fifo));
continue;
}
#endif
#if RC5_DECODER
/* check if we received something via ir */
if (global_rc5.new_data) {
static uint8_t toggle_bit = 2;
/* if key has been pressed again */
if (global_rc5.received_command.toggle_bit != toggle_bit) {
/* if code is 0x01 (key '1' on a default remote) */
if (global_rc5.received_command.code == 0x01) {
/* install script into thread 1 */
script_threads[1].handler.execute = &memory_handler_flash;
script_threads[1].handler.position = (uint16_t) &green_flash;
script_threads[1].flags.disabled = 0;
script_threads[1].handler_stack_offset = 0;
}
/* store new toggle bit state */
toggle_bit = global_rc5.received_command.toggle_bit;
}
/* reset the new_data flag, so that new commands can be received */
global_rc5.new_data = 0;
continue;
}
#endif
#if RS485_CTRL
if (UCSR0A & _BV(RXC0)) {
uint8_t address = UCSR0B & _BV(RXB80); /* read nineth bit, zero if data, one if address */
uint8_t data = UDR0;
static uint8_t buffer[8];
static uint8_t fill = 0;
if (UCSR0A & _BV(MPCM0) || address) { /* if MPCM mode is still active, or ninth bit set, this is an address packet */
/* check if we are ment */
if (data == 0 || data == RS485_ADDRESS) {
/* remove MPCM flag and reset buffer fill counter */
UCSR0A &= ~_BV(MPCM0);
fill = 0;
continue;
} else {/* turn on MPCM */
UCSR0A |= _BV(MPCM0);
continue;
}
}
/* else this is a data packet, put data into buffer */
buffer[fill++] = data;
if (buffer[0] == 0x01) { /* soft reset */
jump_to_bootloader();
} else if (buffer[0] == 0x02 && fill == 4) { /* set color */
for (uint8_t pos = 0; pos < 3; pos++) {
global_pwm.channels[pos].target_brightness = buffer[pos + 1];
global_pwm.channels[pos].brightness = buffer[pos + 1];
}
UCSR0A |= _BV(MPCM0); /* return to MPCM mode */
} else if (buffer[0] == 0x03 && fill == 6) { /* fade to color */
for (uint8_t pos = 0; pos < 3; pos++) {
global_pwm.channels[pos].speed_h = buffer[1];
global_pwm.channels[pos].speed_l = buffer[2];
global_pwm.channels[pos].target_brightness = buffer[pos + 3];
}
UCSR0A |= _BV(MPCM0); /* return to MPCM mode */
}
}
#endif
#if I2C_MASTER
i2c_master_check_queue();
#endif
}
}