Subversion Repositories Projects

Rev

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

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