Rev 453 | Rev 455 | 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> |
||
346 | cascade | 31 | #include <avr/pgmspace.h> |
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; |
||
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 :"; |
||
379 | cascade | 75 | char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
412 | cascade | 76 | stats_item_3, stats_item_4, stats_item_5, stats_item_6}; |
346 | cascade | 77 | |
78 | // store more fixed strings in progmen |
||
379 | cascade | 79 | char ON[] PROGMEM = "ON "; |
346 | cascade | 80 | char OFF[] PROGMEM = "OFF"; |
81 | |||
329 | cascade | 82 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
326 | cascade | 83 | |
84 | // general PAL|NTSC distingiusch stuff |
||
85 | uint8_t top_line = 1; |
||
86 | uint8_t bottom_line = 14; |
||
87 | |||
453 | cascade | 88 | // battery voltages |
89 | uint8_t min_voltage = 0; |
||
90 | uint8_t max_voltage = 0; |
||
91 | |||
326 | cascade | 92 | // Flags |
93 | uint8_t COSD_FLAGS = 0; |
||
94 | |||
321 | cascade | 95 | /* ########################################################################## |
326 | cascade | 96 | * debounce buttons |
97 | * ##########################################################################*/ |
||
98 | int s1_pressed() { |
||
99 | if (S1_PRESSED) { |
||
100 | _delay_ms(25); |
||
101 | if (S1_PRESSED) return 1; |
||
102 | } |
||
103 | return 0; |
||
104 | } |
||
105 | |||
106 | int s2_pressed() { |
||
107 | if (S2_PRESSED) { |
||
108 | _delay_ms(25); |
||
109 | if (S2_PRESSED) return 1; |
||
110 | } |
||
111 | return 0; |
||
112 | } |
||
113 | |||
389 | cascade | 114 | /* ########################################################################## |
115 | * Interrupt handler |
||
116 | * ##########################################################################*/ |
||
321 | cascade | 117 | /** |
389 | cascade | 118 | * handler for undefined Interrupts |
119 | * if not defined AVR will reset in case any unhandled interrupts occur |
||
321 | cascade | 120 | */ |
389 | cascade | 121 | ISR(__vector_default) { |
122 | asm("nop"); |
||
123 | } |
||
321 | cascade | 124 | |
389 | cascade | 125 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 126 | /* ########################################################################## |
127 | * timer stuff |
||
128 | * ##########################################################################*/ |
||
331 | cascade | 129 | /** |
130 | * timer kicks in every 1000uS ^= 1ms |
||
321 | cascade | 131 | */ |
132 | ISR(TIMER0_OVF_vect) { |
||
385 | cascade | 133 | OCR0 = 15; // preload |
321 | cascade | 134 | if (!timer--) { |
135 | uptime++; |
||
136 | timer = 999; |
||
331 | cascade | 137 | seconds_since_last_data++; |
321 | cascade | 138 | } |
139 | } |
||
140 | |||
141 | |||
142 | /* ########################################################################## |
||
326 | cascade | 143 | * A simple config menu for the flags |
144 | * ##########################################################################*/ |
||
339 | cascade | 145 | |
146 | /** |
||
147 | * helper function for menu updating |
||
148 | */ |
||
149 | void config_menu_drawings(uint8_t chosen) { |
||
150 | // clear prevoius _cursor_ |
||
383 | cascade | 151 | write_ascii_string(3, (chosen + 5) % 8, " "); |
339 | cascade | 152 | // draw current _cursor_ |
383 | cascade | 153 | write_ascii_string(3, chosen + 5, ">"); |
339 | cascade | 154 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
383 | cascade | 155 | write_ascii_string_pgm(23, 5, ON); |
156 | } else { |
||
157 | write_ascii_string_pgm(23, 5, OFF); |
||
158 | } |
||
159 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
||
346 | cascade | 160 | write_ascii_string_pgm(23, 6, ON); |
339 | cascade | 161 | } else { |
383 | cascade | 162 | write_ascii_string_pgm(23, 6, OFF); |
339 | cascade | 163 | } |
383 | cascade | 164 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
346 | cascade | 165 | write_ascii_string_pgm(23, 7, ON); |
339 | cascade | 166 | } else { |
383 | cascade | 167 | write_ascii_string_pgm(23, 7, OFF); |
339 | cascade | 168 | } |
169 | if (COSD_FLAGS & COSD_FLAG_STATS) { |
||
346 | cascade | 170 | write_ascii_string_pgm(23, 8, ON); |
339 | cascade | 171 | } else { |
346 | cascade | 172 | write_ascii_string_pgm(23, 8, OFF); |
339 | cascade | 173 | } |
174 | if (COSD_FLAGS & COSD_FLAG_WARNINGS) { |
||
346 | cascade | 175 | write_ascii_string_pgm(23, 9, ON); |
339 | cascade | 176 | } else { |
346 | cascade | 177 | write_ascii_string_pgm(23, 9, OFF); |
339 | cascade | 178 | } |
179 | } |
||
180 | |||
181 | /** |
||
346 | cascade | 182 | * some sort of clicking response in the menu |
183 | */ |
||
184 | void config_menu_doclick(uint8_t chosen, char** menu) { |
||
383 | cascade | 185 | write_ascii_string(4, chosen + 5, "DONE "); |
346 | cascade | 186 | _delay_ms(1000); |
383 | cascade | 187 | write_ascii_string(4, chosen + 5, menu[chosen]); |
346 | cascade | 188 | } |
189 | |||
190 | /** |
||
339 | cascade | 191 | * a simple config menu tryout |
192 | */ |
||
326 | cascade | 193 | void config_menu(void) { |
194 | // disable interrupts (makes the menu more smoothely) |
||
195 | cli(); |
||
321 | cascade | 196 | |
326 | cascade | 197 | // clear screen |
198 | clear(); |
||
199 | |||
383 | cascade | 200 | char* menu[9] = {"Full HUD", |
339 | cascade | 201 | "Art.Horizon in HUD", |
383 | cascade | 202 | "Big Vario bar", |
339 | cascade | 203 | "Statistics", |
204 | "Warnings", // TODO: do it! |
||
346 | cascade | 205 | "Reset uptime", |
206 | "Request OSD-data", |
||
207 | "Disable Debug-data", |
||
339 | cascade | 208 | "EXIT"}; |
326 | cascade | 209 | |
210 | uint8_t inmenu = 1; |
||
211 | uint8_t chosen = 0; |
||
339 | cascade | 212 | write_ascii_string(6, 2, "C-OSD Config Menu"); |
326 | cascade | 213 | |
214 | // wait a bit before doing stuff so user has chance to release button |
||
215 | _delay_ms(250); |
||
216 | |||
383 | cascade | 217 | write_ascii_string(4, 5, menu[0]); |
218 | write_ascii_string(4, 6, menu[1]); |
||
219 | write_ascii_string(4, 7, menu[2]); |
||
220 | write_ascii_string(4, 8, menu[3]); |
||
221 | write_ascii_string(4, 9, menu[4]); |
||
222 | write_ascii_string(4, 10, menu[5]); |
||
223 | write_ascii_string(4, 11, menu[6]); |
||
224 | write_ascii_string(4, 12, menu[7]); |
||
225 | write_ascii_string(4, 13, menu[8]); |
||
339 | cascade | 226 | |
227 | config_menu_drawings(chosen); |
||
228 | |||
326 | cascade | 229 | while (inmenu) { |
230 | if (s2_pressed()) { |
||
383 | cascade | 231 | write_ascii_string(3, chosen+5, " "); |
232 | chosen = (chosen + 1) % 9; |
||
233 | write_ascii_string(3, chosen+5, ">"); |
||
326 | cascade | 234 | _delay_ms(500); |
235 | } else if (s1_pressed()) { |
||
236 | switch (chosen) { |
||
339 | cascade | 237 | case 0: // full HUD |
238 | COSD_FLAGS ^= COSD_FLAG_HUD; |
||
239 | config_menu_drawings(chosen); |
||
326 | cascade | 240 | break; |
339 | cascade | 241 | case 1: // art horizon |
242 | COSD_FLAGS ^= COSD_FLAG_ARTHORIZON; |
||
243 | config_menu_drawings(chosen); |
||
326 | cascade | 244 | break; |
383 | cascade | 245 | case 2: // big vario |
246 | COSD_FLAGS ^= COSD_FLAG_BIGVARIO; |
||
247 | config_menu_drawings(chosen); |
||
248 | break; |
||
249 | case 3: // statistics |
||
339 | cascade | 250 | COSD_FLAGS ^= COSD_FLAG_STATS; |
251 | config_menu_drawings(chosen); |
||
326 | cascade | 252 | break; |
383 | cascade | 253 | case 4: // warnings |
339 | cascade | 254 | COSD_FLAGS ^= COSD_FLAG_WARNINGS; |
255 | config_menu_drawings(chosen); |
||
256 | break; |
||
383 | cascade | 257 | case 5: // reset uptime |
346 | cascade | 258 | uptime = 0; |
259 | config_menu_doclick(chosen, menu); |
||
260 | break; |
||
383 | cascade | 261 | case 6: // re-request OSD data |
454 | cascade | 262 | #if FCONLY |
263 | // request data ever 100ms from FC; |
||
264 | usart1_request_mk_data(0, 'd', 100); |
||
265 | #else |
||
266 | // request OSD Data from NC every 100ms |
||
267 | usart1_request_mk_data(1, 'o', 100); |
||
268 | |||
269 | // and disable debug... |
||
270 | usart1_request_mk_data(0, 'd', 0); |
||
271 | #endif |
||
346 | cascade | 272 | config_menu_doclick(chosen, menu); |
273 | break; |
||
383 | cascade | 274 | case 7: // disable debug data |
346 | cascade | 275 | // disable sending of debug data |
276 | // may result in smoother ddata display |
||
277 | usart1_request_mk_data(0, 'd', 0); |
||
278 | config_menu_doclick(chosen, menu); |
||
279 | break; |
||
383 | cascade | 280 | case 8: // exit |
339 | cascade | 281 | inmenu = 0; |
282 | break; |
||
326 | cascade | 283 | } |
339 | cascade | 284 | _delay_ms(250); |
326 | cascade | 285 | } |
286 | } |
||
287 | |||
288 | // clear screen up again |
||
289 | clear(); |
||
290 | |||
291 | // update flags to paint display again if needed |
||
292 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
293 | |||
294 | // enable interrupts again |
||
295 | sei(); |
||
296 | } |
||
297 | |||
329 | cascade | 298 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 299 | /* ########################################################################## |
300 | * MAIN |
||
301 | * ##########################################################################*/ |
||
302 | int main(void) { |
||
339 | cascade | 303 | // set up FLAGS, compiler should flatten this one |
304 | COSD_FLAGS = (NTSC << 0); |
||
305 | COSD_FLAGS |= (HUD << 1); |
||
306 | COSD_FLAGS |= (ARTHORIZON << 2); |
||
383 | cascade | 307 | COSD_FLAGS |= (BIGVARIO << 3); |
308 | COSD_FLAGS |= (STATS << 4); |
||
309 | COSD_FLAGS |= (WARNINGS << 5); |
||
324 | cascade | 310 | |
339 | cascade | 311 | // set up Atmega162 Ports |
321 | cascade | 312 | DDRA |= (1 << PA1); // PA1 output (/CS) |
313 | MAX_CS_HIGH |
||
314 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
||
315 | MAX_SDIN_LOW |
||
316 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
||
317 | MAX_SCLK_LOW |
||
318 | DDRA |= (1 << PA5); // PA5 output (RESET) |
||
319 | MAX_RESET_HIGH |
||
320 | |||
321 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
||
322 | LED1_OFF |
||
323 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
||
324 | LED2_OFF |
||
325 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
||
326 | LED3_OFF |
||
327 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
||
328 | LED4_OFF |
||
329 | |||
330 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
||
331 | PORTC |= (1 << PC4); // pullup |
||
332 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
||
333 | PORTC |= (1 << PC5); // pullup |
||
334 | |||
339 | cascade | 335 | // set up top and bottom lines |
336 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
337 | bottom_line = 12; |
||
338 | } else { |
||
339 | bottom_line = 14; |
||
340 | } |
||
326 | cascade | 341 | |
339 | cascade | 342 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 343 | MAX_RESET_LOW |
344 | MAX_RESET_HIGH |
||
345 | |||
346 | // give the FC/NC and the maxim time to come up |
||
347 | LED4_ON |
||
348 | _delay_ms(2000); |
||
349 | |||
350 | LED4_OFF |
||
351 | |||
331 | cascade | 352 | |
339 | cascade | 353 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 354 | #if (WRITECHARS != -1) |
339 | cascade | 355 | // DISABLE display (VM0) |
321 | cascade | 356 | spi_send_byte(0x00, 0b00000000); |
339 | cascade | 357 | #include "characters.c" |
321 | cascade | 358 | |
331 | cascade | 359 | #endif |
321 | cascade | 360 | |
339 | cascade | 361 | // Setup Video Mode |
362 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
363 | // NTSC + enable display immediately (VM0) |
||
364 | spi_send_byte(0x00, 0b00001000); |
||
365 | } else { |
||
366 | // PAL + enable display immediately (VM0) |
||
367 | spi_send_byte(0x00, 0b01001000); |
||
368 | } |
||
321 | cascade | 369 | |
404 | cascade | 370 | /*// clear all display-mem (DMM) |
403 | cascade | 371 | spi_send_byte(0x04, 0b00000100); |
372 | |||
373 | // clearing takes 12uS according to maxim so lets wait longer |
||
374 | _delay_us(120); |
||
375 | |||
321 | cascade | 376 | // 8bit mode |
404 | cascade | 377 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 378 | |
404 | cascade | 379 | // clear display memory and set to 8bit mode |
321 | cascade | 380 | clear(); |
381 | |||
329 | cascade | 382 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 383 | // init usart |
384 | usart1_init(); |
||
385 | |||
386 | // set up timer |
||
387 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
||
385 | cascade | 388 | OCR0 = 15; // preload |
321 | cascade | 389 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
390 | |||
326 | cascade | 391 | // enable interrupts |
321 | cascade | 392 | sei(); |
393 | #endif |
||
394 | |||
395 | //write_ascii_string(2, 7, " CaScAdE "); |
||
396 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
397 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
398 | |||
399 | // we are ready |
||
400 | LED3_ON |
||
401 | |||
329 | cascade | 402 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
321 | cascade | 403 | clear(); |
404 | write_all_chars(); |
||
405 | #else |
||
339 | cascade | 406 | // clear serial screen |
407 | //usart1_puts("\x1B[2J\x1B[H"); |
||
408 | //usart1_puts("hello world!\r\n"); |
||
321 | cascade | 409 | |
454 | cascade | 410 | #if FCONLY |
346 | cascade | 411 | // request data ever 100ms from FC; |
454 | cascade | 412 | usart1_request_mk_data(0, 'd', 100); |
413 | #else |
||
339 | cascade | 414 | // request OSD Data from NC every 100ms |
346 | cascade | 415 | usart1_request_mk_data(1, 'o', 100); |
416 | |||
339 | cascade | 417 | // and disable debug... |
385 | cascade | 418 | usart1_request_mk_data(0, 'd', 0); |
454 | cascade | 419 | #endif |
321 | cascade | 420 | |
339 | cascade | 421 | // stats for after flight |
422 | int16_t max_Altimeter = 0; |
||
423 | uint16_t max_GroundSpeed = 0; |
||
424 | int16_t max_Distance = 0; |
||
425 | uint8_t min_UBat = 255; |
||
426 | uint16_t max_FlyingTime = 0; |
||
321 | cascade | 427 | |
339 | cascade | 428 | // flags from last round to check for changes |
429 | uint8_t old_MKFlags = 0; |
||
430 | |||
321 | cascade | 431 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
377 | cascade | 432 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
321 | cascade | 433 | |
434 | while (1) { |
||
339 | cascade | 435 | // write icons at init or after menu/mode-switch |
436 | if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) { |
||
454 | cascade | 437 | #if FCONLY |
438 | // FC Mode ICONS |
||
439 | write_char_xy(10, top_line, 0xCA); // RC-transmitter |
||
440 | write_char_xy(27, top_line, 0xCC); // small meters m height |
||
441 | write_char_xy(7, bottom_line, 0x9E); // small v |
||
442 | #else |
||
443 | // NAVI Mode ICONS |
||
385 | cascade | 444 | write_char_xy(5, top_line, 0xCB); // km/h |
445 | write_char_xy(10, top_line, 0xCA); // RC-transmitter |
||
446 | write_char_xy(16, top_line, 0xD0); // degree symbol |
||
447 | write_char_xy(27, top_line, 0xCC); // small meters m height |
||
448 | write_char_xy(20, top_line+1, 0xB0); // left circle |
||
449 | write_char_xy(22, top_line+1, 0xB2); // right circle |
||
450 | write_char_xy(27, top_line+1, 0xCC); // small meters m home |
||
451 | write_char_xy(7, bottom_line, 0x9E); // small v |
||
452 | write_char_xy(14, bottom_line, 0xD1); // on clock |
||
407 | cascade | 453 | write_char_xy(21, bottom_line, 0xD2); // fly clock |
385 | cascade | 454 | write_char_xy(26, bottom_line, 0xC8); // sat1 |
455 | write_char_xy(27, bottom_line, 0xC9); // sat2 |
||
339 | cascade | 456 | COSD_FLAGS |= COSD_ICONS_WRITTEN; |
454 | cascade | 457 | #endif |
339 | cascade | 458 | } |
321 | cascade | 459 | if (rxd_buffer_locked) { |
339 | cascade | 460 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
454 | cascade | 461 | #if FCONLY |
339 | cascade | 462 | if (rxd_buffer[2] == 'D') { // FC Data |
454 | cascade | 463 | Decode64(); |
339 | cascade | 464 | debugData = *((DebugOut_t*) pRxData); |
454 | cascade | 465 | write_ascii_string(2, 2, "FCONLY MODE"); |
466 | |||
467 | write_ndigit_number_u(7, top_line, debugData.Analog[10], 100, 0); |
||
468 | if (debugData.Analog[10] <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) { |
||
469 | for (uint8_t x = 0; x < 4; x++) |
||
470 | write_char_att_xy(7 + x, top_line, BLINK); |
||
471 | } else if (debugData.Analog[10] > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) { |
||
472 | for (uint8_t x = 0; x < 4; x++) |
||
473 | write_char_att_xy(7 + x, top_line, 0); |
||
474 | } |
||
475 | last_RC_Quality = debugData.Analog[10]; |
||
476 | |||
477 | |||
478 | if (debugData.Analog[5] > 300 || debugData.Analog[5] < -300) { |
||
479 | // above 10m only write full meters |
||
480 | write_ndigit_number_s(23, top_line, debugData.Analog[5] / 30, 1000, 0); |
||
481 | } else { |
||
482 | // up to 10m write meters.dm |
||
483 | write_ndigit_number_s_10th(23, top_line, debugData.Analog[5] / 3, 100, 0); |
||
484 | } |
||
485 | |||
486 | draw_battery(2, bottom_line, min_voltage, debugData.Analog[9], max_voltage); |
||
487 | write_ndigit_number_u_10th(3, bottom_line, debugData.Analog[9], 100, 0); |
||
488 | if (debugData.Analog[9] <= min_voltage && last_UBat > min_voltage) { |
||
489 | for (uint8_t x = 2; x < 8; x++) |
||
490 | write_char_att_xy(x, bottom_line, BLINK); |
||
491 | } else if (debugData.Analog[9] > min_voltage && last_UBat < min_voltage) { |
||
492 | for (uint8_t x = 2; x < 8; x++) |
||
493 | write_char_att_xy(x, bottom_line, 0); |
||
494 | } |
||
495 | // remember last values |
||
496 | last_UBat = debugData.Analog[9]; |
||
497 | |||
498 | /* |
||
499 | debugData.Analog[0]); // AngleNick |
||
500 | debugData.Analog[1]); // AngleRoll |
||
501 | debugData.Analog[5]); // Height |
||
502 | debugData.Analog[9]); // Voltage |
||
503 | debugData.Analog[10]);// RC Signal |
||
504 | debugData.Analog[11]);// Gyro compass |
||
505 | */ |
||
506 | seconds_since_last_data = 0; |
||
507 | } |
||
508 | #else |
||
509 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
||
339 | cascade | 510 | Decode64(); |
511 | naviData = *((NaviData_t*) pRxData); |
||
383 | cascade | 512 | |
453 | cascade | 513 | // init on first data retrival, distinguished by last battery :) |
514 | // TODO: this is testing stuff, strings should go progmem and so on... |
||
515 | if (last_UBat == 255) { |
||
516 | clear(); |
||
517 | // fix for min_bat glitch caused by data only gathered during motors up |
||
518 | min_UBat = naviData.UBat; |
||
519 | write_ascii_string(2, 2, "C-OSD Initialisation"); |
||
520 | uint8_t cellnum = 0; |
||
521 | if (CELL_NUM == -1) { |
||
522 | write_ascii_string(2, 5, "Guessing Number of Cells"); |
||
523 | do { |
||
524 | cellnum++; |
||
525 | } while (naviData.UBat > ((cellnum * CELL_VOLT_MAX) + 10)); |
||
526 | } else { |
||
527 | cellnum = CELL_NUM; |
||
528 | } |
||
529 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
530 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
531 | write_ascii_string(2, 7, "Number of Cells:"); |
||
532 | write_ndigit_number_u(21, 7, cellnum, 1, 0); |
||
533 | write_ascii_string(2, 8, "Warn Voltage :"); |
||
534 | write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0); |
||
535 | write_ascii_string(2, 9, "Max Voltage :"); |
||
536 | write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0); |
||
537 | _delay_ms(200); |
||
538 | clear(); |
||
539 | // update flags to paint display again because of clear |
||
540 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
541 | } |
||
542 | |||
339 | cascade | 543 | // first line |
373 | cascade | 544 | write_ndigit_number_u(2, top_line, (uint16_t) (((uint32_t) naviData.GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0); |
321 | cascade | 545 | |
349 | cascade | 546 | write_ndigit_number_u(7, top_line, naviData.RC_Quality, 100, 0); |
339 | cascade | 547 | if (naviData.RC_Quality <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) { |
548 | for (uint8_t x = 0; x < 4; x++) |
||
549 | write_char_att_xy(7 + x, top_line, BLINK); |
||
550 | } else if (naviData.RC_Quality > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) { |
||
551 | for (uint8_t x = 0; x < 4; x++) |
||
552 | write_char_att_xy(7 + x, top_line, 0); |
||
553 | } |
||
407 | cascade | 554 | last_RC_Quality = naviData.RC_Quality; |
555 | |||
556 | if (naviData.NCFlags & NC_FLAG_NOSERIALLINK) { |
||
557 | write_char_xy(11, top_line, 0); // clear |
||
558 | } else { |
||
559 | write_char_xy(11, top_line, 0xC6); // PC icon |
||
560 | } |
||
321 | cascade | 561 | |
349 | cascade | 562 | write_ndigit_number_u(13, top_line, naviData.CompassHeading, 100, 0); |
321 | cascade | 563 | |
339 | cascade | 564 | write_ascii_string(17, top_line, directions[heading_conv(naviData.CompassHeading)]); |
377 | cascade | 565 | |
383 | cascade | 566 | draw_variometer(21, top_line, naviData.Variometer); |
321 | cascade | 567 | |
339 | cascade | 568 | //note:lephisto:according to several sources it's /30 |
349 | cascade | 569 | if (naviData.Altimeter > 300 || naviData.Altimeter < -300) { |
339 | cascade | 570 | // above 10m only write full meters |
349 | cascade | 571 | write_ndigit_number_s(23, top_line, naviData.Altimeter / 30, 1000, 0); |
339 | cascade | 572 | } else { |
573 | // up to 10m write meters.dm |
||
349 | cascade | 574 | //write_number_u_10th(21, top_line, naviData.Altimeter / 3); |
575 | write_ndigit_number_s_10th(23, top_line, naviData.Altimeter / 3, 100, 0); |
||
339 | cascade | 576 | } |
321 | cascade | 577 | |
339 | cascade | 578 | // seccond line |
579 | draw_compass(11, top_line + 1, naviData.CompassHeading); |
||
321 | cascade | 580 | |
339 | cascade | 581 | // TODO: verify correctness |
582 | uint16_t heading_home = (naviData.HomePositionDeviation.Bearing + 360 - naviData.CompassHeading) % 360; |
||
383 | cascade | 583 | //write_char_xy(21, top_line + 1, arrowdir[heading_conv(heading_home)]); |
377 | cascade | 584 | // finer resolution, 0xa0 is first character and we add the index 0 <= index < 16 |
383 | cascade | 585 | write_char_xy(21, top_line + 1, 0xa0 + heading_fine_conv(heading_home)); |
321 | cascade | 586 | |
355 | shaddi | 587 | write_ndigit_number_u(24, top_line + 1, naviData.HomePositionDeviation.Distance / 10, 100, 0); |
321 | cascade | 588 | |
339 | cascade | 589 | // center |
590 | if (naviData.MKFlags & FLAG_MOTOR_RUN) { // should be engines running |
||
591 | if (!(old_MKFlags & FLAG_MOTOR_RUN)) { // motors just started, clear middle |
||
592 | clear(); |
||
593 | // update flags to paint display again if needed |
||
594 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
595 | } |
||
596 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
||
597 | draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll); |
||
598 | } |
||
407 | cascade | 599 | // motors are on, assume we were/are flying |
600 | COSD_FLAGS |= COSD_WASFLYING; |
||
339 | cascade | 601 | } else { |
336 | cascade | 602 | // stats |
407 | cascade | 603 | if ((COSD_FLAGS & COSD_WASFLYING) && (COSD_FLAGS & COSD_FLAG_STATS)) { |
412 | cascade | 604 | write_ascii_string_pgm(2, 4, stats_item_pointers[0]); // max Altitude |
605 | write_ndigit_number_s(18, 4, max_Altimeter / 30, 1000, 0); |
||
606 | write_char_xy(22, 4, 204); // small meters m |
||
607 | write_ascii_string_pgm(2, 5, stats_item_pointers[1]); // max Speed |
||
608 | write_ndigit_number_u(19, 5, (uint16_t) (((uint32_t) max_GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0); |
||
609 | write_char_xy(22, 5, 203); // km/h |
||
610 | write_ascii_string_pgm(2, 6, stats_item_pointers[2]); // max Distance |
||
611 | write_ndigit_number_u(19, 6, max_Distance / 10, 100, 0); |
||
612 | write_char_xy(22, 6, 204); // small meters m |
||
613 | write_ascii_string_pgm(2, 7, stats_item_pointers[3]); // min voltage |
||
614 | write_ndigit_number_u_10th(18, 7, min_UBat, 100, 0); |
||
615 | write_char_xy(22, 7, 0x9E); // small v |
||
616 | write_ascii_string_pgm(2, 8, stats_item_pointers[4]); // max time |
||
617 | write_time(16, 8, max_FlyingTime); |
||
618 | write_char_xy(22, 8, 210); // fly clock |
||
619 | write_ascii_string_pgm(2, 9, stats_item_pointers[5]); // longitude |
||
620 | write_gps_pos(15, 9, naviData.CurrentPosition.Longitude); |
||
621 | write_ascii_string_pgm(2, 10, stats_item_pointers[6]); // latitude |
||
622 | write_gps_pos(15, 10, naviData.CurrentPosition.Latitude); |
||
339 | cascade | 623 | } else if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { // if no stats there is space horizon |
624 | draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll); |
||
625 | } |
||
626 | } |
||
383 | cascade | 627 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
628 | draw_big_variometer(27, 8, naviData.Variometer); |
||
629 | } |
||
321 | cascade | 630 | |
339 | cascade | 631 | // bottom line |
453 | cascade | 632 | draw_battery(2, bottom_line, min_voltage, naviData.UBat, max_voltage); |
385 | cascade | 633 | write_ndigit_number_u_10th(3, bottom_line, naviData.UBat, 100, 0); |
453 | cascade | 634 | if (naviData.UBat <= min_voltage && last_UBat > min_voltage) { |
385 | cascade | 635 | for (uint8_t x = 2; x < 8; x++) |
339 | cascade | 636 | write_char_att_xy(x, bottom_line, BLINK); |
453 | cascade | 637 | } else if (naviData.UBat > min_voltage && last_UBat < min_voltage) { |
385 | cascade | 638 | for (uint8_t x = 2; x < 8; x++) |
339 | cascade | 639 | write_char_att_xy(x, bottom_line, 0); |
640 | } |
||
453 | cascade | 641 | // remember last values |
642 | last_UBat = naviData.UBat; |
||
321 | cascade | 643 | |
339 | cascade | 644 | write_time(8, bottom_line, uptime); |
407 | cascade | 645 | write_time(15, bottom_line, naviData.FlyingTime); |
321 | cascade | 646 | |
349 | cascade | 647 | write_ndigit_number_u(24, bottom_line, naviData.SatsInUse, 10, 0); |
321 | cascade | 648 | |
407 | cascade | 649 | if (naviData.NCFlags & NC_FLAG_MANUAL_CONTROL) { |
650 | write_char_xy(23, bottom_line, 0xB3); // rc transmitter |
||
651 | } else { |
||
652 | write_char_xy(23, bottom_line, 0); // clear |
||
653 | } |
||
654 | |||
339 | cascade | 655 | if (naviData.NCFlags & NC_FLAG_CH) { |
656 | write_char_xy(27, bottom_line, 231); // gps ch |
||
657 | } else if (naviData.NCFlags & NC_FLAG_PH) { |
||
658 | write_char_xy(27, bottom_line, 230); // gps ph |
||
659 | } else { // (naviData.NCFlags & NC_FLAG_FREE) |
||
660 | write_char_xy(27, bottom_line, 201); // sat2 (free) |
||
661 | } |
||
321 | cascade | 662 | |
339 | cascade | 663 | //write_number_s(8, 5, RxDataLen); |
664 | //write_number_s(16, 5, setsReceived++); |
||
321 | cascade | 665 | |
410 | shaddi | 666 | // remember statistics (only when engines running) |
412 | cascade | 667 | if (naviData.MKFlags & FLAG_MOTOR_RUN) { |
668 | if (naviData.Altimeter > max_Altimeter) max_Altimeter = naviData.Altimeter; |
||
669 | if (naviData.GroundSpeed > max_GroundSpeed) max_GroundSpeed = naviData.GroundSpeed; |
||
670 | if (naviData.HomePositionDeviation.Distance > max_Distance) { |
||
671 | max_Distance = naviData.HomePositionDeviation.Distance; |
||
672 | } |
||
673 | if (naviData.UBat < min_UBat) min_UBat = naviData.UBat; |
||
674 | if (naviData.FlyingTime > max_FlyingTime) max_FlyingTime = naviData.FlyingTime; |
||
675 | } |
||
339 | cascade | 676 | |
677 | old_MKFlags = naviData.MKFlags; |
||
388 | cascade | 678 | seconds_since_last_data = 0; |
339 | cascade | 679 | } |
454 | cascade | 680 | #endif |
339 | cascade | 681 | } |
321 | cascade | 682 | rxd_buffer_locked = 0; |
683 | } |
||
684 | // handle keypress |
||
326 | cascade | 685 | if (s1_pressed()) { |
339 | cascade | 686 | config_menu(); |
321 | cascade | 687 | } |
339 | cascade | 688 | if (seconds_since_last_data > 2) { |
454 | cascade | 689 | #if FCONLY |
690 | // request data ever 100ms from FC; |
||
691 | usart1_request_mk_data(0, 'd', 100); |
||
692 | #else |
||
346 | cascade | 693 | // request OSD Data from NC every 100ms |
694 | usart1_request_mk_data(1, 'o', 100); |
||
454 | cascade | 695 | |
696 | // and disable debug... |
||
697 | usart1_request_mk_data(0, 'd', 0); |
||
698 | #endif |
||
339 | cascade | 699 | } |
321 | cascade | 700 | } |
701 | #endif |
||
702 | return 0; |
||
703 | } |