Single lamps can be controlled. r0ketbeam is working. Input support is
[cerebrum.git] / cerebrum_firmware.c
blob8145842004445fec66ddf1b38225e61aadccc5d8
1 /*
2 Copyright (C) 2012 jaseg <s@jaseg.de>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 3 as published by the Free Software Foundation.
7 */
9 #include "config.h"
10 #include <avr/io.h>
11 #include <util/delay.h>
12 #include <avr/pgmspace.h>
13 #include <avr/interrupt.h>
14 #include "uart.h"
15 #include "util.h"
16 #include "r0ketbeam.h"
17 #include "7seg.h"
18 #include "led.h"
19 #include "pwm.h"
20 #include "input.h"
22 void setup(void);
23 void loop(void);
25 int main(void){
26 setup();
27 for(;;) loop();
30 void setup(){
31 uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(115200, F_CPU));
32 pwm_setup();
33 l7seg_setup();
34 r0ketbeam_setup();
35 input_setup();
36 led_setup();
37 config_setup();
38 sei();
41 char nbuf;
42 char bpos;
43 int state = 0;
45 void loop(){ //one frame
46 static uint8_t escape_state = 0;
47 uint16_t receive_state = 1;
48 //primitive somewhat messy state machine of the uart interface
49 do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often.
50 receive_state = uart_getc();
51 char c = receive_state&0xFF;
52 receive_state &= 0xFF00;
53 //escape code format:
54 // \\ - backslash
55 // \n - newline
56 // \[x] - x
57 //eats [n] commands: 's' (0x73) led value sets led number [led] to [value]
58 // 'b' (0x62) buffer buffer buffer buffer sets the whole frame buffer
59 // 'a' (0x61) meter value sets analog meter number [meter] to [value]
60 // 'r' (0x72) read the frame buffer
61 // 'd' (0x64) display digit digit digit digit sets the 7-segment display (CAUTION: "display" currently is ignored)
62 //this device will utter a "'c' (0x63) num state" when switch [num] changes state to [state]
63 //commands are terminated by \n
64 if(!receive_state){
65 if(!escape_state){
66 if(c == '\\'){
67 receive_state |= 0x02;
68 escape_state = 1;
69 }else if(c == '\n'){
70 receive_state |= 0x02;
71 state = 0;
73 }else{
74 receive_state = 0;
75 escape_state = 0;
76 switch(c){
77 case '\\':
78 break;
79 case 'n':
80 c = '\n';
81 break;
85 if(!receive_state){
86 switch(state){
87 case 0: //Do not assume anything about the variables used
88 //command char
89 switch(c){
90 #ifdef HAS_LED_SUPPORT
91 case 's':
92 state = 2;
93 break;
94 case 'b':
95 nbuf = 0;
96 state = 4;
97 break;
98 #endif//HAS_LED_SUPPORT
99 #ifdef HAS_PWM_SUPPORT
100 case 'a':
101 state = 5;
102 nbuf = 0;
103 break;
104 #endif//HAS_PWM_SUPPORT
105 #ifdef HAS_LED_SUPPORT
106 case 'r':
107 uart_putc('r');
108 for(uint8_t i=0; i<sizeof(frameBuffer); i++){
109 uart_puthex(frameBuffer[i]);
111 uart_putc('\n');
112 break;
113 #endif//HAS_LED_SUPPORT
114 #ifdef HAS_7SEG_SUPPORT
115 case 'd':
116 nbuf = 0;
117 bpos = 0;
118 state = 7;
119 #endif//HAS_7SEG_SUPPORT
121 break;
122 #ifdef HAS_LED_SUPPORT
123 case 2:
124 nbuf=c;
125 state = 3;
126 break;
127 case 3:
128 setLED(nbuf, c);
129 state = 0;
130 break;
131 case 4:
132 secondFrameBuffer[(uint8_t) nbuf] = c;
133 nbuf++;
134 if(nbuf == 7){
135 swapBuffers();
136 state = 0;
138 break;
139 #endif//HAS_LED_SUPPORT
140 #ifdef HAS_PWM_SUPPORT
141 case 5:
142 if(c > PWM_COUNT)
143 c = 0;
144 nbuf = c;
145 state = 6;
146 break;
147 case 6:
148 pwm_val[(uint8_t) nbuf] = c;
149 state = 0;
150 break;
151 #endif//HAS_PWM_SUPPORT
152 #ifdef HAS_7SEG_SUPPORT
153 case 7:
154 nbuf = c;
155 state = 8;
156 break;
157 case 8:
158 l7seg_buf[(uint8_t)bpos] = c;
159 bpos++;
160 if(bpos == 4){
161 state = 0;
163 break;
164 #endif//HAS_7SEG_SUPPORT
167 }while(!receive_state);
168 led_loop();
169 r0ketbeam_loop();
170 l7seg_loop();
171 input_loop();
172 pwm_loop();
173 _delay_ms(1);