Subversion Repositories Projects

Rev

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

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