forked from schneider42/moodlamp-rf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfnordlicht.c
238 lines (212 loc) · 5.54 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
/* vim:fdm=marker ts=4 et ai
* {{{
* moodlamp-rf - fnordlicht firmware next generation
*
* for additional information please
* see http://blinkenlichts.net/
* and http://koeln.ccc.de/prozesse/running/fnordlicht
*
* This is a modified version of the fnordlicht
* (c) by Alexander Neumann <[email protected]>
* Lars Noschinski <[email protected]>
*
* Modifications done by:
* Kiu
* Mazzoo
* Tobias Schneider([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 <stdlib.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include <avr/wdt.h>
#include "common.h"
#include "fnordlicht.h"
#include "pwm.h"
#include "lib/uart.h"
#include "rs485_handler.h"
#include "raw.h"
#include "cmd_handler.h"
#include "control.h"
#if RC5_DECODER
#include "rc5.h"
#endif
#if STATIC_SCRIPTS
#include "static_scripts.h"
#endif
#include "settings.h"
#include "control.h"
#include "packet.h"
#include "pinutils.h"
#include "adc.h"
#include "scripts.h"
#include "leds.h"
#if SERIAL_UART
int uart_putc_file(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putc_file, NULL, _FDEV_SETUP_WRITE);
#endif
/* structs */
volatile struct global_t global = {{0, 0}};
/* prototypes */
uint16_t main_reset = 0;
#if SERIAL_UART
int uart_putc_file(char c, FILE *stream)
{
uart1_putc(c);
return 0;
}
#endif
void jump_to_bootloader(void)
{
//uart1_puts("acDRab");
//cli();
//wdt_enable(WDTO_30MS);
while(1);
}
unsigned int random_seed __attribute__ ((section (".noinit")));
/** main function
*/
int main(void) {
// SPCR &= ~(1<<SPE);
// TIMSK0 &= ~(1<<TOIE1);
wdt_disable();
/* Clear WDRF in MCUSR */
MCUSR &= ~(1<<WDRF);
/* Write logical one to WDCE and WDE */
/* Keep old prescaler setting to prevent unintentional time-out */
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* Turn off WDT */
WDTCSR = 0x00;
//volatile long l;
// for(l=0;l<1000000;l++);
DDR_CONFIG_IN(CONFIG1);
PIN_SET(CONFIG1);
wdt_enable(WDTO_2S);
wdt_reset();
volatile unsigned long l;
for(l=0;l<10;l++);
if( !PIN_HIGH(CONFIG1) ){
global.config = 30;
}else{
global.config = 21;
}
leds_init();
wdt_reset();
if( global.config == 21 ){
DDR_CONFIG_IN(JUMPER1C1);
PIN_SET(JUMPER1C1);
}else if( global.config == 30 ){
DDR_CONFIG_IN(JUMPER1C2);
PIN_SET(JUMPER1C2);
adc_init();
uint16_t bat = adc_getChannel(6);
if( bat < ADC_MINBATIDLE ){
//global.flags.lowbat = 1;
}
}
wdt_reset();
init_pwm();
#if SERIAL_UART
uart1_init( UART_BAUD_SELECT(UART_BAUDRATE,F_CPU));
stdout = &mystdout;
#endif
#if RC5_DECODER
rc5_init();
#endif
wdt_reset();
#if STATIC_SCRIPTS
init_script_threads();
#endif
settings_read();
//if((global.config == 21 && !PIN_HIGH(JUMPER1C1)) || (global.config== 30 && !PIN_HIGH(JUMPER1C2)))
// interfaces_setEnabled(IFACE_RF,0);
control_init();
wdt_reset();
#if RS485_CTRL
rs485_init();
zbus_core_init();
#endif
//rf_init();
packet_init(idbuf[0],0);
wdt_reset();
srandom(random_seed);
random_seed = random();
/* enable interrupts globally */
sei();
// global.state = STATE_RUNNING;
// global.state = STATE_PAUSE;
// global.flags.running = 0;
while (1) {
wdt_reset();
//leds_main();
#if 1
packet_tick();
#else
if(packetbase ){//> 32){
packetbase = 0;
packet_tick();
//if(main_reset++ > 16000)
// jump_to_bootloader();
//uint16_t bat = adc_getChannel(6);
/*if( bat < ADC_MINBATIDLE ){
global.flags.lowbat = 1;
}*/
}
#endif
//if( global.flags.lowbat ){
// control_lowbat();
//}
if( global.flags.timebase ){
//control_tick();
global.flags.timebase=0;
}
/* after the last pwm timeslot, rebuild the timeslot table */
//if (global.flags.last_pulse) {
// global.flags.last_pulse = 0;
//if(global.flags.running)
//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;
if(control_faderunning)
update_brightness();
if(global.flags.running){
#if STATIC_SCRIPTS
// execute_script_threads();
#endif
}
//continue;
}
#if RC5_DECODER
#endif
#if RS485_CTRL
rs485_process();
#endif
#if SERIAL_UART
//serial_process();
#endif
}
}