Subversion Repositories Projects

Rev

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

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