Subversion Repositories Projects

Rev

Rev 507 | Rev 523 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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