Subversion Repositories Projects

Rev

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

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