Subversion Repositories Projects

Rev

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

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