Subversion Repositories Projects

Rev

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

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