Subversion Repositories Projects

Rev

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

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