Subversion Repositories Projects

Rev

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

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