Subversion Repositories Projects

Rev

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

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