Subversion Repositories Projects

Rev

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

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