Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
321 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
 *   Credits to:                                                            *
22
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project       *
23
 *   Gregor "killagreg" Stobrawa for making the MK code readable            *
24
 *   Klaus "akku" Buettner for the hardware                                 *
25
 *   Manuel "KeyOz" Schrape for explaining the MK protocol to me            *
26
 ****************************************************************************/
27
 
28
#include <avr/io.h>
29
#include <avr/interrupt.h>
30
#include <util/delay.h>
455 cascade 31
#include <avr/pgmspace.h>
346 cascade 32
#include "main.h"
33
#include "max7456_software_spi.h"
331 cascade 34
#include "usart1.h"
389 cascade 35
#include "osd_helpers.h"
321 cascade 36
 
37
/* TODO:
38
 * - verifiy correctness of values
324 cascade 39
 * - clean up code :)
321 cascade 40
 */
41
 
329 cascade 42
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
346 cascade 43
// data structs not needed for character flashin
331 cascade 44
#include "mk-data-structs.h"
321 cascade 45
 
46
/* ##########################################################################
47
 * global definitions and global vars
48
 * ##########################################################################*/
346 cascade 49
 
321 cascade 50
volatile uint16_t setsReceived = 0;
51
 
52
volatile NaviData_t naviData;
53
volatile DebugOut_t debugData;
54
 
55
// cache old vars for blinking attribute, checkup is faster than full
56
// attribute write each time
57
volatile uint8_t last_UBat = 255;
58
volatile uint8_t last_RC_Quality = 255;
59
 
60
// 16bit should be enough, normal LiPos don't last that long
61
volatile uint16_t uptime = 0;
62
volatile uint16_t timer = 0;
63
 
331 cascade 64
// remember last time data was received
65
volatile uint8_t seconds_since_last_data = 0;
66
 
346 cascade 67
// store stats description in progmem to save space
68
char stats_item_0[] PROGMEM = "max Altitude:";
69
char stats_item_1[] PROGMEM = "max Speed   :";
70
char stats_item_2[] PROGMEM = "max Distance:";
71
char stats_item_3[] PROGMEM = "min voltage :";
72
char stats_item_4[] PROGMEM = "max time    :";
412 cascade 73
char stats_item_5[] PROGMEM = "longitude   :";
74
char stats_item_6[] PROGMEM = "latitude    :";
455 cascade 75
char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2,
76
    stats_item_3, stats_item_4, stats_item_5, stats_item_6};
346 cascade 77
 
78
// store more fixed strings in progmen
379 cascade 79
char ON[] PROGMEM = "ON ";
346 cascade 80
char OFF[] PROGMEM = "OFF";
81
 
329 cascade 82
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
326 cascade 83
 
84
// general PAL|NTSC distingiusch stuff
85
uint8_t top_line = 1;
86
uint8_t bottom_line = 14;
87
 
453 cascade 88
// battery voltages
89
uint8_t min_voltage = 0;
90
uint8_t max_voltage = 0;
91
 
326 cascade 92
// Flags
93
uint8_t COSD_FLAGS = 0;
94
 
321 cascade 95
/* ##########################################################################
326 cascade 96
 * debounce buttons
97
 * ##########################################################################*/
98
int s1_pressed() {
455 cascade 99
    if (S1_PRESSED) {
100
        _delay_ms(25);
101
        if (S1_PRESSED) return 1;
102
    }
103
    return 0;
326 cascade 104
}
105
 
106
int s2_pressed() {
455 cascade 107
    if (S2_PRESSED) {
108
        _delay_ms(25);
109
        if (S2_PRESSED) return 1;
110
    }
111
    return 0;
326 cascade 112
}
113
 
389 cascade 114
/* ##########################################################################
115
 * Interrupt handler
116
 * ##########################################################################*/
455 cascade 117
 
321 cascade 118
/**
389 cascade 119
 * handler for undefined Interrupts
120
 * if not defined AVR will reset in case any unhandled interrupts occur
321 cascade 121
 */
389 cascade 122
ISR(__vector_default) {
455 cascade 123
    asm("nop");
389 cascade 124
}
321 cascade 125
 
389 cascade 126
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
321 cascade 127
/* ##########################################################################
128
 * timer stuff
129
 * ##########################################################################*/
455 cascade 130
 
331 cascade 131
/**
132
 * timer kicks in every 1000uS ^= 1ms
321 cascade 133
 */
134
ISR(TIMER0_OVF_vect) {
385 cascade 135
    OCR0 = 15; // preload
321 cascade 136
    if (!timer--) {
137
        uptime++;
138
        timer = 999;
455 cascade 139
        seconds_since_last_data++;
321 cascade 140
    }
141
}
142
 
143
 
144
/* ##########################################################################
326 cascade 145
 * A simple config menu for the flags
146
 * ##########################################################################*/
339 cascade 147
 
148
/**
149
 * helper function for menu updating
150
 */
151
void config_menu_drawings(uint8_t chosen) {
152
    // clear prevoius _cursor_
383 cascade 153
    write_ascii_string(3, (chosen + 5) % 8, " ");
339 cascade 154
    // draw current _cursor_
383 cascade 155
    write_ascii_string(3, chosen + 5, ">");
339 cascade 156
    if (COSD_FLAGS & COSD_FLAG_HUD) {
383 cascade 157
        write_ascii_string_pgm(23, 5, ON);
158
    } else {
455 cascade 159
        write_ascii_string_pgm(23, 5, OFF);
383 cascade 160
    }
161
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
346 cascade 162
        write_ascii_string_pgm(23, 6, ON);
339 cascade 163
    } else {
383 cascade 164
        write_ascii_string_pgm(23, 6, OFF);
339 cascade 165
    }
455 cascade 166
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
346 cascade 167
        write_ascii_string_pgm(23, 7, ON);
339 cascade 168
    } else {
455 cascade 169
        write_ascii_string_pgm(23, 7, OFF);
339 cascade 170
    }
455 cascade 171
    if (COSD_FLAGS & COSD_FLAG_STATS) {
346 cascade 172
        write_ascii_string_pgm(23, 8, ON);
339 cascade 173
    } else {
346 cascade 174
        write_ascii_string_pgm(23, 8, OFF);
339 cascade 175
    }
176
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
346 cascade 177
        write_ascii_string_pgm(23, 9, ON);
339 cascade 178
    } else {
346 cascade 179
        write_ascii_string_pgm(23, 9, OFF);
339 cascade 180
    }
181
}
182
 
183
/**
346 cascade 184
 * some sort of clicking response in the menu
185
 */
186
void config_menu_doclick(uint8_t chosen, char** menu) {
455 cascade 187
    write_ascii_string(4, chosen + 5, "DONE              ");
188
    _delay_ms(1000);
189
    write_ascii_string(4, chosen + 5, menu[chosen]);
346 cascade 190
}
191
 
192
/**
339 cascade 193
 * a simple config menu tryout
194
 */
326 cascade 195
void config_menu(void) {
455 cascade 196
    // disable interrupts (makes the menu more smoothely)
197
    cli();
321 cascade 198
 
455 cascade 199
    // clear screen
200
    clear();
326 cascade 201
 
455 cascade 202
    char* menu[9] = {"Full HUD",
203
        "Art.Horizon in HUD",
204
        "Big Vario bar",
205
        "Statistics",
206
        "Warnings", // TODO: do it!
207
        "Reset uptime",
208
        "Request OSD-data",
209
        "Disable Debug-data",
210
        "EXIT"};
326 cascade 211
 
455 cascade 212
    uint8_t inmenu = 1;
213
    uint8_t chosen = 0;
214
    write_ascii_string(6, 2, "C-OSD Config Menu");
326 cascade 215
 
455 cascade 216
    // wait a bit before doing stuff so user has chance to release button
217
    _delay_ms(250);
339 cascade 218
 
455 cascade 219
    write_ascii_string(4, 5, menu[0]);
220
    write_ascii_string(4, 6, menu[1]);
221
    write_ascii_string(4, 7, menu[2]);
222
    write_ascii_string(4, 8, menu[3]);
223
    write_ascii_string(4, 9, menu[4]);
224
    write_ascii_string(4, 10, menu[5]);
225
    write_ascii_string(4, 11, menu[6]);
226
    write_ascii_string(4, 12, menu[7]);
227
    write_ascii_string(4, 13, menu[8]);
339 cascade 228
 
455 cascade 229
    config_menu_drawings(chosen);
230
 
231
    while (inmenu) {
232
        if (s2_pressed()) {
233
            write_ascii_string(3, chosen + 5, " ");
234
            chosen = (chosen + 1) % 9;
235
            write_ascii_string(3, chosen + 5, ">");
236
            _delay_ms(500);
237
        } else if (s1_pressed()) {
238
            switch (chosen) {
239
                case 0: // full HUD
240
                    COSD_FLAGS ^= COSD_FLAG_HUD;
241
                    config_menu_drawings(chosen);
242
                    break;
243
                case 1: // art horizon
244
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
245
                    config_menu_drawings(chosen);
246
                    break;
247
                case 2: // big vario
248
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
249
                    config_menu_drawings(chosen);
250
                    break;
251
                case 3: // statistics
252
                    COSD_FLAGS ^= COSD_FLAG_STATS;
253
                    config_menu_drawings(chosen);
254
                    break;
255
                case 4: // warnings
256
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
257
                    config_menu_drawings(chosen);
258
                    break;
259
                case 5: // reset uptime
260
                    uptime = 0;
261
                    config_menu_doclick(chosen, menu);
262
                    break;
263
                case 6: // re-request OSD data
454 cascade 264
#if FCONLY
455 cascade 265
                    // request data ever 100ms from FC;
266
                    usart1_request_mk_data(0, 'd', 100);
454 cascade 267
#else
455 cascade 268
                    // request OSD Data from NC every 100ms
269
                    usart1_request_mk_data(1, 'o', 100);
454 cascade 270
 
455 cascade 271
                    // and disable debug...
272
                    usart1_request_mk_data(0, 'd', 0);
454 cascade 273
#endif
455 cascade 274
                    config_menu_doclick(chosen, menu);
275
                    break;
276
                case 7: // disable debug data
277
                    // disable sending of debug data
278
                    // may result in smoother ddata display
279
                    usart1_request_mk_data(0, 'd', 0);
280
                    config_menu_doclick(chosen, menu);
281
                    break;
282
                case 8: // exit
283
                    inmenu = 0;
284
                    break;
285
            }
286
            _delay_ms(250);
287
        }
288
    }
326 cascade 289
 
455 cascade 290
    // clear screen up again
291
    clear();
326 cascade 292
 
455 cascade 293
    // update flags to paint display again if needed
294
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
326 cascade 295
 
455 cascade 296
    // enable interrupts again
297
    sei();
326 cascade 298
}
299
 
455 cascade 300
/**
301
 * auto config some stuff on startup, currently only battery cells
302
 * TODO: this is testing stuff, strings should go progmem and so on...
303
 */
459 cascade 304
void auto_config(uint8_t UBat) {
455 cascade 305
    clear();
306
    write_ascii_string(2, 2, "C-OSD Initialisation");
307
#if FCONLY
308
    write_ascii_string(2, 3, "FC only Mode");
309
#else
310
    write_ascii_string(2, 3, "NaviCtrl Mode");
311
#endif
459 cascade 312
        write_ascii_string(2, 4, BUILDDATE);
455 cascade 313
    uint8_t cellnum = 0;
314
    if (CELL_NUM == -1) {
459 cascade 315
        write_ascii_string(2, 6, "Guessing Number of Cells");
455 cascade 316
        do {
317
            cellnum++;
458 cascade 318
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 15));
455 cascade 319
    } else {
320
        cellnum = CELL_NUM;
321
    }
322
    min_voltage = cellnum * CELL_VOLT_MIN;
323
    max_voltage = cellnum * CELL_VOLT_MAX;
324
    write_ascii_string(2, 7, "Number of Cells:");
325
    write_ndigit_number_u(21, 7, cellnum, 1, 0);
326
    write_ascii_string(2, 8, "Warn Voltage   :");
327
    write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0);
328
    write_ascii_string(2, 9, "Max Voltage    :");
329
    write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0);
330
    _delay_ms(200);
331
    clear();
332
    // update flags to paint display again because of clear
333
    COSD_FLAGS &= ~COSD_ICONS_WRITTEN;
334
}
335
 
329 cascade 336
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
455 cascade 337
 
321 cascade 338
/* ##########################################################################
339
 * MAIN
340
 * ##########################################################################*/
341
int main(void) {
339 cascade 342
    // set up FLAGS, compiler should flatten this one
343
    COSD_FLAGS = (NTSC << 0);
344
    COSD_FLAGS |= (HUD << 1);
345
    COSD_FLAGS |= (ARTHORIZON << 2);
455 cascade 346
    COSD_FLAGS |= (BIGVARIO << 3);
347
    COSD_FLAGS |= (STATS << 4);
383 cascade 348
    COSD_FLAGS |= (WARNINGS << 5);
324 cascade 349
 
339 cascade 350
    // set up Atmega162 Ports
321 cascade 351
    DDRA |= (1 << PA1); // PA1 output (/CS)
352
    MAX_CS_HIGH
353
    DDRA |= (1 << PA2); // PA2 output (SDIN)
354
    MAX_SDIN_LOW
355
    DDRA |= (1 << PA3); // PA3 output (SCLK)
356
    MAX_SCLK_LOW
357
    DDRA |= (1 << PA5); // PA5 output (RESET)
358
    MAX_RESET_HIGH
359
 
360
    DDRC |= (1 << PC0); // PC0 output (LED1 gn)
361
    LED1_OFF
362
    DDRC |= (1 << PC1); // PC1 output (LED2 rt)
363
    LED2_OFF
364
    DDRC |= (1 << PC2); // PC2 output (LED3 gn)
365
    LED3_OFF
366
    DDRC |= (1 << PC3); // PC3 output (LED4 rt)
367
    LED4_OFF
368
 
369
    DDRC &= ~(1 << PC4); // PC4 input  (MODE)
370
    PORTC |= (1 << PC4); // pullup
371
    DDRC &= ~(1 << PC5); // PC5 input  (SET)
372
    PORTC |= (1 << PC5); // pullup
373
 
339 cascade 374
    // set up top and bottom lines
375
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
376
        bottom_line = 12;
377
    } else {
378
        bottom_line = 14;
379
    }
326 cascade 380
 
339 cascade 381
    // reset the MAX7456 to be sure any undefined states do no harm
321 cascade 382
    MAX_RESET_LOW
383
    MAX_RESET_HIGH
384
 
385
    // give the FC/NC and the maxim time to come up
386
    LED4_ON
387
    _delay_ms(2000);
388
 
389
    LED4_OFF
390
 
331 cascade 391
 
339 cascade 392
    //Pushing NEW chars to the MAX7456
329 cascade 393
#if (WRITECHARS != -1)
339 cascade 394
    // DISABLE display (VM0)
321 cascade 395
    spi_send_byte(0x00, 0b00000000);
455 cascade 396
    #include "characters.c"
397
#endif
321 cascade 398
 
339 cascade 399
    // Setup Video Mode
400
    if (COSD_FLAGS & COSD_FLAG_NTSC) {
401
        // NTSC + enable display immediately (VM0)
402
        spi_send_byte(0x00, 0b00001000);
403
    } else {
404
        // PAL + enable display immediately (VM0)
405
        spi_send_byte(0x00, 0b01001000);
406
    }
321 cascade 407
 
404 cascade 408
    /*// clear all display-mem (DMM)
403 cascade 409
    spi_send_byte(0x04, 0b00000100);
410
 
411
    // clearing takes 12uS according to maxim so lets wait longer
412
    _delay_us(120);
413
 
321 cascade 414
    // 8bit mode
404 cascade 415
    spi_send_byte(0x04, 0b01000000);*/
321 cascade 416
 
404 cascade 417
    // clear display memory and set to 8bit mode
321 cascade 418
    clear();
419
 
329 cascade 420
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
321 cascade 421
    // init usart
422
    usart1_init();
423
 
424
    // set up timer
425
    TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64
385 cascade 426
    OCR0 = 15; // preload
321 cascade 427
    TIMSK |= (1 << TOIE0); // enable overflow timer0
428
 
326 cascade 429
    // enable interrupts
321 cascade 430
    sei();
431
#endif
432
 
433
    //write_ascii_string(2,  7, "         CaScAdE          ");
434
    //write_ascii_string(2,  8, "is TESTING his open source");
435
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
436
 
437
    // we are ready
438
    LED3_ON
439
 
329 cascade 440
#if ALLCHARSDEBUG | (WRITECHARS != -1)
455 cascade 441
    clear();
321 cascade 442
    write_all_chars();
443
#else
455 cascade 444
    // clear serial screen
445
    //usart1_puts("\x1B[2J\x1B[H");
446
    //usart1_puts("hello world!\r\n");
454 cascade 447
#if FCONLY
455 cascade 448
    // request data ever 100ms from FC;
449
    usart1_request_mk_data(0, 'd', 100);
454 cascade 450
#else
455 cascade 451
    // request OSD Data from NC every 100ms
452
    usart1_request_mk_data(1, 'o', 100);
346 cascade 453
 
339 cascade 454
    // and disable debug...
455 cascade 455
    usart1_request_mk_data(0, 'd', 0);
454 cascade 456
#endif
321 cascade 457
 
339 cascade 458
    // stats for after flight
459
    int16_t max_Altimeter = 0;
459 cascade 460
    uint8_t min_UBat = 255;
461
#if !(FCONLY)
462
        uint16_t max_GroundSpeed = 0;
339 cascade 463
    int16_t max_Distance = 0;
464
    uint16_t max_FlyingTime = 0;
321 cascade 465
 
339 cascade 466
    // flags from last round to check for changes
467
    uint8_t old_MKFlags = 0;
468
 
321 cascade 469
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
377 cascade 470
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
459 cascade 471
#endif
321 cascade 472
 
473
    while (1) {
474
        if (rxd_buffer_locked) {
339 cascade 475
            if (COSD_FLAGS & COSD_FLAG_HUD) {
454 cascade 476
#if FCONLY
339 cascade 477
                if (rxd_buffer[2] == 'D') { // FC Data
454 cascade 478
                    Decode64();
339 cascade 479
                    debugData = *((DebugOut_t*) pRxData);
454 cascade 480
 
455 cascade 481
                    // init on first data retrival, distinguished by last battery :)
482
                    if (last_UBat == 255) {
459 cascade 483
                                                // fix for min_UBat
484
                                                min_UBat = debugData.Analog[9];
485
                        auto_config(debugData.Analog[9]);
455 cascade 486
                    }
459 cascade 487
                                        #include "osd_fcmode_default.c"
454 cascade 488
                }
489
#else
459 cascade 490
                                if (rxd_buffer[2] == 'O') { // NC OSD Data
491
                                        Decode64();
492
                                        naviData = *((NaviData_t*) pRxData);
453 cascade 493
 
459 cascade 494
                                        // init on first data retrival, distinguished by last battery :)
495
                                        if (last_UBat == 255) {
496
                                                // fix for min_UBat
497
                                                min_UBat = naviData.UBat;
498
                                            auto_config(naviData.UBat);
499
                                        }
500
                                        #include "osd_ncmode_default.c"
501
                                }
454 cascade 502
#endif
339 cascade 503
            }
321 cascade 504
            rxd_buffer_locked = 0;
505
        }
506
        // handle keypress
326 cascade 507
        if (s1_pressed()) {
339 cascade 508
            config_menu();
321 cascade 509
        }
339 cascade 510
        if (seconds_since_last_data > 2) {
454 cascade 511
#if FCONLY
455 cascade 512
            // request data ever 100ms from FC;
513
            usart1_request_mk_data(0, 'd', 100);
454 cascade 514
#else
455 cascade 515
            // request OSD Data from NC every 100ms
516
            usart1_request_mk_data(1, 'o', 100);
454 cascade 517
 
455 cascade 518
            // and disable debug...
519
            usart1_request_mk_data(0, 'd', 0);
454 cascade 520
#endif
339 cascade 521
        }
321 cascade 522
    }
523
#endif
524
    return 0;
525
}