Subversion Repositories Projects

Rev

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

Rev 454 Rev 455
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
3
 *   admiralcascade@gmail.com                                               *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
6
 *   This program is free software; you can redistribute it and/or modify   *
7
 *   it under the terms of the GNU General Public License as published by   *
7
 *   it under the terms of the GNU General Public License as published by   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 *                                                                          *
19
 *                                                                          *
20
 *                                                                          *
20
 *                                                                          *
21
 *   Credits to:                                                            *
21
 *   Credits to:                                                            *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project       *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project       *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
24
 *   Klaus "akku" Buettner for the hardware                                 *
24
 *   Klaus "akku" Buettner for the hardware                                 *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
26
 ****************************************************************************/
26
 ****************************************************************************/
27
 
27
 
28
#include <avr/io.h>
28
#include <avr/io.h>
29
#include <avr/interrupt.h>
29
#include <avr/interrupt.h>
30
#include <util/delay.h>
30
#include <util/delay.h>
31
#include <avr/pgmspace.h> 
31
#include <avr/pgmspace.h>
32
#include "main.h"
32
#include "main.h"
33
#include "max7456_software_spi.h"
33
#include "max7456_software_spi.h"
34
#include "usart1.h"
34
#include "usart1.h"
35
#include "osd_helpers.h"
35
#include "osd_helpers.h"
36
 
36
 
37
/* TODO:
37
/* TODO:
38
 * - verifiy correctness of values
38
 * - verifiy correctness of values
39
 * - clean up code :)
39
 * - clean up code :)
40
 */
40
 */
41
 
41
 
42
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
42
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
43
// data structs not needed for character flashin
43
// data structs not needed for character flashin
44
#include "mk-data-structs.h"
44
#include "mk-data-structs.h"
45
 
45
 
46
/* ##########################################################################
46
/* ##########################################################################
47
 * global definitions and global vars
47
 * global definitions and global vars
48
 * ##########################################################################*/
48
 * ##########################################################################*/
49
 
49
 
50
volatile uint16_t setsReceived = 0;
50
volatile uint16_t setsReceived = 0;
51
 
51
 
52
volatile NaviData_t naviData;
52
volatile NaviData_t naviData;
53
volatile DebugOut_t debugData;
53
volatile DebugOut_t debugData;
54
 
54
 
55
// cache old vars for blinking attribute, checkup is faster than full
55
// cache old vars for blinking attribute, checkup is faster than full
56
// attribute write each time
56
// attribute write each time
57
volatile uint8_t last_UBat = 255;
57
volatile uint8_t last_UBat = 255;
58
volatile uint8_t last_RC_Quality = 255;
58
volatile uint8_t last_RC_Quality = 255;
59
 
59
 
60
// 16bit should be enough, normal LiPos don't last that long
60
// 16bit should be enough, normal LiPos don't last that long
61
volatile uint16_t uptime = 0;
61
volatile uint16_t uptime = 0;
62
volatile uint16_t timer = 0;
62
volatile uint16_t timer = 0;
63
 
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_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,
76
                                        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};
77
 
77
 
78
// store more fixed strings in progmen
78
// store more fixed strings in progmen
79
char ON[] PROGMEM = "ON ";
79
char ON[] PROGMEM = "ON ";
80
char OFF[] PROGMEM = "OFF";
80
char OFF[] PROGMEM = "OFF";
81
 
81
 
82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
83
 
83
 
84
// general PAL|NTSC distingiusch stuff
84
// general PAL|NTSC distingiusch stuff
85
uint8_t top_line = 1;
85
uint8_t top_line = 1;
86
uint8_t bottom_line = 14;
86
uint8_t bottom_line = 14;
87
 
87
 
88
// battery voltages
88
// battery voltages
89
uint8_t min_voltage = 0;
89
uint8_t min_voltage = 0;
90
uint8_t max_voltage = 0;
90
uint8_t max_voltage = 0;
91
 
91
 
92
// Flags
92
// Flags
93
uint8_t COSD_FLAGS = 0;
93
uint8_t COSD_FLAGS = 0;
94
 
94
 
95
/* ##########################################################################
95
/* ##########################################################################
96
 * debounce buttons
96
 * debounce buttons
97
 * ##########################################################################*/
97
 * ##########################################################################*/
98
int s1_pressed() {
98
int s1_pressed() {
99
        if (S1_PRESSED) {
99
    if (S1_PRESSED) {
100
                _delay_ms(25);
100
        _delay_ms(25);
101
                if (S1_PRESSED) return 1;
101
        if (S1_PRESSED) return 1;
102
        }
102
    }
103
        return 0;
103
    return 0;
104
}
104
}
105
 
105
 
106
int s2_pressed() {
106
int s2_pressed() {
107
        if (S2_PRESSED) {
107
    if (S2_PRESSED) {
108
                _delay_ms(25);
108
        _delay_ms(25);
109
                if (S2_PRESSED) return 1;
109
        if (S2_PRESSED) return 1;
110
        }
110
    }
111
        return 0;
111
    return 0;
112
}
112
}
113
 
113
 
114
/* ##########################################################################
114
/* ##########################################################################
115
 * Interrupt handler
115
 * Interrupt handler
116
 * ##########################################################################*/
116
 * ##########################################################################*/
-
 
117
 
117
/**
118
/**
118
 * handler for undefined Interrupts
119
 * handler for undefined Interrupts
119
 * 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
120
 */
121
 */
121
ISR(__vector_default) {
122
ISR(__vector_default) {
122
        asm("nop");
123
    asm("nop");
123
}
124
}
124
 
125
 
125
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
126
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
126
/* ##########################################################################
127
/* ##########################################################################
127
 * timer stuff
128
 * timer stuff
128
 * ##########################################################################*/
129
 * ##########################################################################*/
-
 
130
 
129
/**
131
/**
130
 * timer kicks in every 1000uS ^= 1ms
132
 * timer kicks in every 1000uS ^= 1ms
131
 */
133
 */
132
ISR(TIMER0_OVF_vect) {
134
ISR(TIMER0_OVF_vect) {
133
    OCR0 = 15; // preload
135
    OCR0 = 15; // preload
134
    if (!timer--) {
136
    if (!timer--) {
135
        uptime++;
137
        uptime++;
136
        timer = 999;
138
        timer = 999;
137
                seconds_since_last_data++;
139
        seconds_since_last_data++;
138
    }
140
    }
139
}
141
}
140
 
142
 
141
 
143
 
142
/* ##########################################################################
144
/* ##########################################################################
143
 * A simple config menu for the flags
145
 * A simple config menu for the flags
144
 * ##########################################################################*/
146
 * ##########################################################################*/
145
 
147
 
146
/**
148
/**
147
 * helper function for menu updating
149
 * helper function for menu updating
148
 */
150
 */
149
void config_menu_drawings(uint8_t chosen) {
151
void config_menu_drawings(uint8_t chosen) {
150
    // clear prevoius _cursor_
152
    // clear prevoius _cursor_
151
    write_ascii_string(3, (chosen + 5) % 8, " ");
153
    write_ascii_string(3, (chosen + 5) % 8, " ");
152
    // draw current _cursor_
154
    // draw current _cursor_
153
    write_ascii_string(3, chosen + 5, ">");
155
    write_ascii_string(3, chosen + 5, ">");
154
    if (COSD_FLAGS & COSD_FLAG_HUD) {
156
    if (COSD_FLAGS & COSD_FLAG_HUD) {
155
        write_ascii_string_pgm(23, 5, ON);
157
        write_ascii_string_pgm(23, 5, ON);
156
    } else {
158
    } else {
157
                write_ascii_string_pgm(23, 5, OFF);
159
        write_ascii_string_pgm(23, 5, OFF);
158
    }
160
    }
159
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
161
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
160
        write_ascii_string_pgm(23, 6, ON);
162
        write_ascii_string_pgm(23, 6, ON);
161
    } else {
163
    } else {
162
        write_ascii_string_pgm(23, 6, OFF);
164
        write_ascii_string_pgm(23, 6, OFF);
163
    }
165
    }
164
        if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
166
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
165
        write_ascii_string_pgm(23, 7, ON);
167
        write_ascii_string_pgm(23, 7, ON);
166
    } else {
168
    } else {
167
                write_ascii_string_pgm(23, 7, OFF);
169
        write_ascii_string_pgm(23, 7, OFF);
168
    }
170
    }
169
        if (COSD_FLAGS & COSD_FLAG_STATS) {
171
    if (COSD_FLAGS & COSD_FLAG_STATS) {
170
        write_ascii_string_pgm(23, 8, ON);
172
        write_ascii_string_pgm(23, 8, ON);
171
    } else {
173
    } else {
172
        write_ascii_string_pgm(23, 8, OFF);
174
        write_ascii_string_pgm(23, 8, OFF);
173
    }
175
    }
174
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
176
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
175
        write_ascii_string_pgm(23, 9, ON);
177
        write_ascii_string_pgm(23, 9, ON);
176
    } else {
178
    } else {
177
        write_ascii_string_pgm(23, 9, OFF);
179
        write_ascii_string_pgm(23, 9, OFF);
178
    }
180
    }
179
}
181
}
180
 
182
 
181
/**
183
/**
182
 * some sort of clicking response in the menu
184
 * some sort of clicking response in the menu
183
 */
185
 */
184
void config_menu_doclick(uint8_t chosen, char** menu) {
186
void config_menu_doclick(uint8_t chosen, char** menu) {
185
        write_ascii_string(4, chosen + 5, "DONE              ");
187
    write_ascii_string(4, chosen + 5, "DONE              ");
186
        _delay_ms(1000);
188
    _delay_ms(1000);
187
        write_ascii_string(4, chosen + 5, menu[chosen]);
189
    write_ascii_string(4, chosen + 5, menu[chosen]);
188
}
190
}
189
 
191
 
190
/**
192
/**
191
 * a simple config menu tryout
193
 * a simple config menu tryout
192
 */
194
 */
193
void config_menu(void) {
195
void config_menu(void) {
194
        // disable interrupts (makes the menu more smoothely)
196
    // disable interrupts (makes the menu more smoothely)
195
        cli();
197
    cli();
196
 
198
 
197
        // clear screen
199
    // clear screen
198
        clear();
200
    clear();
199
       
201
 
200
        char* menu[9] = {"Full HUD",
202
    char* menu[9] = {"Full HUD",
201
                                         "Art.Horizon in HUD",
203
        "Art.Horizon in HUD",
202
                                         "Big Vario bar",
204
        "Big Vario bar",
203
                                         "Statistics",
205
        "Statistics",
204
                                         "Warnings",    // TODO: do it!
206
        "Warnings", // TODO: do it!
205
                                         "Reset uptime",
207
        "Reset uptime",
206
                                         "Request OSD-data",
208
        "Request OSD-data",
207
                                         "Disable Debug-data",
209
        "Disable Debug-data",
208
                                         "EXIT"};
210
        "EXIT"};
209
 
211
 
210
        uint8_t inmenu = 1;
212
    uint8_t inmenu = 1;
211
        uint8_t chosen = 0;
213
    uint8_t chosen = 0;
212
        write_ascii_string(6,  2, "C-OSD Config Menu");
214
    write_ascii_string(6, 2, "C-OSD Config Menu");
213
 
215
 
214
        // wait a bit before doing stuff so user has chance to release button
216
    // wait a bit before doing stuff so user has chance to release button
215
        _delay_ms(250);
217
    _delay_ms(250);
216
 
218
 
217
        write_ascii_string(4,  5, menu[0]);
219
    write_ascii_string(4, 5, menu[0]);
218
        write_ascii_string(4,  6, menu[1]);
220
    write_ascii_string(4, 6, menu[1]);
219
        write_ascii_string(4,  7, menu[2]);
221
    write_ascii_string(4, 7, menu[2]);
220
        write_ascii_string(4,  8, menu[3]);
222
    write_ascii_string(4, 8, menu[3]);
221
        write_ascii_string(4,  9, menu[4]);
223
    write_ascii_string(4, 9, menu[4]);
222
        write_ascii_string(4, 10, menu[5]);
224
    write_ascii_string(4, 10, menu[5]);
223
        write_ascii_string(4, 11, menu[6]);
225
    write_ascii_string(4, 11, menu[6]);
224
        write_ascii_string(4, 12, menu[7]);
226
    write_ascii_string(4, 12, menu[7]);
225
        write_ascii_string(4, 13, menu[8]);
227
    write_ascii_string(4, 13, menu[8]);
226
 
228
 
227
        config_menu_drawings(chosen);
229
    config_menu_drawings(chosen);
228
 
230
 
229
        while (inmenu) {
231
    while (inmenu) {
230
                        if (s2_pressed()) {
232
        if (s2_pressed()) {
231
                                write_ascii_string(3,  chosen+5, " ");
233
            write_ascii_string(3, chosen + 5, " ");
232
                                chosen = (chosen + 1) % 9;
234
            chosen = (chosen + 1) % 9;
233
                                write_ascii_string(3,  chosen+5, ">");
235
            write_ascii_string(3, chosen + 5, ">");
234
                                _delay_ms(500);
236
            _delay_ms(500);
235
                        } else if (s1_pressed()) {
237
        } else if (s1_pressed()) {
236
                                switch (chosen) {
238
            switch (chosen) {
237
                                        case 0:         // full HUD
239
                case 0: // full HUD
238
                                                COSD_FLAGS ^= COSD_FLAG_HUD;
240
                    COSD_FLAGS ^= COSD_FLAG_HUD;
239
                                                config_menu_drawings(chosen);
241
                    config_menu_drawings(chosen);
240
                                                break;
242
                    break;
241
                                        case 1:         // art horizon
243
                case 1: // art horizon
242
                                                COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
244
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
243
                                                config_menu_drawings(chosen);
245
                    config_menu_drawings(chosen);
244
                                                break;
246
                    break;
245
                                        case 2:         // big vario
247
                case 2: // big vario
246
                                                COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
248
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
247
                                                config_menu_drawings(chosen);
249
                    config_menu_drawings(chosen);
248
                                                break;
250
                    break;
249
                                        case 3:         // statistics
251
                case 3: // statistics
250
                                                COSD_FLAGS ^= COSD_FLAG_STATS;
252
                    COSD_FLAGS ^= COSD_FLAG_STATS;
251
                                                config_menu_drawings(chosen);
253
                    config_menu_drawings(chosen);
252
                                                break;
254
                    break;
253
                                        case 4:         // warnings
255
                case 4: // warnings
254
                                                COSD_FLAGS ^= COSD_FLAG_WARNINGS;
256
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
255
                                                config_menu_drawings(chosen);
257
                    config_menu_drawings(chosen);
256
                                                break;
258
                    break;
257
                                        case 5:         // reset uptime
259
                case 5: // reset uptime
258
                                                uptime = 0;
260
                    uptime = 0;
259
                                                config_menu_doclick(chosen, menu);
261
                    config_menu_doclick(chosen, menu);
260
                                                break;
262
                    break;
261
                                        case 6:         // re-request OSD data
263
                case 6: // re-request OSD data
262
#if FCONLY
264
#if FCONLY
263
                                                // request data ever 100ms from FC;
265
                    // request data ever 100ms from FC;
264
                                        usart1_request_mk_data(0, 'd', 100);
266
                    usart1_request_mk_data(0, 'd', 100);
265
#else
267
#else
266
                                        // request OSD Data from NC every 100ms
268
                    // request OSD Data from NC every 100ms
267
                                        usart1_request_mk_data(1, 'o', 100);
269
                    usart1_request_mk_data(1, 'o', 100);
268
 
270
 
269
                                // and disable debug...
271
                    // and disable debug...
270
                                        usart1_request_mk_data(0, 'd', 0);
272
                    usart1_request_mk_data(0, 'd', 0);
271
#endif
273
#endif
272
                                                config_menu_doclick(chosen, menu);
274
                    config_menu_doclick(chosen, menu);
273
                                                break;
275
                    break;
274
                                        case 7:         // disable debug data
276
                case 7: // disable debug data
275
                                                // disable sending of debug data
277
                    // disable sending of debug data
276
                                                // may result in smoother ddata display
278
                    // may result in smoother ddata display
277
                                                usart1_request_mk_data(0, 'd', 0);
279
                    usart1_request_mk_data(0, 'd', 0);
278
                                                config_menu_doclick(chosen, menu);
280
                    config_menu_doclick(chosen, menu);
279
                                                break;
281
                    break;
280
                                        case 8:         // exit
282
                case 8: // exit
281
                                                inmenu = 0;
283
                    inmenu = 0;
282
                                                break;
284
                    break;
283
                                }
285
            }
284
                                _delay_ms(250);
286
            _delay_ms(250);
285
                        }
287
        }
286
        }
288
    }
287
 
289
 
288
        // clear screen up again
290
    // clear screen up again
289
        clear();
291
    clear();
290
 
292
 
-
 
293
    // update flags to paint display again if needed
-
 
294
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
-
 
295
 
-
 
296
    // enable interrupts again
-
 
297
    sei();
-
 
298
}
-
 
299
 
-
 
300
/**
-
 
301
 * auto config some stuff on startup, currently only battery cells
-
 
302
 * TODO: this is testing stuff, strings should go progmem and so on...
-
 
303
 */
-
 
304
void auto_config(uint8_t UBat, uint8_t* min_UBat) {
-
 
305
    clear();
-
 
306
    // fix for min_bat glitch caused by data only gathered during motors up
-
 
307
    *min_UBat = UBat;
-
 
308
    write_ascii_string(2, 2, "C-OSD Initialisation");
-
 
309
#if FCONLY
-
 
310
    write_ascii_string(2, 3, "FC only Mode");
-
 
311
#else
-
 
312
    write_ascii_string(2, 3, "NaviCtrl Mode");
-
 
313
#endif
-
 
314
    uint8_t cellnum = 0;
-
 
315
    if (CELL_NUM == -1) {
-
 
316
        write_ascii_string(2, 5, "Guessing Number of Cells");
-
 
317
        do {
-
 
318
            cellnum++;
-
 
319
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 10));
-
 
320
    } else {
-
 
321
        cellnum = CELL_NUM;
-
 
322
    }
-
 
323
    min_voltage = cellnum * CELL_VOLT_MIN;
-
 
324
    max_voltage = cellnum * CELL_VOLT_MAX;
-
 
325
    write_ascii_string(2, 7, "Number of Cells:");
-
 
326
    write_ndigit_number_u(21, 7, cellnum, 1, 0);
-
 
327
    write_ascii_string(2, 8, "Warn Voltage   :");
-
 
328
    write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
-
 
329
    write_ascii_string(2, 9, "Max Voltage    :");
291
        // update flags to paint display again if needed
330
    write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
292
        COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
331
    _delay_ms(200);
293
 
332
    clear();
294
        // enable interrupts again
333
    // update flags to paint display again because of clear
295
        sei();
334
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
296
}
335
}
297
 
336
 
298
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
337
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
-
 
338
 
299
/* ##########################################################################
339
/* ##########################################################################
300
 * MAIN
340
 * MAIN
301
 * ##########################################################################*/
341
 * ##########################################################################*/
302
int main(void) {
342
int main(void) {
303
    // set up FLAGS, compiler should flatten this one
343
    // set up FLAGS, compiler should flatten this one
304
    COSD_FLAGS = (NTSC << 0);
344
    COSD_FLAGS = (NTSC << 0);
305
    COSD_FLAGS |= (HUD << 1);
345
    COSD_FLAGS |= (HUD << 1);
306
    COSD_FLAGS |= (ARTHORIZON << 2);
346
    COSD_FLAGS |= (ARTHORIZON << 2);
307
        COSD_FLAGS |= (BIGVARIO << 3);
347
    COSD_FLAGS |= (BIGVARIO << 3);
308
        COSD_FLAGS |= (STATS << 4);
348
    COSD_FLAGS |= (STATS << 4);
309
    COSD_FLAGS |= (WARNINGS << 5);
349
    COSD_FLAGS |= (WARNINGS << 5);
310
 
350
 
311
    // set up Atmega162 Ports
351
    // set up Atmega162 Ports
312
    DDRA |= (1 << PA1); // PA1 output (/CS)
352
    DDRA |= (1 << PA1); // PA1 output (/CS)
313
    MAX_CS_HIGH
353
    MAX_CS_HIGH
314
    DDRA |= (1 << PA2); // PA2 output (SDIN)
354
    DDRA |= (1 << PA2); // PA2 output (SDIN)
315
    MAX_SDIN_LOW
355
    MAX_SDIN_LOW
316
    DDRA |= (1 << PA3); // PA3 output (SCLK)
356
    DDRA |= (1 << PA3); // PA3 output (SCLK)
317
    MAX_SCLK_LOW
357
    MAX_SCLK_LOW
318
    DDRA |= (1 << PA5); // PA5 output (RESET)
358
    DDRA |= (1 << PA5); // PA5 output (RESET)
319
    MAX_RESET_HIGH
359
    MAX_RESET_HIGH
320
 
360
 
321
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
361
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
322
    LED1_OFF
362
    LED1_OFF
323
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
363
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
324
    LED2_OFF
364
    LED2_OFF
325
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
365
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
326
    LED3_OFF
366
    LED3_OFF
327
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
367
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
328
    LED4_OFF
368
    LED4_OFF
329
 
369
 
330
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
370
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
331
    PORTC |= (1 << PC4); // pullup
371
    PORTC |= (1 << PC4); // pullup
332
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
372
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
333
    PORTC |= (1 << PC5); // pullup
373
    PORTC |= (1 << PC5); // pullup
334
 
374
 
335
    // set up top and bottom lines
375
    // set up top and bottom lines
336
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
376
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
337
        bottom_line = 12;
377
        bottom_line = 12;
338
    } else {
378
    } else {
339
        bottom_line = 14;
379
        bottom_line = 14;
340
    }
380
    }
341
 
381
 
342
    // reset the MAX7456 to be sure any undefined states do no harm
382
    // reset the MAX7456 to be sure any undefined states do no harm
343
    MAX_RESET_LOW
383
    MAX_RESET_LOW
344
    MAX_RESET_HIGH
384
    MAX_RESET_HIGH
345
 
385
 
346
    // give the FC/NC and the maxim time to come up
386
    // give the FC/NC and the maxim time to come up
347
    LED4_ON
387
    LED4_ON
348
    _delay_ms(2000);
388
    _delay_ms(2000);
349
 
389
 
350
    LED4_OFF
390
    LED4_OFF
351
 
391
 
352
 
392
 
353
    //Pushing NEW chars to the MAX7456
393
    //Pushing NEW chars to the MAX7456
354
#if (WRITECHARS != -1)
394
#if (WRITECHARS != -1)
355
    // DISABLE display (VM0)
395
    // DISABLE display (VM0)
356
    spi_send_byte(0x00, 0b00000000);
396
    spi_send_byte(0x00, 0b00000000);
357
#include "characters.c"
397
    #include "characters.c"
358
 
-
 
359
#endif 
398
#endif
360
 
399
 
361
    // Setup Video Mode
400
    // Setup Video Mode
362
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
401
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
363
        // NTSC + enable display immediately (VM0)
402
        // NTSC + enable display immediately (VM0)
364
        spi_send_byte(0x00, 0b00001000);
403
        spi_send_byte(0x00, 0b00001000);
365
    } else {
404
    } else {
366
        // PAL + enable display immediately (VM0)
405
        // PAL + enable display immediately (VM0)
367
        spi_send_byte(0x00, 0b01001000);
406
        spi_send_byte(0x00, 0b01001000);
368
    }
407
    }
369
 
408
 
370
    /*// clear all display-mem (DMM)
409
    /*// clear all display-mem (DMM)
371
    spi_send_byte(0x04, 0b00000100);
410
    spi_send_byte(0x04, 0b00000100);
372
 
411
 
373
    // clearing takes 12uS according to maxim so lets wait longer
412
    // clearing takes 12uS according to maxim so lets wait longer
374
    _delay_us(120);
413
    _delay_us(120);
375
 
414
 
376
    // 8bit mode
415
    // 8bit mode
377
    spi_send_byte(0x04, 0b01000000);*/
416
    spi_send_byte(0x04, 0b01000000);*/
378
 
417
 
379
    // clear display memory and set to 8bit mode
418
    // clear display memory and set to 8bit mode
380
    clear();
419
    clear();
381
 
420
 
382
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
421
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
383
    // init usart
422
    // init usart
384
    usart1_init();
423
    usart1_init();
385
 
424
 
386
    // set up timer
425
    // set up timer
387
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
426
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
388
    OCR0 = 15; // preload
427
    OCR0 = 15; // preload
389
    TIMSK |= (1 << TOIE0); // enable overflow timer0
428
    TIMSK |= (1 << TOIE0); // enable overflow timer0
390
 
429
 
391
    // enable interrupts
430
    // enable interrupts
392
    sei();
431
    sei();
393
#endif
432
#endif
394
 
433
 
395
    //write_ascii_string(2,  7, "         CaScAdE          ");
434
    //write_ascii_string(2,  7, "         CaScAdE          ");
396
    //write_ascii_string(2,  8, "is TESTING his open source");
435
    //write_ascii_string(2,  8, "is TESTING his open source");
397
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
436
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
398
 
437
 
399
    // we are ready
438
    // we are ready
400
    LED3_ON
439
    LED3_ON
401
 
440
 
402
#if ALLCHARSDEBUG | (WRITECHARS != -1)
441
#if ALLCHARSDEBUG | (WRITECHARS != -1)
403
        clear();
442
    clear();
404
    write_all_chars();
443
    write_all_chars();
405
#else
444
#else
406
        // clear serial screen
445
    // clear serial screen
407
        //usart1_puts("\x1B[2J\x1B[H");
446
    //usart1_puts("\x1B[2J\x1B[H");
408
        //usart1_puts("hello world!\r\n");
447
    //usart1_puts("hello world!\r\n");
409
 
-
 
410
#if FCONLY
448
#if FCONLY
411
        // request data ever 100ms from FC;
449
    // request data ever 100ms from FC;
412
        usart1_request_mk_data(0, 'd', 100);
450
    usart1_request_mk_data(0, 'd', 100);
413
#else
451
#else
414
        // request OSD Data from NC every 100ms
452
    // request OSD Data from NC every 100ms
415
        usart1_request_mk_data(1, 'o', 100);
453
    usart1_request_mk_data(1, 'o', 100);
416
 
454
 
417
    // and disable debug...
455
    // and disable debug...
418
        usart1_request_mk_data(0, 'd', 0);
456
    usart1_request_mk_data(0, 'd', 0);
419
#endif
457
#endif
420
 
458
 
421
    // stats for after flight
459
    // stats for after flight
422
    int16_t max_Altimeter = 0;
460
    int16_t max_Altimeter = 0;
423
    uint16_t max_GroundSpeed = 0;
461
    uint16_t max_GroundSpeed = 0;
424
    int16_t max_Distance = 0;
462
    int16_t max_Distance = 0;
425
    uint8_t min_UBat = 255;
463
    uint8_t min_UBat = 255;
426
    uint16_t max_FlyingTime = 0;
464
    uint16_t max_FlyingTime = 0;
427
 
465
 
428
    // flags from last round to check for changes
466
    // flags from last round to check for changes
429
    uint8_t old_MKFlags = 0;
467
    uint8_t old_MKFlags = 0;
430
 
468
 
431
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
469
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
432
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
470
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
433
 
471
 
434
    while (1) {
472
    while (1) {
435
        // write icons at init or after menu/mode-switch
473
        // write icons at init or after menu/mode-switch
436
        if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) {
474
        if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) {
437
#if FCONLY
475
#if FCONLY
438
                        // FC Mode ICONS
476
            // FC Mode ICONS
439
                        write_char_xy(10, top_line, 0xCA); // RC-transmitter
477
            write_char_xy(10, top_line, 0xCA); // RC-transmitter
440
                        write_char_xy(27, top_line, 0xCC); // small meters m height
478
            write_char_xy(27, top_line, 0xCC); // small meters m height
441
                        write_char_xy(7, bottom_line, 0x9E); // small v
479
            write_char_xy(7, bottom_line, 0x9E); // small v
442
#else
480
#else
443
                        // NAVI Mode ICONS
481
            // NAVI Mode ICONS
444
            write_char_xy(5, top_line, 0xCB); // km/h
482
            write_char_xy(5, top_line, 0xCB); // km/h
445
            write_char_xy(10, top_line, 0xCA); // RC-transmitter
483
            write_char_xy(10, top_line, 0xCA); // RC-transmitter
446
            write_char_xy(16, top_line, 0xD0); // degree symbol
484
            write_char_xy(16, top_line, 0xD0); // degree symbol
447
            write_char_xy(27, top_line, 0xCC); // small meters m height
485
            write_char_xy(27, top_line, 0xCC); // small meters m height
448
                        write_char_xy(20, top_line+1, 0xB0); // left circle
486
            write_char_xy(20, top_line + 1, 0xB0); // left circle
449
                        write_char_xy(22, top_line+1, 0xB2); // right circle
487
            write_char_xy(22, top_line + 1, 0xB2); // right circle
450
                        write_char_xy(27, top_line+1, 0xCC); // small meters m home
488
            write_char_xy(27, top_line + 1, 0xCC); // small meters m home
451
                        write_char_xy(7, bottom_line, 0x9E); // small v
489
            write_char_xy(7, bottom_line, 0x9E); // small v
452
            write_char_xy(14, bottom_line, 0xD1); // on clock
490
            write_char_xy(14, bottom_line, 0xD1); // on clock
453
            write_char_xy(21, bottom_line, 0xD2); // fly clock
491
            write_char_xy(21, bottom_line, 0xD2); // fly clock
454
            write_char_xy(26, bottom_line, 0xC8); // sat1
492
            write_char_xy(26, bottom_line, 0xC8); // sat1
455
            write_char_xy(27, bottom_line, 0xC9); // sat2
493
            write_char_xy(27, bottom_line, 0xC9); // sat2
456
            COSD_FLAGS |= COSD_ICONS_WRITTEN;
494
            COSD_FLAGS |= COSD_ICONS_WRITTEN;
457
#endif
495
#endif
458
        }
496
        }
459
        if (rxd_buffer_locked) {
497
        if (rxd_buffer_locked) {
460
            if (COSD_FLAGS & COSD_FLAG_HUD) {
498
            if (COSD_FLAGS & COSD_FLAG_HUD) {
461
#if FCONLY
499
#if FCONLY
462
                if (rxd_buffer[2] == 'D') { // FC Data
500
                if (rxd_buffer[2] == 'D') { // FC Data
463
                    Decode64();
501
                    Decode64();
464
                    debugData = *((DebugOut_t*) pRxData);
502
                    debugData = *((DebugOut_t*) pRxData);
465
                                        write_ascii_string(2,  2, "FCONLY MODE");
-
 
-
 
503
 
-
 
504
                    // init on first data retrival, distinguished by last battery :)
-
 
505
                    if (last_UBat == 255) {
-
 
506
                        auto_config(debugData.Analog[9], &min_UBat);
-
 
507
                    }
466
 
508
 
467
                                        write_ndigit_number_u(7, top_line, debugData.Analog[10], 100, 0);
509
                    write_ndigit_number_u(7, top_line, debugData.Analog[10], 100, 0);
468
                                        if (debugData.Analog[10] <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) {
510
                    if (debugData.Analog[10] <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) {
469
                                            for (uint8_t x = 0; x < 4; x++)
511
                        for (uint8_t x = 0; x < 4; x++)
470
                                                write_char_att_xy(7 + x, top_line, BLINK);
512
                            write_char_att_xy(7 + x, top_line, BLINK);
471
                                        } else if (debugData.Analog[10] > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) {
513
                    } else if (debugData.Analog[10] > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) {
472
                                            for (uint8_t x = 0; x < 4; x++)
514
                        for (uint8_t x = 0; x < 4; x++)
473
                                                write_char_att_xy(7 + x, top_line, 0);
515
                            write_char_att_xy(7 + x, top_line, 0);
474
                                        }
516
                    }
475
                                        last_RC_Quality = debugData.Analog[10];
517
                    last_RC_Quality = debugData.Analog[10];
476
 
518
 
477
 
519
 
478
                                if (debugData.Analog[5] > 300 || debugData.Analog[5] < -300) {
520
                    if (debugData.Analog[5] > 300 || debugData.Analog[5] < -300) {
479
                                    // above 10m only write full meters
521
                        // above 10m only write full meters
480
                                    write_ndigit_number_s(23, top_line, debugData.Analog[5] / 30, 1000, 0);
522
                        write_ndigit_number_s(23, top_line, debugData.Analog[5] / 30, 1000, 0);
481
                                } else {
523
                    } else {
482
                                    // up to 10m write meters.dm
524
                        // up to 10m write meters.dm
-
 
525
                        write_ndigit_number_s_10th(23, top_line, debugData.Analog[5] / 3, 100, 0);
483
                                                write_ndigit_number_s_10th(23, top_line, debugData.Analog[5] / 3, 100, 0);
526
                    }
-
 
527
 
-
 
528
                    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
-
 
529
                        draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll);
484
                                        }
530
                    }
485
 
531
 
486
                                        draw_battery(2, bottom_line, min_voltage, debugData.Analog[9], max_voltage);
532
                    draw_battery(2, bottom_line, min_voltage, debugData.Analog[9], max_voltage);
487
                                        write_ndigit_number_u_10th(3, bottom_line, debugData.Analog[9], 100, 0);
533
                    write_ndigit_number_u_10th(3, bottom_line, debugData.Analog[9], 100, 0);
488
                    if (debugData.Analog[9] <= min_voltage && last_UBat > min_voltage) {
534
                    if (debugData.Analog[9] <= min_voltage && last_UBat > min_voltage) {
489
                        for (uint8_t x = 2; x < 8; x++)
535
                        for (uint8_t x = 2; x < 8; x++)
490
                            write_char_att_xy(x, bottom_line, BLINK);
536
                            write_char_att_xy(x, bottom_line, BLINK);
491
                    } else if (debugData.Analog[9] > min_voltage && last_UBat < min_voltage) {
537
                    } else if (debugData.Analog[9] > min_voltage && last_UBat < min_voltage) {
492
                        for (uint8_t x = 2; x < 8; x++)
538
                        for (uint8_t x = 2; x < 8; x++)
493
                            write_char_att_xy(x, bottom_line, 0);
539
                            write_char_att_xy(x, bottom_line, 0);
494
                    }
540
                    }
495
                                        // remember last values
541
                    // remember last values
496
                                        last_UBat = debugData.Analog[9];
542
                    last_UBat = debugData.Analog[9];
497
 
543
 
498
                    /*
544
                    /*
499
                                        debugData.Analog[0]); // AngleNick
545
                    debugData.Analog[0]); // AngleNick
500
                    debugData.Analog[1]); // AngleRoll
546
                    debugData.Analog[1]); // AngleRoll
501
                                        debugData.Analog[5]); // Height
547
                    debugData.Analog[5]); // Height
502
                    debugData.Analog[9]); // Voltage
548
                    debugData.Analog[9]); // Voltage
503
                    debugData.Analog[10]);// RC Signal
549
                    debugData.Analog[10]);// RC Signal
504
                                        debugData.Analog[11]);// Gyro compass
550
                    debugData.Analog[11]);// Gyro compass
505
                                        */
551
                     */
506
                                        seconds_since_last_data = 0;
552
                    seconds_since_last_data = 0;
507
                }
553
                }
508
#else
554
#else
509
                                if (rxd_buffer[2] == 'O') { // NC OSD Data
555
                if (rxd_buffer[2] == 'O') { // NC OSD Data
510
                    Decode64();
556
                    Decode64();
511
                    naviData = *((NaviData_t*) pRxData);
557
                    naviData = *((NaviData_t*) pRxData);
512
                                       
558
 
513
                                        // init on first data retrival, distinguished by last battery :)
-
 
514
                                        // TODO: this is testing stuff, strings should go progmem and so on...
559
                    // init on first data retrival, distinguished by last battery :)
515
                                        if (last_UBat == 255) {
-
 
516
                                                clear();
560
                    if (last_UBat == 255) {
517
                                                // fix for min_bat glitch caused by data only gathered during motors up
-
 
518
                                                min_UBat = naviData.UBat;
-
 
519
                                                write_ascii_string(2,  2, "C-OSD Initialisation");
-
 
520
                                                uint8_t cellnum = 0;
561
                        auto_config(naviData.UBat, &min_UBat);
521
                                                if (CELL_NUM == -1) {
-
 
522
                                                        write_ascii_string(2,  5, "Guessing Number of Cells");
-
 
523
                                                        do {
-
 
524
                                                                cellnum++;
-
 
525
                                                        } while (naviData.UBat > ((cellnum * CELL_VOLT_MAX) + 10));
-
 
526
                                                } else {
-
 
527
                                                        cellnum = CELL_NUM;
-
 
528
                                                }
-
 
529
                                                min_voltage = cellnum * CELL_VOLT_MIN;
-
 
530
                                                max_voltage = cellnum * CELL_VOLT_MAX;
-
 
531
                                                write_ascii_string(2,  7, "Number of Cells:");
-
 
532
                                                write_ndigit_number_u(21, 7, cellnum, 1, 0);
-
 
533
                                                write_ascii_string(2,  8, "Warn Voltage   :");
-
 
534
                                                write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
-
 
535
                                                write_ascii_string(2,  9, "Max Voltage    :");
-
 
536
                                                write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
-
 
537
                                                _delay_ms(200);
-
 
538
                                                clear();
-
 
539
                                                // update flags to paint display again because of clear
-
 
540
                                                COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
-
 
541
                                        }
562
                    }
542
 
563
 
543
                    // first line
564
                    // first line
544
                    write_ndigit_number_u(2, top_line, (uint16_t) (((uint32_t) naviData.GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0);
565
                    write_ndigit_number_u(2, top_line, (uint16_t) (((uint32_t) naviData.GroundSpeed * (uint32_t) 9) / (uint32_t) 250), 100, 0);
545
 
566
 
546
                    write_ndigit_number_u(7, top_line, naviData.RC_Quality, 100, 0);
567
                    write_ndigit_number_u(7, top_line, naviData.RC_Quality, 100, 0);
547
                    if (naviData.RC_Quality <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) {
568
                    if (naviData.RC_Quality <= RCLVL_WRN && last_RC_Quality > RCLVL_WRN) {
548
                        for (uint8_t x = 0; x < 4; x++)
569
                        for (uint8_t x = 0; x < 4; x++)
549
                            write_char_att_xy(7 + x, top_line, BLINK);
570
                            write_char_att_xy(7 + x, top_line, BLINK);
550
                    } else if (naviData.RC_Quality > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) {
571
                    } else if (naviData.RC_Quality > RCLVL_WRN && last_RC_Quality <= RCLVL_WRN) {
551
                        for (uint8_t x = 0; x < 4; x++)
572
                        for (uint8_t x = 0; x < 4; x++)
552
                            write_char_att_xy(7 + x, top_line, 0);
573
                            write_char_att_xy(7 + x, top_line, 0);
553
                    }
574
                    }
554
                                        last_RC_Quality = naviData.RC_Quality;
575
                    last_RC_Quality = naviData.RC_Quality;
555
                                       
576
 
556
                                        if (naviData.NCFlags & NC_FLAG_NOSERIALLINK) {
577
                    if (naviData.NCFlags & NC_FLAG_NOSERIALLINK) {
557
                                                write_char_xy(11, top_line, 0); // clear
578
                        write_char_xy(11, top_line, 0); // clear
558
                                        } else {
579
                    } else {
559
                                                write_char_xy(11, top_line, 0xC6); // PC icon
580
                        write_char_xy(11, top_line, 0xC6); // PC icon
560
                                        }
581
                    }
561
 
582
 
562
                    write_ndigit_number_u(13, top_line, naviData.CompassHeading, 100, 0);
583
                    write_ndigit_number_u(13, top_line, naviData.CompassHeading, 100, 0);
563
 
584
 
564
                    write_ascii_string(17, top_line, directions[heading_conv(naviData.CompassHeading)]);
585
                    write_ascii_string(17, top_line, directions[heading_conv(naviData.CompassHeading)]);
565
                                       
586
 
566
                                        draw_variometer(21, top_line, naviData.Variometer);
587
                    draw_variometer(21, top_line, naviData.Variometer);
567
 
588
 
568
                    //note:lephisto:according to several sources it's /30
589
                    //note:lephisto:according to several sources it's /30
569
                    if (naviData.Altimeter > 300 || naviData.Altimeter < -300) {
590
                    if (naviData.Altimeter > 300 || naviData.Altimeter < -300) {
570
                        // above 10m only write full meters
591
                        // above 10m only write full meters
571
                        write_ndigit_number_s(23, top_line, naviData.Altimeter / 30, 1000, 0);
592
                        write_ndigit_number_s(23, top_line, naviData.Altimeter / 30, 1000, 0);
572
                    } else {
593
                    } else {
573
                        // up to 10m write meters.dm
594
                        // up to 10m write meters.dm
574
                        //write_number_u_10th(21, top_line, naviData.Altimeter / 3);
595
                        //write_number_u_10th(21, top_line, naviData.Altimeter / 3);
575
                                                write_ndigit_number_s_10th(23, top_line, naviData.Altimeter / 3, 100, 0);
596
                        write_ndigit_number_s_10th(23, top_line, naviData.Altimeter / 3, 100, 0);
576
                    }
597
                    }
577
 
598
 
578
                    // seccond line
599
                    // seccond line
579
                    draw_compass(11, top_line + 1, naviData.CompassHeading);
600
                    draw_compass(11, top_line + 1, naviData.CompassHeading);
580
 
601
 
581
                    // TODO: verify correctness
602
                    // TODO: verify correctness
582
                    uint16_t heading_home = (naviData.HomePositionDeviation.Bearing + 360 - naviData.CompassHeading) % 360;
603
                    uint16_t heading_home = (naviData.HomePositionDeviation.Bearing + 360 - naviData.CompassHeading) % 360;
583
                    //write_char_xy(21, top_line + 1, arrowdir[heading_conv(heading_home)]);
604
                    //write_char_xy(21, top_line + 1, arrowdir[heading_conv(heading_home)]);
584
                                        // finer resolution, 0xa0 is first character and we add the index 0 <= index < 16
605
                    // finer resolution, 0xa0 is first character and we add the index 0 <= index < 16
585
                                        write_char_xy(21, top_line + 1, 0xa0 + heading_fine_conv(heading_home));
606
                    write_char_xy(21, top_line + 1, 0xa0 + heading_fine_conv(heading_home));
586
 
607
 
587
                    write_ndigit_number_u(24, top_line + 1, naviData.HomePositionDeviation.Distance / 10, 100, 0);
608
                    write_ndigit_number_u(24, top_line + 1, naviData.HomePositionDeviation.Distance / 10, 100, 0);
588
 
609
 
589
                    // center
610
                    // center
590
                    if (naviData.MKFlags & FLAG_MOTOR_RUN) { // should be engines running
611
                    if (naviData.MKFlags & FLAG_MOTOR_RUN) { // should be engines running
591
                        if (!(old_MKFlags & FLAG_MOTOR_RUN)) { // motors just started, clear middle
612
                        if (!(old_MKFlags & FLAG_MOTOR_RUN)) { // motors just started, clear middle
592
                            clear();
613
                            clear();
593
                            // update flags to paint display again if needed
614
                            // update flags to paint display again if needed
594
                            COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
615
                            COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
595
                        }
616
                        }
596
                        if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
617
                        if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
597
                            draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll);
618
                            draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll);
598
                        }
619
                        }
599
                                                // motors are on, assume we were/are flying
620
                        // motors are on, assume we were/are flying
600
                                                COSD_FLAGS |= COSD_WASFLYING;
621
                        COSD_FLAGS |= COSD_WASFLYING;
601
                    } else {
622
                    } else {
602
                                                // stats
623
                        // stats
603
                                                if ((COSD_FLAGS & COSD_WASFLYING) && (COSD_FLAGS & COSD_FLAG_STATS)) {
624
                        if ((COSD_FLAGS & COSD_WASFLYING) && (COSD_FLAGS & COSD_FLAG_STATS)) {
604
                                                        write_ascii_string_pgm(2, 4, stats_item_pointers[0]); // max Altitude
625
                            write_ascii_string_pgm(2, 4, stats_item_pointers[0]); // max Altitude
605
                                write_ndigit_number_s(18, 4, max_Altimeter / 30, 1000, 0);
626
                            write_ndigit_number_s(18, 4, max_Altimeter / 30, 1000, 0);
606
                                write_char_xy(22, 4, 204); // small meters m
627
                            write_char_xy(22, 4, 204); // small meters m
607
                                                        write_ascii_string_pgm(2, 5, stats_item_pointers[1]); // max Speed
628
                            write_ascii_string_pgm(2, 5, stats_item_pointers[1]); // max Speed
608
                                                        write_ndigit_number_u(19, 5, (uint16_t) (((uint32_t) max_GroundSpeed * (uint32_t)9) / (uint32_t)250), 100, 0);
629
                            write_ndigit_number_u(19, 5, (uint16_t) (((uint32_t) max_GroundSpeed * (uint32_t) 9) / (uint32_t) 250), 100, 0);
609
                                write_char_xy(22, 5, 203); // km/h
630
                            write_char_xy(22, 5, 203); // km/h
610
                                                        write_ascii_string_pgm(2, 6, stats_item_pointers[2]); // max Distance
631
                            write_ascii_string_pgm(2, 6, stats_item_pointers[2]); // max Distance
611
                                write_ndigit_number_u(19, 6, max_Distance / 10, 100, 0);
632
                            write_ndigit_number_u(19, 6, max_Distance / 10, 100, 0);
612
                                write_char_xy(22, 6, 204); // small meters m
633
                            write_char_xy(22, 6, 204); // small meters m
613
                                                        write_ascii_string_pgm(2, 7, stats_item_pointers[3]); // min voltage
634
                            write_ascii_string_pgm(2, 7, stats_item_pointers[3]); // min voltage
614
                                                        write_ndigit_number_u_10th(18, 7, min_UBat, 100, 0);
635
                            write_ndigit_number_u_10th(18, 7, min_UBat, 100, 0);
615
                                                        write_char_xy(22, 7, 0x9E); // small v
636
                            write_char_xy(22, 7, 0x9E); // small v
616
                                                        write_ascii_string_pgm(2, 8, stats_item_pointers[4]); // max time
637
                            write_ascii_string_pgm(2, 8, stats_item_pointers[4]); // max time
617
                                write_time(16, 8, max_FlyingTime);
638
                            write_time(16, 8, max_FlyingTime);
618
                                write_char_xy(22, 8, 210); // fly clock
639
                            write_char_xy(22, 8, 210); // fly clock
619
                                                        write_ascii_string_pgm(2, 9, stats_item_pointers[5]); // longitude
640
                            write_ascii_string_pgm(2, 9, stats_item_pointers[5]); // longitude
620
                                                        write_gps_pos(15, 9, naviData.CurrentPosition.Longitude);
641
                            write_gps_pos(15, 9, naviData.CurrentPosition.Longitude);
621
                                                        write_ascii_string_pgm(2, 10, stats_item_pointers[6]); // latitude
642
                            write_ascii_string_pgm(2, 10, stats_item_pointers[6]); // latitude
622
                                                        write_gps_pos(15, 10, naviData.CurrentPosition.Latitude);
643
                            write_gps_pos(15, 10, naviData.CurrentPosition.Latitude);
623
                                                } else if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { // if no stats there is space horizon
644
                        } else if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { // if no stats there is space horizon
624
                            draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll);
645
                            draw_artificial_horizon(top_line + 2, bottom_line - 1, naviData.AngleNick, naviData.AngleRoll);
625
                        }
646
                        }
626
                    }
647
                    }
627
                                        if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
648
                    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
628
                                                draw_big_variometer(27, 8, naviData.Variometer);
649
                        draw_big_variometer(27, 8, naviData.Variometer);
629
                                        }
650
                    }
630
 
651
 
631
                    // bottom line
652
                    // bottom line
632
                                        draw_battery(2, bottom_line, min_voltage, naviData.UBat, max_voltage);
653
                    draw_battery(2, bottom_line, min_voltage, naviData.UBat, max_voltage);
633
                                        write_ndigit_number_u_10th(3, bottom_line, naviData.UBat, 100, 0);
654
                    write_ndigit_number_u_10th(3, bottom_line, naviData.UBat, 100, 0);
634
                    if (naviData.UBat <= min_voltage && last_UBat > min_voltage) {
655
                    if (naviData.UBat <= min_voltage && last_UBat > min_voltage) {
635
                        for (uint8_t x = 2; x < 8; x++)
656
                        for (uint8_t x = 2; x < 8; x++)
636
                            write_char_att_xy(x, bottom_line, BLINK);
657
                            write_char_att_xy(x, bottom_line, BLINK);
637
                    } else if (naviData.UBat > min_voltage && last_UBat < min_voltage) {
658
                    } else if (naviData.UBat > min_voltage && last_UBat < min_voltage) {
638
                        for (uint8_t x = 2; x < 8; x++)
659
                        for (uint8_t x = 2; x < 8; x++)
639
                            write_char_att_xy(x, bottom_line, 0);
660
                            write_char_att_xy(x, bottom_line, 0);
640
                    }
661
                    }
641
                                        // remember last values
662
                    // remember last values
642
                                        last_UBat = naviData.UBat;
663
                    last_UBat = naviData.UBat;
643
 
664
 
644
                    write_time(8, bottom_line, uptime);
665
                    write_time(8, bottom_line, uptime);
645
                    write_time(15, bottom_line, naviData.FlyingTime);
666
                    write_time(15, bottom_line, naviData.FlyingTime);
646
 
667
 
647
                    write_ndigit_number_u(24, bottom_line, naviData.SatsInUse, 10, 0);
668
                    write_ndigit_number_u(24, bottom_line, naviData.SatsInUse, 10, 0);
648
 
669
 
649
                                        if (naviData.NCFlags & NC_FLAG_MANUAL_CONTROL) {
670
                    if (naviData.NCFlags & NC_FLAG_MANUAL_CONTROL) {
650
                        write_char_xy(23, bottom_line, 0xB3); // rc transmitter
671
                        write_char_xy(23, bottom_line, 0xB3); // rc transmitter
651
                    } else {
672
                    } else {
652
                        write_char_xy(23, bottom_line, 0); // clear
673
                        write_char_xy(23, bottom_line, 0); // clear
653
                    }
674
                    }
654
 
675
 
655
                    if (naviData.NCFlags & NC_FLAG_CH) {
676
                    if (naviData.NCFlags & NC_FLAG_CH) {
656
                        write_char_xy(27, bottom_line, 231); // gps ch
677
                        write_char_xy(27, bottom_line, 231); // gps ch
657
                    } else if (naviData.NCFlags & NC_FLAG_PH) {
678
                    } else if (naviData.NCFlags & NC_FLAG_PH) {
658
                        write_char_xy(27, bottom_line, 230); // gps ph
679
                        write_char_xy(27, bottom_line, 230); // gps ph
659
                    } else { // (naviData.NCFlags & NC_FLAG_FREE)
680
                    } else { // (naviData.NCFlags & NC_FLAG_FREE)
660
                        write_char_xy(27, bottom_line, 201); // sat2 (free)
681
                        write_char_xy(27, bottom_line, 201); // sat2 (free)
661
                    }
682
                    }
662
 
683
 
663
                    //write_number_s(8, 5, RxDataLen);
684
                    //write_number_s(8, 5, RxDataLen);
664
                    //write_number_s(16, 5, setsReceived++);
685
                    //write_number_s(16, 5, setsReceived++);
665
 
686
 
666
                    // remember statistics (only when engines running)
687
                    // remember statistics (only when engines running)
667
                                        if (naviData.MKFlags & FLAG_MOTOR_RUN) {
688
                    if (naviData.MKFlags & FLAG_MOTOR_RUN) {
668
                                                if (naviData.Altimeter > max_Altimeter) max_Altimeter = naviData.Altimeter;
689
                        if (naviData.Altimeter > max_Altimeter) max_Altimeter = naviData.Altimeter;
669
                                                if (naviData.GroundSpeed > max_GroundSpeed) max_GroundSpeed = naviData.GroundSpeed;
690
                        if (naviData.GroundSpeed > max_GroundSpeed) max_GroundSpeed = naviData.GroundSpeed;
670
                                                if (naviData.HomePositionDeviation.Distance > max_Distance) {
691
                        if (naviData.HomePositionDeviation.Distance > max_Distance) {
671
                                                max_Distance = naviData.HomePositionDeviation.Distance;
692
                            max_Distance = naviData.HomePositionDeviation.Distance;
672
                                                }
693
                        }
673
                                                if (naviData.UBat < min_UBat) min_UBat = naviData.UBat;
694
                        if (naviData.UBat < min_UBat) min_UBat = naviData.UBat;
674
                                                if (naviData.FlyingTime > max_FlyingTime) max_FlyingTime = naviData.FlyingTime;
695
                        if (naviData.FlyingTime > max_FlyingTime) max_FlyingTime = naviData.FlyingTime;
675
                                        }
696
                    }
676
 
697
 
677
                    old_MKFlags = naviData.MKFlags;
698
                    old_MKFlags = naviData.MKFlags;
678
                                        seconds_since_last_data = 0;
699
                    seconds_since_last_data = 0;
679
                }
700
                }
680
#endif
701
#endif
681
            }
702
            }
682
            rxd_buffer_locked = 0;
703
            rxd_buffer_locked = 0;
683
        }
704
        }
684
        // handle keypress
705
        // handle keypress
685
        if (s1_pressed()) {
706
        if (s1_pressed()) {
686
            config_menu();
707
            config_menu();
687
        }
708
        }
688
        if (seconds_since_last_data > 2) {
709
        if (seconds_since_last_data > 2) {
689
#if FCONLY
710
#if FCONLY
690
                        // request data ever 100ms from FC;
711
            // request data ever 100ms from FC;
691
                        usart1_request_mk_data(0, 'd', 100);
712
            usart1_request_mk_data(0, 'd', 100);
692
#else
713
#else
693
                        // request OSD Data from NC every 100ms
714
            // request OSD Data from NC every 100ms
694
                        usart1_request_mk_data(1, 'o', 100);
715
            usart1_request_mk_data(1, 'o', 100);
695
 
716
 
696
                // and disable debug...
717
            // and disable debug...
697
                        usart1_request_mk_data(0, 'd', 0);
718
            usart1_request_mk_data(0, 'd', 0);
698
#endif
719
#endif
699
        }
720
        }
700
    }
721
    }
701
#endif
722
#endif
702
    return 0;
723
    return 0;
703
}
724
}
704
 
725