Subversion Repositories Projects

Rev

Rev 2598 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2598 Rev 2601
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009-2017 by Claas Anders "CaScAdE" Rathje               *
2
 *   Copyright (C) 2009-2018 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));
367
                                memcpy((char*)(&debugData), (char*)pRxData, sizeof(str_DebugOut));
368
                                rxd_buffer_locked = 0;
368
                                rxd_buffer_locked = 0;
369
 
369
 
370
                // init on first data retrival, distinguished by last battery :)
370
                // init on first data retrival, distinguished by last battery :)
371
                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
371
                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
372
                    if (debugData.Analog[9] > 40) {
372
                    if (debugData.Analog[9] > 40) {
373
                        // fix for min_UBat
373
                        // fix for min_UBat
374
                        min_UBat = debugData.Analog[9];
374
                        min_UBat = debugData.Analog[9];
375
                        last_UBat = debugData.Analog[9];
375
                        last_UBat = debugData.Analog[9];
376
                        init_cosd(last_UBat);
376
                        init_cosd(last_UBat);
377
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
377
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
378
                    }
378
                    }
379
                } else {
379
                } else {
380
                    osd_fcmode();
380
                    osd_fcmode();
381
                }
381
                }
382
                seconds_since_last_data = 0;
382
                seconds_since_last_data = 0;
383
            } else {                   
383
            } else {                   
384
                                rxd_buffer_locked = 0;
384
                                rxd_buffer_locked = 0;
385
                        }
385
                        }
386
#else
386
#else
387
            if (rxd_buffer[2] == 'O') { // NC OSD Data
387
            if (rxd_buffer[2] == 'O') { // NC OSD Data
388
                Decode64();
388
                Decode64();
389
                //naviData = *((NaviData_t*)pRxData);
389
                //naviData = *((NaviData_t*)pRxData);
390
                                memcpy((char*)(&naviData), (char*)pRxData, sizeof(NaviData_t));
390
                                memcpy((char*)(&naviData), (char*)pRxData, sizeof(NaviData_t));
391
                                rxd_buffer_locked = 0;
391
                                rxd_buffer_locked = 0;
392
 
392
 
393
                #ifdef SHIFTBYminus45
393
                #ifdef SHIFTBYminus45
394
                naviData.AngleNick = (int8_t)(((int16_t)naviData.AngleRoll + (int16_t)naviData.AngleNick) / 2);
394
                naviData.AngleNick = (int8_t)(((int16_t)naviData.AngleRoll + (int16_t)naviData.AngleNick) / 2);
395
                naviData.AngleRoll = (int8_t)(((int16_t)naviData.AngleRoll - (int16_t)naviData.AngleNick) / 2);
395
                naviData.AngleRoll = (int8_t)(((int16_t)naviData.AngleRoll - (int16_t)naviData.AngleNick) / 2);
396
                naviData.CompassHeading = (naviData.CompassHeading + (360 - 45)) % 360;
396
                naviData.CompassHeading = (naviData.CompassHeading + (360 - 45)) % 360;
397
                #endif
397
                #endif
398
 
398
 
399
                // init on first data retrival, distinguished by last battery :)
399
                // init on first data retrival, distinguished by last battery :)
400
                                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
400
                                if (!(COSD_FLAGS_RUNTIME & COSD_OSD_STARTED)) {
401
                    if (naviData.UBat > 40) {
401
                    if (naviData.UBat > 40) {
402
                        // fix for min_UBat
402
                        // fix for min_UBat
403
                        min_UBat = naviData.UBat;
403
                        min_UBat = naviData.UBat;
404
                        last_UBat = naviData.UBat;
404
                        last_UBat = naviData.UBat;
405
                        init_cosd(last_UBat);
405
                        init_cosd(last_UBat);
406
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
406
                                                COSD_FLAGS_RUNTIME |= COSD_OSD_STARTED;
407
                    }
407
                    }
408
                } else {
408
                } else {
409
                    osd_ncmode();
409
                    osd_ncmode();
410
                }
410
                }
411
                //seconds_since_last_data = 0;
411
                //seconds_since_last_data = 0;
412
//            } else if (rxd_buffer[2] == 'C') { // 3D and Stick Data
412
//            } else if (rxd_buffer[2] == 'C') { // 3D and Stick Data
413
//                              Decode64();
413
//                              Decode64();
414
//                              memcpy((char*)(&data3d), (char*)pRxData, sizeof(Data3D_t));                             
414
//                              memcpy((char*)(&data3d), (char*)pRxData, sizeof(Data3D_t));                             
415
//                              rxd_buffer_locked = 0;
415
//                              rxd_buffer_locked = 0;
416
                        } else {
416
                        } else {
417
                                rxd_buffer_locked = 0;
417
                                rxd_buffer_locked = 0;
418
                        }
418
                        }
419
#endif
419
#endif
420
 
420
 
421
            // ONLY FOR TESTING
421
            // ONLY FOR TESTING
422
#ifdef ANTENNATRACKTEST 
422
#ifdef ANTENNATRACKTEST 
423
#include <stdlib.h>
423
#include <stdlib.h>
424
            //#include <float.h>
424
            //#include <float.h>
425
            //#include <math.h>
425
            //#include <math.h>
426
 
426
 
427
            //usart0_puts("\x1B[2J\x1B[H");
427
            //usart0_puts("\x1B[2J\x1B[H");
428
 
428
 
429
 
429
 
430
            /*
430
            /*
431
            naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100)
431
            naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100)
432
            naviData.HomePositionDeviation.Bearing = 35; // 35�
432
            naviData.HomePositionDeviation.Bearing = 35; // 35�
433
            altimeter_offset = 50; // 50m start height
433
            altimeter_offset = 50; // 50m start height
434
            naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000)
434
            naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000)
435
             */
435
             */
436
 
436
 
437
            static char conv_array[7];
437
            static char conv_array[7];
438
 
438
 
439
            // should be float
439
            // should be float
440
            int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset);
440
            int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset);
441
 
441
 
442
            // we need math.h and some faster AVR :)
442
            // we need math.h and some faster AVR :)
443
            //tanheight = rad2deg(atan(tanheight));
443
            //tanheight = rad2deg(atan(tanheight));
444
 
444
 
445
            itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10);
445
            itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10);
446
            usart0_puts("Bearing: ");
446
            usart0_puts("Bearing: ");
447
            usart0_puts(conv_array);
447
            usart0_puts(conv_array);
448
            usart0_puts("\tHeightangle: ");
448
            usart0_puts("\tHeightangle: ");
449
            itoa(tanheight, conv_array, 10);
449
            itoa(tanheight, conv_array, 10);
450
            usart0_puts(conv_array);
450
            usart0_puts(conv_array);
451
            usart0_puts("\r\n");
451
            usart0_puts("\r\n");
452
#endif
452
#endif
453
           
453
           
454
        }
454
        }
455
        // handle keypress
455
        // handle keypress
456
        if (s1_pressed()) {
456
        if (s1_pressed()) {
457
            config_menu();
457
            config_menu();
458
        }
458
        }
459
 
459
 
460
        // reqest data untill there has been some answer
460
        // reqest data untill there has been some answer
461
        if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) ||
461
        if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) ||
462
            // or while not in passive mode
462
            // or while not in passive mode
463
            (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) {
463
            (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) {
464
            usart1_EnableTXD();
464
            usart1_EnableTXD();
465
            //usart1_puts_pgm(PSTR("zu alt\r\n"));
465
            //usart1_puts_pgm(PSTR("zu alt\r\n"));
466
#if FCONLY
466
#if FCONLY
467
            // request data ever 100ms from FC;
467
            // request data ever 100ms from FC;
468
            //usart1_request_mk_data(0, 'd', 100);
468
            //usart1_request_mk_data(0, 'd', 100);
469
            usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
469
            usart1_puts_pgm(PSTR(REQUEST_DBG_DATA));
470
#else                   
470
#else                   
471
            // request OSD Data from NC every 100ms
471
            // request OSD Data from NC every 100ms
472
            //usart1_request_mk_data(1, 'o', 100);
472
            //usart1_request_mk_data(1, 'o', 100);
473
            usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
473
            usart1_puts_pgm(PSTR(REQUEST_OSD_DATA));
474
 
474
 
475
 
475
 
476
                       
476
                       
477
            // request 3D Data from NC every 100ms
477
            // request 3D Data from NC every 100ms
478
            //usart1_request_mk_data(1, 'c', 100);
478
            //usart1_request_mk_data(1, 'c', 100);
479
//                      _delay_ms(10);
479
//                      _delay_ms(10);
480
//            usart1_puts_pgm(PSTR(REQUEST_3DDATA));
480
//            usart1_puts_pgm(PSTR(REQUEST_3DDATA));
481
 
481
 
482
            // and disable debug...
482
            // and disable debug...
483
            //usart1_request_mk_data(0, 'd', 0);
483
            //usart1_request_mk_data(0, 'd', 0);
484
#endif
484
#endif
485
            // reset last time counter
485
            // reset last time counter
486
            seconds_since_last_data = 0;
486
            seconds_since_last_data = 0;
487
            usart1_DisableTXD();
487
            usart1_DisableTXD();
488
 
488
 
489
            // do not spam too much
489
            // do not spam too much
490
            if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) {
490
            if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) {
491
                _delay_ms(100);
491
                _delay_ms(100);
492
            }
492
            }
493
        }
493
        }
494
 
494
 
495
 
495
 
496
        if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore
496
        if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore
497
            if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
497
            if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
498
                clear();
498
                clear();
499
            }
499
            }
500
            COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
500
            COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
501
            COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ;
501
            COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ;
502
        } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again            
502
        } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again            
503
            if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
503
            if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
504
                COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
504
                COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
505
            }
505
            }
506
            COSD_FLAGS_MODES |= COSD_FLAG_HUD;
506
            COSD_FLAGS_MODES |= COSD_FLAG_HUD;
507
            COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ);
507
            COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ);
508
        }
508
        }
509
 
509
 
510
    }
510
    }
511
 
511
 
512
#else // character flashing...
512
#else // character flashing...
513
    clear();
513
    clear();
514
    write_all_chars();
514
    write_all_chars();
515
    LED1_ON
515
    LED1_ON
516
    LED2_ON
516
    LED2_ON
517
    LED3_ON
517
    LED3_ON
518
    LED4_ON
518
    LED4_ON
519
    while (1) {
519
    while (1) {
520
 
520
 
521
    };
521
    };
522
#endif
522
#endif
523
 
523
 
524
 
524
 
525
    return 0;
525
    return 0;
526
}
526
}
527
 
527