MSP device support complete. NOT TESTED YET! Preliminary python host lib
[cerebrum.git] / avr / cerebrum_firmware.c
blobf031bc1f3b6aa5441a8e89c0a84789b920966eda
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(57600, F_CPU));
32 //FIXME what was the following line for?
33 //DDRD |= 0x02;
34 pwm_setup();
35 l7seg_setup();
36 r0ketbeam_setup();
37 input_setup();
38 led_setup();
39 config_setup();
40 sei();
43 char nbuf;
44 char bpos;
45 int state = 0;
46 uint8_t somecycle = 0; //FIXME debug value
48 void loop(){ //one frame
49 static uint8_t escape_state = 0;
50 uint16_t receive_state = 1;
51 //primitive somewhat messy state machine of the uart interface
52 do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often.
53 receive_state = uart_getc();
54 char c = receive_state&0xFF;
55 receive_state &= 0xFF00;
56 //escape code format:
57 // \\ - backslash
58 // \n - newline
59 // \[x] - x
60 //eats [n] commands: 's' (0x73) led value sets led number [led] to [value]
61 // 'b' (0x62) buffer buffer buffer buffer sets the whole frame buffer
62 // 'a' (0x61) meter value sets analog meter number [meter] to [value]
63 // 'r' (0x72) read the frame buffer
64 // 'd' (0x64) display digit digit digit digit sets the 7-segment display (CAUTION: "display" currently is ignored)
65 //this device will utter a "'c' (0x63) num state" when switch [num] changes state to [state]
66 //commands are terminated by \n
67 if(!receive_state){
68 if(!escape_state){
69 if(c == '\\'){
70 receive_state |= 0x02;
71 escape_state = 1;
72 }else if(c == '\n'){
73 receive_state |= 0x02;
74 state = 0;
76 }else{
77 receive_state = 0;
78 escape_state = 0;
79 switch(c){
80 case '\\':
81 break;
82 case 'n':
83 c = '\n';
84 break;
88 if(!receive_state){
89 switch(state){
90 case 0: //Do not assume anything about the variables used
91 //command char
92 switch(c){
93 #ifdef HAS_LED_SUPPORT
94 case 's':
95 state = 2;
96 break;
97 case 'b':
98 nbuf = 0;
99 state = 4;
100 break;
101 #endif//HAS_LED_SUPPORT
102 #ifdef HAS_PWM_SUPPORT
103 case 'a':
104 state = 5;
105 nbuf = 0;
106 break;
107 #endif//HAS_PWM_SUPPORT
108 #ifdef HAS_NOISE_MAKER_SUPPORT
109 case 'x':
110 //DDRF |= 0x01;
111 //PORTF |= 0x01;
112 break;
113 case 'X':
114 //DDRF &= 0xFE;
115 //PORTF &= 0xFE;
116 break;
117 #endif//HAS_NOISE_MAKER_SUPPORT
118 #ifdef HAS_LED_SUPPORT
119 case 'r':
120 uart_putc('r');
121 for(uint8_t i=0; i<sizeof(frameBuffer); i++){
122 uart_puthex(frameBuffer[i]);
124 uart_putc('\n');
125 break;
126 #endif//HAS_LED_SUPPORT
127 #ifdef HAS_7SEG_SUPPORT
128 case 'd':
129 nbuf = 0;
130 bpos = 0;
131 state = 7;
132 #endif//HAS_7SEG_SUPPORT
134 break;
135 #ifdef HAS_LED_SUPPORT
136 case 2:
137 nbuf=c;
138 state = 3;
139 break;
140 case 3:
141 setLED(nbuf, c);
142 state = 0;
143 break;
144 case 4:
145 secondFrameBuffer[(uint8_t) nbuf] = c;
146 nbuf++;
147 if(nbuf == 7){
148 swapBuffers();
149 state = 0;
151 break;
152 #endif//HAS_LED_SUPPORT
153 #ifdef HAS_PWM_SUPPORT
154 case 5:
155 if(c > PWM_COUNT)
156 c = 0;
157 nbuf = c;
158 if(nbuf >= PWM_COUNT)
159 nbuf = 0;
160 state = 6;
161 break;
162 case 6:
163 pwm_val[(uint8_t) nbuf] = c;
164 uart_puts_p(PSTR("ACK\n"));
165 state = 0;
166 break;
167 #endif//HAS_PWM_SUPPORT
168 #ifdef HAS_7SEG_SUPPORT
169 case 7:
170 nbuf = c;
171 state = 8;
172 break;
173 case 8:
174 l7seg_buf[(uint8_t)bpos] = c;
175 bpos++;
176 if(bpos == 4){
177 state = 0;
179 break;
180 #endif//HAS_7SEG_SUPPORT
183 }while(!receive_state);
184 led_loop();
185 r0ketbeam_loop();
186 l7seg_loop();
187 input_loop();
188 pwm_loop();
189 config_loop();
190 _delay_ms(1);