blackbox/main.c

changeset 36
aea84f4f5a12
parent 35
00166228a419
child 37
136a79772098
equal deleted inserted replaced
35:00166228a419 36:aea84f4f5a12
11 #include "driver/adc.h" 11 #include "driver/adc.h"
12 12
13 #include "main.h" 13 #include "main.h"
14 #include "lowlevel.h" 14 #include "lowlevel.h"
15 15
16 #define MAX_SLOTS 5
17 uint8_t speedlimit[MAX_SLOTS];
18
16 volatile uint8_t program_count = 0; 19 volatile uint8_t program_count = 0;
17 volatile uint8_t program_id; 20 volatile uint8_t program_id;
18 volatile uint8_t program_command; 21 volatile uint8_t program_command;
19 volatile uint8_t program_parameter; 22 volatile uint8_t program_parameter;
20 23
43 return 1; 46 return 1;
44 } 47 }
45 return 0; 48 return 0;
46 } 49 }
47 50
51 char ok[] PROGMEM="OK\n";
52 char busy[] PROGMEM="BUSY\n";
53
48 54
49 ISR ( USART_RXC_vect ) { 55 ISR ( USART_RXC_vect ) {
56 uint8_t tmp;
50 char c = UDR; 57 char c = UDR;
51 58
52 // check for buffer overflow 59 // check for buffer overflow
53 if (buffer_len==sizeof(buffer)) { 60 if (buffer_len==sizeof(buffer)) {
54 buffer_len=0; 61 buffer_len=0;
70 program_parameter = buffer[2]-'A'+10; 77 program_parameter = buffer[2]-'A'+10;
71 if (program_command < 4) 78 if (program_command < 4)
72 program_count = 0x02; // send commands twice (fuel, speed, brake) 79 program_count = 0x02; // send commands twice (fuel, speed, brake)
73 else 80 else
74 program_count = 0x01; 81 program_count = 0x01;
75 RS232_puts_p(PSTR("OK\n")); 82 RS232_puts_p(ok);
76 } else RS232_puts_p(PSTR("BUSY\n")); 83 } else RS232_puts_p(busy);
84 break;
85
86 case 'L': // Limit maximum speed for a car
87 tmp = buffer[2]-'0';
88 if (tmp > 9)
89 tmp = buffer[2]-'A'+10;
90 speedlimit[buffer[1]-'0'] = tmp;
91 RS232_puts_p(ok);
77 break; 92 break;
78 93
79 } 94 }
80 95
81 // wait for the next packet 96 // wait for the next packet
93 int do_controller(uint8_t controller) { 108 int do_controller(uint8_t controller) {
94 // read controller X speed & encode controller data packet 109 // read controller X speed & encode controller data packet
95 uint16_t tmp; 110 uint16_t tmp;
96 switch (controller) { 111 switch (controller) {
97 case 0: 112 case 0:
98 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 113 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
114 if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
115 tmp = tmp << 1;
99 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) { 116 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
100 tmp |= (1<<5); 117 tmp |= (1<<5);
101 LED(1,0); 118 LED(1,0);
102 } else LED(1,1); 119 } else LED(1,1);
103 break; 120 break;
104 case 1: 121 case 1:
105 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 122 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
123 if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
124 tmp = tmp << 1;
106 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) { 125 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) {
107 tmp |= (1<<5); 126 tmp |= (1<<5);
108 LED(2,0); 127 LED(2,0);
109 } else LED(2,1); 128 } else LED(2,1);
110 break; 129 break;
111 case 2: 130 case 2:
112 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 131 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
132 if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
133 tmp = tmp << 1;
113 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) { 134 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) {
114 tmp |= (1<<5); 135 tmp |= (1<<5);
115 LED(3,0); 136 LED(3,0);
116 } else LED(3,1); 137 } else LED(3,1);
117 break; 138 break;
118 case 3: 139 case 3:
119 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1; 140 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
141 if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
142 tmp = tmp << 1;
120 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) { 143 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) {
121 tmp |= (1<<5); 144 tmp |= (1<<5);
122 LED(4,0); 145 LED(4,0);
123 } else LED(4,1); 146 } else LED(4,1);
124 break; 147 break;
258 ISR (INT2_vect) { 281 ISR (INT2_vect) {
259 // Lap counter Interrupt 282 // Lap counter Interrupt
260 LED(5,2); 283 LED(5,2);
261 } 284 }
262 285
286 void reset_vars(void) {
287 uint8_t i;
288 for (i=0; i<MAX_SLOTS; i++) {
289 speedlimit[i] = 15;
290 }
291 }
292
263 293
264 int main(void) 294 int main(void)
265 { 295 {
266 296
267 unsigned char s[30]; 297 unsigned char s[30];
282 // 7: race start initiated (next mode will be zero = free drive) 312 // 7: race start initiated (next mode will be zero = free drive)
283 313
284 314
285 315
286 init_hardware(); 316 init_hardware();
317 reset_vars();
287 318
288 // switch on rails power 319 // switch on rails power
289 RAIL_POWER_PORT |= _BV(RAIL_POWER); 320 RAIL_POWER_PORT |= _BV(RAIL_POWER);
290 321
291 while (1) { 322 while (1) {

mercurial