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