Subversion Repositories Projects

Rev

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

Rev 1437 Rev 1801
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009-2012 by Claas Anders "CaScAdE" Rathje               *
2
 *   Copyright (C) 2009-2012 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 "main.h"
28
#include "main.h"
29
#include <avr/io.h>
29
#include <avr/io.h>
30
#include <avr/interrupt.h>
30
#include <avr/interrupt.h>
31
#include <util/delay.h>
31
#include <util/delay.h>
32
#include <avr/pgmspace.h>
32
#include <avr/pgmspace.h>
-
 
33
#include <string.h>
33
#include "max7456_software_spi.h"
34
#include "max7456_software_spi.h"
34
#ifdef ANTENNATRACKTEST 
35
#ifdef ANTENNATRACKTEST 
35
#include "usart0.h"
36
#include "usart0.h"
36
#endif
37
#endif
37
#include "usart1.h"
38
#include "usart1.h"
38
#include "osd_helpers.h"
39
#include "osd_helpers.h"
39
#include "config.h"
40
#include "config.h"
40
#include "spi.h"
41
#include "spi.h"
41
#include "buttons.h"
42
#include "buttons.h"
42
#include "ppm.h"
43
#include "ppm.h"
43
#include "osd_ncmode_default.h"
44
#include "osd_ncmode_default.h"
44
#include "osd_ncmode_minimal.h"
45
#include "osd_ncmode_minimal.h"
45
#include "osd_fcmode_default.h"
46
#include "osd_fcmode_default.h"
46
#include "osd_fcmode_jopl.h"
47
#include "osd_fcmode_jopl.h"
47
 
48
 
48
#if WRITECHARS != -1
49
#if WRITECHARS != -1
49
#include "characters.h"
50
#include "characters.h"
50
#endif
51
#endif
51
 
52
 
52
/* TODO:
53
/* TODO:
53
 * - verifiy correctness of values
54
 * - verifiy correctness of values
54
 * - clean up code :)
55
 * - clean up code :)
55
 */
56
 */
56
 
57
 
57
 
58
 
58
/* ##########################################################################
59
/* ##########################################################################
59
 * global definitions and global vars
60
 * global definitions and global vars
60
 * ##########################################################################*/
61
 * ##########################################################################*/
61
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
62
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
62
 
63
 
63
#if FCONLY
64
#if FCONLY
64
volatile str_DebugOut debugData;
65
volatile str_DebugOut debugData;
65
#else
66
#else
66
volatile NaviData_t naviData;
67
volatile NaviData_t naviData;
-
 
68
volatile Data3D_t data3d;
67
#endif
69
#endif
68
 
70
 
69
// cache old vars for blinking attribute, checkup is faster than full
71
// cache old vars for blinking attribute, checkup is faster than full
70
// attribute write each time
72
// attribute write each time
71
volatile uint8_t last_UBat = 255;
73
volatile uint8_t last_UBat = 255;
72
volatile uint8_t last_RC_Quality = 255;
74
volatile uint8_t last_RC_Quality = 255;
73
 
75
 
74
// 16bit should be enough, normal LiPos don't last that long
76
// 16bit should be enough, normal LiPos don't last that long
75
volatile uint16_t uptime = 0;
77
volatile uint16_t uptime = 0;
76
volatile uint16_t timer = 0;
78
volatile uint16_t timer = 0;
77
volatile uint16_t flytime_fc = 0;
79
volatile uint16_t flytime_fc = 0;
78
 
80
 
79
// remember last time data was received
81
// remember last time data was received
80
volatile uint8_t seconds_since_last_data = 0;
82
volatile uint8_t seconds_since_last_data = 0;
81
 
83
 
82
// general PAL|NTSC distingiusch stuff
84
// general PAL|NTSC distingiusch stuff
83
uint8_t top_line = 1;
85
uint8_t top_line = 1;
84
uint8_t bottom_line = 14;
86
uint8_t bottom_line = 14;
85
 
87
 
86
// battery voltages
88
// battery voltages
87
uint8_t min_voltage = 0;
89
uint8_t min_voltage = 0;
88
uint8_t max_voltage = 0;
90
uint8_t max_voltage = 0;
89
 
91
 
90
uint8_t scope[12] = {
92
uint8_t scope[12] = {
91
    5, 5, 0,
93
    5, 5, 0,
92
    25, 5, 0,
94
    25, 5, 0,
93
    5, 10, 0,
95
    5, 10, 0,
94
    25, 10, 0
96
    25, 10, 0
95
};
97
};
96
 
98
 
97
// Flags
99
// Flags
98
uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0;
100
uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0;
99
 
101
 
100
 
102
 
101
 
103
 
102
// stats for after flight
104
// stats for after flight
103
int16_t max_Altimeter = 0;
105
int16_t max_Altimeter = 0;
104
uint8_t min_UBat = 255;
106
uint8_t min_UBat = 255;
105
uint16_t max_GroundSpeed = 0;
107
uint16_t max_GroundSpeed = 0;
106
int16_t max_Distance = 0;
108
int16_t max_Distance = 0;
107
uint16_t max_FlyingTime = 0;
109
uint16_t max_FlyingTime = 0;
108
 
110
 
109
// flags from last round to check for changes
111
// flags from last round to check for changes
110
uint8_t old_MKFlags = 0;
112
uint8_t old_MKFlags = 0;
111
 
113
 
112
char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
114
char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
113
//char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
115
//char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
114
 
116
 
115
 
117
 
116
/* ##########################################################################
118
/* ##########################################################################
117
 * Different display mode function pointers
119
 * Different display mode function pointers
118
 * ##########################################################################*/
120
 * ##########################################################################*/
119
const char str_1[] PROGMEM = "default";
121
const char str_1[] PROGMEM = "default";
120
const char str_2[] PROGMEM = "minimal";
122
const char str_2[] PROGMEM = "minimal";
121
const char str_3[] PROGMEM = " jopl";
123
const char str_3[] PROGMEM = " jopl";
122
 
124
 
123
#if FCONLY
125
#if FCONLY
124
const displaymode_t fcdisplaymodes[] PROGMEM = {
126
const displaymode_t fcdisplaymodes[] PROGMEM = {
125
    { osd_fcmode_default, (char *)str_1},
127
    { osd_fcmode_default, (char *)str_1},
126
    { osd_fcmode_jopl, (char *)str_3}
128
    { osd_fcmode_jopl, (char *)str_3}
127
};
129
};
128
int (*osd_fcmode)(void) = (int(*)(void)) & osd_fcmode_default;
130
int (*osd_fcmode)(void) = (int(*)(void)) & osd_fcmode_default;
129
#else
131
#else
130
 
132
 
131
const displaymode_t ncdisplaymodes[] PROGMEM = {
133
const displaymode_t ncdisplaymodes[] PROGMEM = {
132
    { osd_ncmode_default, (char *)str_1},
134
    { osd_ncmode_default, (char *)str_1},
133
    { osd_ncmode_minimal, (char *)str_2}
135
    { osd_ncmode_minimal, (char *)str_2}
134
};
136
};
135
int (*osd_ncmode)(void) = (int(*)(void)) & osd_ncmode_default;
137
int (*osd_ncmode)(void) = (int(*)(void)) & osd_ncmode_default;
136
#endif
138
#endif
137
 
139
 
138
 
140
 
139
#endif
141
#endif
140
 
142
 
141
/* ##########################################################################
143
/* ##########################################################################
142
 * Interrupt handler
144
 * Interrupt handler
143
 * ##########################################################################*/
145
 * ##########################################################################*/
144
 
146
 
145
/**
147
/**
146
 * handler for undefined Interrupts
148
 * handler for undefined Interrupts
147
 * if not defined AVR will reset in case any unhandled interrupts occur
149
 * if not defined AVR will reset in case any unhandled interrupts occur
148
 */
150
 */
149
ISR(__vector_default) {
151
ISR(__vector_default) {
150
    asm("nop");
152
    asm("nop");
151
}
153
}
152
 
154
 
153
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
155
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
154
/* ##########################################################################
156
/* ##########################################################################
155
 * timer stuff
157
 * timer stuff
156
 * ##########################################################################*/
158
 * ##########################################################################*/
157
 
159
 
158
static uint8_t delay_spi = 0;
160
static uint8_t delay_spi = 0;
159
 
161
 
160
/**
162
/**
161
 * timer kicks in every 1000uS ^= 1ms
163
 * timer kicks in every 1000uS ^= 1ms
162
 */
164
 */
163
ISR(TIMER0_COMP_vect) {
165
ISR(TIMER0_COMP_vect) {
164
    if (!timer--) {
166
    if (!timer--) {
165
        uptime++;
167
        uptime++;
166
 
168
 
167
#if FCONLY
169
#if FCONLY
168
        if (debugData.Analog[12] > 10) {
170
        if (debugData.Analog[12] > 10) {
169
            flytime_fc++;
171
            flytime_fc++;
170
        }
172
        }
171
#endif
173
#endif
172
 
174
 
173
        timer = 999;
175
        timer = 999;
174
        seconds_since_last_data++;
176
        seconds_since_last_data++;
175
    }
177
    }
176
    // in case there is still some spi data to send do it now
178
    // in case there is still some spi data to send do it now
177
    // delay to give the slave some time to compute values
179
    // delay to give the slave some time to compute values
178
    if (spi_ready && icnt) {
180
    if (spi_ready && icnt) {
179
        if (!delay_spi--) {
181
        if (!delay_spi--) {
180
            delay_spi = 8;
182
            delay_spi = 8;
181
            spi_send_next();
183
            spi_send_next();
182
        }
184
        }
183
    }
185
    }
184
}
186
}
185
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
187
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
186
 
188
 
187
/* ##########################################################################
189
/* ##########################################################################
188
 * MAIN
190
 * MAIN
189
 * ##########################################################################*/
191
 * ##########################################################################*/
190
int main(void) {
192
int main(void) {
191
    // set up FLAGS
193
    // set up FLAGS
192
    COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0;
194
    COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0;
193
#if NTSC
195
#if NTSC
194
    COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC;
196
    COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC;
195
#endif
197
#endif
196
#if HUD
198
#if HUD
197
    COSD_FLAGS_MODES |= COSD_FLAG_HUD;
199
    COSD_FLAGS_MODES |= COSD_FLAG_HUD;
198
#endif
200
#endif
199
#if ARTHORIZON
201
#if ARTHORIZON
200
    COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON;
202
    COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON;
201
#endif
203
#endif
202
#if BIGVARIO
204
#if BIGVARIO
203
    COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO;
205
    COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO;
204
#endif
206
#endif
205
#if STATS
207
#if STATS
206
    COSD_FLAGS_MODES |= COSD_FLAG_STATS;
208
    COSD_FLAGS_MODES |= COSD_FLAG_STATS;
207
#endif
209
#endif
208
#if WARNINGS
210
#if WARNINGS
209
    COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS;
211
    COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS;
210
#endif
212
#endif
211
#if FCONLY
213
#if FCONLY
212
    COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE;
214
    COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE;
213
#endif
215
#endif
214
 
216
 
215
 
217
 
216
    // set up Atmega162 Ports
218
    // set up Atmega162 Ports
217
    DDRA |= ((1 << PA1) | (1 << PA2) | (1 << PA3) | (1 << PA5)); // PA1 output (/CS) | PA2 output (SDIN) |PA3 output (SCLK) | PA5 output (RESET)
219
    DDRA |= ((1 << PA1) | (1 << PA2) | (1 << PA3) | (1 << PA5)); // PA1 output (/CS) | PA2 output (SDIN) |PA3 output (SCLK) | PA5 output (RESET)
218
    MAX_CS_HIGH
220
    MAX_CS_HIGH
219
    MAX_SDIN_LOW
221
    MAX_SDIN_LOW
220
    MAX_SCLK_LOW
222
    MAX_SCLK_LOW
221
    MAX_RESET_HIGH
223
    MAX_RESET_HIGH
222
 
224
 
223
    DDRC |= ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3)); // PC0 output (LED1 gn) | PC1 output (LED2 rt) | PC2 output (LED3 gn) | PC3 output (LED4 rt)
225
    DDRC |= ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3)); // PC0 output (LED1 gn) | PC1 output (LED2 rt) | PC2 output (LED3 gn) | PC3 output (LED4 rt)
224
    LED1_OFF
226
    LED1_OFF
225
    LED2_OFF
227
    LED2_OFF
226
    LED3_OFF
228
    LED3_OFF
227
    LED4_OFF
229
    LED4_OFF
228
 
230
 
229
    DDRC &= ~((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // PC4 input(MODE) | PC5 input(SET) | PC6 input SJ1 | PC7 input SJ2
231
    DDRC &= ~((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // PC4 input(MODE) | PC5 input(SET) | PC6 input SJ1 | PC7 input SJ2
230
    PORTC |= ((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // pullup
232
    PORTC |= ((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // pullup
231
 
233
 
232
 
234
 
233
    // reset the MAX7456 to be sure any undefined states do no harm
235
    // reset the MAX7456 to be sure any undefined states do no harm
234
    MAX_RESET_LOW
236
    MAX_RESET_LOW
235
    MAX_RESET_HIGH
237
    MAX_RESET_HIGH
236
 
238
 
237
    // give the FC/NC and the maxim time to come up
239
    // give the FC/NC and the maxim time to come up
238
    LED4_ON
240
    LED4_ON
239
    _delay_ms(1000);
241
    _delay_ms(1000);
240
    LED4_OFF
242
    LED4_OFF
241
 
243
 
242
    //Pushing NEW chars to the MAX7456
244
    //Pushing NEW chars to the MAX7456
243
#if (WRITECHARS != -1)
245
#if (WRITECHARS != -1)
244
        // DISABLE display (VM0)
246
        // DISABLE display (VM0)
245
        spi_send_byte(0x00, 0b00000000);
247
        spi_send_byte(0x00, 0b00000000);
246
    learn_all_chars_pgm();
248
    learn_all_chars_pgm();
247
#else
249
#else
248
        // read out config for NTSC/PAL distinguishing
250
        // read out config for NTSC/PAL distinguishing
249
        get_eeprom(0);
251
        get_eeprom(0);
250
#endif
252
#endif
251
 
253
 
252
    // Setup Video Mode
254
    // Setup Video Mode
253
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
255
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
254
        // NTSC + enable display immediately (VM0)
256
        // NTSC + enable display immediately (VM0)
255
        spi_send_byte(0x00, 0b00001000);
257
        spi_send_byte(0x00, 0b00001000);
256
 
258
 
257
        bottom_line = 12;
259
        bottom_line = 12;
258
    } else {
260
    } else {
259
        // PAL + enable display immediately (VM0)
261
        // PAL + enable display immediately (VM0)
260
        spi_send_byte(0x00, 0b01001000);
262
        spi_send_byte(0x00, 0b01001000);
261
 
263
 
262
        bottom_line = 14;
264
        bottom_line = 14;
263
    }
265
    }
264
 
266
 
265
    /*// clear all display-mem (DMM)
267
    /*// clear all display-mem (DMM)
266
    spi_send_byte(0x04, 0b00000100);
268
    spi_send_byte(0x04, 0b00000100);
267
 
269
 
268
    // clearing takes 12uS according to maxim so lets wait longer
270
    // clearing takes 12uS according to maxim so lets wait longer
269
    _delay_us(120);
271
    _delay_us(120);
270
 
272
 
271
    // 8bit mode
273
    // 8bit mode
272
    spi_send_byte(0x04, 0b01000000);*/
274
    spi_send_byte(0x04, 0b01000000);*/
273
 
275
 
274
    // clear display memory and set to 8bit mode
276
    // clear display memory and set to 8bit mode
275
    clear();
277
    clear();
276
 
278
 
277
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
279
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
278
    // init usart
280
    // init usart
279
    usart1_init();
281
    usart1_init();
280
 
282
 
281
#ifdef SERIALDEBUGDRAW
283
#ifdef SERIALDEBUGDRAW
282
#define USART0ENABLE 1
284
#define USART0ENABLE 1
283
#endif
285
#endif
284
#ifdef ANTENNATRACKTEST
286
#ifdef ANTENNATRACKTEST
285
#define USART0ENABLE 1
287
#define USART0ENABLE 1
286
#endif
288
#endif
287
 
289
 
288
#ifdef USART0ENABLE
290
#ifdef USART0ENABLE
289
    usart0_init();
291
    usart0_init();
290
    usart0_puts("\x1B[2J\x1B[H");
292
    usart0_puts("\x1B[2J\x1B[H");
291
    usart0_puts("Welcome\r\n");
293
    usart0_puts("Welcome\r\n");
292
#endif
294
#endif
293
 
295
 
294
    // keep serial port clean
296
    // keep serial port clean
295
    usart1_DisableTXD();
297
    usart1_DisableTXD();
296
 
298
 
297
    // set up timer
299
    // set up timer
298
    // CTC, Prescaler /64
300
    // CTC, Prescaler /64
299
    TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
301
    TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00);
300
 
302
 
301
    TCNT0 = 0;
303
    TCNT0 = 0;
302
    OCR0 = 250;
304
    OCR0 = 250;
303
 
305
 
304
    // enable timer output compare interrupt
306
    // enable timer output compare interrupt
305
    TIMSK &= ~(1 << TOIE0);
307
    TIMSK &= ~(1 << TOIE0);
306
    TIMSK |= (1 << OCIE0);
308
    TIMSK |= (1 << OCIE0);
307
 
309
 
308
    // SPI setup
310
    // SPI setup
309
    DDRD |= (1 << PD2); // PD2 output (INT0)
311
    DDRD |= (1 << PD2); // PD2 output (INT0)
310
    SpiMasterInit();
312
    SpiMasterInit();
311
 
313
 
312
    // PPM detection setup
314
    // PPM detection setup
313
    ppm_init();
315
    ppm_init();
314
 
316
 
315
    // enable interrupts
317
    // enable interrupts
316
    sei();
318
    sei();
317
 
319
 
318
    //write_ascii_string(2,7, " CaScAdE");
320
    //write_ascii_string(2,7, " CaScAdE");
319
    //write_ascii_string(2,8, "is TESTING his open source");
321
    //write_ascii_string(2,8, "is TESTING his open source");
320
    //write_ascii_string(2,9, "EPi OSD Firmware");
322
    //write_ascii_string(2,9, "EPi OSD Firmware");
321
 
323
 
322
    // we are ready
324
    // we are ready
323
    LED3_ON
325
    LED3_ON
324
 
326
 
325
 
327
 
326
    // clear serial screen
328
    // clear serial screen
327
    //usart1_puts("\x1B[2J\x1B[H");
329
    //usart1_puts("\x1B[2J\x1B[H");
328
 
330
 
329
    COSD_FLAGS_RUNTIME &= ~COSD_DATARECEIVED;
331
    COSD_FLAGS_RUNTIME &= ~COSD_DATARECEIVED;
330
#if !FCONLY
332
#if !FCONLY
331
    usart1_request_nc_uart();
333
    usart1_request_nc_uart();
332
#endif
334
#endif
333
 
335
 
334
    while (1) {
336
    while (1) {
335
        // in case SPI is ready and there is nothing to send right now
337
        // in case SPI is ready and there is nothing to send right now
336
        if (!icnt && spi_ready) {
338
        if (!icnt && spi_ready) {
337
            // correct transfer ends with d (done)
339
            // correct transfer ends with d (done)
338
            if (SPI_buffer.buffer.chk == 'd') {
340
            if (SPI_buffer.buffer.chk == 'd') {
339
                ampere = SPI_buffer.data.ampere;
341
                ampere = SPI_buffer.data.ampere;
340
                ampere_wasted = SPI_buffer.data.mah;
342
                ampere_wasted = SPI_buffer.data.mah;
341
                s_volt = SPI_buffer.data.volt;
343
                s_volt = SPI_buffer.data.volt;
342
 
344
 
343
                // if this is the first receival we should print the small A
345
                // if this is the first receival we should print the small A
344
                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
346
                if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) {
345
                    clear();
347
                    clear();
346
                    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
348
                    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
347
                    // update this flag
349
                    // update this flag
348
                    COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
350
                    COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC;
349
                }
351
                }
350
            } else {
352
            } else {
351
                // update flags
353
                // update flags
352
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
354
                COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC;
353
            }
355
            }
354
            StartTransfer(9);
356
            StartTransfer(9);
355
        }
357
        }
356
        if (rxd_buffer_locked) {
358
        if (rxd_buffer_locked) {
357
#if FCONLY
359
#if FCONLY
358
            if (rxd_buffer[2] == 'D') { // FC Data
360
            if (rxd_buffer[2] == 'D') { // FC Data
359
                Decode64();
361
                Decode64();
360
                debugData = *((str_DebugOut*)pRxData);
362
                debugData = *((str_DebugOut*)pRxData);
361
 
363
 
362
                // init on first data retrival, distinguished by last battery :)
364
                // init on first data retrival, distinguished by last battery :)
363
                if (last_UBat == 255) {
365
                if (last_UBat == 255) {
364
                    if (debugData.Analog[9] > 40) {
366
                    if (debugData.Analog[9] > 40) {
365
                        // fix for min_UBat
367
                        // fix for min_UBat
366
                        min_UBat = debugData.Analog[9];
368
                        min_UBat = debugData.Analog[9];
367
                        last_UBat = debugData.Analog[9];
369
                        last_UBat = debugData.Analog[9];
368
                        init_cosd(last_UBat);
370
                        init_cosd(last_UBat);
369
                    }
371
                    }
370
                } else {
372
                } else {
371
                    osd_fcmode();
373
                    osd_fcmode();
372
                }
374
                }
373
                seconds_since_last_data = 0;
375
                seconds_since_last_data = 0;
374
            }
376
            }
375
#else
377
#else
376
            if (rxd_buffer[2] == 'O') { // NC OSD Data
378
            if (rxd_buffer[2] == 'O') { // NC OSD Data
377
                Decode64();
379
                Decode64();
378
                naviData = *((NaviData_t*)pRxData);
380
                //naviData = *((NaviData_t*)pRxData);
-
 
381
                                memcpy((char*)(&naviData), (char*)pRxData, sizeof(NaviData_t));
-
 
382
                                rxd_buffer_locked = 0;
379
 
383
 
380
                #ifdef SHIFTBYminus45
384
                #ifdef SHIFTBYminus45
381
                naviData.AngleNick = (int8_t)(((int16_t)naviData.AngleRoll + (int16_t)naviData.AngleNick) / 2);
385
                naviData.AngleNick = (int8_t)(((int16_t)naviData.AngleRoll + (int16_t)naviData.AngleNick) / 2);
382
                naviData.AngleRoll = (int8_t)(((int16_t)naviData.AngleRoll - (int16_t)naviData.AngleNick) / 2);
386
                naviData.AngleRoll = (int8_t)(((int16_t)naviData.AngleRoll - (int16_t)naviData.AngleNick) / 2);
383
                naviData.CompassHeading = (naviData.CompassHeading + (360 - 45)) % 360;
387
                naviData.CompassHeading = (naviData.CompassHeading + (360 - 45)) % 360;
384
                #endif
388
                #endif
385
 
389
 
386
                // init on first data retrival, distinguished by last battery :)
390
                // init on first data retrival, distinguished by last battery :)
387
                if (last_UBat == 255) {
391
                if (last_UBat == 255) {
388
                    if (naviData.UBat > 40) {
392
                    if (naviData.UBat > 40) {
389
                        // fix for min_UBat
393
                        // fix for min_UBat
390
                        min_UBat = naviData.UBat;
394
                        min_UBat = naviData.UBat;
391
                        last_UBat = naviData.UBat;
395
                        last_UBat = naviData.UBat;
392
                        init_cosd(last_UBat);
396
                        init_cosd(last_UBat);
393
                    }
397
                    }
394
                } else {
398
                } else {
395
                    osd_ncmode();
399
                    osd_ncmode();
396
                }
400
                }
397
                //seconds_since_last_data = 0;
401
                //seconds_since_last_data = 0;
-
 
402
//            } else if (rxd_buffer[2] == 'C') { // 3D and Stick Data
-
 
403
//                              Decode64();
-
 
404
//                              memcpy((char*)(&data3d), (char*)pRxData, sizeof(Data3D_t));                             
-
 
405
//                              rxd_buffer_locked = 0;
398
            }
406
                        } else {
-
 
407
                                rxd_buffer_locked = 0;
-
 
408
                        }
399
#endif
409
#endif
400
 
410
 
401
            // ONLY FOR TESTING
411
            // ONLY FOR TESTING
402
#ifdef ANTENNATRACKTEST 
412
#ifdef ANTENNATRACKTEST 
403
#include <stdlib.h>
413
#include <stdlib.h>
404
            //#include <float.h>
414
            //#include <float.h>
405
            //#include <math.h>
415
            //#include <math.h>
406
 
416
 
407
            //usart0_puts("\x1B[2J\x1B[H");
417
            //usart0_puts("\x1B[2J\x1B[H");
408
 
418
 
409
 
419
 
410
            /*
420
            /*
411
            naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100)
421
            naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100)
412
            naviData.HomePositionDeviation.Bearing = 35; // 35�
422
            naviData.HomePositionDeviation.Bearing = 35; // 35�
413
            altimeter_offset = 50; // 50m start height
423
            altimeter_offset = 50; // 50m start height
414
            naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000)
424
            naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000)
415
             */
425
             */
416
 
426
 
417
            static char conv_array[7];
427
            static char conv_array[7];
418
 
428
 
419
            // should be float
429
            // should be float
420
            int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset);
430
            int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset);
421
 
431
 
422
            // we need math.h and some faster AVR :)
432
            // we need math.h and some faster AVR :)
423
            //tanheight = rad2deg(atan(tanheight));
433
            //tanheight = rad2deg(atan(tanheight));
424
 
434
 
425
            itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10);
435
            itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10);
426
            usart0_puts("Bearing: ");
436
            usart0_puts("Bearing: ");
427
            usart0_puts(conv_array);
437
            usart0_puts(conv_array);
428
            usart0_puts("\tHeightangle: ");
438
            usart0_puts("\tHeightangle: ");
429
            itoa(tanheight, conv_array, 10);
439
            itoa(tanheight, conv_array, 10);
430
            usart0_puts(conv_array);
440
            usart0_puts(conv_array);
431
            usart0_puts("\r\n");
441
            usart0_puts("\r\n");
432
#endif
442
#endif
433
            rxd_buffer_locked = 0;
-
 
-
 
443
           
434
        }
444
        }
435
        // handle keypress
445
        // handle keypress
436
        if (s1_pressed()) {
446
        if (s1_pressed()) {
437
            config_menu();
447
            config_menu();
438
        }
448
        }
439
        // reqest data untill there has been some answer
449
        // reqest data untill there has been some answer
440
        if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) ||
450
        if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) ||
441
            // or while not in passive mode
451
            // or while not in passive mode
442
            (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) {
452
            (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) {
443
            usart1_EnableTXD();
453
            usart1_EnableTXD();
444
            //usart1_puts_pgm(PSTR("zu alt\r\n"));
454
            //usart1_puts_pgm(PSTR("zu alt\r\n"));
445
#if FCONLY
455
#if FCONLY
446
            // request data ever 100ms from FC;
456
            // request data ever 100ms from FC;
447
            //usart1_request_mk_data(0, 'd', 100);
457
            //usart1_request_mk_data(0, 'd', 100);
448
            usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
458
            usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
449
#else                   
459
#else                   
450
            // request OSD Data from NC every 100ms
460
            // request OSD Data from NC every 100ms
451
            //usart1_request_mk_data(1, 'o', 100);
461
            //usart1_request_mk_data(1, 'o', 100);
452
            usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
462
            usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
453
 
463
 
-
 
464
 
-
 
465
                       
-
 
466
            // request 3D Data from NC every 100ms
-
 
467
            //usart1_request_mk_data(1, 'c', 100);
-
 
468
//                      _delay_ms(10);
-
 
469
//            usart1_puts_pgm(PSTR(REQUEST_3DDATA));
-
 
470
 
454
            // and disable debug...
471
            // and disable debug...
455
            //usart1_request_mk_data(0, 'd', 0);
472
            //usart1_request_mk_data(0, 'd', 0);
456
#endif
473
#endif
457
            // reset last time counter
474
            // reset last time counter
458
            seconds_since_last_data = 0;
475
            seconds_since_last_data = 0;
459
            usart1_DisableTXD();
476
            usart1_DisableTXD();
460
 
477
 
461
            // do not spam too much
478
            // do not spam too much
462
            if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) {
479
            if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) {
463
                _delay_ms(300);
480
                _delay_ms(100);
464
            }
481
            }
465
        }
482
        }
466
 
483
 
467
 
484
 
468
        if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore
485
        if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore
469
            if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
486
            if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
470
                clear();
487
                clear();
471
            }
488
            }
472
            COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
489
            COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
473
            COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ;
490
            COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ;
474
        } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again            
491
        } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again            
475
            if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
492
            if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
476
                COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
493
                COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
477
            }
494
            }
478
            COSD_FLAGS_MODES |= COSD_FLAG_HUD;
495
            COSD_FLAGS_MODES |= COSD_FLAG_HUD;
479
            COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ);
496
            COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ);
480
        }
497
        }
481
 
498
 
482
    }
499
    }
483
 
500
 
484
#else // character flashing...
501
#else // character flashing...
485
    clear();
502
    clear();
486
    write_all_chars();
503
    write_all_chars();
487
    LED1_ON
504
    LED1_ON
488
    LED2_ON
505
    LED2_ON
489
    LED3_ON
506
    LED3_ON
490
    LED4_ON
507
    LED4_ON
491
    while (1) {
508
    while (1) {
492
 
509
 
493
    };
510
    };
494
#endif
511
#endif
495
 
512
 
496
 
513
 
497
    return 0;
514
    return 0;
498
}
515
}
499
 
516