Subversion Repositories Projects

Rev

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

/****************************************************************************
 *   Copyright (C) 2009-2010 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/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdlib.h>
#include "max7456_software_spi.h"


char conv_array[7]; // general array for number -> char conversation

int pow(int a, int b) {
    if (b <= 0) return 1;
    int res = 1;
    while (b-- > 0) res *= a;
    return res;
}

/* ##########################################################################
 * MAX7456 SPI & Display stuff
 * ##########################################################################*/


/**
 * Send a byte through SPI
 * inline because it increases the codesize currenlty 'only' by 110byte but saves
 * the context-change on every char and attribute which is at least done twice
 * (address and data byte), a significant speed-bost we do not want to miss :)
 */

inline void spi_send(uint8_t byte) {
    for (int8_t i = 7; i >= 0; i--) {
        if (((byte >> i) & 1)) {
            MAX_SDIN_HIGH
        } else {
            MAX_SDIN_LOW
        }
        MAX_SCLK_HIGH
        MAX_SCLK_LOW
    }
}

/**
 *  Send <byte> to <address> of MAX7456
 */

void spi_send_byte(uint8_t address, uint8_t byte) {
    // start sending
    MAX_CS_LOW

    spi_send(address);
    spi_send(byte);

    // end sending
    MAX_CS_HIGH
}

/**
 *  write a <character> to <address> of MAX7456 display memory
 */

void write_char(uint16_t address, char character) {
    spi_send_byte(0x05, (address & 0xFF00) >> 8); // DMAH
    spi_send_byte(0x06, (address & 0x00FF)); // DMAL
    spi_send_byte(0x07, character); // DMDI
}

/**
 *  clear display memory
 *  (also sets 8bit mode)
 */

void clear(void) {
    /*uint16_t memory_address = 0;
    for (unsigned int a = 0; a < 480; a++) {
        write_char(memory_address++, 0);
    }*/

    // clear all display-mem (DMM)
    spi_send_byte(0x04, 0b01000100);

    // clearing takes 12uS according to maxim so lets wait longer
    _delay_us(20);
}


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

/**
 * for testing write all chars to screen
 */

void write_all_chars() {
    uint16_t x = 3, y = 2, t = 0;
    while (t < 256) {
        write_char(x++ + y * 30, t++);
        if (x > 25) {
            y++;
            x = 3;
        }
    }
}

/**
 * let the MAX7456 learn a new character at <number>
 * with <data>.
 */

void learn_char(uint8_t number, unsigned char* data) {
    // select character to write (CMAH)
    spi_send_byte(0x09, number);

    for (uint8_t i = 0; i < 54; i++) {
        // select 4pixel byte of char (CMAL)
        spi_send_byte(0x0A, i);

        // write 4pixel byte of char (CMDI)
        spi_send_byte(0x0B, data[i]);
    }

    // write to the NVM array from the shadow RAM (CMM)
    spi_send_byte(0x08, 0b10100000);

    // according to maxim writing to nvram takes about 12ms, lets wait longer
    _delay_ms(120);
}
#endif

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

/**
 *  write a character <attribute> to <address> of MAX7456 display memory
 */

void write_char_att(uint16_t address, char attribute) {
    // the only important part is that the DMAH[1] is set
    // so we add 2 which binary is the 2nd lowest byte
    spi_send_byte(0x05, ((address & 0xFF00) >> 8) | 2); // DMAH
    spi_send_byte(0x06, (address & 0x00FF)); // DMAL
    spi_send_byte(0x07, attribute); // DMDI
}

/**
 *  write a <character> at <x>/<y> to MAX7456 display memory
 */

void write_char_xy(uint8_t x, uint8_t y, char character) {
    uint16_t address = y * 30 + x;
    write_char(address, character);
}

/**
 *  write a  character <attribute> at <x>/<y> to MAX7456 display memory
 */

void write_char_att_xy(uint8_t x, uint8_t y, char attribute) {
    uint16_t address = y * 30 + x;
    write_char_att(address, attribute);
}

/**
 *  write an ascii <character> to <address> of MAX7456 display memory
 */

void write_ascii_char(uint16_t address, char c) {
    if (c == 32) c = 0; // remap space
    else if (c > 48 && c <= 57) c -= 48; // remap numbers
    else if (c == '0') c = 10; // remap zero
    else if (c >= 65 && c <= 90) c -= 54; // remap big letters
    else if (c >= 97 && c <= 122) c -= 60; // remap small letters
    else if (c == '(') c = 63; // remap
    else if (c == ')') c = 64; // remap
    else if (c == '.') c = 65; // remap
    else if (c == '-') c = 73; // remap minus
    else if (c == ';') c = 67; // remap
    else if (c == ':') c = 68; // remap
    else if (c == ',') c = 69; // remap
        //    else if (c == '?') c = 66; // remap
        //    else if (c == '\'') c = 70; // remap
    else if (c == '/') c = 71; // remap
    //    else if (c == '"') c = 72; // remap
    //    else if (c == '<') c = 74; // remap
    //    else if (c == '>') c = 75; // remap
    //    else if (c == '@') c = 76; // remap
    write_char(address, c);
}

/**
 *  write an ascii <string> at <x>/<y> to MAX7456 display memory
 */

void write_ascii_string(uint8_t x, uint8_t y, char *string) {
    while (*string) {
        write_ascii_char(((x++)+(y * 30)), *string);
        string++;
    }
}

/**
 *  write an ascii <string> with lenght <len> at <x>/<y> to MAX7456 display memory
 */

void write_ascii_string_len(uint8_t x, uint8_t y, char *string, uint8_t len) {
    while (len--) {
        write_ascii_char(((x++)+(y * 30)), *string);
        string++;
    }
}

/**
 *  write an ascii <string> from progmen at <x>/<y> to MAX7456 display memory
 */

void write_ascii_string_pgm(uint8_t x, uint8_t y, const char *string) {
    while (pgm_read_byte(string) != 0x00)
        write_ascii_char(((x++)+(y * 30)), pgm_read_byte(string++));
}

/**
 *  write an <string> from progmen at <x>/<y> downwards to MAX7456 display memory
 */

void write_string_pgm_down(uint8_t x, uint8_t y, const char *string, uint8_t length) {
    while (length--)
        write_char((x + (y++ * 30)), pgm_read_byte(string++));
}

/**
 * Write a unsigned <number> at <x>/<y> to MAX7456 display memory
 * <length> represents the length to rightbound the number
 * <pad> = 1 will cause blank spaced to be filled up with zeros e.g. 007 instead of   7
 */

void write_ndigit_number_u(uint8_t x, uint8_t y, uint16_t number, int16_t length, uint8_t pad) {
    if (number >= pow(10, length)) {
        number = pow(10, length) - 1;
    }
    itoa(number, conv_array, 10);
    for (uint8_t i = 0; i < length - strlen(conv_array); i++) {
        if (pad) write_char((x++)+(y * 30), 10);
        else write_ascii_char((x++)+(y * 30), 0);
    }
    write_ascii_string(x, y, conv_array);
}

/**
 * Write a signed <number> at <x>/<y> to MAX7456 display memory
 * <length> represents the length to rightbound the number
 * <pad> = 1 will cause blank spaced to be filled up with zeros e.g. 007 instead of   7
 */

void write_ndigit_number_s(uint8_t x, uint8_t y, int16_t number, int16_t length, uint8_t pad) {
    if (number <= -pow(10, length)) {
        number = -pow(10, length) + 1;
    } else if (number >= pow(10, length)) {
        number = pow(10, length) - 1;
    }

    itoa(number, conv_array, 10);
    if (strlen(conv_array) < length) {
        for (uint8_t i = 0; i < length - strlen(conv_array); i++) {
            if (pad) write_char((x++)+(y * 30), 10);
            else write_ascii_char((x++)+(y * 30), 0);
        }
    }
    write_ascii_string(x, y, conv_array);
}

/**
 * Write a unsigned <number> at <x>/<y> to MAX7456 display memory as /10th of value
 * <length> represents the length to rightbound the number
 * <pad> = 1 will cause blank spaced to be filled up with zeros e.g. 007 instead of   7
 */

void write_ndigit_number_u_10th(uint8_t x, uint8_t y, uint16_t number, int16_t length, uint8_t pad) {
    if (number >= pow(10, length)) {
        number = pow(10, length) - 1;
    }
    itoa(number, conv_array, 10);
    uint8_t len = strlen(conv_array);
    for (uint8_t i = 0; i < length - len; i++) {
        if (pad) write_char((x++)+(y * 30), 10); //  zero
        else write_char((x++)+(y * 30), 0); // blank
    }
    char rest = conv_array[len - 1];
    conv_array[len - 1] = 0;
    if (len == 1) {
        write_char((x - 1)+(y * 30), 10); // zero
    } else if (len == 2 && conv_array[0] == '-') {
        write_char((x - 1)+(y * 30), 0x49); // minus
        write_char((x)+(y * 30), 10); // zero
    } else {
        write_ascii_string(x, y, conv_array);
    }
    x += len - 1;
    write_char((x++)+(y * 30), 65); // decimal point
    write_ascii_char((x++)+(y * 30), rest); // after dot
}

/**
 * Write a signed <number> at <x>/<y> to MAX7456 display memory as /10th of value
 * <length> represents the length to rightbound the number
 * <pad> = 1 will cause blank spaced to be filled up with zeros e.g. 007 instead of   7
 */

void write_ndigit_number_s_10th(uint8_t x, uint8_t y, int16_t number, int16_t length, uint8_t pad) {
    if (number >= pow(10, length)) {
        number = pow(10, length) - 1;
    }
    itoa(number, conv_array, 10);
    uint8_t len = strlen(conv_array);
    for (uint8_t i = 0; i < length - len; i++) {
        if (pad) write_char((x++)+(y * 30), 10); //  zero
        else write_char((x++)+(y * 30), 0); // blank
    }
    char rest = conv_array[len - 1];
    conv_array[len - 1] = 0;
    if (len == 1) {
        write_char((x - 1)+(y * 30), 10); // zero
    } else if (len == 2 && conv_array[0] == '-') {
        write_char((x - 1)+(y * 30), 0x49); // minus
        write_char((x)+(y * 30), 10); // zero
    } else {
        write_ascii_string(x, y, conv_array);
    }
    x += len - 1;
    write_char((x++)+(y * 30), 65); // decimal point
    write_ascii_char((x++)+(y * 30), rest); // after dot
}

/**
 *  write <seconds> as human readable time at <x>/<y> to MAX7456 display mem
 */

void write_time(uint8_t x, uint8_t y, uint16_t seconds) {
    uint16_t min = seconds / 60;
    seconds -= min * 60;
    write_ndigit_number_u(x, y, min, 3, 0);
    write_char_xy(x + 3, y, 68);
    write_ndigit_number_u(x + 4, y, seconds, 2, 1);
}

/**
 * wirte a <position> at <x>/<y> assuming it is a gps position for long-/latitude
 */

void write_gps_pos(uint8_t x, uint8_t y, int32_t position) {
    if (position < 0) {
        position ^= ~0;
        position++;
        write_char_xy(x++, y, 0x49); // minus
    } else {
        write_char_xy(x++, y, 0); // clear ('+' would be nice, maybe later)
    }
    write_ndigit_number_u(x, y, (uint16_t)(position / (int32_t)10000000), 3, 1);
    write_char_xy(x + 3, y, 65); // decimal point
    position = position - ((position / (int32_t)10000000) * (int32_t)10000000);
    write_ndigit_number_u(x + 4, y, (uint16_t)(position / (int32_t)1000), 4, 1);
    position = position - ((uint16_t)(position / (int32_t)1000) * (int32_t)1000);
    write_ndigit_number_u(x + 8, y, (uint16_t)position, 3, 1);
    write_char_xy(x + 11, y, 0xD0); // degree symbol
}

#endif