Subversion Repositories Projects

Rev

Rev 523 | Rev 677 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
471 cascade 1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
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   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 ****************************************************************************/
20
 
21
#include <avr/io.h>
22
#include <avr/eeprom.h>
23
#include <avr/pgmspace.h>
24
#include <avr/interrupt.h>
25
#include <util/delay.h>
26
#include "max7456_software_spi.h"
27
#include "config.h"
28
#include "main.h"
29
#include "buttons.h"
30
#include "usart1.h"
31
 
32
uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1;
33
uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2;
523 cascade 34
uint8_t EEMEM ee_COSD_FLAGS_MODES = 0;
35
uint8_t EEMEM ee_COSD_FLAGS_CONFIG = 0;
497 cascade 36
uint8_t EEMEM ee_COSD_DISPLAYMODE = 0;
471 cascade 37
 
514 cascade 38
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
39
 
40
// store more fixed strings in progmen
41
char ON[] PROGMEM = "ON ";
42
char OFF[] PROGMEM = "OFF";
43
 
471 cascade 44
// store init strings in progmem
489 woggle 45
const char init_0[] PROGMEM = "C-OSD Initialisation";
46
const char init_1[] PROGMEM = "FC only Mode";
47
const char init_2[] PROGMEM = "NaviCtrl Mode";
48
const char init_3[] PROGMEM = "Guessing Number of Cells";
49
const char init_4[] PROGMEM = "Number of Cells:";
50
const char init_5[] PROGMEM = "Warn Voltage   :";
51
const char init_6[] PROGMEM = "Max Voltage    :";
52
const char* init_point[] PROGMEM = {init_0, init_1, init_2, init_3, init_4, init_5, init_6};
471 cascade 53
 
474 cascade 54
// video modes
489 woggle 55
const char VM_PAL[] PROGMEM = "PAL ";
56
const char VM_NTSC[] PROGMEM = "NTSC";
474 cascade 57
 
471 cascade 58
// menu strings to progmem
489 woggle 59
const char menu_item0[] PROGMEM = "Video Mode";
60
const char menu_item1[] PROGMEM = "Full HUD";
61
const char menu_item2[] PROGMEM = "Art.Horizon in HUD";
62
const char menu_item3[] PROGMEM = "Big Vario bar";
63
const char menu_item4[] PROGMEM = "Statistics";
64
const char menu_item5[] PROGMEM = "Warnings"; // TODO: do it!
507 cascade 65
const char menu_item6[] PROGMEM = "Voltage by C-Strom";
66
const char menu_item7[] PROGMEM = "Reset uptime";
497 cascade 67
const char menu_item8[] PROGMEM = "Display Mode";
523 cascade 68
const char menu_item9[] PROGMEM = "Height by";
69
const char menu_item10[] PROGMEM = "Save config";
70
const char menu_item11[] PROGMEM = "EXIT";
489 woggle 71
const char* menu[] = {menu_item0, menu_item1, menu_item2, menu_item3, menu_item4,
523 cascade 72
        menu_item5, menu_item6, menu_item7, menu_item8, menu_item9, menu_item10, menu_item11};
471 cascade 73
 
497 cascade 74
const displaymode_t * mode;
75
 
514 cascade 76
#endif
77
 
78
const char ee_message0[] PROGMEM = "Loading Data from EEPROM";
79
const char ee_message1[] PROGMEM = "No saved Data in EEPROM";
80
const char* ee_msg[] PROGMEM = {ee_message0, ee_message1};
81
 
471 cascade 82
/**
474 cascade 83
 * read data saved in eeprom, print out message if <verbose> is set
471 cascade 84
 */
474 cascade 85
void get_eeprom(uint8_t verbose) {
471 cascade 86
        if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) {
514 cascade 87
                #if !(ALLCHARSDEBUG|(WRITECHARS != -1))
474 cascade 88
                if (verbose) write_ascii_string_pgm(2, 9, ee_msg[0]); // Loading data
514 cascade 89
                #endif
523 cascade 90
                COSD_FLAGS_MODES = eeprom_read_byte(&ee_COSD_FLAGS_MODES);
91
                COSD_FLAGS_CONFIG = eeprom_read_byte(&ee_COSD_FLAGS_CONFIG);
497 cascade 92
                COSD_DISPLAYMODE = eeprom_read_byte(&ee_COSD_DISPLAYMODE);
519 cascade 93
                //if (verbose) write_ndigit_number_u(23, 11, COSD_DISPLAYMODE, 2, 0);
471 cascade 94
        } else {
514 cascade 95
                #if !(ALLCHARSDEBUG|(WRITECHARS != -1))
96
                if (verbose) write_ascii_string_pgm(2, 9, ee_msg[1]); // Loading data
97
                #endif
471 cascade 98
        }
99
}
100
 
514 cascade 101
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
102
 
471 cascade 103
/**
104
 * save data to eeprom
105
 */
106
void save_eeprom() {
107
        eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1);
108
        eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2);
523 cascade 109
        eeprom_write_byte(&ee_COSD_FLAGS_MODES, COSD_FLAGS_MODES);
110
        eeprom_write_byte(&ee_COSD_FLAGS_CONFIG, COSD_FLAGS_CONFIG);
497 cascade 111
        eeprom_write_byte(&ee_COSD_DISPLAYMODE, COSD_DISPLAYMODE);
471 cascade 112
}
113
 
114
/**
115
 * auto config some stuff on startup, currently only battery cells
116
 * TODO: this is testing stuff, strings should go progmem and so on...
117
 */
118
void init_cosd(uint8_t UBat) {
119
    clear();
120
        write_ascii_string_pgm(2, 1, init_point[0]); // C-OSD Initialisation
121
    //write_ascii_string(2, 1, "C-OSD Initialisation");
122
#if FCONLY
123
        write_ascii_string_pgm(2, 2, init_point[1]); // FC only mode
124
    //write_ascii_string(2, 2, "FC only Mode");
125
#else
126
        write_ascii_string_pgm(2, 2, init_point[2]); // NaviCtrl Mode
127
    //write_ascii_string(2, 2, "NaviCtrl Mode");
128
#endif
523 cascade 129
    write_ascii_string_pgm(2, 3, PSTR(BUILDDATE));
471 cascade 130
    uint8_t cellnum = 0;
131
    if (CELL_NUM == -1) {
132
                write_ascii_string_pgm(2, 4, init_point[3]); // Guessing Number of Cells
133
        //write_ascii_string(2, 6, "Guessing Number of Cells");
134
        do {
135
            cellnum++;
136
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23));
137
    } else {
138
        cellnum = CELL_NUM;
139
    }
140
    min_voltage = cellnum * CELL_VOLT_MIN;
141
    max_voltage = cellnum * CELL_VOLT_MAX;
142
        write_ascii_string_pgm(2, 5, init_point[4]); // Number of Cells
143
    //write_ascii_string(2, 5, "Number of Cells:");
144
    write_ndigit_number_u(21, 5, cellnum, 1, 0);
145
        write_ascii_string_pgm(2, 6, init_point[5]); // Warn Voltage
146
    //write_ascii_string(2, 6, "Warn Voltage   :");
519 cascade 147
    write_ndigit_number_s_10th(20, 6, min_voltage, 3, 0);
471 cascade 148
        write_ascii_string_pgm(2, 7, init_point[6]); // Max Voltage
149
    //write_ascii_string(2, 7, "Max Voltage    :");
519 cascade 150
    write_ndigit_number_s_10th(20, 7, max_voltage, 3, 0);
471 cascade 151
 
474 cascade 152
        get_eeprom(1);
471 cascade 153
 
474 cascade 154
    //write_ascii_string_pgm(23, 2, vm[COSD_FLAGS & COSD_FLAG_NTSC]);
523 cascade 155
        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
474 cascade 156
                write_ascii_string_pgm(23, 2, VM_NTSC);
157
        } else {
158
                write_ascii_string_pgm(23, 2, VM_PAL);
159
        }
160
 
497 cascade 161
 
162
        #if FCONLY
163
        COSD_DISPLAYMODE %= (sizeof(fcdisplaymodes) / sizeof(displaymode_t));
164
        mode = fcdisplaymodes;
165
        mode += COSD_DISPLAYMODE;
166
        osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
167
        #else
168
        COSD_DISPLAYMODE %= (sizeof(ncdisplaymodes) / sizeof(displaymode_t));
169
        mode = ncdisplaymodes;
170
        mode += COSD_DISPLAYMODE;
171
        osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
172
        #endif
173
 
471 cascade 174
    _delay_ms(200);
175
    clear();
176
    // update flags to paint display again because of clear
523 cascade 177
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
471 cascade 178
}
179
 
180
/* ##########################################################################
181
 * A simple config menu for the flags
182
 * ##########################################################################*/
183
 
184
/**
185
 * helper function for menu updating
186
 */
187
void config_menu_drawings(uint8_t chosen) {
188
    // clear prevoius _cursor_
474 cascade 189
    write_ascii_string(3, (chosen + 2) % 10, " ");
471 cascade 190
    // draw current _cursor_
474 cascade 191
    write_ascii_string(3, chosen + 2, ">");
192
 
523 cascade 193
        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
474 cascade 194
                write_ascii_string_pgm(23, 2, VM_NTSC);
195
        } else {
196
                write_ascii_string_pgm(23, 2, VM_PAL);
197
        }
198
 
523 cascade 199
    if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
471 cascade 200
        write_ascii_string_pgm(23, 3, ON);
201
    } else {
202
        write_ascii_string_pgm(23, 3, OFF);
203
    }
523 cascade 204
    if (COSD_FLAGS_MODES & COSD_FLAG_ARTHORIZON) {
471 cascade 205
        write_ascii_string_pgm(23, 4, ON);
206
    } else {
207
        write_ascii_string_pgm(23, 4, OFF);
208
    }
523 cascade 209
    if (COSD_FLAGS_MODES & COSD_FLAG_BIGVARIO) {
471 cascade 210
        write_ascii_string_pgm(23, 5, ON);
211
    } else {
212
        write_ascii_string_pgm(23, 5, OFF);
213
    }
523 cascade 214
    if (COSD_FLAGS_MODES & COSD_FLAG_STATS) {
471 cascade 215
        write_ascii_string_pgm(23, 6, ON);
216
    } else {
217
        write_ascii_string_pgm(23, 6, OFF);
218
    }
523 cascade 219
    if (COSD_FLAGS_MODES & COSD_FLAG_WARNINGS) {
471 cascade 220
        write_ascii_string_pgm(23, 7, ON);
221
    } else {
222
        write_ascii_string_pgm(23, 7, OFF);
223
    }
523 cascade 224
    if (COSD_FLAGS_MODES & COSD_FLAG_STROMVOLT) {
507 cascade 225
        write_ascii_string_pgm(23, 8, ON);
226
    } else {
227
        write_ascii_string_pgm(23, 8, OFF);
228
    }
519 cascade 229
        //write_ndigit_number_u(23, 10, COSD_DISPLAYMODE, 2, 0);
497 cascade 230
        write_ascii_string_pgm(18, 10, (const char *) (pgm_read_word(&(mode->desc))));
231
 
523 cascade 232
        if (COSD_FLAGS_CONFIG & COSD_FLAG_GPSHEIGHT) {
233
                write_ascii_string_pgm(20, 11, PSTR(" GPS"));
234
        } else {
235
                write_ascii_string_pgm(20, 11, PSTR("BARO"));
236
        }
471 cascade 237
}
238
 
239
/**
240
 * some sort of clicking response in the menu
241
 */
489 woggle 242
void config_menu_doclick(uint8_t chosen, const char* menu[]) {
523 cascade 243
    write_ascii_string_pgm(4, chosen + 2, PSTR("DONE              "));
471 cascade 244
    _delay_ms(500);
474 cascade 245
    write_ascii_string_pgm(4, chosen + 2, menu[chosen]);
471 cascade 246
}
247
 
248
/**
249
 * a simple config menu tryout
250
 */
251
void config_menu(void) {
252
    // disable interrupts (makes the menu more smoothely)
253
    cli();
254
 
255
    // clear screen
256
    clear();
257
 
258
    uint8_t inmenu = 1;
259
    uint8_t chosen = 0;
523 cascade 260
    write_ascii_string_pgm(6, 1, PSTR("C-OSD Config Menu"));
471 cascade 261
 
262
    // wait a bit before doing stuff so user has chance to release button
263
    _delay_ms(250);
264
 
474 cascade 265
        write_ascii_string_pgm(4, 2, menu[0]);
266
    write_ascii_string_pgm(4, 3, menu[1]);
267
    write_ascii_string_pgm(4, 4, menu[2]);
268
    write_ascii_string_pgm(4, 5, menu[3]);
269
    write_ascii_string_pgm(4, 6, menu[4]);
270
    write_ascii_string_pgm(4, 7, menu[5]);
271
    write_ascii_string_pgm(4, 8, menu[6]);
272
    write_ascii_string_pgm(4, 9, menu[7]);
273
    write_ascii_string_pgm(4, 10, menu[8]);
274
    write_ascii_string_pgm(4, 11, menu[9]);
275
        write_ascii_string_pgm(4, 12, menu[10]);
523 cascade 276
        write_ascii_string_pgm(4, 13, menu[11]);
471 cascade 277
 
278
    config_menu_drawings(chosen);
279
 
280
    while (inmenu) {
281
        if (s2_pressed()) {
474 cascade 282
            write_ascii_string(3, chosen + 2, " ");
523 cascade 283
            chosen = (chosen + 1) % 12;
474 cascade 284
            write_ascii_string(3, chosen + 2, ">");
471 cascade 285
            _delay_ms(500);
286
        } else if (s1_pressed()) {
287
            switch (chosen) {
523 cascade 288
                case 0: // NTSC or PAL
289
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_NTSC;
474 cascade 290
                                        // Setup Video Mode
523 cascade 291
                                        if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) {
474 cascade 292
                                            // NTSC + enable display immediately (VM0)
293
                                            spi_send_byte(0x00, 0b00001000);
294
 
295
                                                bottom_line = 12;
296
                                        } else {
297
                                            // PAL + enable display immediately (VM0)
298
                                            spi_send_byte(0x00, 0b01001000);
299
 
300
                                                bottom_line = 14;
301
                                        }
302
                    config_menu_drawings(chosen);
303
                    break;
304
                case 1: // full HUD
523 cascade 305
                    COSD_FLAGS_MODES ^= COSD_FLAG_HUD;
471 cascade 306
                    config_menu_drawings(chosen);
307
                    break;
474 cascade 308
                case 2: // art horizon
523 cascade 309
                    COSD_FLAGS_MODES ^= COSD_FLAG_ARTHORIZON;
471 cascade 310
                    config_menu_drawings(chosen);
311
                    break;
474 cascade 312
                case 3: // big vario
523 cascade 313
                    COSD_FLAGS_MODES ^= COSD_FLAG_BIGVARIO;
471 cascade 314
                    config_menu_drawings(chosen);
315
                    break;
474 cascade 316
                case 4: // statistics
523 cascade 317
                    COSD_FLAGS_MODES ^= COSD_FLAG_STATS;
471 cascade 318
                    config_menu_drawings(chosen);
319
                    break;
474 cascade 320
                case 5: // warnings
523 cascade 321
                    COSD_FLAGS_MODES ^= COSD_FLAG_WARNINGS;
471 cascade 322
                    config_menu_drawings(chosen);
323
                    break;
507 cascade 324
                case 6: // 2nd voltage by c-strom
523 cascade 325
                    COSD_FLAGS_MODES ^= COSD_FLAG_STROMVOLT;
507 cascade 326
                    config_menu_drawings(chosen);
327
                    break;
328
                case 7: // reset uptime
471 cascade 329
                    uptime = 0;
330
                    config_menu_doclick(chosen, menu);
331
                    break;
497 cascade 332
                case 8: // change mode
333
                                        #if FCONLY
334
                                        COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof(fcdisplaymodes) / sizeof(displaymode_t));
335
                                        mode = fcdisplaymodes;
336
                                        mode += COSD_DISPLAYMODE;
337
                                        osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun);
338
                                        #else
339
                                        COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof(ncdisplaymodes) / sizeof(displaymode_t));
340
                                        mode = ncdisplaymodes;
341
                                        mode += COSD_DISPLAYMODE;
342
                                        osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun);
343
                                        #endif
344
                                        config_menu_drawings(chosen);
471 cascade 345
                    break;
523 cascade 346
                case 9: // GPS or BARO height
347
                    COSD_FLAGS_CONFIG ^= COSD_FLAG_GPSHEIGHT;
348
                    config_menu_drawings(chosen);
349
                    break;
350
                case 10: // save
471 cascade 351
                    save_eeprom();
352
                    config_menu_doclick(chosen, menu);
353
                    break;
523 cascade 354
                case 11: // exit
471 cascade 355
                    inmenu = 0;
356
                    break;
357
            }
358
            _delay_ms(250);
359
        }
360
    }
361
 
362
    // clear screen up again
363
    clear();
364
 
365
    // update flags to paint display again if needed
523 cascade 366
    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
471 cascade 367
 
368
    // enable interrupts again
369
    sei();
370
}
371
 
372
#endif