Subversion Repositories Projects

Rev

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