blackbox/main.c

changeset 77
cede78304992
parent 75
0b38de31ad5d
child 81
4f2f94a043fd
equal deleted inserted replaced
76:1c8305764b3f 77:cede78304992
100 14 device type bit 2 100 14 device type bit 2
101 15 device type bit 3 101 15 device type bit 3
102 16 stopbit 102 16 stopbit
103 */ 103 */
104 104
105 /* PITLANE STATUS DEFINITION 105 /* PITLANE/TRACKSWITCH STATUS DEFINITION
106 1 = AA 106 1 = AA
107 2 = AB 107 2 = AB
108 3 = BB 108 3 = BB
109 4 = BA 109 4 = BA
110 5 = BC 110 5 = BC
111 6 = ZZ -> pitlane exit 111 6 = ZZ -> pitlane exit
112 */ 112 */
113 113
114 #define STARTSTOP 0b1000000000000001 114 #define STARTSTOP 0b1000000000000001
115 void decode_responsewire(void) { 115 void decode_responsewire(void) {
116 // ATTENTION: THIS IS CALLED IN AN INTERRUPT, KEEP AS SHORT AS POSSIBLE
117
116 uint16_t data = responsewire_data; 118 uint16_t data = responsewire_data;
117 // first check if start + stopbit are set 119 // first check if start + stopbit are set
118 // todo future: unsure but last bit doesnt get set?! 120 // todo future: unsure but last bit doesnt get set?!
119 /* 121 /*
120 if ((data & STARTSTOP) != STARTSTOP) { 122 if ((data & STARTSTOP) != STARTSTOP) {
121 RS232_puts("RW BAD\n"); 123 RS232_puts("RW BAD\n");
122 return; // incomplete response 124 return; // incomplete response
123 } 125 }
124 */ 126 */
125 // now extract the car id, track change status 127 // now extract the car id, track change status
126 uint8_t car = ((data >> 1) & 0b111); 128 data >>= 1;
127 uint8_t status = ((data >> 4) & 0b1111); 129 uint8_t car = (data & 0b111);
128 uint8_t sender = ((data >> 8) & 0b1111); 130 data >>= 3;
129 uint8_t type = ((data >> 12) & 0b111); 131 uint8_t status = (data & 0b1111);
132 data >>= 4;
133 uint8_t sender = (data & 0b1111);
134 data >>= 4;
135 uint8_t type = (data & 0b111);
130 if (type == 4) { 136 if (type == 4) {
131 // pitlane response 137 // pitlane response
132 if (status == 5) slot[car].canrefuel = 1; 138 if (status == 5) slot[car].canrefuel = 1;
133 if (status == 6) for (data=0; data<MAX_SLOTS; data++) slot[data].canrefuel = 0; 139 if (status == 6) for (data=0; data<MAX_SLOTS; data++) slot[data].canrefuel = 0;
134 } 140 }
135 RS232_puts("RW:"); 141 RS232_puts("RW:");
136 itoa(car, s, 16); 142 RS232_putc(car + '0');
137 RS232_puts(s);
138 RS232_putc(':'); 143 RS232_putc(':');
139 itoa(type, s, 16); 144 RS232_putc(type + '0');
140 RS232_puts(s);
141 RS232_putc(':'); 145 RS232_putc(':');
142 itoa(sender, s, 16); 146 itoa(sender, s, 16);
143 RS232_puts(s); 147 RS232_puts(s);
144 RS232_putc(':'); 148 RS232_putc(':');
145 itoa(status, s, 16); 149 itoa(status, s, 16);
150 RS232_puts(s);
151 RS232_putc(':');
152 ultoa(sysclk.value, s, 16);
146 RS232_puts(s); 153 RS232_puts(s);
147 RS232_putc('\n'); 154 RS232_putc('\n');
148 } 155 }
149 156
150 int insert_queue(uint16_t tmp, uint8_t len) { 157 int insert_queue(uint16_t tmp, uint8_t len) {
427 slot[i].jumpstart_time = 0; 434 slot[i].jumpstart_time = 0;
428 slot[i].laps = 0; 435 slot[i].laps = 0;
429 slot[i].seccnt = 0; 436 slot[i].seccnt = 0;
430 slot[i].accel = 15; // full acceleration per default - TODO 437 slot[i].accel = 15; // full acceleration per default - TODO
431 slot[i].canrefuel = 0; 438 slot[i].canrefuel = 0;
439 slot[i].lap_time_start.value = 0;
440 slot[i].lap_time.value = 0;
432 } 441 }
433 sysclk.value = 0; 442 sysclk.value = 0;
434 } 443 }
435 444
436 void countdown_progress(void) { 445 void countdown_progress(void) {
466 diff.value = clk.value - slot[car0-1].lap_time_start.value; 475 diff.value = clk.value - slot[car0-1].lap_time_start.value;
467 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap! 476 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap!
468 if (slot[car0-1].lap_time_start.value != 0) { 477 if (slot[car0-1].lap_time_start.value != 0) {
469 slot[car0-1].lap_time.value = diff.value; 478 slot[car0-1].lap_time.value = diff.value;
470 slot[car0-1].laps++; 479 slot[car0-1].laps++;
471 RS232_putc('L'); 480 RS232_puts("L:3:"); // 3 = BB
472 RS232_putc(':');
473 RS232_putc('B');
474 RS232_putc(':');
475 itoa(slot[car0-1].laps, s, 16); 481 itoa(slot[car0-1].laps, s, 16);
476 RS232_puts(s); 482 RS232_puts(s);
477 RS232_putc(':'); 483 RS232_putc(':');
478 RS232_putc('0'+car0_state); 484 RS232_putc('0'+car0_state);
479 RS232_putc(':'); 485 RS232_putc(':');
480 ultoa(diff.value, s, 16); 486 ultoa(diff.value, s, 16);
487 RS232_puts(s);
488 RS232_putc(':');
489 ultoa(clk.value, s, 16);
481 RS232_puts(s); 490 RS232_puts(s);
482 RS232_putc('\n'); 491 RS232_putc('\n');
483 } 492 }
484 slot[car0-1].lap_time_start.value = clk.value; 493 slot[car0-1].lap_time_start.value = clk.value;
485 } 494 }
492 diff.value = clk.value - slot[car1-1].lap_time_start.value; 501 diff.value = clk.value - slot[car1-1].lap_time_start.value;
493 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap! 502 if ( diff.value > 3000 ) { // minimum 1.5 second for 1 lap!
494 if (slot[car1-1].lap_time_start.value != 0) { 503 if (slot[car1-1].lap_time_start.value != 0) {
495 slot[car1-1].lap_time.value = diff.value; 504 slot[car1-1].lap_time.value = diff.value;
496 slot[car1-1].laps++; 505 slot[car1-1].laps++;
497 RS232_putc('L'); 506 RS232_puts("L:1:"); // 1 = AA
498 RS232_putc(':');
499 RS232_putc('A');
500 RS232_putc(':');
501 itoa(slot[car1-1].laps, s, 16); 507 itoa(slot[car1-1].laps, s, 16);
502 RS232_puts(s); 508 RS232_puts(s);
503 RS232_putc(':'); 509 RS232_putc(':');
504 RS232_putc('0'+car1_state); 510 RS232_putc('0'+car1_state);
505 RS232_putc(':'); 511 RS232_putc(':');
506 ultoa(diff.value, s, 16); 512 ultoa(diff.value, s, 16);
513 RS232_puts(s);
514 RS232_putc(':');
515 ultoa(clk.value, s, 16);
507 RS232_puts(s); 516 RS232_puts(s);
508 RS232_putc('\n'); 517 RS232_putc('\n');
509 } 518 }
510 slot[car1-1].lap_time_start.value = clk.value; 519 slot[car1-1].lap_time_start.value = clk.value;
511 } 520 }
522 RS232_putc(':'); 531 RS232_putc(':');
523 RS232_putc(idx + '0'); 532 RS232_putc(idx + '0');
524 RS232_putc(':'); 533 RS232_putc(':');
525 itoa(slot[idx].fuel, s, 16); 534 itoa(slot[idx].fuel, s, 16);
526 RS232_puts(s); 535 RS232_puts(s);
536 RS232_putc(':');
537 ultoa(sysclk.value, s, 16);
538 RS232_puts(s);
527 RS232_putc('\n'); 539 RS232_putc('\n');
528 540
529 slot[idx].seccnt = 0; 541 slot[idx].seccnt = 0;
530 } else slot[idx].seccnt++; 542 } else slot[idx].seccnt++;
531 } 543 }
552 check_cars(); 564 check_cars();
553 565
554 if (response_len > 0) { 566 if (response_len > 0) {
555 itoa(response, s, 2); 567 itoa(response, s, 2);
556 response_len = 0; 568 response_len = 0;
557 RS232_puts("ANSWER RX: "); 569 //RS232_puts("ANSWER RX: ");
558 RS232_puts(s); 570 //RS232_puts(s);
559 RS232_putc('\n'); 571 //RS232_putc('\n');
560 } 572 }
561 573
562 574
563 // read in button presses 575 // read in button presses
564 btn_start = (PIN(SW_START_PORT) & _BV(SW_START)); 576 btn_start = (PIN(SW_START_PORT) & _BV(SW_START));

mercurial