Subversion Repositories Projects

Rev

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

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