Subversion Repositories Projects

Rev

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

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