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 |
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; |