Subversion Repositories Projects

Rev

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

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