Rev 466 | Rev 468 | 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)) |
466 | cascade | 43 | // data structs not needed for character flashing |
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; |
||
63 | |||
331 | cascade | 64 | // remember last time data was received |
65 | volatile uint8_t seconds_since_last_data = 0; |
||
66 | |||
346 | cascade | 67 | // store stats description in progmem to save space |
68 | char stats_item_0[] PROGMEM = "max Altitude:"; |
||
69 | char stats_item_1[] PROGMEM = "max Speed :"; |
||
70 | char stats_item_2[] PROGMEM = "max Distance:"; |
||
71 | char stats_item_3[] PROGMEM = "min voltage :"; |
||
72 | char stats_item_4[] PROGMEM = "max time :"; |
||
412 | cascade | 73 | char stats_item_5[] PROGMEM = "longitude :"; |
74 | char stats_item_6[] PROGMEM = "latitude :"; |
||
467 | cascade | 75 | char stats_item_7[] PROGMEM = "max current :"; |
455 | cascade | 76 | char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
467 | cascade | 77 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
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 | |||
466 | cascade | 139 | void SpiMasterInit(void) { |
140 | volatile char IOReg; |
||
141 | // set PB4(/SS), PB5(MOSI), PB7(SCK) as output |
||
142 | DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7); |
||
143 | PORTB |= (1 << PB4); // pullup SS |
||
144 | // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128 |
||
145 | SPCR = (1 << SPIE) | (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1); |
||
146 | IOReg = SPSR; // clear SPIF bit in SPSR |
||
147 | IOReg = SPDR; |
||
148 | //sei(); // we do it later |
||
465 | cascade | 149 | } |
150 | |||
151 | volatile size_t icnt = 0; |
||
152 | volatile unsigned char * iptr; |
||
466 | cascade | 153 | volatile unsigned char spi_cmd_buffer[5]; |
465 | cascade | 154 | volatile uint8_t spi_ready = 1; |
467 | cascade | 155 | int16_t ampere = 0, max_ampere = 0; |
465 | cascade | 156 | |
466 | cascade | 157 | /** |
158 | * SPI interrupt handler |
||
159 | */ |
||
160 | ISR(SPI_STC_vect) { |
||
161 | *iptr++ = SPDR; // safe received byte to buffer |
||
162 | icnt--; // dec length |
||
465 | cascade | 163 | if (icnt) { |
466 | cascade | 164 | //SPDR = *iptr; // send next byte |
165 | spi_ready = 1; // we _should_ send later because the slave needs more time |
||
166 | } else { |
||
167 | SPCR &= ~_BV(SPIE); // deactivate interrupt |
||
168 | INT0_HIGH // transfer is done, slave does not need to listen |
||
465 | cascade | 169 | } |
170 | } |
||
171 | |||
466 | cascade | 172 | /** |
173 | * check if SPI transfer is still busy |
||
174 | */ |
||
175 | int TransferIsBusy(void) { |
||
465 | cascade | 176 | return SPCR & _BV(SPIE); |
177 | } |
||
178 | |||
466 | cascade | 179 | /** |
180 | * start a new transfer of <data> with length <len> |
||
181 | */ |
||
182 | void StartTransfer(unsigned char *data, size_t len) { |
||
183 | INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen |
||
184 | |||
185 | // set up pointer and length for interrupt handler |
||
465 | cascade | 186 | iptr = data; |
187 | icnt = len; |
||
466 | cascade | 188 | |
189 | SPCR |= _BV(SPIE); // enable spi interrupt |
||
190 | SPDR = *iptr; // start transfer by first bye |
||
465 | cascade | 191 | } |
192 | |||
193 | |||
194 | /* ########################################################################## |
||
195 | * / STROM TEST END |
||
196 | * ##########################################################################*/ |
||
197 | |||
331 | cascade | 198 | /** |
199 | * timer kicks in every 1000uS ^= 1ms |
||
321 | cascade | 200 | */ |
201 | ISR(TIMER0_OVF_vect) { |
||
385 | cascade | 202 | OCR0 = 15; // preload |
321 | cascade | 203 | if (!timer--) { |
204 | uptime++; |
||
205 | timer = 999; |
||
455 | cascade | 206 | seconds_since_last_data++; |
321 | cascade | 207 | } |
466 | cascade | 208 | // in case there is still some spi data to send do it now |
209 | if (spi_ready && icnt) { |
||
210 | SPDR = *iptr; |
||
211 | } |
||
321 | cascade | 212 | } |
213 | |||
214 | /* ########################################################################## |
||
326 | cascade | 215 | * A simple config menu for the flags |
216 | * ##########################################################################*/ |
||
339 | cascade | 217 | |
218 | /** |
||
219 | * helper function for menu updating |
||
220 | */ |
||
221 | void config_menu_drawings(uint8_t chosen) { |
||
222 | // clear prevoius _cursor_ |
||
383 | cascade | 223 | write_ascii_string(3, (chosen + 5) % 8, " "); |
339 | cascade | 224 | // draw current _cursor_ |
383 | cascade | 225 | write_ascii_string(3, chosen + 5, ">"); |
339 | cascade | 226 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
383 | cascade | 227 | write_ascii_string_pgm(23, 5, ON); |
228 | } else { |
||
455 | cascade | 229 | write_ascii_string_pgm(23, 5, OFF); |
383 | cascade | 230 | } |
231 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
||
346 | cascade | 232 | write_ascii_string_pgm(23, 6, ON); |
339 | cascade | 233 | } else { |
383 | cascade | 234 | write_ascii_string_pgm(23, 6, OFF); |
339 | cascade | 235 | } |
455 | cascade | 236 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
346 | cascade | 237 | write_ascii_string_pgm(23, 7, ON); |
339 | cascade | 238 | } else { |
455 | cascade | 239 | write_ascii_string_pgm(23, 7, OFF); |
339 | cascade | 240 | } |
455 | cascade | 241 | if (COSD_FLAGS & COSD_FLAG_STATS) { |
346 | cascade | 242 | write_ascii_string_pgm(23, 8, ON); |
339 | cascade | 243 | } else { |
346 | cascade | 244 | write_ascii_string_pgm(23, 8, OFF); |
339 | cascade | 245 | } |
246 | if (COSD_FLAGS & COSD_FLAG_WARNINGS) { |
||
346 | cascade | 247 | write_ascii_string_pgm(23, 9, ON); |
339 | cascade | 248 | } else { |
346 | cascade | 249 | write_ascii_string_pgm(23, 9, OFF); |
339 | cascade | 250 | } |
251 | } |
||
252 | |||
253 | /** |
||
346 | cascade | 254 | * some sort of clicking response in the menu |
255 | */ |
||
256 | void config_menu_doclick(uint8_t chosen, char** menu) { |
||
455 | cascade | 257 | write_ascii_string(4, chosen + 5, "DONE "); |
258 | _delay_ms(1000); |
||
259 | write_ascii_string(4, chosen + 5, menu[chosen]); |
||
346 | cascade | 260 | } |
261 | |||
262 | /** |
||
339 | cascade | 263 | * a simple config menu tryout |
264 | */ |
||
326 | cascade | 265 | void config_menu(void) { |
455 | cascade | 266 | // disable interrupts (makes the menu more smoothely) |
267 | cli(); |
||
321 | cascade | 268 | |
455 | cascade | 269 | // clear screen |
270 | clear(); |
||
326 | cascade | 271 | |
455 | cascade | 272 | char* menu[9] = {"Full HUD", |
273 | "Art.Horizon in HUD", |
||
274 | "Big Vario bar", |
||
275 | "Statistics", |
||
276 | "Warnings", // TODO: do it! |
||
277 | "Reset uptime", |
||
278 | "Request OSD-data", |
||
279 | "Disable Debug-data", |
||
280 | "EXIT"}; |
||
326 | cascade | 281 | |
455 | cascade | 282 | uint8_t inmenu = 1; |
283 | uint8_t chosen = 0; |
||
284 | write_ascii_string(6, 2, "C-OSD Config Menu"); |
||
326 | cascade | 285 | |
455 | cascade | 286 | // wait a bit before doing stuff so user has chance to release button |
287 | _delay_ms(250); |
||
339 | cascade | 288 | |
455 | cascade | 289 | write_ascii_string(4, 5, menu[0]); |
290 | write_ascii_string(4, 6, menu[1]); |
||
291 | write_ascii_string(4, 7, menu[2]); |
||
292 | write_ascii_string(4, 8, menu[3]); |
||
293 | write_ascii_string(4, 9, menu[4]); |
||
294 | write_ascii_string(4, 10, menu[5]); |
||
295 | write_ascii_string(4, 11, menu[6]); |
||
296 | write_ascii_string(4, 12, menu[7]); |
||
297 | write_ascii_string(4, 13, menu[8]); |
||
339 | cascade | 298 | |
455 | cascade | 299 | config_menu_drawings(chosen); |
300 | |||
301 | while (inmenu) { |
||
302 | if (s2_pressed()) { |
||
303 | write_ascii_string(3, chosen + 5, " "); |
||
304 | chosen = (chosen + 1) % 9; |
||
305 | write_ascii_string(3, chosen + 5, ">"); |
||
306 | _delay_ms(500); |
||
307 | } else if (s1_pressed()) { |
||
308 | switch (chosen) { |
||
309 | case 0: // full HUD |
||
310 | COSD_FLAGS ^= COSD_FLAG_HUD; |
||
311 | config_menu_drawings(chosen); |
||
312 | break; |
||
313 | case 1: // art horizon |
||
314 | COSD_FLAGS ^= COSD_FLAG_ARTHORIZON; |
||
315 | config_menu_drawings(chosen); |
||
316 | break; |
||
317 | case 2: // big vario |
||
318 | COSD_FLAGS ^= COSD_FLAG_BIGVARIO; |
||
319 | config_menu_drawings(chosen); |
||
320 | break; |
||
321 | case 3: // statistics |
||
322 | COSD_FLAGS ^= COSD_FLAG_STATS; |
||
323 | config_menu_drawings(chosen); |
||
324 | break; |
||
325 | case 4: // warnings |
||
326 | COSD_FLAGS ^= COSD_FLAG_WARNINGS; |
||
327 | config_menu_drawings(chosen); |
||
328 | break; |
||
329 | case 5: // reset uptime |
||
330 | uptime = 0; |
||
331 | config_menu_doclick(chosen, menu); |
||
332 | break; |
||
333 | case 6: // re-request OSD data |
||
454 | cascade | 334 | #if FCONLY |
455 | cascade | 335 | // request data ever 100ms from FC; |
336 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 337 | #else |
455 | cascade | 338 | // request OSD Data from NC every 100ms |
339 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 340 | |
455 | cascade | 341 | // and disable debug... |
342 | usart1_request_mk_data(0, 'd', 0); |
||
454 | cascade | 343 | #endif |
455 | cascade | 344 | config_menu_doclick(chosen, menu); |
345 | break; |
||
346 | case 7: // disable debug data |
||
347 | // disable sending of debug data |
||
348 | // may result in smoother ddata display |
||
349 | usart1_request_mk_data(0, 'd', 0); |
||
350 | config_menu_doclick(chosen, menu); |
||
351 | break; |
||
352 | case 8: // exit |
||
353 | inmenu = 0; |
||
354 | break; |
||
355 | } |
||
356 | _delay_ms(250); |
||
357 | } |
||
358 | } |
||
326 | cascade | 359 | |
455 | cascade | 360 | // clear screen up again |
361 | clear(); |
||
326 | cascade | 362 | |
455 | cascade | 363 | // update flags to paint display again if needed |
364 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
326 | cascade | 365 | |
455 | cascade | 366 | // enable interrupts again |
367 | sei(); |
||
326 | cascade | 368 | } |
369 | |||
455 | cascade | 370 | /** |
371 | * auto config some stuff on startup, currently only battery cells |
||
372 | * TODO: this is testing stuff, strings should go progmem and so on... |
||
373 | */ |
||
459 | cascade | 374 | void auto_config(uint8_t UBat) { |
455 | cascade | 375 | clear(); |
376 | write_ascii_string(2, 2, "C-OSD Initialisation"); |
||
377 | #if FCONLY |
||
378 | write_ascii_string(2, 3, "FC only Mode"); |
||
379 | #else |
||
380 | write_ascii_string(2, 3, "NaviCtrl Mode"); |
||
381 | #endif |
||
466 | cascade | 382 | write_ascii_string(2, 4, BUILDDATE); |
455 | cascade | 383 | uint8_t cellnum = 0; |
384 | if (CELL_NUM == -1) { |
||
459 | cascade | 385 | write_ascii_string(2, 6, "Guessing Number of Cells"); |
455 | cascade | 386 | do { |
387 | cellnum++; |
||
466 | cascade | 388 | } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23)); |
455 | cascade | 389 | } else { |
390 | cellnum = CELL_NUM; |
||
391 | } |
||
392 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
393 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
394 | write_ascii_string(2, 7, "Number of Cells:"); |
||
395 | write_ndigit_number_u(21, 7, cellnum, 1, 0); |
||
396 | write_ascii_string(2, 8, "Warn Voltage :"); |
||
397 | write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0); |
||
398 | write_ascii_string(2, 9, "Max Voltage :"); |
||
399 | write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0); |
||
400 | _delay_ms(200); |
||
401 | clear(); |
||
402 | // update flags to paint display again because of clear |
||
403 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
404 | } |
||
405 | |||
465 | cascade | 406 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
455 | cascade | 407 | |
321 | cascade | 408 | /* ########################################################################## |
409 | * MAIN |
||
410 | * ##########################################################################*/ |
||
411 | int main(void) { |
||
466 | cascade | 412 | |
339 | cascade | 413 | // set up FLAGS, compiler should flatten this one |
414 | COSD_FLAGS = (NTSC << 0); |
||
415 | COSD_FLAGS |= (HUD << 1); |
||
416 | COSD_FLAGS |= (ARTHORIZON << 2); |
||
455 | cascade | 417 | COSD_FLAGS |= (BIGVARIO << 3); |
418 | COSD_FLAGS |= (STATS << 4); |
||
383 | cascade | 419 | COSD_FLAGS |= (WARNINGS << 5); |
324 | cascade | 420 | |
339 | cascade | 421 | // set up Atmega162 Ports |
321 | cascade | 422 | DDRA |= (1 << PA1); // PA1 output (/CS) |
423 | MAX_CS_HIGH |
||
424 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
||
425 | MAX_SDIN_LOW |
||
426 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
||
427 | MAX_SCLK_LOW |
||
428 | DDRA |= (1 << PA5); // PA5 output (RESET) |
||
429 | MAX_RESET_HIGH |
||
430 | |||
431 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
||
432 | LED1_OFF |
||
433 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
||
434 | LED2_OFF |
||
435 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
||
436 | LED3_OFF |
||
437 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
||
438 | LED4_OFF |
||
439 | |||
440 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
||
441 | PORTC |= (1 << PC4); // pullup |
||
442 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
||
443 | PORTC |= (1 << PC5); // pullup |
||
444 | |||
339 | cascade | 445 | // set up top and bottom lines |
446 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
447 | bottom_line = 12; |
||
448 | } else { |
||
449 | bottom_line = 14; |
||
450 | } |
||
326 | cascade | 451 | |
339 | cascade | 452 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 453 | MAX_RESET_LOW |
454 | MAX_RESET_HIGH |
||
455 | |||
456 | // give the FC/NC and the maxim time to come up |
||
457 | LED4_ON |
||
458 | _delay_ms(2000); |
||
459 | |||
460 | LED4_OFF |
||
461 | |||
331 | cascade | 462 | |
339 | cascade | 463 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 464 | #if (WRITECHARS != -1) |
466 | cascade | 465 | // DISABLE display (VM0) |
466 | spi_send_byte(0x00, 0b00000000); |
||
467 | #include "characters.c" |
||
455 | cascade | 468 | #endif |
321 | cascade | 469 | |
339 | cascade | 470 | // Setup Video Mode |
471 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
472 | // NTSC + enable display immediately (VM0) |
||
473 | spi_send_byte(0x00, 0b00001000); |
||
474 | } else { |
||
475 | // PAL + enable display immediately (VM0) |
||
476 | spi_send_byte(0x00, 0b01001000); |
||
477 | } |
||
321 | cascade | 478 | |
404 | cascade | 479 | /*// clear all display-mem (DMM) |
403 | cascade | 480 | spi_send_byte(0x04, 0b00000100); |
481 | |||
482 | // clearing takes 12uS according to maxim so lets wait longer |
||
483 | _delay_us(120); |
||
484 | |||
321 | cascade | 485 | // 8bit mode |
404 | cascade | 486 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 487 | |
404 | cascade | 488 | // clear display memory and set to 8bit mode |
321 | cascade | 489 | clear(); |
490 | |||
329 | cascade | 491 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 492 | // init usart |
493 | usart1_init(); |
||
494 | |||
495 | // set up timer |
||
496 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
||
385 | cascade | 497 | OCR0 = 15; // preload |
321 | cascade | 498 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
499 | |||
466 | cascade | 500 | DDRD |= (1 << PD2); // PD2 output (INT0) |
501 | SpiMasterInit(); |
||
465 | cascade | 502 | |
326 | cascade | 503 | // enable interrupts |
321 | cascade | 504 | sei(); |
466 | cascade | 505 | |
321 | cascade | 506 | #endif |
507 | |||
508 | //write_ascii_string(2, 7, " CaScAdE "); |
||
509 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
510 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
511 | |||
512 | // we are ready |
||
513 | LED3_ON |
||
514 | |||
329 | cascade | 515 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
466 | cascade | 516 | clear(); |
321 | cascade | 517 | write_all_chars(); |
518 | #else |
||
466 | cascade | 519 | // clear serial screen |
520 | //usart1_puts("\x1B[2J\x1B[H"); |
||
521 | //usart1_puts("hello world!\r\n"); |
||
454 | cascade | 522 | #if FCONLY |
466 | cascade | 523 | // request data ever 100ms from FC; |
524 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 525 | #else |
466 | cascade | 526 | // request OSD Data from NC every 100ms |
527 | usart1_request_mk_data(1, 'o', 100); |
||
346 | cascade | 528 | |
339 | cascade | 529 | // and disable debug... |
455 | cascade | 530 | usart1_request_mk_data(0, 'd', 0); |
454 | cascade | 531 | #endif |
321 | cascade | 532 | |
339 | cascade | 533 | // stats for after flight |
534 | int16_t max_Altimeter = 0; |
||
459 | cascade | 535 | uint8_t min_UBat = 255; |
536 | #if !(FCONLY) |
||
466 | cascade | 537 | uint16_t max_GroundSpeed = 0; |
339 | cascade | 538 | int16_t max_Distance = 0; |
539 | uint16_t max_FlyingTime = 0; |
||
321 | cascade | 540 | |
339 | cascade | 541 | // flags from last round to check for changes |
542 | uint8_t old_MKFlags = 0; |
||
543 | |||
321 | cascade | 544 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
377 | cascade | 545 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
459 | cascade | 546 | #endif |
321 | cascade | 547 | |
465 | cascade | 548 | |
549 | |||
321 | cascade | 550 | while (1) { |
466 | cascade | 551 | // in case SPI is ready and there is nothing to send right now |
552 | if (!icnt && spi_ready) { |
||
553 | // correct transfer ends with d (done) |
||
554 | if (spi_cmd_buffer[0] == 'd') { |
||
555 | ampere = spi_cmd_buffer[1] << 8; |
||
556 | ampere |= spi_cmd_buffer[2]; |
||
467 | cascade | 557 | // if this is the first receival we should print the small A |
558 | if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) { |
||
559 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
560 | // update this flag |
||
561 | COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
||
562 | } |
||
466 | cascade | 563 | //write_ascii_char(8+4*30, 'v'); // valid |
564 | } else { |
||
565 | // update flags |
||
566 | COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
||
567 | //write_ascii_char(8+4*30, 'i'); // invalid |
||
568 | } |
||
569 | spi_cmd_buffer[0] = 'A'; |
||
570 | spi_cmd_buffer[1] = 'B'; |
||
571 | spi_cmd_buffer[2] = 'C'; |
||
572 | StartTransfer((unsigned char*) spi_cmd_buffer, 3); |
||
573 | } |
||
321 | cascade | 574 | if (rxd_buffer_locked) { |
339 | cascade | 575 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
454 | cascade | 576 | #if FCONLY |
339 | cascade | 577 | if (rxd_buffer[2] == 'D') { // FC Data |
454 | cascade | 578 | Decode64(); |
339 | cascade | 579 | debugData = *((DebugOut_t*) pRxData); |
454 | cascade | 580 | |
455 | cascade | 581 | // init on first data retrival, distinguished by last battery :) |
582 | if (last_UBat == 255) { |
||
466 | cascade | 583 | // fix for min_UBat |
584 | min_UBat = debugData.Analog[9]; |
||
459 | cascade | 585 | auto_config(debugData.Analog[9]); |
455 | cascade | 586 | } |
466 | cascade | 587 | #include "osd_fcmode_default.c" |
454 | cascade | 588 | } |
589 | #else |
||
466 | cascade | 590 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
591 | Decode64(); |
||
592 | naviData = *((NaviData_t*) pRxData); |
||
453 | cascade | 593 | |
466 | cascade | 594 | // init on first data retrival, distinguished by last battery :) |
595 | if (last_UBat == 255) { |
||
596 | // fix for min_UBat |
||
597 | min_UBat = naviData.UBat; |
||
598 | auto_config(naviData.UBat); |
||
599 | } |
||
600 | #include "osd_ncmode_default.c" |
||
601 | } |
||
454 | cascade | 602 | #endif |
339 | cascade | 603 | } |
321 | cascade | 604 | rxd_buffer_locked = 0; |
605 | } |
||
606 | // handle keypress |
||
326 | cascade | 607 | if (s1_pressed()) { |
339 | cascade | 608 | config_menu(); |
321 | cascade | 609 | } |
339 | cascade | 610 | if (seconds_since_last_data > 2) { |
454 | cascade | 611 | #if FCONLY |
455 | cascade | 612 | // request data ever 100ms from FC; |
613 | usart1_request_mk_data(0, 'd', 100); |
||
454 | cascade | 614 | #else |
455 | cascade | 615 | // request OSD Data from NC every 100ms |
616 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 617 | |
455 | cascade | 618 | // and disable debug... |
619 | usart1_request_mk_data(0, 'd', 0); |
||
454 | cascade | 620 | #endif |
339 | cascade | 621 | } |
321 | cascade | 622 | } |
623 | #endif |
||
624 | return 0; |
||
625 | } |