Subversion Repositories Projects

Rev

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

Rev 1197 Rev 1281
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009-2011 by Claas Anders "CaScAdE" Rathje               *
2
 *   Copyright (C) 2009-2011 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
#include "main.h"
21
#include "main.h"
22
#include <avr/io.h>
22
#include <avr/io.h>
23
#include <avr/eeprom.h>
23
#include <avr/eeprom.h>
24
#include <avr/pgmspace.h>
24
#include <avr/pgmspace.h>
25
#include <avr/interrupt.h>
25
#include <avr/interrupt.h>
26
#include <util/delay.h>
26
#include <util/delay.h>
27
#include "max7456_software_spi.h"
27
#include "max7456_software_spi.h"
28
#include "config.h"
28
#include "config.h"
29
#include "buttons.h"
29
#include "buttons.h"
30
#include "usart1.h"
30
#include "usart1.h"
31
#include "osd_helpers.h"
31
#include "osd_helpers.h"
32
 
32
 
33
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
33
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
34
 
34
 
35
// EEPROM bytes
35
// EEPROM bytes
36
uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1;
36
uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1;
37
uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2;
37
uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2;
38
uint8_t EEMEM ee_COSD_FLAGS_MODES = 0;
38
uint8_t EEMEM ee_COSD_FLAGS_MODES = 0;
39
uint8_t EEMEM ee_COSD_FLAGS_CONFIG = 0;
39
uint8_t EEMEM ee_COSD_FLAGS_CONFIG = 0;
40
uint8_t EEMEM ee_COSD_DISPLAYMODE = 0;
40
uint8_t EEMEM ee_COSD_DISPLAYMODE = 0;
41
uint8_t EEMEM ee_COSD_SCOPE [12] = {
41
uint8_t EEMEM ee_COSD_SCOPE [12] = {
42
    5, 5, 0,
42
    5, 5, 0,
43
    25, 5, 0,
43
    25, 5, 0,
44
    5, 10, 0,
44
    5, 10, 0,
45
    25, 10, 0
45
    25, 10, 0
46
};
46
};
47
 
47
 
48
// video modes
48
// video modes
49
const char VM_PAL[] PROGMEM = "PAL ";
49
const char VM_PAL[] PROGMEM = "PAL ";
50
const char VM_NTSC[] PROGMEM = "NTSC";
50
const char VM_NTSC[] PROGMEM = "NTSC";
51
 
51
 
52
const displaymode_t * mode;
52
const displaymode_t * mode;
53
 
-
 
54
 
53
 
55
/**
54
/**
56
 * read data saved in eeprom, print out message if <verbose> is set
55
 * read data saved in eeprom, print out message if <verbose> is set
57
 */
56
 */
58
void get_eeprom(uint8_t verbose) {
57
void get_eeprom(uint8_t verbose) {
59
    if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) {
58
    if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) {
60
        if (verbose) write_ascii_string_pgm(2, 2, PSTR("Loading from EEPROM")); // Loading data
59
        if (verbose) write_ascii_string_pgm(2, 6, PSTR("Loading from EEPROM")); // Loading data
61
        COSD_FLAGS_MODES = eeprom_read_byte(&ee_COSD_FLAGS_MODES);
60
        COSD_FLAGS_MODES = eeprom_read_byte(&ee_COSD_FLAGS_MODES);
62
        COSD_FLAGS_CONFIG = eeprom_read_byte(&ee_COSD_FLAGS_CONFIG);
61
        COSD_FLAGS_CONFIG = eeprom_read_byte(&ee_COSD_FLAGS_CONFIG);
63
        COSD_DISPLAYMODE = eeprom_read_byte(&ee_COSD_DISPLAYMODE);
62
        COSD_DISPLAYMODE = eeprom_read_byte(&ee_COSD_DISPLAYMODE);
64
        //if (verbose) write_ndigit_number_u(23, 11, COSD_DISPLAYMODE, 2, 0);
63
        //if (verbose) write_ndigit_number_u(23, 11, COSD_DISPLAYMODE, 2, 0);
65
        for (int i = 0; i < 12; i++) {
64
        for (int i = 0; i < 12; i++) {
66
            scope[i] = eeprom_read_byte(&ee_COSD_SCOPE[i]);
65
            scope[i] = eeprom_read_byte(&ee_COSD_SCOPE[i]);
67
        }
66
        }
68
    } else {
67
    } else {
69
        if (verbose) write_ascii_string_pgm(2, 2, PSTR("No Data in EEPROM")); // Loading data
68
        if (verbose) write_ascii_string_pgm(2, 6, PSTR("No Data in EEPROM")); // Loading data
70
    }
69
    }
71
}
70
}
72
 
71
 
73
/**
72
/**
74
 * save data to eeprom
73
 * save data to eeprom
75
 */
74
 */
76
void save_eeprom() {
75
void save_eeprom() {
77
    eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1);
76
    eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1);
78
    eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2);
77
    eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2);
79
    eeprom_write_byte(&ee_COSD_FLAGS_MODES, COSD_FLAGS_MODES);
78
    eeprom_write_byte(&ee_COSD_FLAGS_MODES, COSD_FLAGS_MODES);
80
    eeprom_write_byte(&ee_COSD_FLAGS_CONFIG, COSD_FLAGS_CONFIG);
79
    eeprom_write_byte(&ee_COSD_FLAGS_CONFIG, COSD_FLAGS_CONFIG);
81
    eeprom_write_byte(&ee_COSD_DISPLAYMODE, COSD_DISPLAYMODE);
80
    eeprom_write_byte(&ee_COSD_DISPLAYMODE, COSD_DISPLAYMODE);
82
    for (int i = 0; i < 12; i++) {
81
    for (int i = 0; i < 12; i++) {
83
        eeprom_write_byte(&ee_COSD_SCOPE[i], scope[i]);
82
        eeprom_write_byte(&ee_COSD_SCOPE[i], scope[i]);
84
    }
83
    }
85
}
84
}
86
 
85
 
87
/**
86
/**
88
 * auto config some stuff on startup, currently only battery cells
87
 * auto config some stuff on startup, currently only battery cells
89
 */
88
 */
90
void init_cosd(uint8_t UBat) {
89
void init_cosd(uint8_t UBat) {
91
    clear();
90
    clear();
-
 
91
 
92
    write_ascii_string_pgm(2, 1, PSTR("C-OSD Init"));
92
    write_ascii_string_pgm(2, 1, PSTR("C-OSD"));
-
 
93
    write_ascii_string_pgm(3, 2, PSTR("boot"));
-
 
94
    draw_logo(11, 1);
-
 
95
 
93
    write_ascii_string_pgm(2, 4, PSTR(BUILDDATE));
96
    write_ascii_string_pgm(2, 5, PSTR(BUILDDATE));
-
 
97
 
-
 
98
    get_eeprom(1);
-
 
99
 
-
 
100
    //write_ascii_string_pgm(23, 2, vm[COSD_FLAGS & COSD_FLAG_NTSC]);
-
 
101
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
-
 
102
        write_ascii_string_pgm(23, 1, VM_NTSC);
-
 
103
    } else {
-
 
104
        write_ascii_string_pgm(23, 1, VM_PAL);
-
 
105
    }
-
 
106
 
94
 
107
 
95
#if FCONLY
108
#if FCONLY
96
    write_ascii_string_pgm(2, 3, PSTR("FC only Mode"));
109
    write_ascii_string_pgm(2, 4, PSTR("FC only Mode"));
97
#else
110
#else
98
    write_ascii_string_pgm(2, 3, PSTR("NaviCtrl Mode"));
111
    write_ascii_string_pgm(2, 4, PSTR("NaviCtrl Mode"));
99
 
112
 
100
    usart1_EnableTXD();
113
    usart1_EnableTXD();
101
    usart1_puts_pgm(PSTR(REQUEST_UART_TO_FC));
114
    usart1_puts_pgm(PSTR(REQUEST_UART_TO_FC));
102
    usart1_DisableTXD();
115
    usart1_DisableTXD();
103
#endif
116
#endif
104
 
117
 
105
 
118
 
106
    usart1_request_blocking('Q', PSTR(REQUEST_CURRENT_SETTING));
119
    usart1_request_blocking('Q', PSTR(REQUEST_CURRENT_SETTING));
107
 
120
 
108
    //write_ascii_char(4 + 12 * 30, rxd_buffer[2]);
121
    //write_ascii_char(4 + 12 * 30, rxd_buffer[2]);
109
 
122
 
110
    paramset_serial setting;
123
    paramset_serial setting;
111
    setting = *((paramset_serial*)pRxData);
124
    setting = *((paramset_serial*)pRxData);
112
 
125
 
113
    write_ascii_string_pgm(2, 6, PSTR("Setting  :"));
126
    write_ascii_string_pgm(2, 7, PSTR("Setting  :"));
114
    write_ndigit_number_u(10, 6, setting.SettingsIndex, 1, 1);
127
    write_ndigit_number_u(10, 7, setting.SettingsIndex, 1, 1);
115
    write_ascii_string_len(13, 6, setting.param.Name, 12);
128
    write_ascii_string_len(13, 7, setting.param.Name, 12);
116
 
129
 
117
    uint8_t cells = 0;
130
    uint8_t cells = 0;
118
    write_ascii_string_pgm(2, 8, PSTR("Battery:")); // Guessing Number of Cells
131
    write_ascii_string_pgm(2, 8, PSTR("Battery:")); // Guessing Number of Cells
119
    if (CELL_NUM == -1) {
132
    if (CELL_NUM == -1) {
120
#define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO
133
#define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO
121
 
134
 
122
        if (setting.param.UnterspannungsWarnung < 50) {
135
        if (setting.param.UnterspannungsWarnung < 50) {
123
            // up to 6s LiPo, less than 2s is technical impossible
136
            // up to 6s LiPo, less than 2s is technical impossible
124
            for (cells = 2; cells < 7; cells++) {
137
            for (cells = 2; cells < 7; cells++) {
125
                if (UBat < cells * MAX_CELL_VOLTAGE) break;
138
                if (UBat < cells * MAX_CELL_VOLTAGE) break;
126
            }
139
            }
127
 
140
 
128
            min_voltage = cells * setting.param.UnterspannungsWarnung;
141
            min_voltage = cells * setting.param.UnterspannungsWarnung;
129
        } else {
142
        } else {
130
            min_voltage = setting.param.UnterspannungsWarnung;
143
            min_voltage = setting.param.UnterspannungsWarnung;
131
            cells = min_voltage / CELL_VOLT_MIN;
144
            cells = min_voltage / CELL_VOLT_MIN;
132
        }
145
        }
133
        //write_ndigit_number_u(10, 13, setting.param.UnterspannungsWarnung, 3, 1);
146
        //write_ndigit_number_u(10, 13, setting.param.UnterspannungsWarnung, 3, 1);
134
    } else {
147
    } else {
135
        cells = CELL_NUM;
148
        cells = CELL_NUM;
136
        min_voltage = cells * CELL_VOLT_MIN;
149
        min_voltage = cells * CELL_VOLT_MIN;
137
    }
150
    }
138
 
151
 
139
    max_voltage = cells * CELL_VOLT_MAX;
152
    max_voltage = cells * CELL_VOLT_MAX;
140
 
153
 
141
    write_ndigit_number_u(11, 8, cells, 1, 0);
154
    write_ndigit_number_u(11, 8, cells, 1, 0);
142
    write_ascii_string_pgm(13, 8, PSTR("Cells")); // Number of Cells
155
    write_ascii_string_pgm(13, 8, PSTR("Cells")); // Number of Cells
143
    write_ascii_string_pgm(2, 9, PSTR("Warn:")); // Warn Voltage
156
    write_ascii_string_pgm(2, 9, PSTR("Warn:")); // Warn Voltage
144
    write_ndigit_number_s_10th(8, 9, min_voltage, 3, 0);
157
    write_ndigit_number_s_10th(8, 9, min_voltage, 3, 0);
145
    write_ascii_string_pgm(14, 9, PSTR("Max:")); // Max Voltage
158
    write_ascii_string_pgm(14, 9, PSTR("Max:")); // Max Voltage
146
    write_ndigit_number_s_10th(20, 9, max_voltage, 3, 0);
159
    write_ndigit_number_s_10th(20, 9, max_voltage, 3, 0);
147
 
-
 
148
    get_eeprom(1);
-
 
149
 
-
 
150
    //write_ascii_string_pgm(23, 2, vm[COSD_FLAGS & COSD_FLAG_NTSC]);
-
 
151
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
-
 
152
        write_ascii_string_pgm(23, 1, VM_NTSC);
-
 
153
    } else {
-
 
154
        write_ascii_string_pgm(23, 1, VM_PAL);
160
 
155
    }
161
 
156
 
162
 
157
    // request version from board
163
    // request version from board
158
#if FCONLY
164
#if FCONLY
159
    //usart1_request_mk_data(0, 'v', 0);
165
    //usart1_request_mk_data(0, 'v', 0);
160
    write_ascii_string_pgm(2, 11, PSTR("FC VERSION: ........"));
166
    write_ascii_string_pgm(2, 11, PSTR("FC VERSION: ........"));
161
    usart1_request_blocking('V', PSTR(REQUEST_FC_VERSION));
167
    usart1_request_blocking('V', PSTR(REQUEST_FC_VERSION));
162
#else
168
#else
163
    usart1_request_nc_uart();
169
    usart1_request_nc_uart();
164
    _delay_ms(200);
170
    _delay_ms(200);
165
    usart1_request_nc_uart();
171
    usart1_request_nc_uart();
166
 
172
 
167
    //usart1_request_mk_data(1, 'v', 0);
173
    //usart1_request_mk_data(1, 'v', 0);
168
    write_ascii_string_pgm(2, 11, PSTR("NC VERSION: ........"));
174
    write_ascii_string_pgm(2, 11, PSTR("NC VERSION: ........"));
169
    usart1_request_blocking('V', PSTR(REQUEST_NC_VERSION));
175
    usart1_request_blocking('V', PSTR(REQUEST_NC_VERSION));
170
#endif
176
#endif
171
    str_VersionInfo VersionInfo;
177
    str_VersionInfo VersionInfo;
172
    VersionInfo = *((str_VersionInfo*)pRxData);
178
    VersionInfo = *((str_VersionInfo*)pRxData);
173
 
179
 
174
    write_ndigit_number_u(14, 11, VersionInfo.SWMajor, 3, 1);
180
    write_ndigit_number_u(14, 11, VersionInfo.SWMajor, 3, 1);
175
    write_ndigit_number_u(18, 11, VersionInfo.SWMinor, 3, 1);
181
    write_ndigit_number_u(18, 11, VersionInfo.SWMinor, 3, 1);
176
    write_ascii_char(22 + 11 * 30, 'a' + VersionInfo.SWPatch);
182
    write_ascii_char(22 + 11 * 30, 'a' + VersionInfo.SWPatch);
177
    // end version request
183
    // end version request
178
 
184
 
179
 
185
 
180
 
186
 
181
 
187
 
182
#if FCONLY
188
#if FCONLY
183
    COSD_DISPLAYMODE %= (sizeof (fcdisplaymodes) / sizeof (displaymode_t));
189
    COSD_DISPLAYMODE %= (sizeof (fcdisplaymodes) / sizeof (displaymode_t));
184
    mode = fcdisplaymodes;
190
    mode = fcdisplaymodes;
185
    mode += COSD_DISPLAYMODE;
191
    mode += COSD_DISPLAYMODE;
186
    osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
192
    osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
187
    // re-request data ever 100ms from FC;
193
    // re-request data ever 100ms from FC;
188
    //usart1_request_mk_data(0, 'd', 100);
194
    //usart1_request_mk_data(0, 'd', 100);
189
#else
195
#else
190
    COSD_DISPLAYMODE %= (sizeof (ncdisplaymodes) / sizeof (displaymode_t));
196
    COSD_DISPLAYMODE %= (sizeof (ncdisplaymodes) / sizeof (displaymode_t));
191
    mode = ncdisplaymodes;
197
    mode = ncdisplaymodes;
192
    mode += COSD_DISPLAYMODE;
198
    mode += COSD_DISPLAYMODE;
193
    osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
199
    osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
194
    // re-request OSD Data from NC every 100ms
200
    // re-request OSD Data from NC every 100ms
195
    //usart1_request_mk_data(1, 'o', 100);
201
    //usart1_request_mk_data(1, 'o', 100);
196
#endif
202
#endif
197
 
203
 
198
    _delay_ms(5000);
204
    _delay_ms(5000);
199
    clear();
205
    clear();
200
    // update flags to paint display again because of clear
206
    // update flags to paint display again because of clear
201
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
207
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
202
 
208
 
203
    // we got data
209
    // we got data
204
    COSD_FLAGS_RUNTIME |= COSD_DATARECEIVED;
210
    COSD_FLAGS_RUNTIME |= COSD_DATARECEIVED;
205
}
211
}
206
 
212
 
207
/* ##########################################################################
213
/* ##########################################################################
208
 * A simple config menu for the flags
214
 * A simple config menu for the flags
209
 * ##########################################################################*/
215
 * ##########################################################################*/
210
 
216
 
211
/**
217
/**
212
 * helper function for flag display
218
 * helper function for flag display
213
 */
219
 */
214
void onoff(uint8_t col, uint8_t line, uint8_t onoff) {
220
void onoff(uint8_t col, uint8_t line, uint8_t onoff) {
215
    if (onoff) {
221
    if (onoff) {
216
        write_ascii_string_pgm(col, line, PSTR("ON "));
222
        write_ascii_string_pgm(col, line, PSTR("ON "));
217
    } else {
223
    } else {
218
        write_ascii_string_pgm(col, line, PSTR("OFF"));
224
        write_ascii_string_pgm(col, line, PSTR("OFF"));
219
    }
225
    }
220
}
226
}
221
 
227
 
222
/**
228
/**
223
 * helper function for menu updating
229
 * helper function for menu updating
224
 */
230
 */
225
void config_menu_drawings(uint8_t chosen) {
231
void config_menu_drawings(uint8_t chosen) {
226
    static uint8_t old_y = 0;
232
    static uint8_t old_y = 0;
227
    uint8_t x = MENU_LEFT, y = MENU_TOP, line = MENU_TOP;
233
    uint8_t x = MENU_LEFT, y = MENU_TOP, line = MENU_TOP;
228
    //************************************************* 13 to 14
234
    //************************************************* 13 to 14
229
    if (chosen > 6 && chosen < 15) { // right
235
    if (chosen > 6 && chosen < 15) { // right
230
        x = MENU_MIDDLE;
236
        x = MENU_MIDDLE;
231
        y = chosen - 7 + MENU_TOP;
237
        y = chosen - 7 + MENU_TOP;
232
    } else if (chosen < 8) {
238
    } else if (chosen < 8) {
233
        y = chosen + MENU_TOP;
239
        y = chosen + MENU_TOP;
234
    } else {
240
    } else {
235
        y = chosen - 7 + MENU_TOP;
241
        y = chosen - 7 + MENU_TOP;
236
    }
242
    }
237
 
243
 
238
    // clear prevoius _cursor_ and draw current
244
    // clear prevoius _cursor_ and draw current
239
    for (uint8_t myx = MENU_LEFT; myx < 29; myx++) {
245
    for (uint8_t myx = MENU_LEFT; myx < 29; myx++) {
240
        write_char_att_xy(myx, old_y, 0);
246
        write_char_att_xy(myx, old_y, 0);
241
        if (myx > x - 1 && myx < x + 14) {
247
        if (myx > x - 1 && myx < x + 14) {
242
            write_char_att_xy(myx, y, BLACKBG | INVERT);
248
            write_char_att_xy(myx, y, BLACKBG | INVERT);
243
        }
249
        }
244
    };
250
    };
245
 
251
 
246
    write_ascii_string_pgm(MENU_LEFT, line, PSTR("Video"));
252
    write_ascii_string_pgm(MENU_LEFT, line, PSTR("Video"));
247
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
253
    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
248
        write_ascii_string_pgm(MENU_LEFT + 10, line, VM_NTSC);
254
        write_ascii_string_pgm(MENU_LEFT + 10, line, VM_NTSC);
249
    } else {
255
    } else {
250
        write_ascii_string_pgm(MENU_LEFT + 10, line, VM_PAL);
256
        write_ascii_string_pgm(MENU_LEFT + 10, line, VM_PAL);
251
    }
257
    }
252
 
258
 
253
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Full HUD"));
259
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Full HUD"));
254
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_HUD);
260
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_HUD);
255
 
261
 
256
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Horizon"));
262
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Horizon"));
257
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_ARTHORIZON);
263
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_ARTHORIZON);
258
 
264
 
259
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Aggr.Hor."));
265
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Aggr.Hor."));
260
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_AGGRHORIZON);
266
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_AGGRHORIZON);
261
 
267
 
262
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Stats"));
268
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Stats"));
263
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STATS);
269
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STATS);
264
 
270
 
265
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("A by FC"));
271
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("A by FC"));
266
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT);
272
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT);
267
 
273
 
268
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("GPS Pos"));
274
    write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("GPS Pos"));
269
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_SHOW_COORDS);
275
    onoff(MENU_LEFT + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_SHOW_COORDS);
270
 
276
 
271
    // 2nd col
277
    // 2nd col
272
    line = 2;
278
    line = 2;
273
 
279
 
274
    write_ascii_string_pgm(MENU_MIDDLE, line, PSTR("V C-Strom"));
280
    write_ascii_string_pgm(MENU_MIDDLE, line, PSTR("V C-Strom"));
275
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STROMVOLT);
281
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STROMVOLT);
276
 
282
 
277
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Height by"));
283
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Height by"));
278
    if (COSD_FLAGS_CONFIG & COSD_FLAG_GPSHEIGHT) {
284
    if (COSD_FLAGS_CONFIG & COSD_FLAG_GPSHEIGHT) {
279
        write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR(" GPS"));
285
        write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR(" GPS"));
280
    } else {
286
    } else {
281
        write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR("BARO"));
287
        write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR("BARO"));
282
    }
288
    }
283
 
289
 
284
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Feet/mph"));
290
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Feet/mph"));
285
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_FEET);
291
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_FEET);
286
 
292
 
287
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Big Vario"));
293
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Big Vario"));
288
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_BIGVARIO);
294
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_BIGVARIO);
289
 
295
 
290
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Big Speed"));
296
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Big Speed"));
291
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_BIGSPEED);
297
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_BIGSPEED);
292
 
298
 
293
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Passive"));
299
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Passive"));
294
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE);
300
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE);
295
 
301
 
296
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Cam Scope"));
302
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Cam Scope"));
297
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_SHOW_SCOPE);
303
    onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_SHOW_SCOPE);
298
 
304
 
299
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Move Scope"));
305
    write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Move Scope"));
300
 
306
 
301
 
307
 
302
    // bottom
308
    // bottom
303
    write_ascii_string_pgm(MENU_LEFT, 10, PSTR("Reset uptime"));
309
    write_ascii_string_pgm(MENU_LEFT, 10, PSTR("Reset uptime"));
304
 
310
 
305
    write_ascii_string_pgm(MENU_LEFT, 11, PSTR("Display Mode"));
311
    write_ascii_string_pgm(MENU_LEFT, 11, PSTR("Display Mode"));
306
    write_ascii_string_pgm(15, 11, (const char *)(pgm_read_word(&(mode->desc))));
312
    write_ascii_string_pgm(15, 11, (const char *)(pgm_read_word(&(mode->desc))));
307
 
313
 
308
    write_ascii_string_pgm(MENU_LEFT, 12, PSTR("Save config"));
314
    write_ascii_string_pgm(MENU_LEFT, 12, PSTR("Save config"));
309
    write_ascii_string_pgm(MENU_LEFT, 13, PSTR("EXIT"));
315
    write_ascii_string_pgm(MENU_LEFT, 13, PSTR("EXIT"));
310
 
316
 
311
    old_y = y;
317
    old_y = y;
312
}
318
}
313
 
319
 
314
/**
320
/**
315
 * some sort of clicking response in the menu
321
 * some sort of clicking response in the menu
316
 */
322
 */
317
void config_menu_doclick(uint8_t chosen) {
323
void config_menu_doclick(uint8_t chosen) {
318
    write_ascii_string_pgm(MENU_LEFT, chosen + MENU_TOP - 7, PSTR("DONE              "));
324
    write_ascii_string_pgm(MENU_LEFT, chosen + MENU_TOP - 7, PSTR("DONE              "));
319
    _delay_ms(500);
325
    _delay_ms(500);
320
    config_menu_drawings(chosen);
326
    config_menu_drawings(chosen);
321
}
327
}
322
 
328
 
323
/**
329
/**
324
 * move around the four scope edges
330
 * move around the four scope edges
325
 */
331
 */
326
void move_scope() {
332
void move_scope() {
327
    uint8_t mode = 0;
333
    uint8_t mode = 0;
328
    clear();
334
    clear();
329
    draw_scope();
335
    draw_scope();
330
    _delay_ms(500);
336
    _delay_ms(500);
331
    while (mode < 12) {
337
    while (mode < 12) {
332
        if (s2_pressed()) { // next
338
        if (s2_pressed()) { // next
333
            mode++;
339
            mode++;
334
            _delay_ms(500);
340
            _delay_ms(500);
335
        } else if (s1_pressed()) {
341
        } else if (s1_pressed()) {
336
            if (mode % 3 == 2) {
342
            if (mode % 3 == 2) {
337
                scope[mode] = (scope[mode] + 1) % 2;
343
                scope[mode] = (scope[mode] + 1) % 2;
338
            } else {
344
            } else {
339
                scope[mode] = (scope[mode] + 1) % ((mode % 3) % 2 == 0 ? 30 : bottom_line);
345
                scope[mode] = (scope[mode] + 1) % ((mode % 3) % 2 == 0 ? 30 : bottom_line);
340
            }
346
            }
341
            clear();
347
            clear();
342
            draw_scope();
348
            draw_scope();
343
            _delay_ms(100);
349
            _delay_ms(100);
344
        }
350
        }
345
    }
351
    }
346
    clear();
352
    clear();
347
}
353
}
348
 
354
 
349
/**
355
/**
350
 * a simple config menu tryout
356
 * a simple config menu tryout
351
 */
357
 */
352
void config_menu(void) {
358
void config_menu(void) {
353
    // disable interrupts (makes the menu more smoothely)
359
    // disable interrupts (makes the menu more smoothely)
354
    cli();
360
    cli();
355
 
361
 
356
    // clear screen
362
    // clear screen
357
    clear();
363
    clear();
358
 
364
 
359
    uint8_t chosen = 0;
365
    uint8_t chosen = 0;
360
    uint8_t inmenu = 1;
366
    uint8_t inmenu = 1;
361
    write_ascii_string_pgm(6, 1, PSTR("C-OSD Config Menu"));
367
    write_ascii_string_pgm(6, 1, PSTR("C-OSD Config Menu"));
362
 
368
 
363
    // wait a bit before doing stuff so user has chance to release button
369
    // wait a bit before doing stuff so user has chance to release button
364
    _delay_ms(250);
370
    _delay_ms(250);
365
 
371
 
366
    config_menu_drawings(chosen);
372
    config_menu_drawings(chosen);
367
 
373
 
368
    while (inmenu) {
374
    while (inmenu) {
369
        if (s2_pressed()) {
375
        if (s2_pressed()) {
370
            chosen = (chosen + 1) % 19;
376
            chosen = (chosen + 1) % 19;
371
            //if (chosen == 12) chosen = 13; // SKIP unused menu space for now
377
            //if (chosen == 12) chosen = 13; // SKIP unused menu space for now
372
            config_menu_drawings(chosen);
378
            config_menu_drawings(chosen);
373
            _delay_ms(500);
379
            _delay_ms(500);
374
        } else if (s1_pressed()) {
380
        } else if (s1_pressed()) {
375
            switch (chosen) {
381
            switch (chosen) {
376
                case 0: // NTSC or PAL
382
                case 0: // NTSC or PAL
377
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_NTSC;
383
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_NTSC;
378
                    // Setup Video Mode
384
                    // Setup Video Mode
379
                    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
385
                    if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
380
                        // NTSC + enable display immediately (VM0)
386
                        // NTSC + enable display immediately (VM0)
381
                        spi_send_byte(0x00, 0b00001000);
387
                        spi_send_byte(0x00, 0b00001000);
382
 
388
 
383
                        bottom_line = 12;
389
                        bottom_line = 12;
384
                    } else {
390
                    } else {
385
                        // PAL + enable display immediately (VM0)
391
                        // PAL + enable display immediately (VM0)
386
                        spi_send_byte(0x00, 0b01001000);
392
                        spi_send_byte(0x00, 0b01001000);
387
 
393
 
388
                        bottom_line = 14;
394
                        bottom_line = 14;
389
                    }
395
                    }
390
                    break;
396
                    break;
391
                case 1: // full HUD
397
                case 1: // full HUD
392
                    COSD_FLAGS_MODES ^= COSD_FLAG_HUD;
398
                    COSD_FLAGS_MODES ^= COSD_FLAG_HUD;
393
                    break;
399
                    break;
394
                case 2: // art horizon
400
                case 2: // art horizon
395
                    COSD_FLAGS_MODES ^= COSD_FLAG_ARTHORIZON;
401
                    COSD_FLAGS_MODES ^= COSD_FLAG_ARTHORIZON;
396
                    break;
402
                    break;
397
                case 3: // aggressiva horizon
403
                case 3: // aggressiva horizon
398
                    COSD_FLAGS_MODES ^= COSD_FLAG_AGGRHORIZON;
404
                    COSD_FLAGS_MODES ^= COSD_FLAG_AGGRHORIZON;
399
                    break;
405
                    break;
400
                case 4: // statistics
406
                case 4: // statistics
401
                    COSD_FLAGS_MODES ^= COSD_FLAG_STATS;
407
                    COSD_FLAGS_MODES ^= COSD_FLAG_STATS;
402
                    break;
408
                    break;
403
                case 5: // current by fc
409
                case 5: // current by fc
404
                    COSD_FLAGS_MODES ^= COSD_FLAG_FCCURRENT;
410
                    COSD_FLAGS_MODES ^= COSD_FLAG_FCCURRENT;
405
                    break;
411
                    break;
406
                case 6: // GPS coordinates while flying
412
                case 6: // GPS coordinates while flying
407
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_SHOW_COORDS;
413
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_SHOW_COORDS;
408
                    break;
414
                    break;
409
                case 7: // 2nd voltage by c-strom
415
                case 7: // 2nd voltage by c-strom
410
                    COSD_FLAGS_MODES ^= COSD_FLAG_STROMVOLT;
416
                    COSD_FLAGS_MODES ^= COSD_FLAG_STROMVOLT;
411
                    break;
417
                    break;
412
                case 8: // GPS or BARO height
418
                case 8: // GPS or BARO height
413
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_GPSHEIGHT;
419
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_GPSHEIGHT;
414
                    break;
420
                    break;
415
                case 9: // Feet and mph?
421
                case 9: // Feet and mph?
416
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_FEET;
422
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_FEET;
417
                    break;
423
                    break;
418
                case 10: // big vario
424
                case 10: // big vario
419
                    COSD_FLAGS_MODES ^= COSD_FLAG_BIGVARIO;
425
                    COSD_FLAGS_MODES ^= COSD_FLAG_BIGVARIO;
420
                    break;
426
                    break;
421
                case 11: // big Speed
427
                case 11: // big Speed
422
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_BIGSPEED;
428
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_BIGSPEED;
423
                    break;
429
                    break;
424
                case 12: // passive
430
                case 12: // passive
425
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_PASSIVE;
431
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_PASSIVE;
426
                    break;
432
                    break;
427
                case 13: // scope
433
                case 13: // scope
428
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_SHOW_SCOPE;
434
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_SHOW_SCOPE;
429
                    break;
435
                    break;
430
                case 14: // move scope
436
                case 14: // move scope
431
                    move_scope();
437
                    move_scope();
432
                    break;
438
                    break;
433
                case 15: // reset uptime
439
                case 15: // reset uptime
434
                    uptime = 0;
440
                    uptime = 0;
435
                    config_menu_doclick(chosen);
441
                    config_menu_doclick(chosen);
436
                    break;
442
                    break;
437
                case 16: // change mode
443
                case 16: // change mode
438
#if FCONLY
444
#if FCONLY
439
                    COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof (fcdisplaymodes) / sizeof (displaymode_t));
445
                    COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof (fcdisplaymodes) / sizeof (displaymode_t));
440
                    mode = fcdisplaymodes;
446
                    mode = fcdisplaymodes;
441
                    mode += COSD_DISPLAYMODE;
447
                    mode += COSD_DISPLAYMODE;
442
                    osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
448
                    osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
443
#else
449
#else
444
                    COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof (ncdisplaymodes) / sizeof (displaymode_t));
450
                    COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof (ncdisplaymodes) / sizeof (displaymode_t));
445
                    mode = ncdisplaymodes;
451
                    mode = ncdisplaymodes;
446
                    mode += COSD_DISPLAYMODE;
452
                    mode += COSD_DISPLAYMODE;
447
                    osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
453
                    osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
448
#endif
454
#endif
449
                    break;
455
                    break;
450
                case 17: // save
456
                case 17: // save
451
                    save_eeprom();
457
                    save_eeprom();
452
                    config_menu_doclick(chosen);
458
                    config_menu_doclick(chosen);
453
                    break;
459
                    break;
454
                case 18: // exit
460
                case 18: // exit
455
                    inmenu = 0;
461
                    inmenu = 0;
456
                    config_menu_doclick(chosen);
462
                    config_menu_doclick(chosen);
457
                    break;
463
                    break;
458
            }
464
            }
459
            config_menu_drawings(chosen);
465
            config_menu_drawings(chosen);
460
            _delay_ms(250);
466
            _delay_ms(250);
461
        }
467
        }
462
    }
468
    }
463
 
469
 
464
    // clear screen up again
470
    // clear screen up again
465
    clear();
471
    clear();
466
 
472
 
467
    // update flags to paint display again if needed
473
    // update flags to paint display again if needed
468
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
474
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
469
 
475
 
470
    // enable interrupts again
476
    // enable interrupts again
471
    sei();
477
    sei();
472
}
478
}
473
 
479
 
474
#endif
480
#endif
475
 
481