Subversion Repositories Projects

Rev

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

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