Subversion Repositories Projects

Rev

Rev 465 | Rev 467 | 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))
466 cascade 43
// data structs not needed for character flashing
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
465 cascade 93
uint8_t COSD_FLAGS = 0, COSD_FLAGS2;
326 cascade 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
 
465 cascade 131
 
132
/* ##########################################################################
133
 * STROM TEST
134
 * ##########################################################################*/
135
#define INT0_HIGH                       PORTD |=  (1 << PD2);
136
#define INT0_LOW                        PORTD &= ~(1 << PD2);
137
 
466 cascade 138
void SpiMasterInit(void) {
139
    volatile char IOReg;
140
    // set PB4(/SS), PB5(MOSI), PB7(SCK) as output
141
    DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7);
142
    PORTB |= (1 << PB4); // pullup SS
143
    // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128
144
    SPCR = (1 << SPIE) | (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1);
145
    IOReg = SPSR; // clear SPIF bit in SPSR
146
    IOReg = SPDR;
147
    //sei(); // we do it later
465 cascade 148
}
149
 
150
volatile size_t icnt = 0;
151
volatile unsigned char * iptr;
466 cascade 152
volatile unsigned char spi_cmd_buffer[5];
465 cascade 153
volatile uint8_t spi_ready = 1;
154
int16_t ampere = 0;
155
 
466 cascade 156
/**
157
 * SPI interrupt handler
158
 */
159
ISR(SPI_STC_vect) {
160
    *iptr++ = SPDR; // safe received byte to buffer
161
    icnt--; // dec length
465 cascade 162
    if (icnt) {
466 cascade 163
        //SPDR = *iptr; // send next byte
164
        spi_ready = 1; // we _should_ send later because the slave needs more time
165
    } else {
166
        SPCR &= ~_BV(SPIE); // deactivate interrupt
167
        INT0_HIGH // transfer is done, slave does not need to listen
465 cascade 168
    }
169
}
170
 
466 cascade 171
/**
172
 * check if SPI transfer is still busy
173
 */
174
int TransferIsBusy(void) {
465 cascade 175
    return SPCR & _BV(SPIE);
176
}
177
 
466 cascade 178
/**
179
 * start a new transfer of <data> with length <len>
180
 */
181
void StartTransfer(unsigned char *data, size_t len) {
182
    INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen
183
 
184
    // set up pointer and length for interrupt handler
465 cascade 185
    iptr = data;
186
    icnt = len;
466 cascade 187
 
188
    SPCR |= _BV(SPIE); // enable spi interrupt
189
    SPDR = *iptr; // start transfer by first bye
465 cascade 190
}
191
 
192
 
193
/* ##########################################################################
194
 * / STROM TEST END
195
 * ##########################################################################*/
196
 
331 cascade 197
/**
198
 * timer kicks in every 1000uS ^= 1ms
321 cascade 199
 */
200
ISR(TIMER0_OVF_vect) {
385 cascade 201
    OCR0 = 15; // preload
321 cascade 202
    if (!timer--) {
203
        uptime++;
204
        timer = 999;
455 cascade 205
        seconds_since_last_data++;
321 cascade 206
    }
466 cascade 207
    // in case there is still some spi data to send do it now
208
    if (spi_ready && icnt) {
209
        SPDR = *iptr;
210
    }
321 cascade 211
}
212
 
213
/* ##########################################################################
326 cascade 214
 * A simple config menu for the flags
215
 * ##########################################################################*/
339 cascade 216
 
217
/**
218
 * helper function for menu updating
219
 */
220
void config_menu_drawings(uint8_t chosen) {
221
    // clear prevoius _cursor_
383 cascade 222
    write_ascii_string(3, (chosen + 5) % 8, " ");
339 cascade 223
    // draw current _cursor_
383 cascade 224
    write_ascii_string(3, chosen + 5, ">");
339 cascade 225
    if (COSD_FLAGS & COSD_FLAG_HUD) {
383 cascade 226
        write_ascii_string_pgm(23, 5, ON);
227
    } else {
455 cascade 228
        write_ascii_string_pgm(23, 5, OFF);
383 cascade 229
    }
230
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
346 cascade 231
        write_ascii_string_pgm(23, 6, ON);
339 cascade 232
    } else {
383 cascade 233
        write_ascii_string_pgm(23, 6, OFF);
339 cascade 234
    }
455 cascade 235
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
346 cascade 236
        write_ascii_string_pgm(23, 7, ON);
339 cascade 237
    } else {
455 cascade 238
        write_ascii_string_pgm(23, 7, OFF);
339 cascade 239
    }
455 cascade 240
    if (COSD_FLAGS & COSD_FLAG_STATS) {
346 cascade 241
        write_ascii_string_pgm(23, 8, ON);
339 cascade 242
    } else {
346 cascade 243
        write_ascii_string_pgm(23, 8, OFF);
339 cascade 244
    }
245
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
346 cascade 246
        write_ascii_string_pgm(23, 9, ON);
339 cascade 247
    } else {
346 cascade 248
        write_ascii_string_pgm(23, 9, OFF);
339 cascade 249
    }
250
}
251
 
252
/**
346 cascade 253
 * some sort of clicking response in the menu
254
 */
255
void config_menu_doclick(uint8_t chosen, char** menu) {
455 cascade 256
    write_ascii_string(4, chosen + 5, "DONE              ");
257
    _delay_ms(1000);
258
    write_ascii_string(4, chosen + 5, menu[chosen]);
346 cascade 259
}
260
 
261
/**
339 cascade 262
 * a simple config menu tryout
263
 */
326 cascade 264
void config_menu(void) {
455 cascade 265
    // disable interrupts (makes the menu more smoothely)
266
    cli();
321 cascade 267
 
455 cascade 268
    // clear screen
269
    clear();
326 cascade 270
 
455 cascade 271
    char* menu[9] = {"Full HUD",
272
        "Art.Horizon in HUD",
273
        "Big Vario bar",
274
        "Statistics",
275
        "Warnings", // TODO: do it!
276
        "Reset uptime",
277
        "Request OSD-data",
278
        "Disable Debug-data",
279
        "EXIT"};
326 cascade 280
 
455 cascade 281
    uint8_t inmenu = 1;
282
    uint8_t chosen = 0;
283
    write_ascii_string(6, 2, "C-OSD Config Menu");
326 cascade 284
 
455 cascade 285
    // wait a bit before doing stuff so user has chance to release button
286
    _delay_ms(250);
339 cascade 287
 
455 cascade 288
    write_ascii_string(4, 5, menu[0]);
289
    write_ascii_string(4, 6, menu[1]);
290
    write_ascii_string(4, 7, menu[2]);
291
    write_ascii_string(4, 8, menu[3]);
292
    write_ascii_string(4, 9, menu[4]);
293
    write_ascii_string(4, 10, menu[5]);
294
    write_ascii_string(4, 11, menu[6]);
295
    write_ascii_string(4, 12, menu[7]);
296
    write_ascii_string(4, 13, menu[8]);
339 cascade 297
 
455 cascade 298
    config_menu_drawings(chosen);
299
 
300
    while (inmenu) {
301
        if (s2_pressed()) {
302
            write_ascii_string(3, chosen + 5, " ");
303
            chosen = (chosen + 1) % 9;
304
            write_ascii_string(3, chosen + 5, ">");
305
            _delay_ms(500);
306
        } else if (s1_pressed()) {
307
            switch (chosen) {
308
                case 0: // full HUD
309
                    COSD_FLAGS ^= COSD_FLAG_HUD;
310
                    config_menu_drawings(chosen);
311
                    break;
312
                case 1: // art horizon
313
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
314
                    config_menu_drawings(chosen);
315
                    break;
316
                case 2: // big vario
317
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
318
                    config_menu_drawings(chosen);
319
                    break;
320
                case 3: // statistics
321
                    COSD_FLAGS ^= COSD_FLAG_STATS;
322
                    config_menu_drawings(chosen);
323
                    break;
324
                case 4: // warnings
325
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
326
                    config_menu_drawings(chosen);
327
                    break;
328
                case 5: // reset uptime
329
                    uptime = 0;
330
                    config_menu_doclick(chosen, menu);
331
                    break;
332
                case 6: // re-request OSD data
454 cascade 333
#if FCONLY
455 cascade 334
                    // request data ever 100ms from FC;
335
                    usart1_request_mk_data(0, 'd', 100);
454 cascade 336
#else
455 cascade 337
                    // request OSD Data from NC every 100ms
338
                    usart1_request_mk_data(1, 'o', 100);
454 cascade 339
 
455 cascade 340
                    // and disable debug...
341
                    usart1_request_mk_data(0, 'd', 0);
454 cascade 342
#endif
455 cascade 343
                    config_menu_doclick(chosen, menu);
344
                    break;
345
                case 7: // disable debug data
346
                    // disable sending of debug data
347
                    // may result in smoother ddata display
348
                    usart1_request_mk_data(0, 'd', 0);
349
                    config_menu_doclick(chosen, menu);
350
                    break;
351
                case 8: // exit
352
                    inmenu = 0;
353
                    break;
354
            }
355
            _delay_ms(250);
356
        }
357
    }
326 cascade 358
 
455 cascade 359
    // clear screen up again
360
    clear();
326 cascade 361
 
455 cascade 362
    // update flags to paint display again if needed
363
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
326 cascade 364
 
455 cascade 365
    // enable interrupts again
366
    sei();
326 cascade 367
}
368
 
455 cascade 369
/**
370
 * auto config some stuff on startup, currently only battery cells
371
 * TODO: this is testing stuff, strings should go progmem and so on...
372
 */
459 cascade 373
void auto_config(uint8_t UBat) {
455 cascade 374
    clear();
375
    write_ascii_string(2, 2, "C-OSD Initialisation");
376
#if FCONLY
377
    write_ascii_string(2, 3, "FC only Mode");
378
#else
379
    write_ascii_string(2, 3, "NaviCtrl Mode");
380
#endif
466 cascade 381
    write_ascii_string(2, 4, BUILDDATE);
455 cascade 382
    uint8_t cellnum = 0;
383
    if (CELL_NUM == -1) {
459 cascade 384
        write_ascii_string(2, 6, "Guessing Number of Cells");
455 cascade 385
        do {
386
            cellnum++;
466 cascade 387
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23));
455 cascade 388
    } else {
389
        cellnum = CELL_NUM;
390
    }
391
    min_voltage = cellnum * CELL_VOLT_MIN;
392
    max_voltage = cellnum * CELL_VOLT_MAX;
393
    write_ascii_string(2, 7, "Number of Cells:");
394
    write_ndigit_number_u(21, 7, cellnum, 1, 0);
395
    write_ascii_string(2, 8, "Warn Voltage   :");
396
    write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
397
    write_ascii_string(2, 9, "Max Voltage    :");
398
    write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
399
    _delay_ms(200);
400
    clear();
401
    // update flags to paint display again because of clear
402
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
403
}
404
 
465 cascade 405
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
455 cascade 406
 
321 cascade 407
/* ##########################################################################
408
 * MAIN
409
 * ##########################################################################*/
410
int main(void) {
466 cascade 411
 
339 cascade 412
    // set up FLAGS, compiler should flatten this one
413
    COSD_FLAGS = (NTSC << 0);
414
    COSD_FLAGS |= (HUD << 1);
415
    COSD_FLAGS |= (ARTHORIZON << 2);
455 cascade 416
    COSD_FLAGS |= (BIGVARIO << 3);
417
    COSD_FLAGS |= (STATS << 4);
383 cascade 418
    COSD_FLAGS |= (WARNINGS << 5);
324 cascade 419
 
339 cascade 420
    // set up Atmega162 Ports
321 cascade 421
    DDRA |= (1 << PA1); // PA1 output (/CS)
422
    MAX_CS_HIGH
423
    DDRA |= (1 << PA2); // PA2 output (SDIN)
424
    MAX_SDIN_LOW
425
    DDRA |= (1 << PA3); // PA3 output (SCLK)
426
    MAX_SCLK_LOW
427
    DDRA |= (1 << PA5); // PA5 output (RESET)
428
    MAX_RESET_HIGH
429
 
430
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
431
    LED1_OFF
432
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
433
    LED2_OFF
434
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
435
    LED3_OFF
436
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
437
    LED4_OFF
438
 
439
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
440
    PORTC |= (1 << PC4); // pullup
441
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
442
    PORTC |= (1 << PC5); // pullup
443
 
339 cascade 444
    // set up top and bottom lines
445
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
446
        bottom_line = 12;
447
    } else {
448
        bottom_line = 14;
449
    }
326 cascade 450
 
339 cascade 451
    // reset the MAX7456 to be sure any undefined states do no harm
321 cascade 452
    MAX_RESET_LOW
453
    MAX_RESET_HIGH
454
 
455
    // give the FC/NC and the maxim time to come up
456
    LED4_ON
457
    _delay_ms(2000);
458
 
459
    LED4_OFF
460
 
331 cascade 461
 
339 cascade 462
    //Pushing NEW chars to the MAX7456
329 cascade 463
#if (WRITECHARS != -1)
466 cascade 464
            // DISABLE display (VM0)
465
            spi_send_byte(0x00, 0b00000000);
466
#include "characters.c"
455 cascade 467
#endif
321 cascade 468
 
339 cascade 469
    // Setup Video Mode
470
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
471
        // NTSC + enable display immediately (VM0)
472
        spi_send_byte(0x00, 0b00001000);
473
    } else {
474
        // PAL + enable display immediately (VM0)
475
        spi_send_byte(0x00, 0b01001000);
476
    }
321 cascade 477
 
404 cascade 478
    /*// clear all display-mem (DMM)
403 cascade 479
    spi_send_byte(0x04, 0b00000100);
480
 
481
    // clearing takes 12uS according to maxim so lets wait longer
482
    _delay_us(120);
483
 
321 cascade 484
    // 8bit mode
404 cascade 485
    spi_send_byte(0x04, 0b01000000);*/
321 cascade 486
 
404 cascade 487
    // clear display memory and set to 8bit mode
321 cascade 488
    clear();
489
 
329 cascade 490
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
321 cascade 491
    // init usart
492
    usart1_init();
493
 
494
    // set up timer
495
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
385 cascade 496
    OCR0 = 15; // preload
321 cascade 497
    TIMSK |= (1 << TOIE0); // enable overflow timer0
498
 
466 cascade 499
    DDRD |= (1 << PD2); // PD2 output (INT0)
500
    SpiMasterInit();
465 cascade 501
 
326 cascade 502
    // enable interrupts
321 cascade 503
    sei();
466 cascade 504
 
321 cascade 505
#endif
506
 
507
    //write_ascii_string(2,  7, "         CaScAdE          ");
508
    //write_ascii_string(2,  8, "is TESTING his open source");
509
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
510
 
511
    // we are ready
512
    LED3_ON
513
 
329 cascade 514
#if ALLCHARSDEBUG | (WRITECHARS != -1)
466 cascade 515
            clear();
321 cascade 516
    write_all_chars();
517
#else
466 cascade 518
            // clear serial screen
519
            //usart1_puts("\x1B[2J\x1B[H");
520
            //usart1_puts("hello world!\r\n");
454 cascade 521
#if FCONLY
466 cascade 522
            // request data ever 100ms from FC;
523
            usart1_request_mk_data(0, 'd', 100);
454 cascade 524
#else
466 cascade 525
            // request OSD Data from NC every 100ms
526
            usart1_request_mk_data(1, 'o', 100);
346 cascade 527
 
339 cascade 528
    // and disable debug...
455 cascade 529
    usart1_request_mk_data(0, 'd', 0);
454 cascade 530
#endif
321 cascade 531
 
339 cascade 532
    // stats for after flight
533
    int16_t max_Altimeter = 0;
459 cascade 534
    uint8_t min_UBat = 255;
535
#if !(FCONLY)
466 cascade 536
    uint16_t max_GroundSpeed = 0;
339 cascade 537
    int16_t max_Distance = 0;
538
    uint16_t max_FlyingTime = 0;
321 cascade 539
 
339 cascade 540
    // flags from last round to check for changes
541
    uint8_t old_MKFlags = 0;
542
 
321 cascade 543
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
377 cascade 544
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
459 cascade 545
#endif
321 cascade 546
 
465 cascade 547
 
548
 
321 cascade 549
    while (1) {
466 cascade 550
        // in case SPI is ready and there is nothing to send right now
551
        if (!icnt && spi_ready) {
552
            // correct transfer ends with d (done)
553
            if (spi_cmd_buffer[0] == 'd') {
554
                ampere = spi_cmd_buffer[1] << 8;
555
                ampere |= spi_cmd_buffer[2];
556
                // update flags
557
                COSD_FLAGS2 |= COSD_FLAG_STROMREC;
558
                //write_ascii_char(8+4*30, 'v'); // valid
559
            } else {
560
                // update flags
561
                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
562
                //write_ascii_char(8+4*30, 'i'); // invalid
563
            }
564
            spi_cmd_buffer[0] = 'A';
565
            spi_cmd_buffer[1] = 'B';
566
            spi_cmd_buffer[2] = 'C';
567
            StartTransfer((unsigned char*) spi_cmd_buffer, 3);
568
        }
321 cascade 569
        if (rxd_buffer_locked) {
339 cascade 570
            if (COSD_FLAGS & COSD_FLAG_HUD) {
454 cascade 571
#if FCONLY
339 cascade 572
                if (rxd_buffer[2] == 'D') { // FC Data
454 cascade 573
                    Decode64();
339 cascade 574
                    debugData = *((DebugOut_t*) pRxData);
454 cascade 575
 
455 cascade 576
                    // init on first data retrival, distinguished by last battery :)
577
                    if (last_UBat == 255) {
466 cascade 578
                        // fix for min_UBat
579
                        min_UBat = debugData.Analog[9];
459 cascade 580
                        auto_config(debugData.Analog[9]);
455 cascade 581
                    }
466 cascade 582
                    #include "osd_fcmode_default.c"
454 cascade 583
                }
584
#else
466 cascade 585
                if (rxd_buffer[2] == 'O') { // NC OSD Data
586
                    Decode64();
587
                    naviData = *((NaviData_t*) pRxData);
453 cascade 588
 
466 cascade 589
                    // init on first data retrival, distinguished by last battery :)
590
                    if (last_UBat == 255) {
591
                        // fix for min_UBat
592
                        min_UBat = naviData.UBat;
593
                        auto_config(naviData.UBat);
594
                    }
595
                    #include "osd_ncmode_default.c"
596
                }
454 cascade 597
#endif
339 cascade 598
            }
321 cascade 599
            rxd_buffer_locked = 0;
600
        }
601
        // handle keypress
326 cascade 602
        if (s1_pressed()) {
339 cascade 603
            config_menu();
321 cascade 604
        }
339 cascade 605
        if (seconds_since_last_data > 2) {
454 cascade 606
#if FCONLY
455 cascade 607
            // request data ever 100ms from FC;
608
            usart1_request_mk_data(0, 'd', 100);
454 cascade 609
#else
455 cascade 610
            // request OSD Data from NC every 100ms
611
            usart1_request_mk_data(1, 'o', 100);
454 cascade 612
 
455 cascade 613
            // and disable debug...
614
            usart1_request_mk_data(0, 'd', 0);
454 cascade 615
#endif
339 cascade 616
        }
321 cascade 617
    }
618
#endif
619
    return 0;
620
}