blackbox/main.c

Thu, 08 Dec 2011 17:46:21 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Thu, 08 Dec 2011 17:46:21 +0100
changeset 52
6b26d7e3ecd0
parent 51
ec9e8cc27012
child 53
f659e6faf18f
permissions
-rw-r--r--

simplified do_controller function

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
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
16 char ok[] PROGMEM="OK\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
17 char busy[] PROGMEM="BUSY\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
18 char prepare[] PROGMEM="!RACE PREPARE\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
19 char countdownstart[] PROGMEM="!COUNTDOWN\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
20 char racestart[] PROGMEM="!RACE START\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
21
45
da820f138498 switched sysclk to u32
Malte Bayer <mbayer@neo-soft.org>
parents: 44
diff changeset
22 typedef union {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
23 uint32_t value;
45
da820f138498 switched sysclk to u32
Malte Bayer <mbayer@neo-soft.org>
parents: 44
diff changeset
24 uint16_t word[2]; // high, low word
da820f138498 switched sysclk to u32
Malte Bayer <mbayer@neo-soft.org>
parents: 44
diff changeset
25 uint8_t byte[4]; // all four bytes
da820f138498 switched sysclk to u32
Malte Bayer <mbayer@neo-soft.org>
parents: 44
diff changeset
26 } u32;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
27
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
28 #define MAX_SLOTS 6
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
29 typedef struct {
49
3957ff8e86c6 more memsave through packed structure#
Malte Bayer <mbayer@neo-soft.org>
parents: 48
diff changeset
30 unsigned speedlimit:4; // 4bits speedlimit
3957ff8e86c6 more memsave through packed structure#
Malte Bayer <mbayer@neo-soft.org>
parents: 48
diff changeset
31 unsigned speedminimum:4; // 4bits speedminimum
3957ff8e86c6 more memsave through packed structure#
Malte Bayer <mbayer@neo-soft.org>
parents: 48
diff changeset
32 unsigned trackswitch:1; // 1bit bool
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
33 uint8_t fuel;
47
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
34 uint16_t jumpstart_time, laps;
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
35 u32 lap_time_start, lap_time;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
36 } cardata;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
37
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
38 static unsigned char s[8];
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
39 static uint8_t countdown, countdown_loops;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
40 uint8_t mode = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
41 // valid race modes:
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
42 // 0: free drive / idle
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
43 // 1: waiting for countdown start
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
44 // 2: race countdown initiated
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
45 // 3: Race start condition
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
46
45
da820f138498 switched sysclk to u32
Malte Bayer <mbayer@neo-soft.org>
parents: 44
diff changeset
47 volatile u32 sysclk;
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
48 volatile uint8_t sysclk_packettimer = 0;
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
49 volatile cardata slot[MAX_SLOTS];
44
4f4326794ba4 implemented lap time, there is some bug in 32bit substraction yet
Malte Bayer <mbayer@neo-soft.org>
parents: 43
diff changeset
50
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
51 volatile uint8_t car0, car1;
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
52 volatile uint16_t car0_new, car0_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
53 volatile uint16_t car1_new, car1_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
54 uint8_t car0_state, car1_state;
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
55
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
56 volatile uint8_t program_count = 0;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
57 volatile uint8_t program_id;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
58 volatile uint8_t program_command;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
59 volatile uint8_t program_parameter;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
60
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
61 volatile uint8_t datalen = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 char data[10]; // 8 bytes data buffer + string termination
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
63
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
64 static char buffer[RS232_BUFSIZE+1];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65 static uint8_t buffer_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
67 volatile uint16_t transmit_buffer;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
68 volatile uint16_t transmit_buffer_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
69 volatile uint8_t transmit_len;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
70 volatile uint8_t transmit_len_next;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
71 volatile uint8_t transmit_len_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
72
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
73 volatile uint16_t response;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
74 volatile uint8_t response_len;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
75 volatile uint8_t timer0_delay;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
76
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
77
35
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
78 int insert_queue(uint16_t tmp, uint8_t len) {
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
79 if (transmit_buffer_queue == 0) {
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
80 transmit_buffer_queue = tmp;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
81 transmit_len_queue = len;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
82 return 1;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
83 }
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
84 return 0;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
85 }
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
86
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87 ISR ( USART_RXC_vect ) {
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
88 uint8_t tmp;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89 char c = UDR;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
91 // check for buffer overflow
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
92 if (buffer_len==sizeof(buffer)) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
93 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
94 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
95 // collect characters until end of line
35
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
96 if ( (c=='\n') ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
97 buffer[buffer_len]=0;
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 // packet end received, parse the received packet
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
100 switch (buffer[0]) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
101 case 'P': // inject a program data word to the rails
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
102 if (program_count == 0) {
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
103 program_id = buffer[3]-'0';
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
104 program_command = buffer[1]-'0';
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
105 if (program_command > 9)
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
106 program_command = buffer[1]-'A'+10;
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
107 program_parameter = buffer[2]-'0';
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
108 if (program_parameter > 9)
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
109 program_parameter = buffer[2]-'A'+10;
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
110 if (program_command < 4)
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
111 program_count = 0x02; // send commands twice (fuel, speed, brake)
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
112 else
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
113 program_count = 0x01;
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
114 RS232_puts_p(ok);
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
115 } else RS232_puts_p(busy);
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
116 break;
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
117
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
118 case 'L': // Limit maximum speed for a car
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
119 tmp = buffer[2]-'0';
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
120 if (tmp > 9)
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
121 tmp = buffer[2]-'A'+10;
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
122 slot[buffer[1]-'0'].speedlimit = tmp;
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
123 RS232_puts_p(ok);
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
124 break;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
125
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
126 case 'S': // set minimum speed for a car
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
127 tmp = buffer[2]-'0';
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
128 if (tmp > 9)
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
129 tmp = buffer[2]-'A'+10;
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
130 slot[buffer[1]-'0'].speedminimum = tmp;
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
131 RS232_puts_p(ok);
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
132 break;
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
133
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
134 case 'I': // get Information data (incl. important global parameter dump)
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
135 RS232_puts(VERSION);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
136 RS232_putc(':');
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
137 for (tmp=0;tmp<MAX_SLOTS;tmp++) RS232_putc(slot[tmp].speedlimit); // output speed limits
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
138 RS232_putc(':');
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
139 for (tmp=0;tmp<MAX_SLOTS;tmp++) RS232_putc(slot[tmp].speedminimum); // output minimum speed
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
140 RS232_putc(':');
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
141 for (tmp=0;tmp<MAX_SLOTS;tmp++) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
142 itoa(slot[tmp].fuel, s, 16);
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
143 RS232_putc(s); // output fuel levels (0=empty, 100=full, 0xff=no fuel option)
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
144 RS232_putc(',');
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
145 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
146 RS232_putc(':');
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
147 for (tmp=0;tmp<MAX_SLOTS;tmp++) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
148 itoa(slot[tmp].jumpstart_time, s, 16);
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
149 RS232_puts(s); // output jumpstart times
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
150 RS232_putc(',');
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
151 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
152 RS232_putc(':');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
153 RS232_putc('\n');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
154 break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
155
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
156 }
0
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 // wait for the next packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
159 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
161 buffer[buffer_len++]=c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
162 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
163 }
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
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
166
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
167 void jumpstart(uint8_t controller) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
168 if (slot[controller].jumpstart_time == 0) {
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
169 slot[controller].jumpstart_time = sysclk.word[0]; // low word
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
170 RS232_putc('~');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
171 RS232_putc('0'-controller);
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
172 RS232_putc(':');
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
173 itoa(slot[controller].jumpstart_time, s, 16);
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
174 RS232_puts(s);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
175 RS232_putc('\n');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
176 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
177
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
178 }
0
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 int do_controller(uint8_t controller) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
182 // read controller X speed & encode controller data packet
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
183 uint16_t tmp = 0;
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
184 uint8_t trackchange = 0xff;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
185
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
186 if ( (PIN(SW_PACECAR_PORT) & _BV(SW_PACECAR)) == 0 ) {
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
187 // map controller 1+2 to 5+6
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
188 if (controller == 4) tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
189 if (controller == 5) tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
190 if (controller == 4) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW));
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
191 if (controller == 5) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW));
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
192 } else {
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
193 // read speeds
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
194 if ((controller == 0) && (mode!=1)) tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
195 if ((controller == 1) && (mode!=1)) tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
196 if ((controller == 2) && (mode!=1)) tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
197 if ((controller == 3) && (mode!=1)) tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
198 if (controller == 0) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW));
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
199 if (controller == 1) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW));
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
200 if (controller == 2) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW));
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
201 if (controller == 3) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW));
52
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
202
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
203 if (controller < 4) {
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
204 if (tmp < slot[controller].speedminimum) tmp = slot[controller].speedminimum;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
205 if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 0; }
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
206 if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
207 tmp = tmp << 1;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
208 } else {
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
209 if ((mode == 0) && (tmp < slot[controller].speedminimum)) tmp = slot[controller].speedminimum;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
210 if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
211 tmp = tmp << 1;
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
212 if (trackchange || slot[controller].trackswitch) tmp |= (1<<5);
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
213 }
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
214 }
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
215
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
216 switch (controller) {
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
217 case 0:
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
218 if (trackchange != 0) {
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
219 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
220 if (mode == 0) LED(1,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
221 } else if (mode == 0) LED(1,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
222 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
223 case 1:
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
224 if (trackchange != 0) {
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
225 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
226 if (mode == 0) LED(2,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
227 } else if (mode == 0) LED(2,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
228 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
229 case 2:
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
230 if (trackchange != 0) {
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
231 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
232 if (mode == 0) LED(4,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
233 } else if (mode == 0) LED(4,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
234 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
235 case 3:
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
236 if (trackchange != 0) {
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
237 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
238 if (mode == 0) LED(5,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
239 } else if (mode == 0) LED(5,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
240 break;
52
6b26d7e3ecd0 simplified do_controller function
Malte Bayer <mbayer@neo-soft.org>
parents: 51
diff changeset
241 }
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
242
34
d12d095e3282 some code cleanup
Malte Bayer <mbayer@neo-soft.org>
parents: 33
diff changeset
243 tmp |= (0b1000000000 | (controller << 6));
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
244 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
245 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
246 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
247
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
248 uint8_t mirror( uint8_t n ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
249 n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
250 n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
251 n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
252 return n;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
253 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
254
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
255 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
256 // send program data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
257 uint16_t tmp;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
258 parameter = mirror(parameter);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
259 controller = mirror(controller);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
260 command = mirror(command);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
261 tmp = 0b1000000000000 | (parameter << 4) | command | (controller >> 5);
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
262 return insert_queue(tmp, 12);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
263 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
264
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
265 int do_active(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
266 // send controller active data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
267 uint16_t tmp = 0b10000000;
51
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
268 if ( (PIN(SW_PACECAR_PORT) & _BV(SW_PACECAR)) == 0 ) {
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
269 // map controller 1+2 to 5+6
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
270 if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10000101;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
271 if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10000011;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
272 } else {
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
273 if ((slot[0].speedminimum != 0) || ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b11000001;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
274 if ((slot[1].speedminimum != 0) || ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10100001;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
275 if ((slot[2].speedminimum != 0) || ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10010001;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
276 if ((slot[3].speedminimum != 0) || ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10001001;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
277 if (slot[4].speedminimum != 0) tmp |= 0b10000101;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
278 if (slot[5].speedminimum != 0) tmp |= 0b10000011;
ec9e8cc27012 car id setting for slot 5+6 working via pressing pacecar button
Malte Bayer <mbayer@neo-soft.org>
parents: 49
diff changeset
279 }
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
280 // todo: wenn Daten enpfangen wurden hier eine Quittierung senden anstatt dem Active Word
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
281
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
282 return insert_queue(tmp, 7);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
283 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
284
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
285 int do_pace_ghost(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
286 // send ghost and pacecar data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
287 // todo: at the moment, both disabled!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
288 uint16_t tmp = 0b1111100000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
289 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
290 // todo: PC, NH, TK, (KFR, FR)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
291
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
292 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
293 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
294
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
295 ISR ( TIMER1_COMPA_vect ) {
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
296 //PORTC ^= _BV(PC0); // DEBUG OUTPUT SYSTEM CLOCK
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
297
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
298 // trigger packet transfer:
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
299 if (sysclk_packettimer == 14) { // 15*500 = 7500 NS
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
300 transmit_len = transmit_len_next;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
301 sysclk_packettimer = 0;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
302 } else sysclk_packettimer++;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
303 // here is some more time to do something else...
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
304
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
305 // reset both car counters to overflow
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
306 car0_old = TIMER1_500NS;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
307 car1_old = TIMER1_500NS;
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
308
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
309 sysclk.value++; // increment 500ns timer
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
310 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
311
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
312 ISR ( TIMER2_COMP_vect ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
313 //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!!
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
314 // data packet timer 100µs pro bit...
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
315 if (transmit_len >= 0xFE) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
316 if (transmit_len != 0xFF) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
317 RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
318 transmit_len = 0xFF;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
319 transmit_buffer = transmit_buffer_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
320 transmit_buffer_queue = 0;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
321 transmit_len_next = transmit_len_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
322
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
323 // start the response receiver timer
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
324 // TODO: only on 8 timeslots, not on every transmission
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
325 // TODO: give slot number to timer - then store the transmission to 8 slots array
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
326 TCNT0 = TIMER0_250US;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
327 timer0_delay = TIMER0_2300NS;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
328 response = 0;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
329 response_len = 0;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
330 TIMSK |= _BV(TOIE0);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
331
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
332 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
333 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
334 uint16_t bit = (1<<(transmit_len & 0b01111111));
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
335 uint16_t clock;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
336 if ((transmit_len & 0b10000000) == 0) clock = 0; else clock = 0xffff;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
337 if ( ((transmit_buffer ^ clock) & bit) != 0 )
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
338 RAIL_POWER_PORT |= _BV(RAIL_POWER); else
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
339 RAIL_POWER_PORT &= ~_BV(RAIL_POWER);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
340 if ( (transmit_len & 0b10000000) == 0 ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
341 // block 0
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
342 //if (transmit_len == 0) transmit_len = 0xFF; else transmit_len |= 0b10000000; // set clock
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
343 transmit_len |= 0b10000000; // set clock
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
344 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
345 // block 1, output the current bit
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
346 transmit_len &= 0b01111111; // reset clock
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
347 //if (transmit_len != 0) transmit_len--; // next bit
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
348 if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
349 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
350 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
351 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
352
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
353
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
354 ISR ( TIMER0_OVF_vect ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
355 // TODO: last bit should be set by the sender, not from us!
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
356 TCNT0 = TIMER0_250US;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
357 if (timer0_delay == 0) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
358 RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // pull rails low
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
359 _delay_us(28); // wait some cycles
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
360 if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) { // check for logic zero
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
361 if (response == 0) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
362 // there is no start bit, so stop the timer and cancel response receiving
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
363 TIMSK &= ~_BV(TOIE0);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
364 } else {
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
365 // we received a bit (logic low)
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
366 response = response << 1;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
367 response_len++;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
368 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
369 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
370 // okay, we have logic high
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
371 response = response << 1;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
372 response |= 1;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
373 response_len++;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
374 }
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
375 if (response_len == 15) { // maximum response length reached
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
376 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
377 TIMSK &= ~_BV(TOIE0);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
378 } else {
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
379 _delay_us(20); // wait some cycles
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
380 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
381 }
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
382 } else timer0_delay--; // 2.3 ms delay not reached yet
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
383 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
384
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
385 ISR (INT0_vect) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
386 // car0 detector
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
387 uint16_t tmp = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
388 car0_new = TCNT1; // get current counter
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
389 if (car0_old < car0_new) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
390 // calculate difference
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
391 if (car0 == 0) tmp = car0_new-car0_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
392 if ( (tmp > 54) && (tmp < 74) ) car0 = 1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
393 if ( (tmp > 118) && (tmp < 138) ) car0 = 2;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
394 if ( (tmp > 186) && (tmp < 206) ) car0 = 3;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
395 if ( (tmp > 246) && (tmp < 266) ) car0 = 4;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
396 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
397 car0_old = car0_new;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
398 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
399
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
400 ISR (INT1_vect) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
401 // car1 detector
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
402 uint16_t tmp = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
403 car1_new = TCNT1; // get current counter
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
404 if (car1_old < car1_new) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
405 // calculate difference
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
406 if (car1 == 0) tmp = car1_new-car1_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
407 if ( (tmp > 54) && (tmp < 74) ) car1 = 1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
408 if ( (tmp > 118) && (tmp < 138) ) car1 = 2;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
409 if ( (tmp > 186) && (tmp < 206) ) car1 = 3;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
410 if ( (tmp > 246) && (tmp < 266) ) car1 = 4;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
411 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
412 car1_old = car1_new;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
413 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
414
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
415
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
416 ISR (INT2_vect) {
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
417 // Lap counter Interrupt
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
418 // do not know if this ever occurs ?! this is normally an output pin to trigger the counter start
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
419 }
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
420
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
421 void reset_vars(void) {
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
422 uint8_t i;
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
423 for (i=0; i<MAX_SLOTS; i++) {
48
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
424 slot[i].speedlimit = 15;
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
425 slot[i].speedminimum = 0;
5bba01aad0a6 memsave and implemented minimum slot speed for virtual cars
Malte Bayer <mbayer@neo-soft.org>
parents: 47
diff changeset
426 slot[i].trackswitch = 0;
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
427 slot[i].fuel = 100;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
428 slot[i].jumpstart_time = 0;
47
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
429 slot[i].laps = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
430 }
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
431 sysclk.value = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
432 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
433
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
434 void countdown_progress(void) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
435 // decrement COUNTDOWN_LOOPS
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
436 if (countdown_loops>0) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
437 countdown_loops--;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
438 } else {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
439 countdown--;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
440 countdown_loops = COUNTDOWN_DELAY;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
441 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
442 switch (countdown) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
443 case 5: LED(1, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
444 case 4: LED(2, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
445 case 3: LED(3, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
446 case 2: LED(4, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
447 case 1: LED(5, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
448 case 0: { // RACE START!
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
449 sysclk.value = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
450 LEDS_OFF();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
451 LED(3, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
452 mode = 3;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
453 } break;
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
454 }
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
455 }
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
456
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
457 void check_cars(void) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
458 u32 clk, diff;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
459 clk.value = sysclk.value; // freeze system clock time
44
4f4326794ba4 implemented lap time, there is some bug in 32bit substraction yet
Malte Bayer <mbayer@neo-soft.org>
parents: 43
diff changeset
460
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
461 if (car0 != car0_state) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
462 car0_state = car0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
463 if (car0_state != 0) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
464 diff.value = clk.value - slot[car0-1].lap_time_start.value;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
465 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap!
47
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
466 if (slot[car0-1].lap_time_start.value != 0) {
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
467 slot[car0-1].lap_time.value = diff.value;
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
468 slot[car0-1].laps++;
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
469 RS232_putc('L');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
470 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
471 RS232_putc('A');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
472 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
473 itoa(slot[car0-1].laps, s, 16);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
474 RS232_puts(s);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
475 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
476 RS232_putc('0'+car0_state);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
477 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
478 ultoa(diff.value, s, 16);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
479 RS232_puts(s);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
480 RS232_putc('\n');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
481 }
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
482 slot[car0-1].lap_time_start.value = clk.value;
44
4f4326794ba4 implemented lap time, there is some bug in 32bit substraction yet
Malte Bayer <mbayer@neo-soft.org>
parents: 43
diff changeset
483 }
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
484 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
485 } car0 = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
486
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
487 if (car1 != car1_state) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
488 car1_state = car1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
489 if (car1_state != 0) {
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
490 diff.value = clk.value - slot[car1-1].lap_time_start.value;
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
491 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap!
47
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
492 if (slot[car1-1].lap_time_start.value != 0) {
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
493 slot[car1-1].lap_time.value = diff.value;
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
494 slot[car1-1].laps++;
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
495 RS232_putc('L');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
496 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
497 RS232_putc('B');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
498 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
499 itoa(slot[car1-1].laps, s, 16);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
500 RS232_puts(s);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
501 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
502 RS232_putc('0'+car1_state);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
503 RS232_putc(':');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
504 ultoa(diff.value, s, 16);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
505 RS232_puts(s);
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
506 RS232_putc('\n');
34ac9f92bc1e implemented lap counter, doesnt count first sensor touch
Malte Bayer <mbayer@neo-soft.org>
parents: 46
diff changeset
507 }
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
508 slot[car1-1].lap_time_start.value = clk.value;
44
4f4326794ba4 implemented lap time, there is some bug in 32bit substraction yet
Malte Bayer <mbayer@neo-soft.org>
parents: 43
diff changeset
509 }
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
510 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
511 } car1 = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
512 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
513
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
514
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
515 int main(void)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
516 {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
517
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
518 uint8_t packet_index = 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
519
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
520 uint8_t btn_start = _BV(SW_START);
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
521 uint8_t old_start = btn_start;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
522
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
523
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
524 init_hardware();
36
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
525 reset_vars();
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
526 LED(3, 1); // enable middle led == idle mode
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
527
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
528 // switch on rails power
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
529 RAIL_POWER_PORT |= _BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
530
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
531 while (1) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
532 // check for short circuit on the rails
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
533 check_rails_shortcut();
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
534 check_cars();
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
535
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
536 if (response_len > 0) {
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
537 itoa(response, s, 2);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
538 response_len = 0;
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
539 RS232_puts("ANSWER RX: ");
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
540 RS232_puts(s);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
541 RS232_putc('\n');
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
542 }
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
543
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
544
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
545 // read in button presses
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
546 btn_start = (PIN(SW_START_PORT) & _BV(SW_START));
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
547 if (old_start != btn_start) {
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
548 // start button changed
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
549 if (btn_start == 0) {
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
550 // start button press active
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
551 if (mode == 0) {
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
552 mode = 1; // set wait for race start mode
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
553 reset_vars();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
554 LED(1, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
555 LED(2, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
556 LED(3, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
557 LED(4, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
558 LED(5, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
559 RS232_puts_p(prepare);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
560 } else if (mode == 1) {
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
561 // Initiate race countdown
46
d200e3d8a550 corrected lap times, packed data struct for each slotcar
Malte Bayer <mbayer@neo-soft.org>
parents: 45
diff changeset
562 sysclk.value = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
563 countdown = 5;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
564 countdown_loops = COUNTDOWN_DELAY;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
565 mode = 2;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
566 LED(1, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
567 LED(2, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
568 LED(3, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
569 LED(4, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
570 LED(5, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
571 RS232_puts_p(countdownstart);
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
572 }
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
573 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
574 old_start = btn_start;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
575 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
576
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
577 if (mode==3) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
578 // RACE START!
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
579 // issue reset command to lap counter
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
580 mode = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
581 RS232_puts_p(racestart);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
582 program_command = 6;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
583 program_parameter = 9;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
584 program_id = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
585 program_count = 1;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
586 LAP_COUNTER_PORT |= _BV(LAP_COUNTER);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
587 // TODO: beep long
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
588 _delay_us(50);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
589 LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
590 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
591
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
592
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
593 switch (packet_index) {
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
594 case 1:
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
595 if (program_count > 0) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
596 // command in queue
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
597 if (do_program(program_id, program_command, program_parameter)) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
598 packet_index++;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
599 program_count--;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
600 }
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
601 } else {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
602 // output idle command
11
69c2a1408619 done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents: 10
diff changeset
603 if (do_program(7, 19, 0)) packet_index++; // reset
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
604 //if (do_program(7, 20, 15)) packet_index++; // reset / pitstop detect
11
69c2a1408619 done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents: 10
diff changeset
605 //if (insert_queue(0, 0)) packet_index++; // null command
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
606 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
607
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
608 if ( (packet_index>1) && (mode == 2) ) countdown_progress();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
609
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
610 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
611 case 2:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
612 if (do_pace_ghost()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
613 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
614 case 3:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
615 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
616 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
617 case 4:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
618 if (do_controller(0)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
619 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
620 case 5:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
621 if (do_controller(4)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
622 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
623 case 6:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
624 if (do_controller(1)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
625 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
626 case 7:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
627 if (do_controller(5)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
628 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
629 case 8:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
630 if (do_controller(2)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
631 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
632 case 9:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
633 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
634 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
635 case 10:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
636 if (do_controller(3)) packet_index = 1;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
637 // last packet, so reset packet index
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
638 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
639 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
640
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
641
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
642 } // main loop end
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
643 };
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
644

mercurial