Subversion Repositories Projects

Rev

Rev 474 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/****************************************************************************
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
 *   admiralcascade@gmail.com                                               *
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
 *                                                                          *
 *   This program is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 2 of the License.         *
 *                                                                          *
 *   This program is distributed in the hope that it will be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to the                          *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/


#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "max7456_software_spi.h"
#include "config.h"
#include "main.h"
#include "buttons.h"
#include "usart1.h"

#if !(ALLCHARSDEBUG|(WRITECHARS != -1))

// store more fixed strings in progmen
char ON[] PROGMEM = "ON ";
char OFF[] PROGMEM = "OFF";

uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1;
uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2;
uint16_t EEMEM ee_cal_ampere = 512;
uint8_t EEMEM ee_sensor = 50;
uint8_t EEMEM ee_COSD_FLAGS;

// store init strings in progmem
char init_0[] PROGMEM = "C-OSD Initialisation";
char init_1[] PROGMEM = "FC only Mode";
char init_2[] PROGMEM = "NaviCtrl Mode";
char init_3[] PROGMEM = "Guessing Number of Cells";
char init_4[] PROGMEM = "Number of Cells:";
char init_5[] PROGMEM = "Warn Voltage   :";
char init_6[] PROGMEM = "Max Voltage    :";
char* init_point[] PROGMEM = {init_0, init_1, init_2, init_3, init_4, init_5, init_6};

char ee_message0[] PROGMEM = "Loading Data from EEPROM";
char ee_message1[] PROGMEM = "No saved Data in EEPROM";
char* ee_msg[] PROGMEM = {ee_message0, ee_message1};

// menu strings to progmem
char menu_item0[] PROGMEM = "Full HUD";
char menu_item1[] PROGMEM = "Art.Horizon in HUD";
char menu_item2[] PROGMEM = "Big Vario bar";
char menu_item3[] PROGMEM = "Statistics";
char menu_item4[] PROGMEM = "Warnings"; // TODO: do it!
char menu_item5[] PROGMEM = "Reset uptime";
char menu_item6[] PROGMEM = "Request OSD-data";
char menu_item7[] PROGMEM = "Disable Debug-data";
char menu_item8[] PROGMEM = "Save config";
char menu_item9[] PROGMEM = "EXIT";
char* menu[] = {menu_item0, menu_item1, menu_item2, menu_item3, menu_item4,
        menu_item5, menu_item6, menu_item7, menu_item8, menu_item9};

/**
 * read data saved in eeprom
 */

void get_eeprom() {
        if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) {
                write_ascii_string_pgm(2, 9, ee_msg[0]); // Loading data
                COSD_FLAGS = eeprom_read_byte(&ee_COSD_FLAGS);
        } else {
                write_ascii_string_pgm(2, 9, ee_msg[1]); // No data found
        }
}

/**
 * save data to eeprom
 */

void save_eeprom() {
        eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1);
        eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2);
        eeprom_write_byte(&ee_COSD_FLAGS, COSD_FLAGS);
}

/**
 * auto config some stuff on startup, currently only battery cells
 * TODO: this is testing stuff, strings should go progmem and so on...
 */

void init_cosd(uint8_t UBat) {
    clear();
        write_ascii_string_pgm(2, 1, init_point[0]); // C-OSD Initialisation
    //write_ascii_string(2, 1, "C-OSD Initialisation");
#if FCONLY
        write_ascii_string_pgm(2, 2, init_point[1]); // FC only mode
    //write_ascii_string(2, 2, "FC only Mode");
#else
        write_ascii_string_pgm(2, 2, init_point[2]); // NaviCtrl Mode
    //write_ascii_string(2, 2, "NaviCtrl Mode");
#endif
    write_ascii_string(2, 3, BUILDDATE);
    uint8_t cellnum = 0;
    if (CELL_NUM == -1) {
                write_ascii_string_pgm(2, 4, init_point[3]); // Guessing Number of Cells
        //write_ascii_string(2, 6, "Guessing Number of Cells");
        do {
            cellnum++;
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23));
    } else {
        cellnum = CELL_NUM;
    }
    min_voltage = cellnum * CELL_VOLT_MIN;
    max_voltage = cellnum * CELL_VOLT_MAX;
        write_ascii_string_pgm(2, 5, init_point[4]); // Number of Cells
    //write_ascii_string(2, 5, "Number of Cells:");
    write_ndigit_number_u(21, 5, cellnum, 1, 0);
        write_ascii_string_pgm(2, 6, init_point[5]); // Warn Voltage
    //write_ascii_string(2, 6, "Warn Voltage   :");
    write_ndigit_number_s_10th(20, 6, min_voltage, 100, 0);
        write_ascii_string_pgm(2, 7, init_point[6]); // Max Voltage
    //write_ascii_string(2, 7, "Max Voltage    :");
    write_ndigit_number_s_10th(20, 7, max_voltage, 100, 0);

        get_eeprom();

    _delay_ms(200);
    clear();
    // update flags to paint display again because of clear
    COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
}

/* ##########################################################################
 * A simple config menu for the flags
 * ##########################################################################*/


/**
 * helper function for menu updating
 */

void config_menu_drawings(uint8_t chosen) {
    // clear prevoius _cursor_
    write_ascii_string(3, (chosen + 3) % 9, " ");
    // draw current _cursor_
    write_ascii_string(3, chosen + 3, ">");
    if (COSD_FLAGS & COSD_FLAG_HUD) {
        write_ascii_string_pgm(23, 3, ON);
    } else {
        write_ascii_string_pgm(23, 3, OFF);
    }
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
        write_ascii_string_pgm(23, 4, ON);
    } else {
        write_ascii_string_pgm(23, 4, OFF);
    }
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
        write_ascii_string_pgm(23, 5, ON);
    } else {
        write_ascii_string_pgm(23, 5, OFF);
    }
    if (COSD_FLAGS & COSD_FLAG_STATS) {
        write_ascii_string_pgm(23, 6, ON);
    } else {
        write_ascii_string_pgm(23, 6, OFF);
    }
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
        write_ascii_string_pgm(23, 7, ON);
    } else {
        write_ascii_string_pgm(23, 7, OFF);
    }
}

/**
 * some sort of clicking response in the menu
 */

void config_menu_doclick(uint8_t chosen, char** menu) {
    write_ascii_string(4, chosen + 3, "DONE              ");
    _delay_ms(500);
    write_ascii_string_pgm(4, chosen + 3, menu[chosen]);
}

/**
 * a simple config menu tryout
 */

void config_menu(void) {
    // disable interrupts (makes the menu more smoothely)
    cli();

    // clear screen
    clear();

    uint8_t inmenu = 1;
    uint8_t chosen = 0;
    write_ascii_string(6, 2, "C-OSD Config Menu");

    // wait a bit before doing stuff so user has chance to release button
    _delay_ms(250);

    write_ascii_string_pgm(4, 3, menu[0]);
    write_ascii_string_pgm(4, 4, menu[1]);
    write_ascii_string_pgm(4, 5, menu[2]);
    write_ascii_string_pgm(4, 6, menu[3]);
    write_ascii_string_pgm(4, 7, menu[4]);
    write_ascii_string_pgm(4, 8, menu[5]);
    write_ascii_string_pgm(4, 9, menu[6]);
    write_ascii_string_pgm(4, 10, menu[7]);
    write_ascii_string_pgm(4, 11, menu[8]);
        write_ascii_string_pgm(4, 12, menu[9]);

    config_menu_drawings(chosen);

    while (inmenu) {
        if (s2_pressed()) {
            write_ascii_string(3, chosen + 3, " ");
            chosen = (chosen + 1) % 10;
            write_ascii_string(3, chosen + 3, ">");
            _delay_ms(500);
        } else if (s1_pressed()) {
            switch (chosen) {
                case 0: // full HUD
                    COSD_FLAGS ^= COSD_FLAG_HUD;
                    config_menu_drawings(chosen);
                    break;
                case 1: // art horizon
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
                    config_menu_drawings(chosen);
                    break;
                case 2: // big vario
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
                    config_menu_drawings(chosen);
                    break;
                case 3: // statistics
                    COSD_FLAGS ^= COSD_FLAG_STATS;
                    config_menu_drawings(chosen);
                    break;
                case 4: // warnings
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
                    config_menu_drawings(chosen);
                    break;
                case 5: // reset uptime
                    uptime = 0;
                    config_menu_doclick(chosen, menu);
                    break;
                case 6: // re-request OSD data
#if FCONLY
                    // request data ever 100ms from FC;
                    usart1_request_mk_data(0, 'd', 100);
#else
                    // request OSD Data from NC every 100ms
                    usart1_request_mk_data(1, 'o', 100);

                    // and disable debug...
                    usart1_request_mk_data(0, 'd', 0);
#endif
                    config_menu_doclick(chosen, menu);
                    break;
                case 7: // disable debug data
                    // disable sending of debug data
                    // may result in smoother ddata display
                    usart1_request_mk_data(0, 'd', 0);
                    config_menu_doclick(chosen, menu);
                    break;
                case 8: // save
                    save_eeprom();
                    config_menu_doclick(chosen, menu);
                    break;
                case 9: // exit
                    inmenu = 0;
                    break;
            }
            _delay_ms(250);
        }
    }

    // clear screen up again
    clear();

    // update flags to paint display again if needed
    COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;

    // enable interrupts again
    sei();
}

#endif