Subversion Repositories Projects

Rev

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

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