Subversion Repositories Projects

Rev

Rev 471 | Rev 477 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 471 Rev 474
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
3
 *   admiralcascade@gmail.com                                               *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
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   *
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.         *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 *                                                                          *
19
 *                                                                          *
20
 *                                                                          *
20
 *                                                                          *
21
 *   Credits to:                                                            *
21
 *   Credits to:                                                            *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
24
 *   Klaus "akku" Buettner for the hardware                                 *
24
 *   Klaus "akku" Buettner for the hardware                                 *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
26
 ****************************************************************************/
26
 ****************************************************************************/
27
 
27
 
28
#include <avr/io.h>
28
#include <avr/io.h>
29
#include <avr/interrupt.h>
29
#include <avr/interrupt.h>
30
#include <util/delay.h>
30
#include <util/delay.h>
31
#include <avr/pgmspace.h>
31
#include <avr/pgmspace.h>
32
#include "main.h"
32
#include "main.h"
33
#include "max7456_software_spi.h"
33
#include "max7456_software_spi.h"
34
#include "usart1.h"
34
#include "usart1.h"
35
#include "osd_helpers.h"
35
#include "osd_helpers.h"
36
#include "config.h"
36
#include "config.h"
37
#include "spi.h"
37
#include "spi.h"
38
#include "buttons.h"
38
#include "buttons.h"
39
 
39
 
40
/* TODO:
40
/* TODO:
41
 * - verifiy correctness of values
41
 * - verifiy correctness of values
42
 * - clean up code :)
42
 * - clean up code :)
43
 */
43
 */
44
 
44
 
45
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
45
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
46
// data structs not needed for character flashing
46
// data structs not needed for character flashing
47
#include "mk-data-structs.h"
47
#include "mk-data-structs.h"
48
 
48
 
49
/* ##########################################################################
49
/* ##########################################################################
50
 * global definitions and global vars
50
 * global definitions and global vars
51
 * ##########################################################################*/
51
 * ##########################################################################*/
52
 
52
 
53
volatile uint16_t setsReceived = 0;
53
volatile uint16_t setsReceived = 0;
54
 
54
 
55
volatile NaviData_t naviData;
55
volatile NaviData_t naviData;
56
volatile DebugOut_t debugData;
56
volatile DebugOut_t debugData;
57
 
57
 
58
// cache old vars for blinking attribute, checkup is faster than full
58
// cache old vars for blinking attribute, checkup is faster than full
59
// attribute write each time
59
// attribute write each time
60
volatile uint8_t last_UBat = 255;
60
volatile uint8_t last_UBat = 255;
61
volatile uint8_t last_RC_Quality = 255;
61
volatile uint8_t last_RC_Quality = 255;
62
 
62
 
63
// 16bit should be enough, normal LiPos don't last that long
63
// 16bit should be enough, normal LiPos don't last that long
64
volatile uint16_t uptime = 0;
64
volatile uint16_t uptime = 0;
65
volatile uint16_t timer = 0;
65
volatile uint16_t timer = 0;
66
 
66
 
67
// remember last time data was received
67
// remember last time data was received
68
volatile uint8_t seconds_since_last_data = 0;
68
volatile uint8_t seconds_since_last_data = 0;
69
 
69
 
70
// store stats description in progmem to save space
70
// store stats description in progmem to save space
71
char stats_item_0[] PROGMEM = "max Altitude:";
71
char stats_item_0[] PROGMEM = "max Altitude:";
72
char stats_item_1[] PROGMEM = "max Speed   :";
72
char stats_item_1[] PROGMEM = "max Speed   :";
73
char stats_item_2[] PROGMEM = "max Distance:";
73
char stats_item_2[] PROGMEM = "max Distance:";
74
char stats_item_3[] PROGMEM = "min voltage :";
74
char stats_item_3[] PROGMEM = "min voltage :";
75
char stats_item_4[] PROGMEM = "max time    :";
75
char stats_item_4[] PROGMEM = "max time    :";
76
char stats_item_5[] PROGMEM = "longitude   :";
76
char stats_item_5[] PROGMEM = "longitude   :";
77
char stats_item_6[] PROGMEM = "latitude    :";
77
char stats_item_6[] PROGMEM = "latitude    :";
78
char stats_item_7[] PROGMEM = "max current :";
78
char stats_item_7[] PROGMEM = "max current :";
79
char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
79
char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
80
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
80
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
81
 
81
 
82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
83
 
83
 
84
// general PAL|NTSC distingiusch stuff
84
// general PAL|NTSC distingiusch stuff
85
uint8_t top_line = 1;
85
uint8_t top_line = 1;
86
uint8_t bottom_line = 14;
86
uint8_t bottom_line = 14;
87
 
87
 
88
// battery voltages
88
// battery voltages
89
uint8_t min_voltage = 0;
89
uint8_t min_voltage = 0;
90
uint8_t max_voltage = 0;
90
uint8_t max_voltage = 0;
91
 
91
 
92
// Flags
92
// Flags
93
uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0;
93
uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0;
94
 
94
 
95
/* ##########################################################################
95
/* ##########################################################################
96
 * Interrupt handler
96
 * Interrupt handler
97
 * ##########################################################################*/
97
 * ##########################################################################*/
98
 
98
 
99
/**
99
/**
100
 * handler for undefined Interrupts
100
 * handler for undefined Interrupts
101
 * if not defined AVR will reset in case any unhandled interrupts occur
101
 * if not defined AVR will reset in case any unhandled interrupts occur
102
 */
102
 */
103
ISR(__vector_default) {
103
ISR(__vector_default) {
104
    asm("nop");
104
    asm("nop");
105
}
105
}
106
 
106
 
107
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
107
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
108
/* ##########################################################################
108
/* ##########################################################################
109
 * timer stuff
109
 * timer stuff
110
 * ##########################################################################*/
110
 * ##########################################################################*/
111
 
111
 
112
/**
112
/**
113
 * timer kicks in every 1000uS ^= 1ms
113
 * timer kicks in every 1000uS ^= 1ms
114
 */
114
 */
115
ISR(TIMER0_OVF_vect) {
115
ISR(TIMER0_OVF_vect) {
116
    OCR0 = 15; // preload
116
    OCR0 = 15; // preload
117
    if (!timer--) {
117
    if (!timer--) {
118
        uptime++;
118
        uptime++;
119
        timer = 999;
119
        timer = 999;
120
        seconds_since_last_data++;
120
        seconds_since_last_data++;
121
    }
121
    }
122
    // in case there is still some spi data to send do it now
122
    // in case there is still some spi data to send do it now
123
    if (spi_ready && icnt) {
123
    if (spi_ready && icnt) {
124
        SPDR = *iptr;
124
        SPDR = *iptr;
125
    }
125
    }
126
}
126
}
127
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
127
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
128
 
128
 
129
/* ##########################################################################
129
/* ##########################################################################
130
 * MAIN
130
 * MAIN
131
 * ##########################################################################*/
131
 * ##########################################################################*/
132
int main(void) {
132
int main(void) {
133
 
133
 
134
    // set up FLAGS, compiler should flatten this one
134
    // set up FLAGS, compiler should flatten this one
135
    COSD_FLAGS = (NTSC << 0);
135
    COSD_FLAGS = (NTSC << 0);
136
    COSD_FLAGS |= (HUD << 1);
136
    COSD_FLAGS |= (HUD << 1);
137
    COSD_FLAGS |= (ARTHORIZON << 2);
137
    COSD_FLAGS |= (ARTHORIZON << 2);
138
    COSD_FLAGS |= (BIGVARIO << 3);
138
    COSD_FLAGS |= (BIGVARIO << 3);
139
    COSD_FLAGS |= (STATS << 4);
139
    COSD_FLAGS |= (STATS << 4);
140
    COSD_FLAGS |= (WARNINGS << 5);
140
    COSD_FLAGS |= (WARNINGS << 5);
141
 
141
 
142
    // set up Atmega162 Ports
142
    // set up Atmega162 Ports
143
    DDRA |= (1 << PA1); // PA1 output (/CS)
143
    DDRA |= (1 << PA1); // PA1 output (/CS)
144
    MAX_CS_HIGH
144
    MAX_CS_HIGH
145
    DDRA |= (1 << PA2); // PA2 output (SDIN)
145
    DDRA |= (1 << PA2); // PA2 output (SDIN)
146
    MAX_SDIN_LOW
146
    MAX_SDIN_LOW
147
    DDRA |= (1 << PA3); // PA3 output (SCLK)
147
    DDRA |= (1 << PA3); // PA3 output (SCLK)
148
    MAX_SCLK_LOW
148
    MAX_SCLK_LOW
149
    DDRA |= (1 << PA5); // PA5 output (RESET)
149
    DDRA |= (1 << PA5); // PA5 output (RESET)
150
    MAX_RESET_HIGH
150
    MAX_RESET_HIGH
151
 
151
 
152
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
152
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
153
    LED1_OFF
153
    LED1_OFF
154
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
154
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
155
    LED2_OFF
155
    LED2_OFF
156
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
156
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
157
    LED3_OFF
157
    LED3_OFF
158
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
158
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
159
    LED4_OFF
159
    LED4_OFF
160
 
160
 
161
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
161
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
162
    PORTC |= (1 << PC4); // pullup
162
    PORTC |= (1 << PC4); // pullup
163
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
163
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
164
    PORTC |= (1 << PC5); // pullup
164
    PORTC |= (1 << PC5); // pullup
165
 
-
 
166
    // set up top and bottom lines
-
 
167
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
-
 
168
        bottom_line = 12;
-
 
169
    } else {
-
 
170
        bottom_line = 14;
-
 
171
    }
-
 
172
 
165
 
173
    // reset the MAX7456 to be sure any undefined states do no harm
166
    // reset the MAX7456 to be sure any undefined states do no harm
174
    MAX_RESET_LOW
167
    MAX_RESET_LOW
175
    MAX_RESET_HIGH
168
    MAX_RESET_HIGH
176
 
169
 
177
    // give the FC/NC and the maxim time to come up
170
    // give the FC/NC and the maxim time to come up
178
    LED4_ON
171
    LED4_ON
179
    _delay_ms(2000);
172
    _delay_ms(2000);
180
 
-
 
181
    LED4_OFF
173
    LED4_OFF
182
 
-
 
183
 
174
 
184
    //Pushing NEW chars to the MAX7456
175
    //Pushing NEW chars to the MAX7456
185
#if (WRITECHARS != -1)
176
#if (WRITECHARS != -1)
186
            // DISABLE display (VM0)
177
        // DISABLE display (VM0)
187
            spi_send_byte(0x00, 0b00000000);
178
        spi_send_byte(0x00, 0b00000000);
-
 
179
#include "characters.c"
-
 
180
#else
-
 
181
        // read out config for NTSC/PAL distinguishing
188
#include "characters.c"
182
        get_eeprom(0);
189
#endif
183
#endif
190
 
184
 
191
    // Setup Video Mode
185
        // Setup Video Mode
192
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
186
        if (COSD_FLAGS & COSD_FLAG_NTSC) {
-
 
187
            // NTSC + enable display immediately (VM0)
-
 
188
            spi_send_byte(0x00, 0b00001000);
193
        // NTSC + enable display immediately (VM0)
189
 
194
        spi_send_byte(0x00, 0b00001000);
190
                bottom_line = 12;
195
    } else {
191
        } else {
-
 
192
            // PAL + enable display immediately (VM0)
-
 
193
            spi_send_byte(0x00, 0b01001000);
196
        // PAL + enable display immediately (VM0)
194
 
197
        spi_send_byte(0x00, 0b01001000);
195
                bottom_line = 14;
198
    }
196
        }
199
 
197
 
200
    /*// clear all display-mem (DMM)
198
    /*// clear all display-mem (DMM)
201
    spi_send_byte(0x04, 0b00000100);
199
    spi_send_byte(0x04, 0b00000100);
202
 
200
 
203
    // clearing takes 12uS according to maxim so lets wait longer
201
    // clearing takes 12uS according to maxim so lets wait longer
204
    _delay_us(120);
202
    _delay_us(120);
205
 
203
 
206
    // 8bit mode
204
    // 8bit mode
207
    spi_send_byte(0x04, 0b01000000);*/
205
    spi_send_byte(0x04, 0b01000000);*/
208
 
206
 
209
    // clear display memory and set to 8bit mode
207
    // clear display memory and set to 8bit mode
210
    clear();
208
    clear();
211
 
209
 
212
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
210
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
213
    // init usart
211
    // init usart
214
    usart1_init();
212
    usart1_init();
215
 
213
 
216
    // set up timer
214
    // set up timer
217
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
215
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
218
    OCR0 = 15; // preload
216
    OCR0 = 15; // preload
219
    TIMSK |= (1 << TOIE0); // enable overflow timer0
217
    TIMSK |= (1 << TOIE0); // enable overflow timer0
220
 
218
 
221
    DDRD |= (1 << PD2); // PD2 output (INT0)
219
    DDRD |= (1 << PD2); // PD2 output (INT0)
222
    SpiMasterInit();
220
    SpiMasterInit();
223
 
221
 
224
    // enable interrupts
222
    // enable interrupts
225
    sei();
223
    sei();
226
 
224
 
227
#endif
225
#endif
228
 
226
 
229
    //write_ascii_string(2,  7, "         CaScAdE          ");
227
    //write_ascii_string(2,  7, "         CaScAdE          ");
230
    //write_ascii_string(2,  8, "is TESTING his open source");
228
    //write_ascii_string(2,  8, "is TESTING his open source");
231
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
229
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
232
 
230
 
233
    // we are ready
231
    // we are ready
234
    LED3_ON
232
    LED3_ON
235
 
233
 
236
#if ALLCHARSDEBUG | (WRITECHARS != -1)
234
#if ALLCHARSDEBUG | (WRITECHARS != -1)
237
        clear();
235
        clear();
238
    write_all_chars();
236
    write_all_chars();
239
#else
237
#else
240
        // clear serial screen
238
        // clear serial screen
241
        //usart1_puts("\x1B[2J\x1B[H");
239
        //usart1_puts("\x1B[2J\x1B[H");
242
        //usart1_puts("hello world!\r\n");
240
        //usart1_puts("hello world!\r\n");
243
#if FCONLY
241
#if FCONLY
244
        // request data ever 100ms from FC;
242
        // request data ever 100ms from FC;
245
        usart1_request_mk_data(0, 'd', 100);
243
        usart1_request_mk_data(0, 'd', 100);
246
#else
244
#else
247
        // request OSD Data from NC every 100ms
245
        // request OSD Data from NC every 100ms
248
        usart1_request_mk_data(1, 'o', 100);
246
        usart1_request_mk_data(1, 'o', 100);
249
 
247
 
250
    // and disable debug...
248
    // and disable debug...
251
    usart1_request_mk_data(0, 'd', 0);
249
    usart1_request_mk_data(0, 'd', 0);
252
#endif
250
#endif
253
 
251
 
254
    // stats for after flight
252
    // stats for after flight
255
    int16_t max_Altimeter = 0;
253
    int16_t max_Altimeter = 0;
256
    uint8_t min_UBat = 255;
254
    uint8_t min_UBat = 255;
257
#if !(FCONLY)
255
#if !(FCONLY)
258
    uint16_t max_GroundSpeed = 0;
256
    uint16_t max_GroundSpeed = 0;
259
    int16_t max_Distance = 0;
257
    int16_t max_Distance = 0;
260
    uint16_t max_FlyingTime = 0;
258
    uint16_t max_FlyingTime = 0;
261
 
259
 
262
    // flags from last round to check for changes
260
    // flags from last round to check for changes
263
    uint8_t old_MKFlags = 0;
261
    uint8_t old_MKFlags = 0;
264
 
262
 
265
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
263
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
266
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
264
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
267
#endif
265
#endif
268
 
266
 
269
    while (1) {
267
    while (1) {
270
        // in case SPI is ready and there is nothing to send right now
268
        // in case SPI is ready and there is nothing to send right now
271
        if (!icnt && spi_ready) {
269
        if (!icnt && spi_ready) {
272
            // correct transfer ends with d (done)
270
            // correct transfer ends with d (done)
273
            if (spi_cmd_buffer[0] == 'd') {
271
            if (spi_cmd_buffer[0] == 'd') {
274
                ampere = spi_cmd_buffer[1] << 8;
272
                ampere = spi_cmd_buffer[1] << 8;
275
                ampere |= spi_cmd_buffer[2];
273
                ampere |= spi_cmd_buffer[2];
276
                                ampere_wasted = spi_cmd_buffer[3];
274
                                ampere_wasted = spi_cmd_buffer[3];
277
                                ampere_wasted = ampere_wasted << 8;
275
                                ampere_wasted = ampere_wasted << 8;
278
                                ampere_wasted |= spi_cmd_buffer[4];
276
                                ampere_wasted |= spi_cmd_buffer[4];
279
                                ampere_wasted = ampere_wasted << 8;
277
                                ampere_wasted = ampere_wasted << 8;
280
                                ampere_wasted |= spi_cmd_buffer[5];
278
                                ampere_wasted |= spi_cmd_buffer[5];
281
                                ampere_wasted = ampere_wasted << 8;
279
                                ampere_wasted = ampere_wasted << 8;
282
                                ampere_wasted |= spi_cmd_buffer[6];
280
                                ampere_wasted |= spi_cmd_buffer[6];
283
                                //ampere = *((int16_t *) spi_cmd_buffer + 1);
281
                                //ampere = *((int16_t *) spi_cmd_buffer + 1);
284
                                //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3);
282
                                //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3);
285
                                // if this is the first receival we should print the small A
283
                                // if this is the first receival we should print the small A
286
                                if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) {
284
                                if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) {
-
 
285
                                        clear();
287
                                        COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
286
                                        COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
288
                                        // update this flag
287
                                        // update this flag
289
                        COSD_FLAGS2 |= COSD_FLAG_STROMREC;
288
                        COSD_FLAGS2 |= COSD_FLAG_STROMREC;
290
                                }
289
                                }
291
                //write_ascii_char(8+4*30, 'v'); // valid
290
                //write_ascii_char(8+4*30, 'v'); // valid
292
            } else {
291
            } else {
293
                // update flags
292
                // update flags
294
                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
293
                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
295
                //write_ascii_char(8+4*30, 'i'); // invalid
294
                //write_ascii_char(8+4*30, 'i'); // invalid
296
            }
295
            }
297
            spi_cmd_buffer[0] = 'A';
296
            spi_cmd_buffer[0] = 'A';
298
            spi_cmd_buffer[1] = 'B';
297
            spi_cmd_buffer[1] = 'B';
299
            spi_cmd_buffer[2] = 'C';
298
            spi_cmd_buffer[2] = 'C';
300
                        spi_cmd_buffer[3] = 'D';
299
                        spi_cmd_buffer[3] = 'D';
301
                        spi_cmd_buffer[4] = 'E';
300
                        spi_cmd_buffer[4] = 'E';
302
                        spi_cmd_buffer[5] = 'F';
301
                        spi_cmd_buffer[5] = 'F';
303
                        spi_cmd_buffer[6] = 'G';
302
                        spi_cmd_buffer[6] = 'G';
304
            StartTransfer((unsigned char*) spi_cmd_buffer, 7);
303
            StartTransfer((unsigned char*) spi_cmd_buffer, 7);
305
        }
304
        }
306
        if (rxd_buffer_locked) {
305
        if (rxd_buffer_locked) {
307
#if FCONLY
306
#if FCONLY
308
            if (rxd_buffer[2] == 'D') { // FC Data
307
            if (rxd_buffer[2] == 'D') { // FC Data
309
                Decode64();
308
                Decode64();
310
                debugData = *((DebugOut_t*) pRxData);
309
                debugData = *((DebugOut_t*) pRxData);
311
 
310
 
312
                // init on first data retrival, distinguished by last battery :)
311
                // init on first data retrival, distinguished by last battery :)
313
                if (last_UBat == 255) {
312
                if (last_UBat == 255) {
314
                    // fix for min_UBat
313
                    // fix for min_UBat
315
                    min_UBat = debugData.Analog[9];
314
                    min_UBat = debugData.Analog[9];
316
                    init_cosd(debugData.Analog[9]);
315
                    init_cosd(debugData.Analog[9]);
317
                }
316
                }
318
                #include "osd_fcmode_default.c"
317
                #include "osd_fcmode_default.c"
319
            }
318
            }
320
#else
319
#else
321
            if (rxd_buffer[2] == 'O') { // NC OSD Data
320
            if (rxd_buffer[2] == 'O') { // NC OSD Data
322
                Decode64();
321
                Decode64();
323
                naviData = *((NaviData_t*) pRxData);
322
                naviData = *((NaviData_t*) pRxData);
324
 
323
 
325
                // init on first data retrival, distinguished by last battery :)
324
                // init on first data retrival, distinguished by last battery :)
326
                if (last_UBat == 255) {
325
                if (last_UBat == 255) {
327
                    // fix for min_UBat
326
                    // fix for min_UBat
328
                    min_UBat = naviData.UBat;
327
                    min_UBat = naviData.UBat;
329
                    init_cosd(naviData.UBat);
328
                    init_cosd(naviData.UBat);
330
                }
329
                }
331
                #include "osd_ncmode_default.c"
330
                #include "osd_ncmode_default.c"
332
            }
331
            }
333
#endif
332
#endif
334
            rxd_buffer_locked = 0;
333
            rxd_buffer_locked = 0;
335
        }
334
        }
336
        // handle keypress
335
        // handle keypress
337
        if (s1_pressed()) {
336
        if (s1_pressed()) {
338
            config_menu();
337
            config_menu();
339
        }
338
        }
340
        if (seconds_since_last_data > 2) {
339
        if (seconds_since_last_data > 2) {
341
#if FCONLY
340
#if FCONLY
342
            // request data ever 100ms from FC;
341
            // request data ever 100ms from FC;
343
            usart1_request_mk_data(0, 'd', 100);
342
            usart1_request_mk_data(0, 'd', 100);
344
#else
343
#else
345
            // request OSD Data from NC every 100ms
344
            // request OSD Data from NC every 100ms
346
            usart1_request_mk_data(1, 'o', 100);
345
            usart1_request_mk_data(1, 'o', 100);
347
 
346
 
348
            // and disable debug...
347
            // and disable debug...
349
            usart1_request_mk_data(0, 'd', 0);
348
            usart1_request_mk_data(0, 'd', 0);
350
#endif
349
#endif
351
        }
350
        }
352
    }
351
    }
353
#endif
352
#endif
354
    return 0;
353
    return 0;
355
}
354
}
356
 
355