blackbox/main.c

Mon, 14 Nov 2011 21:38:00 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Mon, 14 Nov 2011 21:38:00 +0100
changeset 3
1a0ef40ef458
parent 2
6c59b4293fa9
child 4
58529e14c45c
permissions
-rw-r--r--

fixed track change keys

0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
1 #include <avr/interrupt.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
2 #include <avr/io.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
3 #include <avr/wdt.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
4 #include <avr/eeprom.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
5 #include <stdlib.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
6 #include <stdint.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
7 #include <avr/pgmspace.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
8 #include <util/delay.h>
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
9
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
10 #include "driver/rs232.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
11 #include "driver/adc.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
12
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
13 #include "main.h"
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
14 #include "lowlevel.h"
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
15
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
16 volatile uint8_t datalen = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
17 char data[10]; // 8 bytes data buffer + string termination
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
18
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
19 static char buffer[RS232_BUFSIZE+1];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
20 static uint8_t buffer_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
21
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
22
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
23 // USART0 RX interrupt
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
24 ISR ( USART_RXC_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
25 char c = UDR;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
26
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
27 // check for buffer overflow
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
28 if (buffer_len==sizeof(buffer)) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
29 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30 if (c == 27) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 // escape sequence, store to empty buffer
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
32 buffer[buffer_len++] = c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
33 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
34 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
35 // collect characters until end of line
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
36 if (c == 27) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
37 // escape sequence, clear buffer
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
38 buffer_len = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
39 buffer[buffer_len++] = c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
40 } else if ( (c==0xff) && (buffer_len > 3) ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
41 buffer[buffer_len]=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
42
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
43 // packet end received, parse the received packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
44
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
45 // wait for the next packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
46 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
47 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
48 buffer[buffer_len++]=c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
49 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
50 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
51 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54 volatile uint16_t transmit_buffer;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
55 volatile uint16_t transmit_buffer_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
56 volatile uint8_t transmit_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57 volatile uint8_t transmit_len_next;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 volatile uint8_t transmit_len_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59 ISR ( TIMER2_COMP_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
60 //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
61 // data packet timer 100µs pro bit...
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 if (transmit_len >= 0xFE) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
63 if (transmit_len != 0xFF) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
64 RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65 transmit_len = 0xFF;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66 transmit_buffer = transmit_buffer_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
67 transmit_buffer_queue = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
68 transmit_len_next = transmit_len_queue;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
69 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
70 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
71 uint16_t bit = (1<<(transmit_len & 0b01111111));
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
72 uint16_t clock;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
73 if ((transmit_len & 0b10000000) == 0) clock = 0; else clock = 0xffff;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
74 if ( ((transmit_buffer ^ clock) & bit) != 0 )
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
75 RAIL_POWER_PORT |= _BV(RAIL_POWER); else
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
76 RAIL_POWER_PORT &= ~_BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
77 if ( (transmit_len & 0b10000000) == 0 ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
78 // block 0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
79 //if (transmit_len == 0) transmit_len = 0xFF; else transmit_len |= 0b10000000; // set clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
80 transmit_len |= 0b10000000; // set clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
81 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
82 // block 1, output the current bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
83 transmit_len &= 0b01111111; // reset clock
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
84 //if (transmit_len != 0) transmit_len--; // next bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
85 if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
88 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90 int insert_queue(uint16_t tmp, uint8_t len) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
91 if (transmit_buffer_queue == 0) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
92 transmit_buffer_queue = tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
93 transmit_len_queue = len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
94 return 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
95 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
96 return 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
97 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
98
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
99
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
100 int do_controller(uint8_t controller) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
101 // read controller X speed & encode controller data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
102 uint16_t tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
103 switch (controller) {
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
104 case 0:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
105 tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
106 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
107 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
108 case 1:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
109 tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
110 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
111 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
112 case 2:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
113 tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
114 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
115 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
116 case 3:
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
117 tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0xF) << 1;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
118 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) tmp |= (1<<5);
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
119 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
120 case 4: tmp = (1<<5); break; // todo regler 5
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
121 case 5: tmp = (1<<5); break; // todo regler 6
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
122 }
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
123 tmp |= 0b1000000000 | (controller << 6);
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
124 if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
125
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
126 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
127 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
128
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
129 unsigned char mirror( unsigned char n ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
130 // mirror all 8 bits
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
131 n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
132 n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
133 n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
134 return n;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
135 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
136
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
137
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138 int do_program(uint8_t controller, uint8_t command, uint8_t parameter) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 // send program data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 uint16_t tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
141 tmp = 0b1000000000000 | (mirror(parameter) << 4) | mirror(command)| (mirror(controller) >> 5);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
142
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
143 return insert_queue(tmp, 12);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
144 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
145
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
146 int do_active(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
147 // send controller active data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
148 uint16_t tmp = 0b10000000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
149 if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
150 if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
151 if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
152 if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
153 // todo: regler 5 und 6
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
154
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
155 return insert_queue(tmp, 7);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
156 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
157
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
158 int do_pace_ghost(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
159 // send ghost and pacecar data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160 // todo: at the moment, both disabled!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
161 uint16_t tmp = 0b1111100000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
162 if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
163 // todo: PC, NH, TK, (KFR, FR)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
164
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
165 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
166 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
167
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
168 ISR ( TIMER1_COMPA_vect ) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
169 //OCR2 = 0xFF; // make sure that timer2 is synced to timer1, give enough cycles to prepare
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
170 LED(4,2);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
171 // trigger packet transfer:
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
172 transmit_len = transmit_len_next;
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
173
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
174 // here is some more time to do something else...
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
175 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
176
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
177
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
178 int main(void)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
179 {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
180
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
181 unsigned char s[30];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
182 uint16_t tmp;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
183 uint8_t packet_index = 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
184
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
185
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
186 init_hardware();
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
187
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
188 itoa(TIMER2_50US, s, 10);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
189 RS232_puts(s);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
190
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
191 // switch on rails power
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
192 RAIL_POWER_PORT |= _BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
193
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
194 while (1) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
195 // check for short circuit on the rails
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
196 check_rails_shortcut();
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
197
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
198 switch (packet_index) {
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
199 case 1:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
200 if (do_program(7, 19, 0)) packet_index++; // reset
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
201 //do_program(7, 16, 3); // led an startampel
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
202 //do_program(0, 4, 0); //
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
203 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
204 case 2:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
205 if (do_pace_ghost()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
206 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
207 case 3:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
208 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
209 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
210 case 4:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
211 if (do_controller(0)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
212 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
213 case 5:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
214 if (do_controller(4)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
215 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
216 case 6:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
217 if (do_controller(1)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
218 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
219 case 7:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
220 if (do_controller(5)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
221 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
222 case 8:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
223 if (do_controller(2)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
224 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
225 case 9:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
226 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
227 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
228 case 10:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
229 if (do_controller(3)) packet_index = 1;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
230 // last packet, so reset packet index
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
231 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
232 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
233
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
234
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
235 } // main loop end
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
236 };
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
237

mercurial