Fri, 27 Dec 2013 11:56:22 +0100
addedd car activity packet to receiver in car firmware
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
1 | #include <avr/interrupt.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
2 | #include <avr/io.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
3 | #include <avr/wdt.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include <avr/eeprom.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include <stdlib.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
6 | #include <stdint.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
7 | #include <avr/pgmspace.h> |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
8 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | #include "main.h" |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | #include "util/delay.h" |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
13 | ISR ( USART_RXC_vect ) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | #define PULSE_PORT PORTD |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | #define PULSE_BIT PD2 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
19 | typedef struct { |
152 | 20 | uint8_t calibration; // AVR Chip calibration byte written by avrdude |
21 | uint8_t initialized; // if 0xff, reset config to defaults on first boot | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
22 | uint8_t slot; |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
23 | uint8_t light; |
153 | 24 | uint8_t brake; |
25 | uint8_t accel; | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
26 | uint8_t program; // 0xff = inactive ; programming mode active on slot X |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
27 | } config_t; |
153 | 28 | config_t EEMEM eeconfig = {0,0xff,0,0,15,15,0}; |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
29 | config_t config; |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | volatile uint16_t data = 0; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | volatile uint8_t data_len = 0; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | volatile uint8_t bitbuf_len = 0; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | volatile uint16_t bitbuf = 0; |
152 | 35 | volatile uint8_t car_speed[MAX_SLOTS]; |
36 | volatile uint8_t car_switch[MAX_SLOTS]; | |
154
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
37 | volatile uint8_t car_act; |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
38 | |
152 | 39 | volatile uint8_t car_timeout[MAX_SLOTS]; |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
40 | volatile uint8_t timeout = 0; |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
41 | volatile uint8_t brake_timeout = 0; |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
42 | |
152 | 43 | uint8_t old_switch[MAX_SLOTS]; |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | uint8_t my_switch; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | uint8_t my_speed; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
47 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
48 | ISR ( INT0_vect ) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
49 | GICR &= ~_BV(INT0) ; // Disable INT0 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
50 | // Startsignal erkannt, ab hier den Timer2 starten, |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
51 | // der liest dann alle 50µs den Zustand ein und schreibt das |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
52 | // empfangene Bit in den Puffer |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
53 | bitbuf = 0; // init |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
54 | bitbuf_len = 0b10000000; // init 1 pulse received |
152 | 55 | |
153 | 56 | //TCNT2 = 10; |
57 | TCNT2 = 9; | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
58 | TIMSK |= _BV(OCIE2); //enable timer2 interrupt |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
59 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
60 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
61 | ISR ( TIMER2_COMP_vect ) { |
153 | 62 | // TCNT2 = 0; |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
63 | uint8_t clock; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
64 | uint8_t state; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
65 | uint8_t state2; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
66 | if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
67 | if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
68 | if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0xff; else state2 = 0; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
69 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
70 | if (clock) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
71 | bitbuf_len &= ~_BV(7); // switch clock to low |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
72 | // second pulse of bit |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
73 | if ((state==state2) & state2) { |
152 | 74 | TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt |
75 | ||
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
76 | // two cycles high: packet end received |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
77 | data_len = (bitbuf_len & 0b00111111); |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
78 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
79 | //data = bitbuf; // output data |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
80 | // write data of controllers to array |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
81 | if (data_len == 10) { // controller data packet |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
82 | clock = (bitbuf >> 6) & 0b00000111; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
83 | car_speed[clock] = (bitbuf >> 1) & 0x0F; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
84 | car_switch[clock] = (bitbuf >> 5) & 1; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
85 | // current response for this car? |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
86 | /* |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
87 | if (response != 0) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
88 | if ( ((response & 0b00001110) >> 1) == clock) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
89 | // add our ID to response: |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
90 | send_response(response | self_id << 6); |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
91 | response = 0; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
92 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
93 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
94 | */ |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
95 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
96 | |
154
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
97 | if (data_len == 8) { // activity data packet |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
98 | car_act = (bitbuf >> 1) & 0b00111111; |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
99 | for (clock = 0; clock<MAX_SLOTS; clock++) { |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
100 | if (((car_act >> clock) & 1) == 0) car_speed[MAX_SLOTS-1-clock] = 0; |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
101 | } |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
102 | } |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
103 | |
46822de910ff
addedd car activity packet to receiver in car firmware
mbayer
parents:
153
diff
changeset
|
104 | |
152 | 105 | GICR |= _BV(INT0) ; // Enable INT0 |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
106 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
107 | } else { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
108 | bitbuf_len++; // increment bit counter |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
109 | bitbuf = bitbuf << 1; // shift bits |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
110 | if (state2 == 0) bitbuf |= 1; // receive logic one |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
111 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
112 | } else { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
113 | bitbuf_len |= _BV(7); // switch clock to high |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
114 | // first pulse of bit |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
115 | if (state2) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
116 | bitbuf_len |= _BV(6); // store new state |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
117 | } else { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
118 | bitbuf_len &= ~_BV(6); // store new state |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
119 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
120 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
121 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
122 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
123 | ISR (INT1_vect) { |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
124 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
125 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
126 | ISR (TIMER0_OVF_vect) { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
127 | TCNT0 = 100; // TIMER0 vorladen mit 100 |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
128 | if (brake_timeout > 1) brake_timeout--; |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
129 | if (timeout > 1) timeout--; |
152 | 130 | for (uint8_t i=0; i<MAX_SLOTS; i++) |
131 | if (car_timeout[i] > 1) car_timeout[i]--; | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
132 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
133 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
134 | #define LIGHT_PORT PORTC |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
135 | #define LIGHT_FRONT 2 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
136 | #define LIGHT_BRAKE 4 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
137 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
138 | #define IR_PORT PORTB |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
139 | #define IR_LED 3 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
140 | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
141 | #define LIGHT_MODES 1 // anzahl der lichtmodi (ohne den modus "aus") |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
142 | #define BRAKE_OFF_TIMEOUT 60 // value * 10ms |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
143 | |
151 | 144 | //#define CAR_DEBUG 1 |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
145 | #define EE_CONFIG_ADDR 64 |
151 | 146 | #define DOUBLE_CLICK_TIMEOUT 50 // 500ms |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
147 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
148 | void config_save(void) { |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
149 | eeprom_write_block( &config, &eeconfig, sizeof(config_t) ); |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
150 | } |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
151 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
152 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
153 | void brake_on(void) { |
153 | 154 | OCR1A = (int) ((float)0xff * (float)((float)config.brake / (float)15)); |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
155 | LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on |
152 | 156 | DDRB |= _BV(1); // PB1 PWM Output enable |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
157 | brake_timeout = BRAKE_OFF_TIMEOUT; |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
158 | } |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
159 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
160 | void brake_off(void) { |
153 | 161 | OCR1A = 0; |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
162 | LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off |
151 | 163 | DDRB &= ~_BV(1); // PB1 PWM Output disable |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
164 | brake_timeout = 0; |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
165 | } |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
166 | |
151 | 167 | uint8_t set_id(void) { |
168 | _delay_ms(100); // short wait | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
169 | uint8_t temp; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
170 | timeout = 1; |
151 | 171 | brake_timeout = 0xff; |
172 | // wait for key press and assign on double click | |
173 | while ((car_speed[config.slot] == 0) && (brake_timeout > 1)) { | |
174 | temp = car_switch[config.program]; | |
175 | if (temp == 0) { | |
176 | // wait for second key press within timeout period to assign successfully | |
177 | brake_timeout = DOUBLE_CLICK_TIMEOUT; | |
178 | timeout = 1; | |
179 | while (brake_timeout > 1) { | |
180 | if (temp != car_switch[config.program]) { | |
181 | temp = car_switch[config.program]; | |
182 | if (temp == 0) { | |
183 | config.slot = config.program; | |
184 | return 1; | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
185 | } |
151 | 186 | } |
187 | // toggle lights if timeout | |
188 | if (timeout == 1) { | |
189 | LIGHT_PORT ^= _BV(LIGHT_FRONT); | |
190 | timeout = 5; | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
191 | } |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
192 | } |
151 | 193 | return 0; |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
194 | } |
151 | 195 | |
196 | // toggle lights if timeout | |
197 | if (timeout == 1) { | |
198 | LIGHT_PORT ^= _BV(LIGHT_FRONT); | |
199 | timeout = 10; | |
200 | } | |
201 | } | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
202 | return 0; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
203 | } |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
204 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
205 | int main(void) |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
206 | { |
152 | 207 | // config (from eeprom!) |
208 | eeprom_read_block( &config, &eeconfig, sizeof(config_t) ); | |
209 | ||
210 | // set the internal calibration byte | |
211 | OSCCAL = config.calibration; | |
212 | // TODO: Vielleicht den internen Takt des AVR anhand der Bitclock auf den Schienen synchronisieren??? | |
213 | // Das Calibration byte scheint nicht zu stimmen | |
214 | ||
215 | if (config.initialized == 0xff) { | |
216 | config.slot = 0; | |
217 | config.light = 0; | |
153 | 218 | config.brake = 15; |
219 | config.accel = 15; | |
152 | 220 | config.program = 0xff; |
221 | config.initialized = 0; | |
222 | config_save(); | |
223 | } | |
224 | ||
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
225 | uint8_t temp; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
226 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
227 | // setup data bit timer2 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
228 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
229 | OCR2 = TIMER2_50US; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
230 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
231 | |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
232 | // enable both external interrupts |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
233 | // int 0 = data RX |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
234 | MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
235 | GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1 |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
236 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
237 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
238 | DDR(LIGHT_PORT) |= _BV(LIGHT_FRONT) | _BV(LIGHT_BRAKE); |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
239 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
240 | TCCR1A = (1<<WGM10)|(1<<COM1A1) // Set up the two Control registers of Timer1. |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
241 | |(1<<COM1B1); // Wave Form Generation is Fast PWM 8 Bit, |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
242 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
243 | TCCR1B = (1<<WGM12)|(1<<CS10); // OC1A and OC1B are cleared on compare match |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
244 | // and set at BOTTOM. Clock Prescaler is 1. |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
245 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
246 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
247 | //OCR1A = 63; // Dutycycle of OC1A = 25% |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
248 | //OCR1B = 127; // Dutycycle of OC1B = 50% |
153 | 249 | OCR1A = 0; // brake PWM! |
151 | 250 | OCR1B = 0; // Motor drive PWM |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
251 | DDRB &= ~_BV(2); // PB2 PWM Output disable |
151 | 252 | DDRB &= ~_BV(1); // PB1 PWM Output disable |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
253 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
254 | // configure TIMER0 to overflow every 10ms at 4 MHz |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
255 | TIMSK = _BV(TOIE0); // Timer0 Overflow INT erlauben |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
256 | TCNT0 = 100; // TIMER0 vorladen mit 100 |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
257 | TCCR0 = _BV(CS02) ; // Vorteiler auf 256, ab hier läuft der TIMER0 |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
258 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
259 | sei(); |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
260 | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
261 | |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
262 | if ((config.program != 0xff) || (config.slot > 5 )) { |
151 | 263 | temp = set_id(); |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
264 | config.program = 0xff; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
265 | config_save(); |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
266 | if (temp == 1) { |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
267 | // acknowledge with the engine |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
268 | OCR1B = 25; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
269 | DDRB &= ~_BV(2); // PB2 PWM Output disable |
151 | 270 | for (temp = 128; temp > 0; temp--) { |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
271 | DDRB ^= _BV(2); // PB2 PWM Output toggle |
151 | 272 | _delay_ms(2); |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
273 | DDRB ^= _BV(2); // PB2 PWM Output toggle |
151 | 274 | _delay_ms(3); |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
275 | } |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
276 | |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
277 | } |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
278 | timeout = 0; |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
279 | } |
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
280 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
281 | |
153 | 282 | float my_accel; |
151 | 283 | my_switch = car_switch[config.slot]; // initialize |
284 | my_speed = car_speed[config.slot]; // initialize | |
153 | 285 | my_accel = (float)config.accel / (float)15; |
151 | 286 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
287 | while (1) { |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
288 | // main loop |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
289 | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
290 | if (brake_timeout == 1) brake_off(); |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
291 | |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
292 | if (my_speed != car_speed[config.slot]) { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
293 | my_speed = car_speed[config.slot]; |
153 | 294 | OCR1B = (int) ((float)0xff * (float)((float)my_speed / (float)15) * my_accel ); |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
295 | if (my_speed == 0) { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
296 | DDRB &= ~_BV(2); // PB2 PWM Output disable |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
297 | brake_on(); |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
298 | } else { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
299 | brake_off(); |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
300 | DDRB |= _BV(2); // PB2 PWM Output enable |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
301 | } |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
302 | } |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
303 | |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
304 | // Light cycle if switch pressed without speed |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
305 | if (car_speed[config.slot] == 0) { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
306 | if (my_switch != car_switch[config.slot]) { |
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
307 | my_switch = car_switch[config.slot]; |
151 | 308 | if (my_switch == 0) { |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
309 | // cycle light |
152 | 310 | if (config.light >= LIGHT_MODES) config.light = 0; else config.light++; |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
311 | config_save(); |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
312 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
313 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
314 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
315 | |
152 | 316 | // check any car switch for a double click and speed = 0 |
317 | for (temp = 0; temp<MAX_SLOTS; temp++) if (car_switch[temp] != old_switch[temp]) { | |
318 | old_switch[temp] = car_switch[temp]; | |
319 | if ((car_speed[temp] == 0) && (old_switch[temp] == 0)) { | |
320 | // key pressed | |
321 | if (car_timeout[temp] > 1) { | |
322 | // second key press within timeout, enter program mode for this key | |
323 | config.program = temp; | |
324 | config_save(); | |
325 | car_timeout[temp] = 0xff; // the car has to be reset within this timeout | |
326 | } else { | |
327 | car_timeout[temp] = DOUBLE_CLICK_TIMEOUT; | |
328 | } | |
329 | } | |
330 | ||
331 | if (car_timeout[temp] == 1) { | |
332 | if (config.program == temp) { | |
333 | // cancel ID programming mode | |
334 | config.program = 0xff; | |
335 | config_save(); | |
336 | } | |
337 | car_timeout[temp] = 0; | |
338 | } | |
339 | } | |
340 | ||
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
341 | switch (config.light) { |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
342 | case 0: |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
343 | LIGHT_PORT &= ~_BV(LIGHT_FRONT); // switch lights off |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
344 | break; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
345 | case 1: |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
346 | LIGHT_PORT |= _BV(LIGHT_FRONT); // switch lights on |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
347 | break; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
348 | } |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
349 | |
148
08cb88614d69
car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents:
147
diff
changeset
|
350 | |
149
1c3425af9aa0
car firmware suspended, avr chip not responding after fuse error (note to self, never program an avr when powered via the SCK line :-)
Malte Bayer <mbayer@neo-soft.org>
parents:
148
diff
changeset
|
351 | // timeout reset |
151 | 352 | if (timeout == 1) timeout = 0; |
147
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
353 | } // main loop end |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
354 | }; |
f66c5b3b3ed2
started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
355 |