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