Subversion Repositories Projects

Rev

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

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