Subversion Repositories Projects

Rev

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

Rev 459 Rev 465
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
2
 *   Copyright (C) 2009 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       *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project       *
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
 
36
 
37
/* TODO:
37
/* TODO:
38
 * - verifiy correctness of values
38
 * - verifiy correctness of values
39
 * - clean up code :)
39
 * - clean up code :)
40
 */
40
 */
41
 
41
 
42
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
42
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
43
// data structs not needed for character flashin
43
// data structs not needed for character flashin
44
#include "mk-data-structs.h"
44
#include "mk-data-structs.h"
45
 
45
 
46
/* ##########################################################################
46
/* ##########################################################################
47
 * global definitions and global vars
47
 * global definitions and global vars
48
 * ##########################################################################*/
48
 * ##########################################################################*/
49
 
49
 
50
volatile uint16_t setsReceived = 0;
50
volatile uint16_t setsReceived = 0;
51
 
51
 
52
volatile NaviData_t naviData;
52
volatile NaviData_t naviData;
53
volatile DebugOut_t debugData;
53
volatile DebugOut_t debugData;
54
 
54
 
55
// cache old vars for blinking attribute, checkup is faster than full
55
// cache old vars for blinking attribute, checkup is faster than full
56
// attribute write each time
56
// attribute write each time
57
volatile uint8_t last_UBat = 255;
57
volatile uint8_t last_UBat = 255;
58
volatile uint8_t last_RC_Quality = 255;
58
volatile uint8_t last_RC_Quality = 255;
59
 
59
 
60
// 16bit should be enough, normal LiPos don't last that long
60
// 16bit should be enough, normal LiPos don't last that long
61
volatile uint16_t uptime = 0;
61
volatile uint16_t uptime = 0;
62
volatile uint16_t timer = 0;
62
volatile uint16_t timer = 0;
-
 
63
volatile uint8_t short_timer = 0;
63
 
64
 
64
// remember last time data was received
65
// remember last time data was received
65
volatile uint8_t seconds_since_last_data = 0;
66
volatile uint8_t seconds_since_last_data = 0;
66
 
67
 
67
// store stats description in progmem to save space
68
// store stats description in progmem to save space
68
char stats_item_0[] PROGMEM = "max Altitude:";
69
char stats_item_0[] PROGMEM = "max Altitude:";
69
char stats_item_1[] PROGMEM = "max Speed   :";
70
char stats_item_1[] PROGMEM = "max Speed   :";
70
char stats_item_2[] PROGMEM = "max Distance:";
71
char stats_item_2[] PROGMEM = "max Distance:";
71
char stats_item_3[] PROGMEM = "min voltage :";
72
char stats_item_3[] PROGMEM = "min voltage :";
72
char stats_item_4[] PROGMEM = "max time    :";
73
char stats_item_4[] PROGMEM = "max time    :";
73
char stats_item_5[] PROGMEM = "longitude   :";
74
char stats_item_5[] PROGMEM = "longitude   :";
74
char stats_item_6[] PROGMEM = "latitude    :";
75
char stats_item_6[] PROGMEM = "latitude    :";
75
char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
76
char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
76
    stats_item_3, stats_item_4, stats_item_5, stats_item_6};
77
    stats_item_3, stats_item_4, stats_item_5, stats_item_6};
77
 
78
 
78
// store more fixed strings in progmen
79
// store more fixed strings in progmen
79
char ON[] PROGMEM = "ON ";
80
char ON[] PROGMEM = "ON ";
80
char OFF[] PROGMEM = "OFF";
81
char OFF[] PROGMEM = "OFF";
81
 
82
 
82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
83
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
83
 
84
 
84
// general PAL|NTSC distingiusch stuff
85
// general PAL|NTSC distingiusch stuff
85
uint8_t top_line = 1;
86
uint8_t top_line = 1;
86
uint8_t bottom_line = 14;
87
uint8_t bottom_line = 14;
87
 
88
 
88
// battery voltages
89
// battery voltages
89
uint8_t min_voltage = 0;
90
uint8_t min_voltage = 0;
90
uint8_t max_voltage = 0;
91
uint8_t max_voltage = 0;
91
 
92
 
92
// Flags
93
// Flags
93
uint8_t COSD_FLAGS = 0;
94
uint8_t COSD_FLAGS = 0, COSD_FLAGS2;
94
 
95
 
95
/* ##########################################################################
96
/* ##########################################################################
96
 * debounce buttons
97
 * debounce buttons
97
 * ##########################################################################*/
98
 * ##########################################################################*/
98
int s1_pressed() {
99
int s1_pressed() {
99
    if (S1_PRESSED) {
100
    if (S1_PRESSED) {
100
        _delay_ms(25);
101
        _delay_ms(25);
101
        if (S1_PRESSED) return 1;
102
        if (S1_PRESSED) return 1;
102
    }
103
    }
103
    return 0;
104
    return 0;
104
}
105
}
105
 
106
 
106
int s2_pressed() {
107
int s2_pressed() {
107
    if (S2_PRESSED) {
108
    if (S2_PRESSED) {
108
        _delay_ms(25);
109
        _delay_ms(25);
109
        if (S2_PRESSED) return 1;
110
        if (S2_PRESSED) return 1;
110
    }
111
    }
111
    return 0;
112
    return 0;
112
}
113
}
113
 
114
 
114
/* ##########################################################################
115
/* ##########################################################################
115
 * Interrupt handler
116
 * Interrupt handler
116
 * ##########################################################################*/
117
 * ##########################################################################*/
117
 
118
 
118
/**
119
/**
119
 * handler for undefined Interrupts
120
 * handler for undefined Interrupts
120
 * if not defined AVR will reset in case any unhandled interrupts occur
121
 * if not defined AVR will reset in case any unhandled interrupts occur
121
 */
122
 */
122
ISR(__vector_default) {
123
ISR(__vector_default) {
123
    asm("nop");
124
    asm("nop");
124
}
125
}
125
 
126
 
126
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
127
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
127
/* ##########################################################################
128
/* ##########################################################################
128
 * timer stuff
129
 * timer stuff
129
 * ##########################################################################*/
130
 * ##########################################################################*/
-
 
131
 
-
 
132
 
-
 
133
/* ##########################################################################
-
 
134
 * STROM TEST
-
 
135
 * ##########################################################################*/
-
 
136
#define INT0_HIGH                       PORTD |=  (1 << PD2);
-
 
137
#define INT0_LOW                        PORTD &= ~(1 << PD2);
-
 
138
 
-
 
139
void SpiMasterInit(void)
-
 
140
{
-
 
141
        volatile char IOReg;
-
 
142
        // set PB4(/SS), PB5(MOSI), PB7(SCK) as output
-
 
143
        DDRB  = (1<<PB4)|(1<<PB5)|(1<<PB7);
-
 
144
                PORTB |= (1 << PB4); // pullup SS
-
 
145
        // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128
-
 
146
        SPCR  = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
-
 
147
        IOReg   = SPSR;                         // clear SPIF bit in SPSR
-
 
148
        IOReg   = SPDR;
-
 
149
        //sei(); // we do it later
-
 
150
}
-
 
151
 
-
 
152
volatile size_t icnt = 0;
-
 
153
volatile unsigned char * iptr;
-
 
154
volatile unsigned char spi_cmd[5];       
-
 
155
volatile uint8_t spi_ready = 1;
-
 
156
int16_t ampere = 0;
-
 
157
 
-
 
158
ISR(SPI_STC_vect)
-
 
159
{
-
 
160
    /* Empfangenes Byte im Puffer ablegen. */
-
 
161
    *iptr++ = SPDR;
-
 
162
    /* Zähler dekrementieren. */
-
 
163
    icnt--;
-
 
164
    /* Falls Zähler noch nicht 0 ist... */
-
 
165
    if (icnt) {
-
 
166
                ///_delay_ms(1);
-
 
167
        /* ...nächstes Byte senden. */
-
 
168
        //SPDR = *iptr;
-
 
169
                spi_ready = 1;
-
 
170
    }
-
 
171
    else {
-
 
172
        /* ...andernfalls Interrupt Modus deaktivieren. */
-
 
173
        SPCR &= ~_BV(SPIE);
-
 
174
                INT0_HIGH
-
 
175
                //_delay_ms(1);
-
 
176
    }
-
 
177
}
-
 
178
 
-
 
179
int TransferIsBusy(void)
-
 
180
{
-
 
181
    return SPCR & _BV(SPIE);
-
 
182
}
-
 
183
 
-
 
184
void StartTransfer(unsigned char *data, size_t len)
-
 
185
{
-
 
186
        INT0_LOW
-
 
187
    /* Interrupt Datenpointer und Zähler setzen. */
-
 
188
    iptr = data;
-
 
189
    icnt = len;
-
 
190
 
-
 
191
    /* SPI Interrupt aktivieren und Transfer anstossen. */
-
 
192
    SPCR |= _BV(SPIE);
-
 
193
    SPDR = *iptr;
-
 
194
}
-
 
195
 
-
 
196
 
-
 
197
/* ##########################################################################
-
 
198
 * / STROM TEST END
-
 
199
 * ##########################################################################*/
-
 
200
 
130
 
201
 
131
/**
202
/**
132
 * timer kicks in every 1000uS ^= 1ms
203
 * timer kicks in every 1000uS ^= 1ms
133
 */
204
 */
134
ISR(TIMER0_OVF_vect) {
205
ISR(TIMER0_OVF_vect) {
135
    OCR0 = 15; // preload
206
    OCR0 = 15; // preload
136
    if (!timer--) {
207
    if (!timer--) {
137
        uptime++;
208
        uptime++;
138
        timer = 999;
209
        timer = 999;
139
        seconds_since_last_data++;
210
        seconds_since_last_data++;
140
    }
211
    }
-
 
212
        short_timer++;
-
 
213
        if (spi_ready && icnt) {
-
 
214
                SPDR = *iptr;
-
 
215
        }
141
}
216
}
142
 
-
 
143
 
217
 
144
/* ##########################################################################
218
/* ##########################################################################
145
 * A simple config menu for the flags
219
 * A simple config menu for the flags
146
 * ##########################################################################*/
220
 * ##########################################################################*/
147
 
221
 
148
/**
222
/**
149
 * helper function for menu updating
223
 * helper function for menu updating
150
 */
224
 */
151
void config_menu_drawings(uint8_t chosen) {
225
void config_menu_drawings(uint8_t chosen) {
152
    // clear prevoius _cursor_
226
    // clear prevoius _cursor_
153
    write_ascii_string(3, (chosen + 5) % 8, " ");
227
    write_ascii_string(3, (chosen + 5) % 8, " ");
154
    // draw current _cursor_
228
    // draw current _cursor_
155
    write_ascii_string(3, chosen + 5, ">");
229
    write_ascii_string(3, chosen + 5, ">");
156
    if (COSD_FLAGS & COSD_FLAG_HUD) {
230
    if (COSD_FLAGS & COSD_FLAG_HUD) {
157
        write_ascii_string_pgm(23, 5, ON);
231
        write_ascii_string_pgm(23, 5, ON);
158
    } else {
232
    } else {
159
        write_ascii_string_pgm(23, 5, OFF);
233
        write_ascii_string_pgm(23, 5, OFF);
160
    }
234
    }
161
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
235
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
162
        write_ascii_string_pgm(23, 6, ON);
236
        write_ascii_string_pgm(23, 6, ON);
163
    } else {
237
    } else {
164
        write_ascii_string_pgm(23, 6, OFF);
238
        write_ascii_string_pgm(23, 6, OFF);
165
    }
239
    }
166
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
240
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
167
        write_ascii_string_pgm(23, 7, ON);
241
        write_ascii_string_pgm(23, 7, ON);
168
    } else {
242
    } else {
169
        write_ascii_string_pgm(23, 7, OFF);
243
        write_ascii_string_pgm(23, 7, OFF);
170
    }
244
    }
171
    if (COSD_FLAGS & COSD_FLAG_STATS) {
245
    if (COSD_FLAGS & COSD_FLAG_STATS) {
172
        write_ascii_string_pgm(23, 8, ON);
246
        write_ascii_string_pgm(23, 8, ON);
173
    } else {
247
    } else {
174
        write_ascii_string_pgm(23, 8, OFF);
248
        write_ascii_string_pgm(23, 8, OFF);
175
    }
249
    }
176
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
250
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
177
        write_ascii_string_pgm(23, 9, ON);
251
        write_ascii_string_pgm(23, 9, ON);
178
    } else {
252
    } else {
179
        write_ascii_string_pgm(23, 9, OFF);
253
        write_ascii_string_pgm(23, 9, OFF);
180
    }
254
    }
181
}
255
}
182
 
256
 
183
/**
257
/**
184
 * some sort of clicking response in the menu
258
 * some sort of clicking response in the menu
185
 */
259
 */
186
void config_menu_doclick(uint8_t chosen, char** menu) {
260
void config_menu_doclick(uint8_t chosen, char** menu) {
187
    write_ascii_string(4, chosen + 5, "DONE              ");
261
    write_ascii_string(4, chosen + 5, "DONE              ");
188
    _delay_ms(1000);
262
    _delay_ms(1000);
189
    write_ascii_string(4, chosen + 5, menu[chosen]);
263
    write_ascii_string(4, chosen + 5, menu[chosen]);
190
}
264
}
191
 
265
 
192
/**
266
/**
193
 * a simple config menu tryout
267
 * a simple config menu tryout
194
 */
268
 */
195
void config_menu(void) {
269
void config_menu(void) {
196
    // disable interrupts (makes the menu more smoothely)
270
    // disable interrupts (makes the menu more smoothely)
197
    cli();
271
    cli();
198
 
272
 
199
    // clear screen
273
    // clear screen
200
    clear();
274
    clear();
201
 
275
 
202
    char* menu[9] = {"Full HUD",
276
    char* menu[9] = {"Full HUD",
203
        "Art.Horizon in HUD",
277
        "Art.Horizon in HUD",
204
        "Big Vario bar",
278
        "Big Vario bar",
205
        "Statistics",
279
        "Statistics",
206
        "Warnings", // TODO: do it!
280
        "Warnings", // TODO: do it!
207
        "Reset uptime",
281
        "Reset uptime",
208
        "Request OSD-data",
282
        "Request OSD-data",
209
        "Disable Debug-data",
283
        "Disable Debug-data",
210
        "EXIT"};
284
        "EXIT"};
211
 
285
 
212
    uint8_t inmenu = 1;
286
    uint8_t inmenu = 1;
213
    uint8_t chosen = 0;
287
    uint8_t chosen = 0;
214
    write_ascii_string(6, 2, "C-OSD Config Menu");
288
    write_ascii_string(6, 2, "C-OSD Config Menu");
215
 
289
 
216
    // wait a bit before doing stuff so user has chance to release button
290
    // wait a bit before doing stuff so user has chance to release button
217
    _delay_ms(250);
291
    _delay_ms(250);
218
 
292
 
219
    write_ascii_string(4, 5, menu[0]);
293
    write_ascii_string(4, 5, menu[0]);
220
    write_ascii_string(4, 6, menu[1]);
294
    write_ascii_string(4, 6, menu[1]);
221
    write_ascii_string(4, 7, menu[2]);
295
    write_ascii_string(4, 7, menu[2]);
222
    write_ascii_string(4, 8, menu[3]);
296
    write_ascii_string(4, 8, menu[3]);
223
    write_ascii_string(4, 9, menu[4]);
297
    write_ascii_string(4, 9, menu[4]);
224
    write_ascii_string(4, 10, menu[5]);
298
    write_ascii_string(4, 10, menu[5]);
225
    write_ascii_string(4, 11, menu[6]);
299
    write_ascii_string(4, 11, menu[6]);
226
    write_ascii_string(4, 12, menu[7]);
300
    write_ascii_string(4, 12, menu[7]);
227
    write_ascii_string(4, 13, menu[8]);
301
    write_ascii_string(4, 13, menu[8]);
228
 
302
 
229
    config_menu_drawings(chosen);
303
    config_menu_drawings(chosen);
230
 
304
 
231
    while (inmenu) {
305
    while (inmenu) {
232
        if (s2_pressed()) {
306
        if (s2_pressed()) {
233
            write_ascii_string(3, chosen + 5, " ");
307
            write_ascii_string(3, chosen + 5, " ");
234
            chosen = (chosen + 1) % 9;
308
            chosen = (chosen + 1) % 9;
235
            write_ascii_string(3, chosen + 5, ">");
309
            write_ascii_string(3, chosen + 5, ">");
236
            _delay_ms(500);
310
            _delay_ms(500);
237
        } else if (s1_pressed()) {
311
        } else if (s1_pressed()) {
238
            switch (chosen) {
312
            switch (chosen) {
239
                case 0: // full HUD
313
                case 0: // full HUD
240
                    COSD_FLAGS ^= COSD_FLAG_HUD;
314
                    COSD_FLAGS ^= COSD_FLAG_HUD;
241
                    config_menu_drawings(chosen);
315
                    config_menu_drawings(chosen);
242
                    break;
316
                    break;
243
                case 1: // art horizon
317
                case 1: // art horizon
244
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
318
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
245
                    config_menu_drawings(chosen);
319
                    config_menu_drawings(chosen);
246
                    break;
320
                    break;
247
                case 2: // big vario
321
                case 2: // big vario
248
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
322
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
249
                    config_menu_drawings(chosen);
323
                    config_menu_drawings(chosen);
250
                    break;
324
                    break;
251
                case 3: // statistics
325
                case 3: // statistics
252
                    COSD_FLAGS ^= COSD_FLAG_STATS;
326
                    COSD_FLAGS ^= COSD_FLAG_STATS;
253
                    config_menu_drawings(chosen);
327
                    config_menu_drawings(chosen);
254
                    break;
328
                    break;
255
                case 4: // warnings
329
                case 4: // warnings
256
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
330
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
257
                    config_menu_drawings(chosen);
331
                    config_menu_drawings(chosen);
258
                    break;
332
                    break;
259
                case 5: // reset uptime
333
                case 5: // reset uptime
260
                    uptime = 0;
334
                    uptime = 0;
261
                    config_menu_doclick(chosen, menu);
335
                    config_menu_doclick(chosen, menu);
262
                    break;
336
                    break;
263
                case 6: // re-request OSD data
337
                case 6: // re-request OSD data
264
#if FCONLY
338
#if FCONLY
265
                    // request data ever 100ms from FC;
339
                    // request data ever 100ms from FC;
266
                    usart1_request_mk_data(0, 'd', 100);
340
                    usart1_request_mk_data(0, 'd', 100);
267
#else
341
#else
268
                    // request OSD Data from NC every 100ms
342
                    // request OSD Data from NC every 100ms
269
                    usart1_request_mk_data(1, 'o', 100);
343
                    usart1_request_mk_data(1, 'o', 100);
270
 
344
 
271
                    // and disable debug...
345
                    // and disable debug...
272
                    usart1_request_mk_data(0, 'd', 0);
346
                    usart1_request_mk_data(0, 'd', 0);
273
#endif
347
#endif
274
                    config_menu_doclick(chosen, menu);
348
                    config_menu_doclick(chosen, menu);
275
                    break;
349
                    break;
276
                case 7: // disable debug data
350
                case 7: // disable debug data
277
                    // disable sending of debug data
351
                    // disable sending of debug data
278
                    // may result in smoother ddata display
352
                    // may result in smoother ddata display
279
                    usart1_request_mk_data(0, 'd', 0);
353
                    usart1_request_mk_data(0, 'd', 0);
280
                    config_menu_doclick(chosen, menu);
354
                    config_menu_doclick(chosen, menu);
281
                    break;
355
                    break;
282
                case 8: // exit
356
                case 8: // exit
283
                    inmenu = 0;
357
                    inmenu = 0;
284
                    break;
358
                    break;
285
            }
359
            }
286
            _delay_ms(250);
360
            _delay_ms(250);
287
        }
361
        }
288
    }
362
    }
289
 
363
 
290
    // clear screen up again
364
    // clear screen up again
291
    clear();
365
    clear();
292
 
366
 
293
    // update flags to paint display again if needed
367
    // update flags to paint display again if needed
294
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
368
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
295
 
369
 
296
    // enable interrupts again
370
    // enable interrupts again
297
    sei();
371
    sei();
298
}
372
}
299
 
373
 
300
/**
374
/**
301
 * auto config some stuff on startup, currently only battery cells
375
 * auto config some stuff on startup, currently only battery cells
302
 * TODO: this is testing stuff, strings should go progmem and so on...
376
 * TODO: this is testing stuff, strings should go progmem and so on...
303
 */
377
 */
304
void auto_config(uint8_t UBat) {
378
void auto_config(uint8_t UBat) {
305
    clear();
379
    clear();
306
    write_ascii_string(2, 2, "C-OSD Initialisation");
380
    write_ascii_string(2, 2, "C-OSD Initialisation");
307
#if FCONLY
381
#if FCONLY
308
    write_ascii_string(2, 3, "FC only Mode");
382
    write_ascii_string(2, 3, "FC only Mode");
309
#else
383
#else
310
    write_ascii_string(2, 3, "NaviCtrl Mode");
384
    write_ascii_string(2, 3, "NaviCtrl Mode");
311
#endif
385
#endif
312
        write_ascii_string(2, 4, BUILDDATE);
386
        write_ascii_string(2, 4, BUILDDATE);
313
    uint8_t cellnum = 0;
387
    uint8_t cellnum = 0;
314
    if (CELL_NUM == -1) {
388
    if (CELL_NUM == -1) {
315
        write_ascii_string(2, 6, "Guessing Number of Cells");
389
        write_ascii_string(2, 6, "Guessing Number of Cells");
316
        do {
390
        do {
317
            cellnum++;
391
            cellnum++;
318
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 15));
392
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 15));
319
    } else {
393
    } else {
320
        cellnum = CELL_NUM;
394
        cellnum = CELL_NUM;
321
    }
395
    }
322
    min_voltage = cellnum * CELL_VOLT_MIN;
396
    min_voltage = cellnum * CELL_VOLT_MIN;
323
    max_voltage = cellnum * CELL_VOLT_MAX;
397
    max_voltage = cellnum * CELL_VOLT_MAX;
324
    write_ascii_string(2, 7, "Number of Cells:");
398
    write_ascii_string(2, 7, "Number of Cells:");
325
    write_ndigit_number_u(21, 7, cellnum, 1, 0);
399
    write_ndigit_number_u(21, 7, cellnum, 1, 0);
326
    write_ascii_string(2, 8, "Warn Voltage   :");
400
    write_ascii_string(2, 8, "Warn Voltage   :");
327
    write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
401
    write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
328
    write_ascii_string(2, 9, "Max Voltage    :");
402
    write_ascii_string(2, 9, "Max Voltage    :");
329
    write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
403
    write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
330
    _delay_ms(200);
404
    _delay_ms(200);
331
    clear();
405
    clear();
332
    // update flags to paint display again because of clear
406
    // update flags to paint display again because of clear
333
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
407
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
334
}
408
}
335
 
409
 
336
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
410
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
337
 
411
 
338
/* ##########################################################################
412
/* ##########################################################################
339
 * MAIN
413
 * MAIN
340
 * ##########################################################################*/
414
 * ##########################################################################*/
341
int main(void) {
415
int main(void) {
-
 
416
       
342
    // set up FLAGS, compiler should flatten this one
417
    // set up FLAGS, compiler should flatten this one
343
    COSD_FLAGS = (NTSC << 0);
418
    COSD_FLAGS = (NTSC << 0);
344
    COSD_FLAGS |= (HUD << 1);
419
    COSD_FLAGS |= (HUD << 1);
345
    COSD_FLAGS |= (ARTHORIZON << 2);
420
    COSD_FLAGS |= (ARTHORIZON << 2);
346
    COSD_FLAGS |= (BIGVARIO << 3);
421
    COSD_FLAGS |= (BIGVARIO << 3);
347
    COSD_FLAGS |= (STATS << 4);
422
    COSD_FLAGS |= (STATS << 4);
348
    COSD_FLAGS |= (WARNINGS << 5);
423
    COSD_FLAGS |= (WARNINGS << 5);
349
 
424
 
350
    // set up Atmega162 Ports
425
    // set up Atmega162 Ports
351
    DDRA |= (1 << PA1); // PA1 output (/CS)
426
    DDRA |= (1 << PA1); // PA1 output (/CS)
352
    MAX_CS_HIGH
427
    MAX_CS_HIGH
353
    DDRA |= (1 << PA2); // PA2 output (SDIN)
428
    DDRA |= (1 << PA2); // PA2 output (SDIN)
354
    MAX_SDIN_LOW
429
    MAX_SDIN_LOW
355
    DDRA |= (1 << PA3); // PA3 output (SCLK)
430
    DDRA |= (1 << PA3); // PA3 output (SCLK)
356
    MAX_SCLK_LOW
431
    MAX_SCLK_LOW
357
    DDRA |= (1 << PA5); // PA5 output (RESET)
432
    DDRA |= (1 << PA5); // PA5 output (RESET)
358
    MAX_RESET_HIGH
433
    MAX_RESET_HIGH
359
 
434
 
360
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
435
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
361
    LED1_OFF
436
    LED1_OFF
362
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
437
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
363
    LED2_OFF
438
    LED2_OFF
364
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
439
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
365
    LED3_OFF
440
    LED3_OFF
366
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
441
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
367
    LED4_OFF
442
    LED4_OFF
368
 
443
 
369
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
444
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
370
    PORTC |= (1 << PC4); // pullup
445
    PORTC |= (1 << PC4); // pullup
371
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
446
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
372
    PORTC |= (1 << PC5); // pullup
447
    PORTC |= (1 << PC5); // pullup
373
 
448
 
374
    // set up top and bottom lines
449
    // set up top and bottom lines
375
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
450
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
376
        bottom_line = 12;
451
        bottom_line = 12;
377
    } else {
452
    } else {
378
        bottom_line = 14;
453
        bottom_line = 14;
379
    }
454
    }
380
 
455
 
381
    // reset the MAX7456 to be sure any undefined states do no harm
456
    // reset the MAX7456 to be sure any undefined states do no harm
382
    MAX_RESET_LOW
457
    MAX_RESET_LOW
383
    MAX_RESET_HIGH
458
    MAX_RESET_HIGH
384
 
459
 
385
    // give the FC/NC and the maxim time to come up
460
    // give the FC/NC and the maxim time to come up
386
    LED4_ON
461
    LED4_ON
387
    _delay_ms(2000);
462
    _delay_ms(2000);
388
 
463
 
389
    LED4_OFF
464
    LED4_OFF
390
 
465
 
391
 
466
 
392
    //Pushing NEW chars to the MAX7456
467
    //Pushing NEW chars to the MAX7456
393
#if (WRITECHARS != -1)
468
#if (WRITECHARS != -1)
394
    // DISABLE display (VM0)
469
    // DISABLE display (VM0)
395
    spi_send_byte(0x00, 0b00000000);
470
    spi_send_byte(0x00, 0b00000000);
396
    #include "characters.c"
471
    #include "characters.c"
397
#endif
472
#endif
398
 
473
 
399
    // Setup Video Mode
474
    // Setup Video Mode
400
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
475
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
401
        // NTSC + enable display immediately (VM0)
476
        // NTSC + enable display immediately (VM0)
402
        spi_send_byte(0x00, 0b00001000);
477
        spi_send_byte(0x00, 0b00001000);
403
    } else {
478
    } else {
404
        // PAL + enable display immediately (VM0)
479
        // PAL + enable display immediately (VM0)
405
        spi_send_byte(0x00, 0b01001000);
480
        spi_send_byte(0x00, 0b01001000);
406
    }
481
    }
407
 
482
 
408
    /*// clear all display-mem (DMM)
483
    /*// clear all display-mem (DMM)
409
    spi_send_byte(0x04, 0b00000100);
484
    spi_send_byte(0x04, 0b00000100);
410
 
485
 
411
    // clearing takes 12uS according to maxim so lets wait longer
486
    // clearing takes 12uS according to maxim so lets wait longer
412
    _delay_us(120);
487
    _delay_us(120);
413
 
488
 
414
    // 8bit mode
489
    // 8bit mode
415
    spi_send_byte(0x04, 0b01000000);*/
490
    spi_send_byte(0x04, 0b01000000);*/
416
 
491
 
417
    // clear display memory and set to 8bit mode
492
    // clear display memory and set to 8bit mode
418
    clear();
493
    clear();
419
 
494
 
420
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
495
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
421
    // init usart
496
    // init usart
422
    usart1_init();
497
    usart1_init();
423
 
498
 
424
    // set up timer
499
    // set up timer
425
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
500
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
426
    OCR0 = 15; // preload
501
    OCR0 = 15; // preload
427
    TIMSK |= (1 << TOIE0); // enable overflow timer0
502
    TIMSK |= (1 << TOIE0); // enable overflow timer0
-
 
503
 
-
 
504
        DDRD |= (1 << PD2); // PD2 output (INT0)  
-
 
505
        SpiMasterInit();
428
 
506
 
429
    // enable interrupts
507
    // enable interrupts
-
 
508
    sei();
430
    sei();
509
       
431
#endif
510
#endif
432
 
511
 
433
    //write_ascii_string(2,  7, "         CaScAdE          ");
512
    //write_ascii_string(2,  7, "         CaScAdE          ");
434
    //write_ascii_string(2,  8, "is TESTING his open source");
513
    //write_ascii_string(2,  8, "is TESTING his open source");
435
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
514
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
436
 
515
 
437
    // we are ready
516
    // we are ready
438
    LED3_ON
517
    LED3_ON
439
 
518
 
440
#if ALLCHARSDEBUG | (WRITECHARS != -1)
519
#if ALLCHARSDEBUG | (WRITECHARS != -1)
441
    clear();
520
    clear();
442
    write_all_chars();
521
    write_all_chars();
443
#else
522
#else
444
    // clear serial screen
523
    // clear serial screen
445
    //usart1_puts("\x1B[2J\x1B[H");
524
    //usart1_puts("\x1B[2J\x1B[H");
446
    //usart1_puts("hello world!\r\n");
525
    //usart1_puts("hello world!\r\n");
447
#if FCONLY
526
#if FCONLY
448
    // request data ever 100ms from FC;
527
    // request data ever 100ms from FC;
449
    usart1_request_mk_data(0, 'd', 100);
528
    usart1_request_mk_data(0, 'd', 100);
450
#else
529
#else
451
    // request OSD Data from NC every 100ms
530
    // request OSD Data from NC every 100ms
452
    usart1_request_mk_data(1, 'o', 100);
531
    usart1_request_mk_data(1, 'o', 100);
453
 
532
 
454
    // and disable debug...
533
    // and disable debug...
455
    usart1_request_mk_data(0, 'd', 0);
534
    usart1_request_mk_data(0, 'd', 0);
456
#endif
535
#endif
457
 
536
 
458
    // stats for after flight
537
    // stats for after flight
459
    int16_t max_Altimeter = 0;
538
    int16_t max_Altimeter = 0;
460
    uint8_t min_UBat = 255;
539
    uint8_t min_UBat = 255;
461
#if !(FCONLY)
540
#if !(FCONLY)
462
        uint16_t max_GroundSpeed = 0;
541
        uint16_t max_GroundSpeed = 0;
463
    int16_t max_Distance = 0;
542
    int16_t max_Distance = 0;
464
    uint16_t max_FlyingTime = 0;
543
    uint16_t max_FlyingTime = 0;
465
 
544
 
466
    // flags from last round to check for changes
545
    // flags from last round to check for changes
467
    uint8_t old_MKFlags = 0;
546
    uint8_t old_MKFlags = 0;
468
 
547
 
469
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
548
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
470
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
549
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
471
#endif
550
#endif
-
 
551
 
-
 
552
 
472
 
553
 
-
 
554
    while (1) {
-
 
555
                if (!icnt && spi_ready) {
-
 
556
                        short_timer = 0;
-
 
557
                        ampere = spi_cmd[1] << 8;
-
 
558
                        ampere |= spi_cmd[2];
-
 
559
                        //write_ascii_char(7+4*30, spi_cmd[0]);
-
 
560
                       
-
 
561
                        if (spi_cmd[0] == 'd') {
-
 
562
                                COSD_FLAGS2 |= COSD_FLAG_STROMREC;
-
 
563
                                //write_ascii_char(8+4*30, 'v'); // valid
-
 
564
                        } else {
-
 
565
                                COSD_FLAGS2 &= ~COSD_FLAG_STROMREC;
-
 
566
                                //write_ascii_char(8+4*30, 'i'); // invalid
-
 
567
                        }
-
 
568
                        spi_cmd[0] = 'A';
-
 
569
                        spi_cmd[1] = 'B';
-
 
570
                        spi_cmd[2] = 'C';
-
 
571
                        StartTransfer((unsigned char*)spi_cmd, 3);
473
    while (1) {
572
                }
474
        if (rxd_buffer_locked) {
573
        if (rxd_buffer_locked) {
475
            if (COSD_FLAGS & COSD_FLAG_HUD) {
574
            if (COSD_FLAGS & COSD_FLAG_HUD) {
476
#if FCONLY
575
#if FCONLY
477
                if (rxd_buffer[2] == 'D') { // FC Data
576
                if (rxd_buffer[2] == 'D') { // FC Data
478
                    Decode64();
577
                    Decode64();
479
                    debugData = *((DebugOut_t*) pRxData);
578
                    debugData = *((DebugOut_t*) pRxData);
480
 
579
 
481
                    // init on first data retrival, distinguished by last battery :)
580
                    // init on first data retrival, distinguished by last battery :)
482
                    if (last_UBat == 255) {
581
                    if (last_UBat == 255) {
483
                                                // fix for min_UBat
582
                                                // fix for min_UBat
484
                                                min_UBat = debugData.Analog[9];
583
                                                min_UBat = debugData.Analog[9];
485
                        auto_config(debugData.Analog[9]);
584
                        auto_config(debugData.Analog[9]);
486
                    }
585
                    }
487
                                        #include "osd_fcmode_default.c"
586
                                        #include "osd_fcmode_default.c"
488
                }
587
                }
489
#else
588
#else
490
                                if (rxd_buffer[2] == 'O') { // NC OSD Data
589
                                if (rxd_buffer[2] == 'O') { // NC OSD Data
491
                                        Decode64();
590
                                        Decode64();
492
                                        naviData = *((NaviData_t*) pRxData);
591
                                        naviData = *((NaviData_t*) pRxData);
493
 
592
 
494
                                        // init on first data retrival, distinguished by last battery :)
593
                                        // init on first data retrival, distinguished by last battery :)
495
                                        if (last_UBat == 255) {
594
                                        if (last_UBat == 255) {
496
                                                // fix for min_UBat
595
                                                // fix for min_UBat
497
                                                min_UBat = naviData.UBat;
596
                                                min_UBat = naviData.UBat;
498
                                            auto_config(naviData.UBat);
597
                                            auto_config(naviData.UBat);
499
                                        }
598
                                        }
500
                                        #include "osd_ncmode_default.c"
599
                                        #include "osd_ncmode_default.c"
501
                                }
600
                                }
502
#endif
601
#endif
503
            }
602
            }
504
            rxd_buffer_locked = 0;
603
            rxd_buffer_locked = 0;
505
        }
604
        }
506
        // handle keypress
605
        // handle keypress
507
        if (s1_pressed()) {
606
        if (s1_pressed()) {
508
            config_menu();
607
            config_menu();
509
        }
608
        }
510
        if (seconds_since_last_data > 2) {
609
        if (seconds_since_last_data > 2) {
511
#if FCONLY
610
#if FCONLY
512
            // request data ever 100ms from FC;
611
            // request data ever 100ms from FC;
513
            usart1_request_mk_data(0, 'd', 100);
612
            usart1_request_mk_data(0, 'd', 100);
514
#else
613
#else
515
            // request OSD Data from NC every 100ms
614
            // request OSD Data from NC every 100ms
516
            usart1_request_mk_data(1, 'o', 100);
615
            usart1_request_mk_data(1, 'o', 100);
517
 
616
 
518
            // and disable debug...
617
            // and disable debug...
519
            usart1_request_mk_data(0, 'd', 0);
618
            usart1_request_mk_data(0, 'd', 0);
520
#endif
619
#endif
521
        }
620
        }
522
    }
621
    }
523
#endif
622
#endif
524
    return 0;
623
    return 0;
525
}
624
}
526
 
625