Subversion Repositories Projects

Rev

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