blackbox/main.c

changeset 10
6d6e982bbc41
parent 8
84249ab5d691
child 11
69c2a1408619
equal deleted inserted replaced
9:20dbe0546a36 10:6d6e982bbc41
52 // TODO: at the moment only parameters 0..9 supported 52 // TODO: at the moment only parameters 0..9 supported
53 // needs to build a "human" parser 53 // needs to build a "human" parser
54 if (program_count == 0) { 54 if (program_count == 0) {
55 program_id = buffer[3]-'0'; 55 program_id = buffer[3]-'0';
56 program_command = buffer[1]-'0'; 56 program_command = buffer[1]-'0';
57 if (program_command > 9)
58 program_command = buffer[1]-'A'+10;
57 program_parameter = buffer[2]-'0'; 59 program_parameter = buffer[2]-'0';
60 if (program_parameter > 9)
61 program_parameter = buffer[2]-'A'+10;
58 if (program_command < 4) 62 if (program_command < 4)
59 program_count = 0x02; // send commands twice (fuel, speed, brake) 63 program_count = 0x02; // send commands twice (fuel, speed, brake)
60 else 64 else
61 program_count = 0x01; 65 program_count = 0x01;
62 RS232_puts_p(PSTR("OK\n")); 66 RS232_puts_p(PSTR("OK\n"));
89 // read controller X speed & encode controller data packet 93 // read controller X speed & encode controller data packet
90 uint16_t tmp; 94 uint16_t tmp;
91 switch (controller) { 95 switch (controller) {
92 case 0: 96 case 0:
93 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 97 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
94 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) tmp |= (1<<5); 98 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
99 tmp |= (1<<5);
100 LED(1,0);
101 } else LED(1,1);
95 break; 102 break;
96 case 1: 103 case 1:
97 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 104 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
98 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) tmp |= (1<<5); 105 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) {
106 tmp |= (1<<5);
107 LED(2,0);
108 } else LED(2,1);
99 break; 109 break;
100 case 2: 110 case 2:
101 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 111 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
102 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) tmp |= (1<<5); 112 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) {
113 tmp |= (1<<5);
114 LED(3,0);
115 } else LED(3,1);
103 break; 116 break;
104 case 3: 117 case 3:
105 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 118 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
106 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) tmp |= (1<<5); 119 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) {
120 tmp |= (1<<5);
121 LED(4,0);
122 } else LED(4,1);
107 break; 123 break;
108 case 4: tmp = (1<<5); break; // todo regler 5 124 case 4: tmp = (1<<5); break; // todo regler 5
109 case 5: tmp = (1<<5); break; // todo regler 6 125 case 5: tmp = (1<<5); break; // todo regler 6
110 } 126 }
111 tmp |= 0b1000000000 | (controller << 6); 127 tmp |= 0b1000000000 | (controller << 6);
155 } 171 }
156 172
157 ISR ( TIMER1_COMPA_vect ) { 173 ISR ( TIMER1_COMPA_vect ) {
158 // trigger packet transfer: 174 // trigger packet transfer:
159 transmit_len = transmit_len_next; 175 transmit_len = transmit_len_next;
160 LED(2,2); 176 //LED(2,2);
161 177
162 // here is some more time to do something else... 178 // here is some more time to do something else...
163 } 179 }
164 180
165 ISR ( TIMER2_COMP_vect ) { 181 ISR ( TIMER2_COMP_vect ) {
199 //if (transmit_len != 0) transmit_len--; // next bit 215 //if (transmit_len != 0) transmit_len--; // next bit
200 if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit 216 if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit
201 } 217 }
202 } 218 }
203 219
204 LED(3,2); 220 //LED(3,2);
205 } 221 }
206 222
207 223
208 ISR ( TIMER0_OVF_vect ) { 224 ISR ( TIMER0_OVF_vect ) {
209 // TODO: last bit should be set by the sender, not from us! 225 // TODO: last bit should be set by the sender, not from us!
210 TCNT0 = TIMER0_250US; 226 TCNT0 = TIMER0_250US;
211 LED(1,2); 227 //LED(1,2);
212 if (timer0_delay == 0) { 228 if (timer0_delay == 0) {
213 RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // pull rails low 229 RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // pull rails low
214 _delay_us(28); // wait some cycles 230 _delay_us(28); // wait some cycles
215 if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) { // check for logic zero 231 if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) { // check for logic zero
216 if (response == 0) { 232 if (response == 0) {
281 // issue reset command to lap counter 297 // issue reset command to lap counter
282 program_command = 6; 298 program_command = 6;
283 program_parameter = 9; 299 program_parameter = 9;
284 program_id = 0; 300 program_id = 0;
285 program_count = 1; 301 program_count = 1;
302 LAP_COUNTER_PORT |= _BV(LAP_COUNTER);
286 } else { 303 } else {
287 // do a reset, switch back to free drive mode 0 304 // do a reset, switch back to free drive mode 0
288 mode = 0; 305 mode = 0;
289 // issue reset command 306 // issue reset command
290 program_command = 19; 307 program_command = 19;
291 program_parameter = 0; 308 program_parameter = 0;
292 program_id = 7; 309 program_id = 7;
293 program_count = 1; 310 program_count = 1;
311 LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER);
294 } 312 }
295 } 313 }
296 //LAP_COUNTER_PORT |= _BV(LAP_COUNTER); 314 //LAP_COUNTER_PORT |= _BV(LAP_COUNTER);
297 } else old_start = btn_start; 315 } else old_start = btn_start;
298 // LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER); 316 // LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER);

mercurial