Subversion Repositories Projects

Rev

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

Rev 489 Rev 495
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
#include "ppm.h"
39
#include "ppm.h"
40
 
40
 
41
/* TODO:
41
/* TODO:
42
 * - verifiy correctness of values
42
 * - verifiy correctness of values
43
 * - clean up code :)
43
 * - clean up code :)
44
 */
44
 */
45
 
45
 
46
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
46
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
47
// data structs not needed for character flashing
47
// data structs not needed for character flashing
48
#include "mk-data-structs.h"
48
#include "mk-data-structs.h"
49
 
49
 
50
/* ##########################################################################
50
/* ##########################################################################
51
 * global definitions and global vars
51
 * global definitions and global vars
52
 * ##########################################################################*/
52
 * ##########################################################################*/
53
 
53
 
54
volatile uint16_t setsReceived = 0;
54
volatile uint16_t setsReceived = 0;
55
 
55
 
56
volatile NaviData_t naviData;
56
volatile NaviData_t naviData;
57
volatile DebugOut_t debugData;
57
volatile DebugOut_t debugData;
58
 
58
 
59
// cache old vars for blinking attribute, checkup is faster than full
59
// cache old vars for blinking attribute, checkup is faster than full
60
// attribute write each time
60
// attribute write each time
61
volatile uint8_t last_UBat = 255;
61
volatile uint8_t last_UBat = 255;
62
volatile uint8_t last_RC_Quality = 255;
62
volatile uint8_t last_RC_Quality = 255;
63
 
63
 
64
// 16bit should be enough, normal LiPos don't last that long
64
// 16bit should be enough, normal LiPos don't last that long
65
volatile uint16_t uptime = 0;
65
volatile uint16_t uptime = 0;
66
volatile uint16_t timer = 0;
66
volatile uint16_t timer = 0;
67
 
67
 
68
// remember last time data was received
68
// remember last time data was received
69
volatile uint8_t seconds_since_last_data = 0;
69
volatile uint8_t seconds_since_last_data = 0;
70
 
70
 
71
// store stats description in progmem to save space
71
// store stats description in progmem to save space
72
const char stats_item_0[] PROGMEM = "max Altitude:";
72
const char stats_item_0[] PROGMEM = "max Altitude:";
73
const char stats_item_1[] PROGMEM = "max Speed   :";
73
const char stats_item_1[] PROGMEM = "max Speed   :";
74
const char stats_item_2[] PROGMEM = "max Distance:";
74
const char stats_item_2[] PROGMEM = "max Distance:";
75
const char stats_item_3[] PROGMEM = "min Voltage :";
75
const char stats_item_3[] PROGMEM = "min Voltage :";
76
const char stats_item_4[] PROGMEM = "max Time    :";
76
const char stats_item_4[] PROGMEM = "max Time    :";
77
const char stats_item_5[] PROGMEM = "longitude   :";
77
const char stats_item_5[] PROGMEM = "longitude   :";
78
const char stats_item_6[] PROGMEM = "latitude    :";
78
const char stats_item_6[] PROGMEM = "latitude    :";
79
const char stats_item_7[] PROGMEM = "max current :";
79
const char stats_item_7[] PROGMEM = "max current :";
80
const char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
80
const char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
81
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
81
    stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7};
82
 
82
 
83
#if !(FCONLY)
83
#if !(FCONLY)
84
//char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
84
//char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
85
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
85
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
86
const char str_NE[] PROGMEM = "NE";
86
const char str_NE[] PROGMEM = "NE";
87
const char str_E[] PROGMEM  = "E ";
87
const char str_E[] PROGMEM  = "E ";
88
const char str_SE[] PROGMEM = "SE";
88
const char str_SE[] PROGMEM = "SE";
89
const char str_S[] PROGMEM  = "S ";
89
const char str_S[] PROGMEM  = "S ";
90
const char str_SW[] PROGMEM = "SW";
90
const char str_SW[] PROGMEM = "SW";
91
const char str_W[] PROGMEM  = "W ";
91
const char str_W[] PROGMEM  = "W ";
92
const char str_NW[] PROGMEM = "NW";
92
const char str_NW[] PROGMEM = "NW";
93
const char str_N[] PROGMEM  = "N ";
93
const char str_N[] PROGMEM  = "N ";
94
const char *directions[8] PROGMEM = {
94
const char *directions[8] PROGMEM = {
95
        str_NE,
95
        str_NE,
96
        str_E,
96
        str_E,
97
        str_SE,
97
        str_SE,
98
        str_S,
98
        str_S,
99
        str_SW,
99
        str_SW,
100
        str_W,
100
        str_W,
101
        str_NW,
101
        str_NW,
102
        str_N};
102
        str_N};
103
#endif
103
#endif
104
 
104
 
105
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
105
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
106
 
106
 
107
// general PAL|NTSC distingiusch stuff
107
// general PAL|NTSC distingiusch stuff
108
uint8_t top_line = 1;
108
uint8_t top_line = 1;
109
uint8_t bottom_line = 14;
109
uint8_t bottom_line = 14;
110
 
110
 
111
// battery voltages
111
// battery voltages
112
uint8_t min_voltage = 0;
112
uint8_t min_voltage = 0;
113
uint8_t max_voltage = 0;
113
uint8_t max_voltage = 0;
114
 
114
 
115
// Flags
115
// Flags
116
uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0;
116
uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0;
117
 
117
 
118
/* ##########################################################################
118
/* ##########################################################################
119
 * Interrupt handler
119
 * Interrupt handler
120
 * ##########################################################################*/
120
 * ##########################################################################*/
121
 
121
 
122
/**
122
/**
123
 * handler for undefined Interrupts
123
 * handler for undefined Interrupts
124
 * if not defined AVR will reset in case any unhandled interrupts occur
124
 * if not defined AVR will reset in case any unhandled interrupts occur
125
 */
125
 */
126
ISR(__vector_default) {
126
ISR(__vector_default) {
127
    asm("nop");
127
    asm("nop");
128
}
128
}
129
 
129
 
130
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
130
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
131
/* ##########################################################################
131
/* ##########################################################################
132
 * timer stuff
132
 * timer stuff
133
 * ##########################################################################*/
133
 * ##########################################################################*/
134
 
134
 
135
/**
135
/**
136
 * timer kicks in every 1000uS ^= 1ms
136
 * timer kicks in every 1000uS ^= 1ms
137
 */
137
 */
138
ISR(TIMER0_COMP_vect) {
138
ISR(TIMER0_COMP_vect) {
139
    if (!timer--) {
139
    if (!timer--) {
140
        uptime++;
140
        uptime++;
141
        timer = 999;
141
        timer = 999;
142
        seconds_since_last_data++;
142
        seconds_since_last_data++;
143
    }
143
    }
144
    // in case there is still some spi data to send do it now
144
    // in case there is still some spi data to send do it now
145
    if (spi_ready && icnt) {
145
    if (spi_ready && icnt) {
146
        SPDR = *iptr;
146
        SPDR = *iptr;
147
    }
147
    }
148
}
148
}
149
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
149
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
150
 
150
 
151
/* ##########################################################################
151
/* ##########################################################################
152
 * MAIN
152
 * MAIN
153
 * ##########################################################################*/
153
 * ##########################################################################*/
154
int main(void) {
154
int main(void) {
155
 
155
 
156
    // set up FLAGS, compiler should flatten this one
156
    // set up FLAGS, compiler should flatten this one
157
    COSD_FLAGS = (NTSC << 0);
157
    COSD_FLAGS = (NTSC << 0);
158
    COSD_FLAGS |= (HUD << 1);
158
    COSD_FLAGS |= (HUD << 1);
159
    COSD_FLAGS |= (ARTHORIZON << 2);
159
    COSD_FLAGS |= (ARTHORIZON << 2);
160
    COSD_FLAGS |= (BIGVARIO << 3);
160
    COSD_FLAGS |= (BIGVARIO << 3);
161
    COSD_FLAGS |= (STATS << 4);
161
    COSD_FLAGS |= (STATS << 4);
162
    COSD_FLAGS |= (WARNINGS << 5);
162
    COSD_FLAGS |= (WARNINGS << 5);
163
 
163
 
164
    // set up Atmega162 Ports
164
    // set up Atmega162 Ports
165
    DDRA |= (1 << PA1); // PA1 output (/CS)
165
    DDRA |= (1 << PA1); // PA1 output (/CS)
166
    MAX_CS_HIGH
166
    MAX_CS_HIGH
167
    DDRA |= (1 << PA2); // PA2 output (SDIN)
167
    DDRA |= (1 << PA2); // PA2 output (SDIN)
168
    MAX_SDIN_LOW
168
    MAX_SDIN_LOW
169
    DDRA |= (1 << PA3); // PA3 output (SCLK)
169
    DDRA |= (1 << PA3); // PA3 output (SCLK)
170
    MAX_SCLK_LOW
170
    MAX_SCLK_LOW
171
    DDRA |= (1 << PA5); // PA5 output (RESET)
171
    DDRA |= (1 << PA5); // PA5 output (RESET)
172
    MAX_RESET_HIGH
172
    MAX_RESET_HIGH
173
 
173
 
174
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
174
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
175
    LED1_OFF
175
    LED1_OFF
176
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
176
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
177
    LED2_OFF
177
    LED2_OFF
178
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
178
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
179
    LED3_OFF
179
    LED3_OFF
180
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
180
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
181
    LED4_OFF
181
    LED4_OFF
182
 
182
 
183
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
183
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
184
    PORTC |= (1 << PC4); // pullup
184
    PORTC |= (1 << PC4); // pullup
185
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
185
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
186
    PORTC |= (1 << PC5); // pullup
186
    PORTC |= (1 << PC5); // pullup
187
 
187
 
188
    // reset the MAX7456 to be sure any undefined states do no harm
188
    // reset the MAX7456 to be sure any undefined states do no harm
189
    MAX_RESET_LOW
189
    MAX_RESET_LOW
190
    MAX_RESET_HIGH
190
    MAX_RESET_HIGH
191
 
191
 
192
    // give the FC/NC and the maxim time to come up
192
    // give the FC/NC and the maxim time to come up
193
    LED4_ON
193
    LED4_ON
194
    _delay_ms(2000);
194
    _delay_ms(2000);
195
    LED4_OFF
195
    LED4_OFF
196
 
196
 
197
    //Pushing NEW chars to the MAX7456
197
    //Pushing NEW chars to the MAX7456
198
#if (WRITECHARS != -1)
198
#if (WRITECHARS != -1)
199
        // DISABLE display (VM0)
199
        // DISABLE display (VM0)
200
        spi_send_byte(0x00, 0b00000000);
200
        spi_send_byte(0x00, 0b00000000);
201
#include "characters.c"
201
#include "characters.c"
202
#else
202
#else
203
        // read out config for NTSC/PAL distinguishing
203
        // read out config for NTSC/PAL distinguishing
204
        get_eeprom(0);
204
        get_eeprom(0);
205
#endif
205
#endif
206
 
206
 
207
        // Setup Video Mode
207
        // Setup Video Mode
208
        if (COSD_FLAGS & COSD_FLAG_NTSC) {
208
        if (COSD_FLAGS & COSD_FLAG_NTSC) {
209
            // NTSC + enable display immediately (VM0)
209
            // NTSC + enable display immediately (VM0)
210
            spi_send_byte(0x00, 0b00001000);
210
            spi_send_byte(0x00, 0b00001000);
211
 
211
 
212
                bottom_line = 12;
212
                bottom_line = 12;
213
        } else {
213
        } else {
214
            // PAL + enable display immediately (VM0)
214
            // PAL + enable display immediately (VM0)
215
            spi_send_byte(0x00, 0b01001000);
215
            spi_send_byte(0x00, 0b01001000);
216
 
216
 
217
                bottom_line = 14;
217
                bottom_line = 14;
218
        }
218
        }
219
 
219
 
220
    /*// clear all display-mem (DMM)
220
    /*// clear all display-mem (DMM)
221
    spi_send_byte(0x04, 0b00000100);
221
    spi_send_byte(0x04, 0b00000100);
222
 
222
 
223
    // clearing takes 12uS according to maxim so lets wait longer
223
    // clearing takes 12uS according to maxim so lets wait longer
224
    _delay_us(120);
224
    _delay_us(120);
225
 
225
 
226
    // 8bit mode
226
    // 8bit mode
227
    spi_send_byte(0x04, 0b01000000);*/
227
    spi_send_byte(0x04, 0b01000000);*/
228
 
228
 
229
    // clear display memory and set to 8bit mode
229
    // clear display memory and set to 8bit mode
230
    clear();
230
    clear();
231
 
231
 
232
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
232
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
233
    // init usart
233
    // init usart
234
    usart1_init();
234
    usart1_init();
235
 
235
 
236
    // set up timer
236
    // set up timer
237
        // CTC, Prescaler /64
237
        // CTC, Prescaler /64
238
        TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
238
        TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
239
 
239
 
240
        TCNT0 = 0;
240
        TCNT0 = 0;
241
        OCR0 = 250;
241
        OCR0 = 250;
242
 
242
 
243
        // enable timer output compare interrupt
243
        // enable timer output compare interrupt
244
        TIMSK &= ~(1 << TOIE0);
244
        TIMSK &= ~(1 << TOIE0);
245
        TIMSK |= (1 << OCIE0);
245
        TIMSK |= (1 << OCIE0);
246
 
246
 
247
        // SPI setup
247
        // SPI setup
248
    DDRD |= (1 << PD2); // PD2 output (INT0)
248
    DDRD |= (1 << PD2); // PD2 output (INT0)
249
    SpiMasterInit();
249
    SpiMasterInit();
250
 
250
 
251
        // PPM detection setup
251
        // PPM detection setup
252
        ppm_init();
252
        ppm_init();
253
 
253
 
254
    // enable interrupts
254
    // enable interrupts
255
 
255
 
256
    sei();
256
    sei();
257
#endif
257
#endif
258
 
258
 
259
    //write_ascii_string(2,  7, "         CaScAdE          ");
259
    //write_ascii_string(2,  7, "         CaScAdE          ");
260
    //write_ascii_string(2,  8, "is TESTING his open source");
260
    //write_ascii_string(2,  8, "is TESTING his open source");
261
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
261
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
262
 
262
 
263
    // we are ready
263
    // we are ready
264
    LED3_ON
264
    LED3_ON
265
 
265
 
266
#if ALLCHARSDEBUG | (WRITECHARS != -1)
266
#if ALLCHARSDEBUG | (WRITECHARS != -1)
267
        clear();
267
        clear();
268
    write_all_chars();
268
    write_all_chars();
269
#else
269
#else
270
 
270
 
271
        // clear serial screen
271
        // clear serial screen
272
        //usart1_puts("\x1B[2J\x1B[H");
272
        //usart1_puts("\x1B[2J\x1B[H");
273
        //usart1_puts("hello world!123\r\n");
273
        //usart1_puts("hello world!123\r\n");
274
 
274
 
275
#if FCONLY
275
#if FCONLY
276
        // request data ever 100ms from FC;
276
        // request data ever 100ms from FC;
277
        usart1_request_mk_data(0, 'd', 100);
277
        usart1_request_mk_data(0, 'd', 100);
278
#else
278
#else
279
        // request OSD Data from NC every 100ms
279
        // request OSD Data from NC every 100ms
280
        usart1_request_mk_data(1, 'o', 100);
280
        usart1_request_mk_data(1, 'o', 100);
281
 
281
 
282
    // and disable debug...
282
    // and disable debug...
283
    usart1_request_mk_data(0, 'd', 0);
283
    usart1_request_mk_data(0, 'd', 0);
284
#endif
284
#endif
285
 
285
 
286
    // stats for after flight
286
    // stats for after flight
287
    int16_t max_Altimeter = 0;
287
    int16_t max_Altimeter = 0;
288
    uint8_t min_UBat = 255;
288
    uint8_t min_UBat = 255;
289
#if !(FCONLY)
289
#if !(FCONLY)
290
    uint16_t max_GroundSpeed = 0;
290
    uint16_t max_GroundSpeed = 0;
291
    int16_t max_Distance = 0;
291
    int16_t max_Distance = 0;
292
    uint16_t max_FlyingTime = 0;
292
    uint16_t max_FlyingTime = 0;
293
 
293
 
294
    // flags from last round to check for changes
294
    // flags from last round to check for changes
295
    uint8_t old_MKFlags = 0;
295
    uint8_t old_MKFlags = 0;
296
#endif
296
#endif
297
 
297
 
298
    while (1) {
298
    while (1) {
299
        // in case SPI is ready and there is nothing to send right now
299
        // in case SPI is ready and there is nothing to send right now
300
        if (!icnt && spi_ready) {
300
        if (!icnt && spi_ready) {
301
            // correct transfer ends with d (done)
301
            // correct transfer ends with d (done)
302
            if (spi_cmd_buffer[0] == 'd') {
302
            if (spi_cmd_buffer[0] == 'd') {
303
                ampere = spi_cmd_buffer[1] << 8;
303
                ampere = spi_cmd_buffer[1] << 8;
304
                ampere |= spi_cmd_buffer[2];
304
                ampere |= spi_cmd_buffer[2];
305
                                ampere_wasted = spi_cmd_buffer[3];
305
                                ampere_wasted = spi_cmd_buffer[3];
306
                                ampere_wasted = ampere_wasted << 8;
306
                                ampere_wasted = ampere_wasted << 8;
307
                                ampere_wasted |= spi_cmd_buffer[4];
307
                                ampere_wasted |= spi_cmd_buffer[4];
308
                                ampere_wasted = ampere_wasted << 8;
308
                                ampere_wasted = ampere_wasted << 8;
309
                                ampere_wasted |= spi_cmd_buffer[5];
309
                                ampere_wasted |= spi_cmd_buffer[5];
310
                                ampere_wasted = ampere_wasted << 8;
310
                                ampere_wasted = ampere_wasted << 8;
311
                                ampere_wasted |= spi_cmd_buffer[6];
311
                                ampere_wasted |= spi_cmd_buffer[6];
312
                                //ampere = *((int16_t *) spi_cmd_buffer + 1);
312
                                //ampere = *((int16_t *) spi_cmd_buffer + 1);
313
                                //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3);
313
                                //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3);
314
                                // if this is the first receival we should print the small A
314
                                // if this is the first receival we should print the small A
315
                                if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) {
315
                                if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) {
316
                                        clear();
316
                                        clear();
317
                                        COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
317
                                        COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
318
                                        // update this flag
318
                                        // update this flag
319
                        COSD_FLAGS2 |= COSD_FLAG_STROMREC;
319
                        COSD_FLAGS2 |= COSD_FLAG_STROMREC;
320
                                }
320
                                }
321
                //write_ascii_char(8+4*30, 'v'); // valid
321
                //write_ascii_char(8+4*30, 'v'); // valid
322
            } else {
322
            } else {
323
                // update flags
323
                // update flags
324
                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
324
                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
325
                //write_ascii_char(8+4*30, 'i'); // invalid
325
                //write_ascii_char(8+4*30, 'i'); // invalid
326
            }
326
            }
327
            spi_cmd_buffer[0] = 'A';
327
            spi_cmd_buffer[0] = 'A';
328
            spi_cmd_buffer[1] = 'B';
328
            spi_cmd_buffer[1] = 'B';
329
            spi_cmd_buffer[2] = 'C';
329
            spi_cmd_buffer[2] = 'C';
330
                        spi_cmd_buffer[3] = 'D';
330
                        spi_cmd_buffer[3] = 'D';
331
                        spi_cmd_buffer[4] = 'E';
331
                        spi_cmd_buffer[4] = 'E';
332
                        spi_cmd_buffer[5] = 'F';
332
                        spi_cmd_buffer[5] = 'F';
333
                        spi_cmd_buffer[6] = 'G';
333
                        spi_cmd_buffer[6] = 'G';
334
            StartTransfer((unsigned char*) spi_cmd_buffer, 7);
334
            StartTransfer((unsigned char*) spi_cmd_buffer, 7);
335
        }
335
        }
336
        if (rxd_buffer_locked) {
336
        if (rxd_buffer_locked) {
337
#if FCONLY
337
#if FCONLY
338
            if (rxd_buffer[2] == 'D') { // FC Data
338
            if (rxd_buffer[2] == 'D') { // FC Data
339
                Decode64();
339
                Decode64();
340
                debugData = *((DebugOut_t*) pRxData);
340
                debugData = *((DebugOut_t*) pRxData);
341
 
341
 
342
                // init on first data retrival, distinguished by last battery :)
342
                // init on first data retrival, distinguished by last battery :)
343
                if (last_UBat == 255) {
343
                if (last_UBat == 255) {
344
                    // fix for min_UBat
344
                    // fix for min_UBat
345
                    min_UBat = debugData.Analog[9];
345
                    min_UBat = debugData.Analog[9];
346
                    init_cosd(debugData.Analog[9]);
346
                    init_cosd(debugData.Analog[9]);
347
                }
347
                }
348
                #include "osd_fcmode_default.c"
348
                #include OSD_FCMODE
349
            }
349
            }
350
#else
350
#else
351
            if (rxd_buffer[2] == 'O') { // NC OSD Data
351
            if (rxd_buffer[2] == 'O') { // NC OSD Data
352
                Decode64();
352
                Decode64();
353
                naviData = *((NaviData_t*) pRxData);
353
                naviData = *((NaviData_t*) pRxData);
354
 
354
 
355
                // init on first data retrival, distinguished by last battery :)
355
                // init on first data retrival, distinguished by last battery :)
356
                if (last_UBat == 255) {
356
                if (last_UBat == 255) {
357
                    // fix for min_UBat
357
                    // fix for min_UBat
358
                    min_UBat = naviData.UBat;
358
                    min_UBat = naviData.UBat;
359
                    init_cosd(naviData.UBat);
359
                    init_cosd(naviData.UBat);
360
                }
360
                }
361
                #include "osd_ncmode_default.c"
361
                #include OSD_NCMODE
362
            }
362
            }
363
#endif
363
#endif
364
            rxd_buffer_locked = 0;
364
            rxd_buffer_locked = 0;
365
        }
365
        }
366
        // handle keypress
366
        // handle keypress
367
        if (s1_pressed()) {
367
        if (s1_pressed()) {
368
            config_menu();
368
            config_menu();
369
        }
369
        }
370
        if (seconds_since_last_data > 2) {
370
        if (seconds_since_last_data > 2) {
371
#if FCONLY
371
#if FCONLY
372
            // request data ever 100ms from FC;
372
            // request data ever 100ms from FC;
373
            usart1_request_mk_data(0, 'd', 100);
373
            usart1_request_mk_data(0, 'd', 100);
374
#else
374
#else
375
            // request OSD Data from NC every 100ms
375
            // request OSD Data from NC every 100ms
376
            usart1_request_mk_data(1, 'o', 100);
376
            usart1_request_mk_data(1, 'o', 100);
377
 
377
 
378
            // and disable debug...
378
            // and disable debug...
379
            usart1_request_mk_data(0, 'd', 0);
379
            usart1_request_mk_data(0, 'd', 0);
380
 
380
 
381
                        // reset last time counter
381
                        // reset last time counter
382
                        seconds_since_last_data = 0;
382
                        seconds_since_last_data = 0;
383
#endif
383
#endif
384
        }
384
        }
385
    }
385
    }
386
#endif
386
#endif
387
    return 0;
387
    return 0;
388
}
388
}
389
 
389