Subversion Repositories Projects

Rev

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

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