Subversion Repositories Projects

Rev

Rev 389 | Rev 489 | 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/pgmspace.h>
#include "osd_helpers.h"
#include "max7456_software_spi.h"

#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
/* ##########################################################################
 * compass stuff
 * ##########################################################################*/


/**
 * convert the <heading> gotton from NC into an index
 */

uint8_t heading_conv(uint16_t heading) {
    if (heading > 23 && heading < 68) {
        //direction = "NE";
        return 0;
    } else if (heading > 67 && heading < 113) {
        //direction = "E ";
        return 1;
    } else if (heading > 112 && heading < 158) {
        //direction = "SE";
        return 2;
    } else if (heading > 157 && heading < 203) {
        //direction = "S ";
        return 3;
    } else if (heading > 202 && heading < 248) {
        //direction = "SW";
        return 4;
    } else if (heading > 247 && heading < 293) {
        //direction = "W ";
        return 5;
    } else if (heading > 292 && heading < 338) {
        //direction = "NW";
        return 6;
    }
    //direction = "N ";
    return 7;
}

/**
 * convert the <heading> gotton from NC into a more
 * precise index
 */

uint8_t heading_fine_conv(uint16_t heading) {
        heading = ((heading * 10) + 113) % 3600;
        return (heading / 225);
}

/**
 * draw a compass rose at <x>/<y> for <heading>
 */

void draw_compass(uint8_t x, uint8_t y, uint16_t heading) {
    //char* rose = "---N---O---S---W---N---O---S---W---N---O---S---W";
    char rose[48] = {216, 215, 216, 211, 216, 215, 216, 213, 216, 215, 216, 212,
                    216, 215, 216, 214, 216, 215, 216, 211, 216, 215, 216, 213,
                    216, 215, 216, 212, 216, 215, 216, 214, 216, 215, 216, 211,
                    216, 215, 216, 213, 216, 215, 216, 212, 216, 215, 216, 214};
        // the center is char 19 (north), we add the current heading in 8th
        // which would be 22.5 degrees, but float would bloat up the code
        // and *10 / 225 would take ages... so we take the uncorrect way
    uint8_t front = 19 + (heading / 22);
    for (uint8_t i = 0; i < 9; i++) {
                write_char_xy(x++, y, rose[front - 4 + i]);
    }
}

/* ##########################################################################
 * battery index
 * ##########################################################################*/

/**
 * draw a battery symbol at <x>/<y> according to <voltage>
 */

void draw_battery(uint8_t x, uint8_t y, uint16_t voltage) {
        uint8_t percent = (100* (voltage - UBAT_WRN) / (UBAT_MAX - UBAT_WRN));
        if (percent > 100) percent = 100;
        if (voltage < UBAT_WRN) percent = 0;
        write_char_xy(x, y, 0x9d - (percent * 13 / 100));
        //write_ndigit_number_u(x, y-1, percent * 13 / 100, 100, 0);
}

/* ##########################################################################
 * variometer
 * ##########################################################################*/

/**
 * draw variometer arrows at <x>/<y> according to <variometer>
 */

void draw_variometer(uint8_t x, uint8_t y, int16_t variometer) {
        if (variometer == 0) {
                write_char_xy(x, y, 0xbb); // plain line
        } else if (variometer > 0) { // gain height
                switch (variometer / 5){
                        case 0:
                                //write_char_xy(x, y, 0xba); // smallest arrow up
                                write_char_xy(x, y, 0x70); // one arrow up
                                break;
                        case 1:
                                //write_char_xy(x, y, 0xb9); // small arrow up
                                write_char_xy(x, y, 0x71); // two arrows up
                                break;
                        case 2:
                                //write_char_xy(x, y, 0xb8); // large arrow up
                                write_char_xy(x, y, 0x72); // three arrows up
                                break;
                        default:
                                //write_char_xy(x, y, 0xb7); // largest arrow up
                                write_char_xy(x, y, 0x73); // three black arrows up
                }
        } else { // sink
                switch (variometer / -5){
                        case 0:
                                //write_char_xy(x, y, 0xbc); // smallest arrow down
                                write_char_xy(x, y, 0x77); // one arrow down
                                break;
                        case 1:
                                //write_char_xy(x, y, 0xbd); // small arrow down
                                write_char_xy(x, y, 0x76); // two arrows down
                                break;
                        case 2:
                                //write_char_xy(x, y, 0xbe); // large arrow down
                                write_char_xy(x, y, 0x75); // three arrows down
                                break;
                        default:
                                //write_char_xy(x, y, 0xbf); // largest arrow down
                                write_char_xy(x, y, 0x74); // three black arrows down
                }
        }
}

// big vario arrays
char vario_00[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xff};
char vario_01[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xc0};
char vario_02[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xc1};
char vario_03[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0x00};
char vario_04[5] PROGMEM = {0x00, 0x00, 0xc2, 0xc0, 0x00};
char vario_05[5] PROGMEM = {0x00, 0x00, 0xc2, 0xc1, 0x00};
char vario_06[5] PROGMEM = {0x00, 0x00, 0xc2, 0x00, 0x00};
char vario_07[5] PROGMEM = {0x00, 0x00, 0xbb, 0x00, 0x00};
char vario_08[5] PROGMEM = {0x00, 0x00, 0xc3, 0x00, 0x00};
char vario_09[5] PROGMEM = {0x00, 0xc4, 0xc3, 0x00, 0x00};
char vario_10[5] PROGMEM = {0x00, 0xc5, 0xc3, 0x00, 0x00};
char vario_11[5] PROGMEM = {0x00, 0xff, 0xc3, 0x00, 0x00};
char vario_12[5] PROGMEM = {0xc4, 0xff, 0xc3, 0x00, 0x00};
char vario_13[5] PROGMEM = {0xc5, 0xff, 0xc3, 0x00, 0x00};
char vario_14[5] PROGMEM = {0xff, 0xff, 0xc3, 0x00, 0x00};
char* vario_pnt[15] PROGMEM = {vario_00, vario_01, vario_02, vario_03, vario_04,
                                vario_05, vario_06, vario_07, vario_08,
                                vario_09, vario_10, vario_11, vario_12,
                                vario_13, vario_14};

/**
 * draw a bigger vario with middle at <x>/<y> acording to <variometer>
 */

void draw_big_variometer(uint8_t x, uint8_t y, int16_t variometer) {   
        int16_t index = 7 + variometer;
        if (index > 14) index = 14;
        else if (index < 0) index = 0;
       
        // TODO: why does write_string_pgm_down(x, y-2, vario_pnt[index], 5);
        // not work??? WTF?!
        switch (index) {
                case 0: write_string_pgm_down(x, y-2, vario_pnt[0], 5);
                        break;
                case 1: write_string_pgm_down(x, y-2, vario_pnt[1], 5);
                        break;
                case 2: write_string_pgm_down(x, y-2, vario_pnt[2], 5);
                        break;
                case 3: write_string_pgm_down(x, y-2, vario_pnt[3], 5);
                        break;
                case 4: write_string_pgm_down(x, y-2, vario_pnt[4], 5);
                        break;
                case 5: write_string_pgm_down(x, y-2, vario_pnt[5], 5);
                        break;
                case 6: write_string_pgm_down(x, y-2, vario_pnt[6], 5);
                        break;
                case 7: write_string_pgm_down(x, y-2, vario_pnt[7], 5);
                        break;
                case 8: write_string_pgm_down(x, y-2, vario_pnt[8], 5);
                        break;
                case 9: write_string_pgm_down(x, y-2, vario_pnt[9], 5);
                        break;
                case 10: write_string_pgm_down(x, y-2, vario_pnt[10], 5);
                        break;
                case 11: write_string_pgm_down(x, y-2, vario_pnt[11], 5);
                        break;
                case 12: write_string_pgm_down(x, y-2, vario_pnt[12], 5);
                        break;
                case 13: write_string_pgm_down(x, y-2, vario_pnt[13], 5);
                        break;
                default: write_string_pgm_down(x, y-2, vario_pnt[14], 5);
        }              
}

/* ##########################################################################
 * artificial horizon
 * ##########################################################################*/

// remember last time displayed values
int8_t old_af_x = -1, old_af_y = -1;

/**
 * draw roll und nick indicators (could be enhanced to full artificial horizon)
 * from line <firstline> to <listlines> for given <nick> and <roll> values
 */

void draw_artificial_horizon(uint8_t firstline, uint8_t lastline, int16_t nick, int16_t roll) {
        char noodle[5] = {225, 225, 226, 227, 227};
        uint8_t center_x = 15;
        uint8_t center_y = lastline - firstline;
        center_y = 7;
        write_char_xy(center_x,center_y,228);
        uint8_t cpos, nicky, rollx;
       
        // which line
        int8_t ypos =  nick / 20;
        // which character from the array?
        if (nick < 0) {
                cpos = -1*((nick - (ypos * 20))/4);
                ypos--;
        } else cpos = 4-((nick - (ypos * 20))/4);
        if (cpos > 4) cpos = 4;

        nicky = center_y - ypos;
        if (nicky > lastline) nicky = lastline;
        else if (nicky < firstline) nicky = firstline;

        // ensure roll-borders
        rollx = (roll / 8)+15;
        if (rollx < 2) rollx = 2;
        else if (rollx > 28) rollx = 28;


        // clear roll
        if (old_af_x != rollx && old_af_x >= 0) {
                write_char_xy(old_af_x, lastline, 0);
        }

        // clear nick
        if (old_af_y != nicky && old_af_y >= 0) {
                write_char_xy(center_x-1, old_af_y, 0);
                write_char_xy(center_x+1, old_af_y, 0);
        }


        // draw nick
        write_char_xy(center_x-1, nicky, noodle[cpos]);
        write_char_xy(center_x+1, nicky, noodle[cpos]);

        // draw roll
        write_char_xy(rollx, lastline, 229);

        // update old vars
        old_af_x = rollx;
        old_af_y = nicky;

        // debug numbers
        //write_3digit_number_u(20,6,cpos);
        //write_number_s(20,7,ypos);   
        //write_number_s(0,7,nick);            
        //write_number_s(18,11,roll);  
}

#endif