Rev 412 | Rev 454 | 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 |
346 | cascade | 262 | // request OSD Data from NC every 100ms |
263 | usart1_request_mk_data(1, 'o', 100); |
||
264 | config_menu_doclick(chosen, menu); |
||
265 | break; |
||
383 | cascade | 266 | case 7: // disable debug data |
346 | cascade | 267 | // disable sending of debug data |
268 | // may result in smoother ddata display |
||
269 | usart1_request_mk_data(0, 'd', 0); |
||
270 | config_menu_doclick(chosen, menu); |
||
271 | break; |
||
383 | cascade | 272 | case 8: // exit |
339 | cascade | 273 | inmenu = 0; |
274 | break; |
||
326 | cascade | 275 | } |
339 | cascade | 276 | _delay_ms(250); |
326 | cascade | 277 | } |
278 | } |
||
279 | |||
280 | // clear screen up again |
||
281 | clear(); |
||
282 | |||
283 | // update flags to paint display again if needed |
||
284 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
285 | |||
286 | // enable interrupts again |
||
287 | sei(); |
||
288 | } |
||
289 | |||
329 | cascade | 290 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 291 | /* ########################################################################## |
292 | * MAIN |
||
293 | * ##########################################################################*/ |
||
294 | int main(void) { |
||
339 | cascade | 295 | // set up FLAGS, compiler should flatten this one |
296 | COSD_FLAGS = (NTSC << 0); |
||
297 | COSD_FLAGS |= (HUD << 1); |
||
298 | COSD_FLAGS |= (ARTHORIZON << 2); |
||
383 | cascade | 299 | COSD_FLAGS |= (BIGVARIO << 3); |
300 | COSD_FLAGS |= (STATS << 4); |
||
301 | COSD_FLAGS |= (WARNINGS << 5); |
||
324 | cascade | 302 | |
339 | cascade | 303 | // set up Atmega162 Ports |
321 | cascade | 304 | DDRA |= (1 << PA1); // PA1 output (/CS) |
305 | MAX_CS_HIGH |
||
306 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
||
307 | MAX_SDIN_LOW |
||
308 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
||
309 | MAX_SCLK_LOW |
||
310 | DDRA |= (1 << PA5); // PA5 output (RESET) |
||
311 | MAX_RESET_HIGH |
||
312 | |||
313 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
||
314 | LED1_OFF |
||
315 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
||
316 | LED2_OFF |
||
317 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
||
318 | LED3_OFF |
||
319 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
||
320 | LED4_OFF |
||
321 | |||
322 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
||
323 | PORTC |= (1 << PC4); // pullup |
||
324 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
||
325 | PORTC |= (1 << PC5); // pullup |
||
326 | |||
339 | cascade | 327 | // set up top and bottom lines |
328 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
329 | bottom_line = 12; |
||
330 | } else { |
||
331 | bottom_line = 14; |
||
332 | } |
||
326 | cascade | 333 | |
339 | cascade | 334 | // reset the MAX7456 to be sure any undefined states do no harm |
321 | cascade | 335 | MAX_RESET_LOW |
336 | MAX_RESET_HIGH |
||
337 | |||
338 | // give the FC/NC and the maxim time to come up |
||
339 | LED4_ON |
||
340 | _delay_ms(2000); |
||
341 | |||
342 | LED4_OFF |
||
343 | |||
331 | cascade | 344 | |
339 | cascade | 345 | //Pushing NEW chars to the MAX7456 |
329 | cascade | 346 | #if (WRITECHARS != -1) |
339 | cascade | 347 | // DISABLE display (VM0) |
321 | cascade | 348 | spi_send_byte(0x00, 0b00000000); |
339 | cascade | 349 | #include "characters.c" |
321 | cascade | 350 | |
331 | cascade | 351 | #endif |
321 | cascade | 352 | |
339 | cascade | 353 | // Setup Video Mode |
354 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
||
355 | // NTSC + enable display immediately (VM0) |
||
356 | spi_send_byte(0x00, 0b00001000); |
||
357 | } else { |
||
358 | // PAL + enable display immediately (VM0) |
||
359 | spi_send_byte(0x00, 0b01001000); |
||
360 | } |
||
321 | cascade | 361 | |
404 | cascade | 362 | /*// clear all display-mem (DMM) |
403 | cascade | 363 | spi_send_byte(0x04, 0b00000100); |
364 | |||
365 | // clearing takes 12uS according to maxim so lets wait longer |
||
366 | _delay_us(120); |
||
367 | |||
321 | cascade | 368 | // 8bit mode |
404 | cascade | 369 | spi_send_byte(0x04, 0b01000000);*/ |
321 | cascade | 370 | |
404 | cascade | 371 | // clear display memory and set to 8bit mode |
321 | cascade | 372 | clear(); |
373 | |||
329 | cascade | 374 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
321 | cascade | 375 | // init usart |
376 | usart1_init(); |
||
377 | |||
378 | // set up timer |
||
379 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
||
385 | cascade | 380 | OCR0 = 15; // preload |
321 | cascade | 381 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
382 | |||
326 | cascade | 383 | // enable interrupts |
321 | cascade | 384 | sei(); |
385 | #endif |
||
386 | |||
387 | //write_ascii_string(2, 7, " CaScAdE "); |
||
388 | //write_ascii_string(2, 8, "is TESTING his open source"); |
||
389 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
||
390 | |||
391 | // we are ready |
||
392 | LED3_ON |
||
393 | |||
329 | cascade | 394 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
321 | cascade | 395 | clear(); |
396 | write_all_chars(); |
||
397 | #else |
||
339 | cascade | 398 | // clear serial screen |
399 | //usart1_puts("\x1B[2J\x1B[H"); |
||
400 | //usart1_puts("hello world!\r\n"); |
||
321 | cascade | 401 | |
339 | cascade | 402 | |
346 | cascade | 403 | // request data ever 100ms from FC; |
404 | //usart1_request_mk_data(0, 'd', 100); |
||
339 | cascade | 405 | |
406 | // request OSD Data from NC every 100ms |
||
346 | cascade | 407 | usart1_request_mk_data(1, 'o', 100); |
408 | |||
339 | cascade | 409 | // and disable debug... |
385 | cascade | 410 | usart1_request_mk_data(0, 'd', 0); |
321 | cascade | 411 | |
339 | cascade | 412 | // stats for after flight |
413 | int16_t max_Altimeter = 0; |
||
414 | uint16_t max_GroundSpeed = 0; |
||
415 | int16_t max_Distance = 0; |
||
416 | uint8_t min_UBat = 255; |
||
417 | uint16_t max_FlyingTime = 0; |
||
321 | cascade | 418 | |
339 | cascade | 419 | // flags from last round to check for changes |
420 | uint8_t old_MKFlags = 0; |
||
421 | |||
321 | cascade | 422 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
377 | cascade | 423 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
321 | cascade | 424 | |
425 | while (1) { |
||
339 | cascade | 426 | // write icons at init or after menu/mode-switch |
427 | if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) { |
||
385 | cascade | 428 | write_char_xy(5, top_line, 0xCB); // km/h |
429 | write_char_xy(10, top_line, 0xCA); // RC-transmitter |
||
430 | write_char_xy(16, top_line, 0xD0); // degree symbol |
||
431 | write_char_xy(27, top_line, 0xCC); // small meters m height |
||
432 | write_char_xy(20, top_line+1, 0xB0); // left circle |
||
433 | write_char_xy(22, top_line+1, 0xB2); // right circle |
||
434 | write_char_xy(27, top_line+1, 0xCC); // small meters m home |
||
435 | write_char_xy(7, bottom_line, 0x9E); // small v |
||
436 | write_char_xy(14, bottom_line, 0xD1); // on clock |
||
407 | cascade | 437 | write_char_xy(21, bottom_line, 0xD2); // fly clock |
385 | cascade | 438 | write_char_xy(26, bottom_line, 0xC8); // sat1 |
439 | write_char_xy(27, bottom_line, 0xC9); // sat2 |
||
339 | cascade | 440 | COSD_FLAGS |= COSD_ICONS_WRITTEN; |
441 | } |
||
321 | cascade | 442 | if (rxd_buffer_locked) { |
339 | cascade | 443 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
444 | if (rxd_buffer[2] == 'D') { // FC Data |
||
445 | /*Decode64(); |
||
446 | debugData = *((DebugOut_t*) pRxData); |
||
447 | write_number_s(12, 2, RxDataLen); |
||
448 | write_number_s(20, 2, setsReceived++); |
||
449 | write_number_s(12, 3, debugData.Analog[0]); // AngleNick |
||
450 | write_number_s(12, 4, debugData.Analog[1]); // AngleRoll |
||
336 | cascade | 451 | write_number_s(12, 5, debugData.Analog[5]); // Height |
339 | cascade | 452 | write_number_s(12, 6, debugData.Analog[9]); // Voltage |
453 | write_number_s(12, 7, debugData.Analog[10]);// RC Signal |
||
388 | cascade | 454 | write_number_s(12, 8, debugData.Analog[11]);// Gyro compass |
455 | seconds_since_last_data = 0;*/ |
||
339 | cascade | 456 | } else if (rxd_buffer[2] == 'O') { // NC OSD Data |
457 | Decode64(); |
||
458 | naviData = *((NaviData_t*) pRxData); |
||
383 | cascade | 459 | |
453 | cascade | 460 | // init on first data retrival, distinguished by last battery :) |
461 | // TODO: this is testing stuff, strings should go progmem and so on... |
||
462 | if (last_UBat == 255) { |
||
463 | clear(); |
||
464 | // fix for min_bat glitch caused by data only gathered during motors up |
||
465 | min_UBat = naviData.UBat; |
||
466 | write_ascii_string(2, 2, "C-OSD Initialisation"); |
||
467 | uint8_t cellnum = 0; |
||
468 | if (CELL_NUM == -1) { |
||
469 | write_ascii_string(2, 5, "Guessing Number of Cells"); |
||
470 | do { |
||
471 | cellnum++; |
||
472 | } while (naviData.UBat > ((cellnum * CELL_VOLT_MAX) + 10)); |
||
473 | } else { |
||
474 | cellnum = CELL_NUM; |
||
475 | } |
||
476 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
477 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
478 | write_ascii_string(2, 7, "Number of Cells:"); |
||
479 | write_ndigit_number_u(21, 7, cellnum, 1, 0); |
||
480 | write_ascii_string(2, 8, "Warn Voltage :"); |
||
481 | write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0); |
||
482 | write_ascii_string(2, 9, "Max Voltage :"); |
||
483 | write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0); |
||
484 | _delay_ms(200); |
||
485 | clear(); |
||
486 | // update flags to paint display again because of clear |
||
487 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
488 | } |
||
489 | |||
339 | cascade | 490 | // first line |
373 | cascade | 491 | write_ndigit_number_u(2, top_line, (uint16_t) (((uint32_t) naviData.GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0); |
321 | cascade | 492 | |
349 | cascade | 493 | write_ndigit_number_u(7, top_line, naviData.RC_Quality, 100, 0); |
339 | cascade | 494 | if (naviData.RC_Quality <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) { |
495 | for (uint8_t x = 0; x < 4; x++) |
||
496 | write_char_att_xy(7 + x, top_line, BLINK); |
||
497 | } else if (naviData.RC_Quality > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) { |
||
498 | for (uint8_t x = 0; x < 4; x++) |
||
499 | write_char_att_xy(7 + x, top_line, 0); |
||
500 | } |
||
407 | cascade | 501 | last_RC_Quality = naviData.RC_Quality; |
502 | |||
503 | if (naviData.NCFlags & NC_FLAG_NOSERIALLINK) { |
||
504 | write_char_xy(11, top_line, 0); // clear |
||
505 | } else { |
||
506 | write_char_xy(11, top_line, 0xC6); // PC icon |
||
507 | } |
||
321 | cascade | 508 | |
349 | cascade | 509 | write_ndigit_number_u(13, top_line, naviData.CompassHeading, 100, 0); |
321 | cascade | 510 | |
339 | cascade | 511 | write_ascii_string(17, top_line, directions[heading_conv(naviData.CompassHeading)]); |
377 | cascade | 512 | |
383 | cascade | 513 | draw_variometer(21, top_line, naviData.Variometer); |
321 | cascade | 514 | |
339 | cascade | 515 | //note:lephisto:according to several sources it's /30 |
349 | cascade | 516 | if (naviData.Altimeter > 300 || naviData.Altimeter < -300) { |
339 | cascade | 517 | // above 10m only write full meters |
349 | cascade | 518 | write_ndigit_number_s(23, top_line, naviData.Altimeter / 30, 1000, 0); |
339 | cascade | 519 | } else { |
520 | // up to 10m write meters.dm |
||
349 | cascade | 521 | //write_number_u_10th(21, top_line, naviData.Altimeter / 3); |
522 | write_ndigit_number_s_10th(23, top_line, naviData.Altimeter / 3, 100, 0); |
||
339 | cascade | 523 | } |
321 | cascade | 524 | |
339 | cascade | 525 | // seccond line |
526 | draw_compass(11, top_line + 1, naviData.CompassHeading); |
||
321 | cascade | 527 | |
339 | cascade | 528 | // TODO: verify correctness |
529 | uint16_t heading_home = (naviData.HomePositionDeviation.Bearing + 360 - naviData.CompassHeading) % 360; |
||
383 | cascade | 530 | //write_char_xy(21, top_line + 1, arrowdir[heading_conv(heading_home)]); |
377 | cascade | 531 | // finer resolution, 0xa0 is first character and we add the index 0 <= index < 16 |
383 | cascade | 532 | write_char_xy(21, top_line + 1, 0xa0 + heading_fine_conv(heading_home)); |
321 | cascade | 533 | |
355 | shaddi | 534 | write_ndigit_number_u(24, top_line + 1, naviData.HomePositionDeviation.Distance / 10, 100, 0); |
321 | cascade | 535 | |
339 | cascade | 536 | // center |
537 | if (naviData.MKFlags & FLAG_MOTOR_RUN) { // should be engines running |
||
538 | if (!(old_MKFlags & FLAG_MOTOR_RUN)) { // motors just started, clear middle |
||
539 | clear(); |
||
540 | // update flags to paint display again if needed |
||
541 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
||
542 | } |
||
543 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
||
544 | draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll); |
||
545 | } |
||
407 | cascade | 546 | // motors are on, assume we were/are flying |
547 | COSD_FLAGS |= COSD_WASFLYING; |
||
339 | cascade | 548 | } else { |
336 | cascade | 549 | // stats |
407 | cascade | 550 | if ((COSD_FLAGS & COSD_WASFLYING) && (COSD_FLAGS & COSD_FLAG_STATS)) { |
412 | cascade | 551 | write_ascii_string_pgm(2, 4, stats_item_pointers[0]); // max Altitude |
552 | write_ndigit_number_s(18, 4, max_Altimeter / 30, 1000, 0); |
||
553 | write_char_xy(22, 4, 204); // small meters m |
||
554 | write_ascii_string_pgm(2, 5, stats_item_pointers[1]); // max Speed |
||
555 | write_ndigit_number_u(19, 5, (uint16_t) (((uint32_t) max_GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0); |
||
556 | write_char_xy(22, 5, 203); // km/h |
||
557 | write_ascii_string_pgm(2, 6, stats_item_pointers[2]); // max Distance |
||
558 | write_ndigit_number_u(19, 6, max_Distance / 10, 100, 0); |
||
559 | write_char_xy(22, 6, 204); // small meters m |
||
560 | write_ascii_string_pgm(2, 7, stats_item_pointers[3]); // min voltage |
||
561 | write_ndigit_number_u_10th(18, 7, min_UBat, 100, 0); |
||
562 | write_char_xy(22, 7, 0x9E); // small v |
||
563 | write_ascii_string_pgm(2, 8, stats_item_pointers[4]); // max time |
||
564 | write_time(16, 8, max_FlyingTime); |
||
565 | write_char_xy(22, 8, 210); // fly clock |
||
566 | write_ascii_string_pgm(2, 9, stats_item_pointers[5]); // longitude |
||
567 | write_gps_pos(15, 9, naviData.CurrentPosition.Longitude); |
||
568 | write_ascii_string_pgm(2, 10, stats_item_pointers[6]); // latitude |
||
569 | write_gps_pos(15, 10, naviData.CurrentPosition.Latitude); |
||
339 | cascade | 570 | } else if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { // if no stats there is space horizon |
571 | draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll); |
||
572 | } |
||
573 | } |
||
383 | cascade | 574 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
575 | draw_big_variometer(27, 8, naviData.Variometer); |
||
576 | } |
||
321 | cascade | 577 | |
339 | cascade | 578 | // bottom line |
453 | cascade | 579 | draw_battery(2, bottom_line, min_voltage, naviData.UBat, max_voltage); |
385 | cascade | 580 | write_ndigit_number_u_10th(3, bottom_line, naviData.UBat, 100, 0); |
453 | cascade | 581 | if (naviData.UBat <= min_voltage && last_UBat > min_voltage) { |
385 | cascade | 582 | for (uint8_t x = 2; x < 8; x++) |
339 | cascade | 583 | write_char_att_xy(x, bottom_line, BLINK); |
453 | cascade | 584 | } else if (naviData.UBat > min_voltage && last_UBat < min_voltage) { |
385 | cascade | 585 | for (uint8_t x = 2; x < 8; x++) |
339 | cascade | 586 | write_char_att_xy(x, bottom_line, 0); |
587 | } |
||
453 | cascade | 588 | // remember last values |
589 | last_UBat = naviData.UBat; |
||
321 | cascade | 590 | |
339 | cascade | 591 | write_time(8, bottom_line, uptime); |
407 | cascade | 592 | write_time(15, bottom_line, naviData.FlyingTime); |
321 | cascade | 593 | |
349 | cascade | 594 | write_ndigit_number_u(24, bottom_line, naviData.SatsInUse, 10, 0); |
321 | cascade | 595 | |
407 | cascade | 596 | if (naviData.NCFlags & NC_FLAG_MANUAL_CONTROL) { |
597 | write_char_xy(23, bottom_line, 0xB3); // rc transmitter |
||
598 | } else { |
||
599 | write_char_xy(23, bottom_line, 0); // clear |
||
600 | } |
||
601 | |||
339 | cascade | 602 | if (naviData.NCFlags & NC_FLAG_CH) { |
603 | write_char_xy(27, bottom_line, 231); // gps ch |
||
604 | } else if (naviData.NCFlags & NC_FLAG_PH) { |
||
605 | write_char_xy(27, bottom_line, 230); // gps ph |
||
606 | } else { // (naviData.NCFlags & NC_FLAG_FREE) |
||
607 | write_char_xy(27, bottom_line, 201); // sat2 (free) |
||
608 | } |
||
321 | cascade | 609 | |
339 | cascade | 610 | //write_number_s(8, 5, RxDataLen); |
611 | //write_number_s(16, 5, setsReceived++); |
||
321 | cascade | 612 | |
410 | shaddi | 613 | // remember statistics (only when engines running) |
412 | cascade | 614 | if (naviData.MKFlags & FLAG_MOTOR_RUN) { |
615 | if (naviData.Altimeter > max_Altimeter) max_Altimeter = naviData.Altimeter; |
||
616 | if (naviData.GroundSpeed > max_GroundSpeed) max_GroundSpeed = naviData.GroundSpeed; |
||
617 | if (naviData.HomePositionDeviation.Distance > max_Distance) { |
||
618 | max_Distance = naviData.HomePositionDeviation.Distance; |
||
619 | } |
||
620 | if (naviData.UBat < min_UBat) min_UBat = naviData.UBat; |
||
621 | if (naviData.FlyingTime > max_FlyingTime) max_FlyingTime = naviData.FlyingTime; |
||
622 | } |
||
339 | cascade | 623 | |
624 | old_MKFlags = naviData.MKFlags; |
||
388 | cascade | 625 | seconds_since_last_data = 0; |
339 | cascade | 626 | } |
627 | } |
||
321 | cascade | 628 | rxd_buffer_locked = 0; |
629 | } |
||
630 | // handle keypress |
||
326 | cascade | 631 | if (s1_pressed()) { |
339 | cascade | 632 | config_menu(); |
321 | cascade | 633 | } |
339 | cascade | 634 | if (seconds_since_last_data > 2) { |
346 | cascade | 635 | // request OSD Data from NC every 100ms |
636 | usart1_request_mk_data(1, 'o', 100); |
||
389 | cascade | 637 | seconds_since_last_data = 0; |
339 | cascade | 638 | } |
321 | cascade | 639 | } |
640 | #endif |
||
641 | return 0; |
||
642 | } |