Rev 459 | Rev 466 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
321 | cascade | 1 | /**************************************************************************** |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
||
3 | * admiralcascade@gmail.com * |
||
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
||
5 | * * |
||
6 | * This program is free software; you can redistribute it and/or modify * |
||
7 | * it under the terms of the GNU General Public License as published by * |
||
8 | * the Free Software Foundation; either version 2 of the License. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | * * |
||
20 | * * |
||
21 | * Credits to: * |
||
22 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project * |
||
23 | * Gregor "killagreg" Stobrawa for making the MK code readable * |
||
24 | * Klaus "akku" Buettner for the hardware * |
||
25 | * Manuel "KeyOz" Schrape for explaining the MK protocol to me * |
||
26 | ****************************************************************************/ |
||
27 | |||
28 | #include <avr/io.h> |
||
29 | #include <avr/interrupt.h> |
||
30 | #include <util/delay.h> |
||
455 | cascade | 31 | #include <avr/pgmspace.h> |
346 | cascade | 32 | #include "main.h" |
33 | #include "max7456_software_spi.h" |
||
331 | cascade | 34 | #include "usart1.h" |
389 | cascade | 35 | #include "osd_helpers.h" |
321 | cascade | 36 | |
37 | /* TODO: |
||
38 | * - verifiy correctness of values |
||
324 | cascade | 39 | * - clean up code :) |
321 | cascade | 40 | */ |
41 | |||
329 | cascade | 42 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
346 | cascade | 43 | // data structs not needed for character flashin |
331 | cascade | 44 | #include "mk-data-structs.h" |
321 | cascade | 45 | |
46 | /* ########################################################################## |
||
47 | * global definitions and global vars |
||
48 | * ##########################################################################*/ |
||
346 | cascade | 49 | |
321 | cascade | 50 | volatile uint16_t setsReceived = 0; |
51 | |||
52 | volatile NaviData_t naviData; |
||
53 | volatile DebugOut_t debugData; |
||
54 | |||
55 | // cache old vars for blinking attribute, checkup is faster than full |
||
56 | // attribute write each time |
||
57 | volatile uint8_t last_UBat = 255; |
||
58 | volatile uint8_t last_RC_Quality = 255; |
||
59 | |||
60 | // 16bit should be enough, normal LiPos don't last that long |
||
61 | volatile uint16_t uptime = 0; |
||
62 | volatile uint16_t timer = 0; |
||
465 | cascade | 63 | volatile uint8_t short_timer = 0; |
321 | cascade | 64 | |
331 | cascade | 65 | // remember last time data was received |
66 | volatile uint8_t seconds_since_last_data = 0; |
||
67 | |||
346 | cascade | 68 | // store stats description in progmem to save space |
69 | char stats_item_0[] PROGMEM = "max Altitude:"; |
||
70 | char stats_item_1[] PROGMEM = "max Speed :"; |
||
71 | char stats_item_2[] PROGMEM = "max Distance:"; |
||
72 | char stats_item_3[] PROGMEM = "min voltage :"; |
||
73 | char stats_item_4[] PROGMEM = "max time :"; |
||
412 | cascade | 74 | char stats_item_5[] PROGMEM = "longitude :"; |
75 | char stats_item_6[] PROGMEM = "latitude :"; |
||
455 | cascade | 76 | char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
77 | stats_item_3, stats_item_4, stats_item_5, stats_item_6}; |
||
346 | cascade | 78 | |
79 | // store more fixed strings in progmen |
||
379 | cascade | 80 | char ON[] PROGMEM = "ON "; |
346 | cascade | 81 | char OFF[] PROGMEM = "OFF"; |
82 | |||
329 | cascade | 83 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
326 | cascade | 84 | |
85 | // general PAL|NTSC distingiusch stuff |
||
86 | uint8_t top_line = 1; |
||
87 | uint8_t bottom_line = 14; |
||
88 | |||
453 | cascade | 89 | // battery voltages |
90 | uint8_t min_voltage = 0; |
||
91 | uint8_t max_voltage = 0; |
||
92 | |||
326 | cascade | 93 | // Flags |
465 | cascade | 94 | uint8_t COSD_FLAGS = 0, COSD_FLAGS2; |
326 | cascade | 95 | |
321 | cascade | 96 | /* ########################################################################## |
326 | cascade | 97 | * debounce buttons |
98 | * ##########################################################################*/ |
||
99 | int s1_pressed() { |
||
455 | cascade | 100 | if (S1_PRESSED) { |
101 | _delay_ms(25); |
||
102 | if (S1_PRESSED) return 1; |
||
103 | } |
||
104 | return 0; |
||
326 | cascade | 105 | } |
106 | |||
107 | int s2_pressed() { |
||
455 | cascade | 108 | if (S2_PRESSED) { |
109 | _delay_ms(25); |
||
110 | if (S2_PRESSED) return 1; |
||
111 | } |
||
112 | return 0; |
||
326 | cascade | 113 | } |
114 | |||
389 | cascade | 115 | /* ########################################################################## |
116 | * Interrupt handler |
||
117 | * ##########################################################################*/ |
||
455 | cascade | 118 | |
321 | cascade | 119 | /** |
389 | cascade | 120 | * handler for undefined Interrupts |
121 | * if not defined AVR will reset in case any unhandled interrupts occur |
||
321 | cascade | 122 | */ |
389 | cascade | 123 | ISR(__vector_default) { |
455 | cascade | 124 | asm("nop"); |
389 | cascade | 125 | } |
321 | cascade | 126 | |
389 | cascade | 127 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 128 | /* ########################################################################## |
129 | * timer stuff |
||
130 | * ##########################################################################*/ |
||
455 | cascade | 131 | |
465 | cascade | 132 | |
133 | /* ########################################################################## |
||
134 | * STROM TEST |
||
135 | * ##########################################################################*/ |
||
136 | #define INT0_HIGH PORTD |= (1 << PD2); |
||
137 | #define INT0_LOW PORTD &= ~(1 << PD2); |
||
138 | |||
139 | void SpiMasterInit(void) |
||
140 | { |
||
141 | volatile char IOReg; |
||
142 | // set PB4(/SS), PB5(MOSI), PB7(SCK) as output |
||
143 | DDRB = (1<<PB4)|(1<<PB5)|(1<<PB7); |
||
144 | PORTB |= (1 << PB4); // pullup SS |
||
145 | // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128 |
||
146 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1); |
||
147 | IOReg = SPSR; // clear SPIF bit in SPSR |
||
148 | IOReg = SPDR; |
||
149 | //sei(); // we do it later |
||
150 | } |
||
151 | |||
152 | volatile size_t icnt = 0; |
||
153 | volatile unsigned char * iptr; |
||
154 | volatile unsigned char spi_cmd[5]; |
||
155 | volatile uint8_t spi_ready = 1; |
||
156 | int16_t ampere = 0; |
||
157 | |||
158 | ISR(SPI_STC_vect) |
||
159 | { |
||
160 | /* Empfangenes Byte im Puffer ablegen. */ |
||
161 | *iptr++ = SPDR; |
||
162 | /* Zähler dekrementieren. */ |
||
163 | icnt--; |
||
164 | /* Falls Zähler noch nicht 0 ist... */ |
||
165 | if (icnt) { |
||
166 | ///_delay_ms(1); |
||
167 | /* ...nächstes Byte senden. */ |
||
168 | //SPDR = *iptr; |
||
169 | spi_ready = 1; |
||
170 | } |
||
171 | else { |
||
172 | /* ...andernfalls Interrupt Modus deaktivieren. */ |
||
173 | SPCR &= ~_BV(SPIE); |
||
174 | INT0_HIGH |
||
175 | //_delay_ms(1); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | int TransferIsBusy(void) |
||
180 | { |
||
181 | return SPCR & _BV(SPIE); |
||
182 | } |
||
183 | |||
184 | void StartTransfer(unsigned char *data, size_t len) |
||
185 | { |
||
186 | INT0_LOW |
||
187 | /* Interrupt Datenpointer und Zähler setzen. */ |
||
188 | iptr = data; |
||
189 | icnt = len; |
||
190 | |||
191 | /* SPI Interrupt aktivieren und Transfer anstossen. */ |
||
192 | SPCR |= _BV(SPIE); |
||
193 | SPDR = *iptr; |
||
194 | } |
||
195 | |||
196 | |||
197 | /* ########################################################################## |
||
198 | * / STROM TEST END |
||
199 | * ##########################################################################*/ |
||
200 | |||
201 | |||
331 | cascade | 202 | /** |
203 | * timer kicks in every 1000uS ^= 1ms |
||
321 | cascade | 204 | */ |
205 | ISR(TIMER0_OVF_vect) { |
||
385 | cascade | 206 | OCR0 = 15; // preload |
321 | cascade | 207 | if (!timer--) { |
208 | uptime++; |
||
209 | timer = 999; |
||
455 | cascade | 210 | seconds_since_last_data++; |
321 | cascade | 211 | } |
465 | cascade | 212 | short_timer++; |
213 | if (spi_ready && icnt) { |
||
214 | SPDR = *iptr; |
||
215 | } |
||
321 | cascade | 216 | } |
217 | |||
218 | /* ########################################################################## |
||
326 | cascade | 219 | * A simple config menu for the flags |
220 | * ##########################################################################*/ |
||
339 | cascade | 221 | |
222 | /** |
||
223 | * helper function for menu updating |
||
224 | */ |
||
225 | void config_menu_drawings(uint8_t chosen) { |
||
226 | // clear prevoius _cursor_ |
||
383 | cascade | 227 | write_ascii_string(3, (chosen + 5) % 8, " "); |
339 | cascade | 228 | // draw current _cursor_ |
383 | cascade | 229 | write_ascii_string(3, chosen + 5, ">"); |
339 | cascade | 230 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
383 | cascade | 231 | write_ascii_string_pgm(23, 5, ON); |
232 | } else { |
||
455 | cascade | 233 | write_ascii_string_pgm(23, 5, OFF); |
383 | cascade | 234 | } |
235 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
||
346 | cascade | 236 | write_ascii_string_pgm(23, 6, ON); |
339 | cascade | 237 | } else { |
383 | cascade | 238 | write_ascii_string_pgm(23, 6, OFF); |
339 | cascade | 239 | } |
455 | cascade | 240 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
346 | cascade | 241 | write_ascii_string_pgm(23, 7, ON); |
339 | cascade | 242 | } else { |
455 | cascade | 243 | write_ascii_string_pgm(23, 7, OFF); |
339 | cascade | 244 | } |
455 | cascade | 245 | if (COSD_FLAGS & COSD_FLAG_STATS) { |
346 | cascade | 246 | write_ascii_string_pgm(23, 8, ON); |
339 | cascade | 247 | } else { |
346 | cascade | 248 | write_ascii_string_pgm(23, 8, OFF); |
339 | cascade | 249 | } |
250 | if (COSD_FLAGS & COSD_FLAG_WARNINGS) { |
||
346 | cascade | 251 | write_ascii_string_pgm(23, 9, ON); |
339 | cascade | 252 | } else { |
346 | cascade | 253 | write_ascii_string_pgm(23, 9, OFF); |
339 | cascade | 254 | } |
255 | } |
||
256 | |||
257 | /** |
||
346 | cascade | 258 | * some sort of clicking response in the menu |
259 | */ |
||
260 | void config_menu_doclick(uint8_t chosen, char** menu) { |
||
455 | cascade | 261 | write_ascii_string(4, chosen + 5, "DONE "); |
262 | _delay_ms(1000); |
||
263 | write_ascii_string(4, chosen + 5, menu[chosen]); |
||
346 | cascade | 264 | } |
265 | |||
266 | /** |
||
339 | cascade | 267 | * a simple config menu tryout |
268 | */ |
||
326 | cascade | 269 | void config_menu(void) { |
455 | cascade | 270 | // disable interrupts (makes the menu more smoothely) |
271 | cli(); |
||
321 | cascade | 272 | |
455 | cascade | 273 | // clear screen |
274 | clear(); |
||
326 | cascade | 275 | |
455 | cascade | 276 | char* menu[9] = {"Full HUD", |
277 | "Art.Horizon in HUD", |
||
278 | "Big Vario bar", |
||
279 | "Statistics", |
||
280 | "Warnings", // TODO: do it! |
||
281 | "Reset uptime", |
||
282 | "Request OSD-data", |
||
283 | "Disable Debug-data", |
||
284 | "EXIT"}; |
||
326 | cascade | 285 | |
455 | cascade | 286 | uint8_t inmenu = 1; |
287 | uint8_t chosen = 0; |
||
288 | write_ascii_string(6, 2, "C-OSD Config Menu"); |
||
326 | cascade | 289 | |
455 | cascade | 290 | // wait a bit before doing stuff so user has chance to release button |
291 | _delay_ms(250); |
||
339 | cascade | 292 | |
455 | cascade | 293 | write_ascii_string(4, 5, menu[0]); |
294 | write_ascii_string(4, 6, menu[1]); |
||
295 | write_ascii_string(4, 7, menu[2]); |
||
296 | write_ascii_string(4, 8, menu[3]); |
||
297 | write_ascii_string(4, 9, menu[4]); |
||
298 | write_ascii_string(4, 10, menu[5]); |
||
299 | write_ascii_string(4, 11, menu[6]); |
||
300 | write_ascii_string(4, 12, menu[7]); |
||
301 | write_ascii_string(4, 13, menu[8]); |
||
339 | cascade | 302 | |
455 | cascade | 303 | config_menu_drawings(chosen); |
304 | |||
305 | while (inmenu) { |
||
306 | if (s2_pressed()) { |
||
307 | write_ascii_string(3, chosen + 5, " "); |
||
308 | chosen = (chosen + 1) % 9; |
||
309 | write_ascii_string(3, chosen + 5, ">"); |
||
310 | _delay_ms(500); |
||
311 | } else if (s1_pressed()) { |
||
312 | switch (chosen) { |
||
313 | case 0: // full HUD |
||
314 | COSD_FLAGS ^= COSD_FLAG_HUD; |
||
315 | config_menu_drawings(chosen); |
||
316 | break; |
||
317 | case 1: // art horizon |
||
318 | COSD_FLAGS ^= COSD_FLAG_ARTHORIZON; |
||
319 | config_menu_drawings(chosen); |
||
320 | break; |
||
321 | case 2: // big vario |
||
322 | COSD_FLAGS ^= COSD_FLAG_BIGVARIO; |
||
323 | config_menu_drawings(chosen); |
||
324 | break; |
||
325 | case 3: // statistics |
||
326 | COSD_FLAGS ^= COSD_FLAG_STATS; |
||
327 | config_menu_drawings(chosen); |
||
328 | break; |
||
329 | case 4: // warnings |
||
330 | COSD_FLAGS ^= COSD_FLAG_WARNINGS; |
||
331 | config_menu_drawings(chosen); |
||
332 | break; |
||
333 | case 5: // reset uptime |
||
334 | uptime = 0; |
||
335 | config_menu_doclick(chosen, menu); |
||
336 | break; |
||
337 | case 6: // re-request OSD data |
||
454 | cascade | 338 | #if FCONLY |
455 | cascade | 339 | // request data ever 100ms from FC; |
340 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 341 | #else |
455 | cascade | 342 | // request OSD Data from NC every 100ms |
343 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 344 | |
455 | cascade | 345 | // and disable debug... |
346 | usart1_request_mk_data(0, 'd', 0); |
||
454 | cascade | 347 | #endif |
455 | cascade | 348 | config_menu_doclick(chosen, menu); |
349 | break; |
||
350 | case 7: // disable debug data |
||
351 | // disable sending of debug data |
||
352 | // may result in smoother ddata display |
||
353 | usart1_request_mk_data(0, 'd', 0); |
||
354 | config_menu_doclick(chosen, menu); |
||
355 | break; |
||
356 | case 8: // exit |
||
357 | inmenu = 0; |
||
358 | break; |
||
359 | } |
||
360 | _delay_ms(250); |
||
361 | } |
||
362 | } |
||
326 | cascade | 363 | |
455 | cascade | 364 | // clear screen up again |
365 | clear(); |
||
326 | cascade | 366 | |
455 | cascade | 367 | // update flags to paint display again if needed |
368 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
326 | cascade | 369 | |
455 | cascade | 370 | // enable interrupts again |
371 | sei(); |
||
326 | cascade | 372 | } |
373 | |||
455 | cascade | 374 | /** |
375 | * auto config some stuff on startup, currently only battery cells |
||
376 | * TODO: this is testing stuff, strings should go progmem and so on... |
||
377 | */ |
||
459 | cascade | 378 | void auto_config(uint8_t UBat) { |
455 | cascade | 379 | clear(); |
380 | write_ascii_string(2, 2, "C-OSD Initialisation"); |
||
381 | #if FCONLY |
||
382 | write_ascii_string(2, 3, "FC only Mode"); |
||
383 | #else |
||
384 | write_ascii_string(2, 3, "NaviCtrl Mode"); |
||
385 | #endif |
||
459 | cascade | 386 | write_ascii_string(2, 4, BUILDDATE); |
455 | cascade | 387 | uint8_t cellnum = 0; |
388 | if (CELL_NUM == -1) { |
||
459 | cascade | 389 | write_ascii_string(2, 6, "Guessing Number of Cells"); |
455 | cascade | 390 | do { |
391 | cellnum++; |
||
458 | cascade | 392 | } while (UBat > ((cellnum * CELL_VOLT_MAX) + 15)); |
455 | cascade | 393 | } else { |
394 | cellnum = CELL_NUM; |
||
395 | } |
||
396 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
397 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
398 | write_ascii_string(2, 7, "Number of Cells:"); |
||
399 | write_ndigit_number_u(21, 7, cellnum, 1, 0); |
||
400 | write_ascii_string(2, 8, "Warn Voltage :"); |
||
401 | write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0); |
||
402 | write_ascii_string(2, 9, "Max Voltage :"); |
||
403 | write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0); |
||
404 | _delay_ms(200); |
||
405 | clear(); |
||
406 | // update flags to paint display again because of clear |
||
407 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
408 | } |
||
409 | |||
465 | cascade | 410 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
455 | cascade | 411 | |
321 | cascade | 412 | /* ########################################################################## |
413 | * MAIN |
||
414 | * ##########################################################################*/ |
||
415 | int main(void) { |
||
465 | cascade | 416 | |
339 | cascade | 417 | // set up FLAGS, compiler should flatten this one |
418 | COSD_FLAGS = (NTSC << 0); |
||
419 | COSD_FLAGS |= (HUD << 1); |
||
420 | COSD_FLAGS |= (ARTHORIZON << 2); |
||
455 | cascade | 421 | COSD_FLAGS |= (BIGVARIO << 3); |
422 | COSD_FLAGS |= (STATS << 4); |
||
383 | cascade | 423 | COSD_FLAGS |= (WARNINGS << 5); |
324 | cascade | 424 | |
339 | cascade | 425 | // set up Atmega162 Ports |
321 | cascade | 426 | DDRA |= (1 << PA1); // PA1 output (/CS) |
427 | MAX_CS_HIGH |
||
428 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
||
429 | MAX_SDIN_LOW |
||
430 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
||
431 | MAX_SCLK_LOW |
||
432 | DDRA |= (1 << PA5); // PA5 output (RESET) |
||
433 | MAX_RESET_HIGH |
||
434 | |||
435 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
||
436 | LED1_OFF |
||
437 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
||
438 | LED2_OFF |
||
439 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
||
440 | LED3_OFF |
||
441 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
||
442 | LED4_OFF |
||
443 | |||
444 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
||
445 | PORTC |= (1 << PC4); // pullup |
||
446 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
||
447 | PORTC |= (1 << PC5); // pullup |
||
448 | |||
339 | cascade | 449 | // set up top and bottom lines |
450 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
451 | bottom_line = 12; |
||
452 | } else { |
||
453 | bottom_line = 14; |
||
454 | } |
||
326 | cascade | 455 | |
339 | cascade | 456 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 457 | MAX_RESET_LOW |
458 | MAX_RESET_HIGH |
||
459 | |||
460 | // give the FC/NC and the maxim time to come up |
||
461 | LED4_ON |
||
462 | _delay_ms(2000); |
||
463 | |||
464 | LED4_OFF |
||
465 | |||
331 | cascade | 466 | |
339 | cascade | 467 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 468 | #if (WRITECHARS != -1) |
339 | cascade | 469 | // DISABLE display (VM0) |
321 | cascade | 470 | spi_send_byte(0x00, 0b00000000); |
455 | cascade | 471 | #include "characters.c" |
472 | #endif |
||
321 | cascade | 473 | |
339 | cascade | 474 | // Setup Video Mode |
475 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
476 | // NTSC + enable display immediately (VM0) |
||
477 | spi_send_byte(0x00, 0b00001000); |
||
478 | } else { |
||
479 | // PAL + enable display immediately (VM0) |
||
480 | spi_send_byte(0x00, 0b01001000); |
||
481 | } |
||
321 | cascade | 482 | |
404 | cascade | 483 | /*// clear all display-mem (DMM) |
403 | cascade | 484 | spi_send_byte(0x04, 0b00000100); |
485 | |||
486 | // clearing takes 12uS according to maxim so lets wait longer |
||
487 | _delay_us(120); |
||
488 | |||
321 | cascade | 489 | // 8bit mode |
404 | cascade | 490 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 491 | |
404 | cascade | 492 | // clear display memory and set to 8bit mode |
321 | cascade | 493 | clear(); |
494 | |||
329 | cascade | 495 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 496 | // init usart |
497 | usart1_init(); |
||
498 | |||
499 | // set up timer |
||
500 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
||
385 | cascade | 501 | OCR0 = 15; // preload |
321 | cascade | 502 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
503 | |||
465 | cascade | 504 | DDRD |= (1 << PD2); // PD2 output (INT0) |
505 | SpiMasterInit(); |
||
506 | |||
326 | cascade | 507 | // enable interrupts |
321 | cascade | 508 | sei(); |
465 | cascade | 509 | |
321 | cascade | 510 | #endif |
511 | |||
512 | //write_ascii_string(2, 7, " CaScAdE "); |
||
513 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
514 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
515 | |||
516 | // we are ready |
||
517 | LED3_ON |
||
518 | |||
329 | cascade | 519 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
455 | cascade | 520 | clear(); |
321 | cascade | 521 | write_all_chars(); |
522 | #else |
||
455 | cascade | 523 | // clear serial screen |
524 | //usart1_puts("\x1B[2J\x1B[H"); |
||
525 | //usart1_puts("hello world!\r\n"); |
||
454 | cascade | 526 | #if FCONLY |
455 | cascade | 527 | // request data ever 100ms from FC; |
528 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 529 | #else |
455 | cascade | 530 | // request OSD Data from NC every 100ms |
531 | usart1_request_mk_data(1, 'o', 100); |
||
346 | cascade | 532 | |
339 | cascade | 533 | // and disable debug... |
455 | cascade | 534 | usart1_request_mk_data(0, 'd', 0); |
454 | cascade | 535 | #endif |
321 | cascade | 536 | |
339 | cascade | 537 | // stats for after flight |
538 | int16_t max_Altimeter = 0; |
||
459 | cascade | 539 | uint8_t min_UBat = 255; |
540 | #if !(FCONLY) |
||
541 | uint16_t max_GroundSpeed = 0; |
||
339 | cascade | 542 | int16_t max_Distance = 0; |
543 | uint16_t max_FlyingTime = 0; |
||
321 | cascade | 544 | |
339 | cascade | 545 | // flags from last round to check for changes |
546 | uint8_t old_MKFlags = 0; |
||
547 | |||
321 | cascade | 548 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
377 | cascade | 549 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
459 | cascade | 550 | #endif |
321 | cascade | 551 | |
465 | cascade | 552 | |
553 | |||
321 | cascade | 554 | while (1) { |
465 | cascade | 555 | if (!icnt && spi_ready) { |
556 | short_timer = 0; |
||
557 | ampere = spi_cmd[1] << 8; |
||
558 | ampere |= spi_cmd[2]; |
||
559 | //write_ascii_char(7+4*30, spi_cmd[0]); |
||
560 | |||
561 | if (spi_cmd[0] == 'd') { |
||
562 | COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
||
563 | //write_ascii_char(8+4*30, 'v'); // valid |
||
564 | } else { |
||
565 | COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
||
566 | //write_ascii_char(8+4*30, 'i'); // invalid |
||
567 | } |
||
568 | spi_cmd[0] = 'A'; |
||
569 | spi_cmd[1] = 'B'; |
||
570 | spi_cmd[2] = 'C'; |
||
571 | StartTransfer((unsigned char*)spi_cmd, 3); |
||
572 | } |
||
321 | cascade | 573 | if (rxd_buffer_locked) { |
339 | cascade | 574 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
454 | cascade | 575 | #if FCONLY |
339 | cascade | 576 | if (rxd_buffer[2] == 'D') { // FC Data |
454 | cascade | 577 | Decode64(); |
339 | cascade | 578 | debugData = *((DebugOut_t*) pRxData); |
454 | cascade | 579 | |
455 | cascade | 580 | // init on first data retrival, distinguished by last battery :) |
581 | if (last_UBat == 255) { |
||
459 | cascade | 582 | // fix for min_UBat |
583 | min_UBat = debugData.Analog[9]; |
||
584 | auto_config(debugData.Analog[9]); |
||
455 | cascade | 585 | } |
459 | cascade | 586 | #include "osd_fcmode_default.c" |
454 | cascade | 587 | } |
588 | #else |
||
459 | cascade | 589 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
590 | Decode64(); |
||
591 | naviData = *((NaviData_t*) pRxData); |
||
453 | cascade | 592 | |
459 | cascade | 593 | // init on first data retrival, distinguished by last battery :) |
594 | if (last_UBat == 255) { |
||
595 | // fix for min_UBat |
||
596 | min_UBat = naviData.UBat; |
||
597 | auto_config(naviData.UBat); |
||
598 | } |
||
599 | #include "osd_ncmode_default.c" |
||
600 | } |
||
454 | cascade | 601 | #endif |
339 | cascade | 602 | } |
321 | cascade | 603 | rxd_buffer_locked = 0; |
604 | } |
||
605 | // handle keypress |
||
326 | cascade | 606 | if (s1_pressed()) { |
339 | cascade | 607 | config_menu(); |
321 | cascade | 608 | } |
339 | cascade | 609 | if (seconds_since_last_data > 2) { |
454 | cascade | 610 | #if FCONLY |
455 | cascade | 611 | // request data ever 100ms from FC; |
612 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 613 | #else |
455 | cascade | 614 | // request OSD Data from NC every 100ms |
615 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 616 | |
455 | cascade | 617 | // and disable debug... |
618 | usart1_request_mk_data(0, 'd', 0); |
||
454 | cascade | 619 | #endif |
339 | cascade | 620 | } |
321 | cascade | 621 | } |
622 | #endif |
||
623 | return 0; |
||
624 | } |