Subversion Repositories Projects

Rev

Rev 454 | Rev 457 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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