Subversion Repositories Projects

Rev

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

/****************************************************************************
 *   Copyright (C) 2009-2017 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 "main.h"
#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";
    static const char rose[] PROGMEM = {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 % 360) / 22);
    for (uint8_t i = 0; i < 9; i++) {
        write_char_xy(x++, y, pgm_read_byte(&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, uint8_t min_voltage, uint8_t voltage, uint8_t max_voltage) {
    uint8_t percent = (100 * (voltage - min_voltage) / (max_voltage - min_voltage));
    if (percent > 100) percent = 100;
    if (voltage < min_voltage) percent = 0;
    write_char_xy(x, y, 0x9d - (percent * 13 / 100));
    //write_ndigit_number_u(x, y-1, percent * 13 / 100, 100, 0);
}



/* ##########################################################################
 * draw logo
 * ##########################################################################*/


/**
 * draw the logo at <x>/<y>
 */

void draw_logo(uint8_t x, uint8_t y) {
    uint8_t chr = 0x50;

    for (uint8_t yy = y; yy < y + 2; yy++) {
        for (uint8_t xx = x; xx < x + 8; xx++) {
            write_char_xy(xx, yy, chr++);
        }
    }
}

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


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

void draw_variometer(uint8_t x, uint8_t y, int16_t variometer) {
    uint8_t chr = 0xbb;
    if (variometer > 0) { // gain height
        chr = 0x70 + (variometer / 5);
        if (chr > 0x73) chr = 0x73;
    } else { // sink
        chr = 0x77 - (variometer / -5);
        if (chr < 0x74) chr = 0x74;
    }
    write_char_xy(x, y, chr);
}


// big vario array
const char vario_pnt[15][5] PROGMEM = {
        {0x00, 0x00, 0xc2, 0xff, 0xff},
        {0x00, 0x00, 0xc2, 0xff, 0xc0},
        {0x00, 0x00, 0xc2, 0xff, 0xc1},
        {0x00, 0x00, 0xc2, 0xff, 0x00},
        {0x00, 0x00, 0xc2, 0xc0, 0x00},
        {0x00, 0x00, 0xc2, 0xc1, 0x00},
        {0x00, 0x00, 0xc2, 0x00, 0x00},
        {0x00, 0x00, 0xbb, 0x00, 0x00},
        {0x00, 0x00, 0xc3, 0x00, 0x00},
        {0x00, 0xc4, 0xc3, 0x00, 0x00},
        {0x00, 0xc5, 0xc3, 0x00, 0x00},
        {0x00, 0xff, 0xc3, 0x00, 0x00},
        {0xc4, 0xff, 0xc3, 0x00, 0x00},
        {0xc5, 0xff, 0xc3, 0x00, 0x00},
        {0xff, 0xff, 0xc3, 0x00, 0x00}
};

/**
 * 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;

    write_string_pgm_down(x, y - 2, vario_pnt[index], 5);
}


/* ##########################################################################
 * NEW artificial horizon     By AGRESSiVA --=-- COPTERTRONiC
 * ##########################################################################*/

// draw routine

int draw_noodles(int8_t pos_x, int8_t pos_y, int8_t num, int8_t old_num) {
    const char noodle[5] = {0x78, 0x79, 0x7A, 0x7B, 0x7C};
    int8_t line, car;

    line = num / 5;
    car = num - (line * 5);
    if (num != old_num) {
        write_char_xy(15 - pos_x, pos_y + (old_num), 0);
    }

    if (num < 0) {
        car = -1 * car;
        car = 4 - car;
        line--;
        num = num - 5;
    }
    write_char_xy(15 - pos_x, pos_y + line, noodle[car]);

    return line;
}

/**
 * calculate the rails of artificial horizon
 * receive <nick> and <roll> values
 */

void draw_agressiva_artificial_horizon(uint8_t firstline, uint8_t lastline, int16_t nick, int16_t roll) {
    static int8_t old_ticy = 1, old_ticx = 0;
    static int8_t old1 = 0, old2 = 0, old3 = 0, old4, old5 = 0, old6 = 0, old7 = 0, old8 = 0;
    int8_t ticy = 0, ticx = 0;
    uint8_t center_x = 15;
    uint8_t center_y = lastline - firstline;
    center_y = 7;

#ifndef INDICATORARSLEVEL
        // original indicator bars
    write_char_xy(center_x - 7, center_y, 226); // left bar
    write_char_xy(center_x + 6, center_y, 226); // right bar
#else
        // indicator bars on same level as horizon bars
    write_char_xy(center_x - 7, center_y, 0x78); // left bar
    write_char_xy(center_x + 6, center_y, 0x78); // right bar
#endif


#if FCONLY
    ticy = -1 * (roll / 10); // rescale roll from FC debug data
    ticx = -1 * (nick * 10); // rescale nick from FC debug data
#else
    ticy = -1 * (roll / 2);
    ticx = -1 * (nick / 1);
#endif

    if (ticy >= 30) ticy = 30; // limit y
    if (ticx >= 30) ticx = 30; // limit x

    //write_ndigit_number_u (0 , 5 , ticy ,3, 1);
    //write_ndigit_number_u (0 , 6 , ticx ,3, 1);

    //if ((ticy != old_ticy) || (ticx != old_ticx)) {
    old1 = draw_noodles(4, 3, (ticy / 2) + 20 + ticx, old1);
    old2 = draw_noodles(3, 3, (ticy / 3) + 20 + ticx, old2);
    old3 = draw_noodles(2, 3, (ticy / 6) + 20 + ticx, old3);
    old4 = draw_noodles(1, 3, (ticy / 14) + 20 + ticx, old4);
    old5 = draw_noodles(-3, 3, -(ticy / 2) + 20 + ticx, old5);
    old6 = draw_noodles(-2, 3, -(ticy / 3) + 20 + ticx, old6);
    old7 = draw_noodles(-1, 3, -(ticy / 6) + 20 + ticx, old7);
    old8 = draw_noodles(0, 3, -(ticy / 14) + 20 + ticx, old8);
    //}
    // update old vars
    old_ticy = ticy;
    old_ticx = ticx;
}


/* ##########################################################################
 * OLD 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) {
    const 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;
}

/**
 * draw scope of a second camera
 */

void draw_scope() {
    /*
        write_char_xy(scope[0], scope[1], 0xEC);
        write_char_xy(scope[2], scope[3], 0xED);
        write_char_xy(scope[4], scope[5], 0xEE);
        write_char_xy(scope[6], scope[7], 0xEF);
     */

    // save 20 byte :)
    for (int i = 0; i < 4; i++) {
        write_char_xy(
            scope[i * 3],
            scope[(i * 3) + 1],
            0xEC + i + (scope[(i * 3) + 2] * 4));
    }
}

/**
 * draw stats
 */

void draw_stats() {
#if FCONLY
#else
    uint8_t line = 3;
    write_ascii_string_pgm(1, line,   PSTR("max Alt :")); // max Altitude
    write_ascii_string_pgm(1, ++line, PSTR("max Spd :")); // max Speed
    write_ascii_string_pgm(1, ++line, PSTR("max Dist:")); // max Distance

     draw_logo(19, 4);

    if (COSD_FLAGS_CONFIG & COSD_FLAG_FEET) {
        write_ndigit_number_s(13, line - 2, max_Altimeter * 32 / 10, 4, 0);
        write_char_xy(17, line - 2, 0x7E); // small feet ft
        write_ndigit_number_u(14, line - 1, (uint16_t)(((uint32_t)max_GroundSpeed * (uint32_t)279) / (uint32_t)12500), 3, 0);
        write_char_xy(17, line - 1, 0x7D); // mp/h
        write_ndigit_number_u(13, line - 0, max_Distance / 10 * 32 / 10, 4, 0);
        write_char_xy(17, line - 0, 0x7E); // small feet ft
    } else {
        write_ndigit_number_s(13, line - 2, max_Altimeter, 4, 0);
        write_char_xy(17, line - 2, 204); // small meters m
        write_ndigit_number_u(14, line - 1, (uint16_t)(((uint32_t)max_GroundSpeed * (uint32_t)9) / (uint32_t)250), 3, 0);
        write_char_xy(17, line - 1, 203); // km/h
        write_ndigit_number_u(13, line - 0, max_Distance / 10, 4, 0);
        write_char_xy(17, line - 0, 204); // small meters m
    }

    write_ascii_string_pgm(1, ++line,     PSTR("min Volt:")); // min voltage
    write_ndigit_number_u_10th(13, line, min_UBat, 3, 0);
    write_char_xy(17, line, 0x9E); // small V
    if ((COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC) || (COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT)) {
        write_ascii_string_pgm(1, ++line, PSTR("max curr:")); // ampere
        write_ndigit_number_u_10th(13, line, max_ampere / 10, 3, 0);
        write_char_xy(17, line, 0x9F); // small A

        // wasted mampere in this flight (will count up after landing)
        if ((COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC) && !(COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT)) {
            write_ndigit_number_u(18, line, (ampere_wasted / 10) - wasted_ampere_offset, 5, 0);
        } else if (COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT) {

            write_ndigit_number_u(18, line, naviData.UsedCapacity - wasted_ampere_offset, 5, 0);
        }

        write_char_xy(23, line, 0xB5); // mah
    }
    write_ascii_string_pgm(1, ++line, PSTR("max Time:")); // max time
    write_time(11, line, max_FlyingTime);
    write_char_xy(17, line, 210); // fly clock
    write_ascii_string_pgm(1, ++line, PSTR("long    :")); // longitude
    write_gps_pos(11, line, naviData.CurrentPosition.Longitude);
    write_ascii_string_pgm(1, ++line, PSTR("lat     :")); // latitude
    write_gps_pos(11, line, naviData.CurrentPosition.Latitude);
#endif
}

#endif