Subversion Repositories Code-Repo

Rev

Rev 147 | Blame | Last modification | View Log | RSS feed

#include "led_HT16K33.h"
#include "i2c.h"

static const char numbertable[] = {
    0x3F /* 0 */,
    0x06 /* 1 */,
    0x5B /* 2 */,
    0x4F /* 3 */,
    0x66 /* 4 */,
    0x6D /* 5 */,
    0x7D, /* 6 */
    0x07, /* 7 */
    0x7F, /* 8 */
    0x6F, /* 9 */
};

static const char alphatable[] = {
    0x77, /* a */
    0x7C, /* b */
    0x39, /* C */
    0x5E, /* d */
    0x79, /* E */
    0x71, /* F */
};

static LED_DATA led_data;
static LED_DATA *led_data_p = &led_data;

void LED_Init() {
    led_data_p->i2c_address = 0x70;
}

void LED_Start() {
    unsigned char result;
    unsigned char c = 0x21;     // Cmd to turn on oscillator
    
    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
    result = I2C_Get_Status();
    while (!result) {
        result = I2C_Get_Status();
    }

    LED_Blink_Rate(HT16K33_BLINK_OFF);
    LED_Set_Brightness(15);  // Max brightness
    LED_Clear();
    LED_Write_Display();
}

void LED_Set_Brightness(unsigned char c) {
    unsigned char result;
    
    if (c > 15) c = 15;
    c |= 0xE0;

    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
    result = I2C_Get_Status();
    while (!result) {
        result = I2C_Get_Status();
    }
}

void LED_Blink_Rate(unsigned char c) {
    unsigned char buffer;

    if (c > 3) c = 0;

    buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (c << 1);

    I2C_Master_Send(led_data_p->i2c_address, 1, &buffer);
    buffer = I2C_Get_Status();
    while (!buffer) {
        buffer = I2C_Get_Status();
    }
}

void LED_Write_Display() {
    unsigned char result;
    
    led_data_p->display_buffer[0] = 0x00;  // Start at address 0x00
    I2C_Master_Send(led_data_p->i2c_address, 17, led_data_p->display_buffer);

    result = I2C_Get_Status();
    while (!result) {
        result = I2C_Get_Status();
    }
}

void LED_Clear() {
    unsigned char c;
    for (c = 0; c < 17; c++) {
        led_data_p->display_buffer[c] = 0;
    }
}

void LED_Draw_Colon(unsigned char c) {
    if (c) {
        led_data_p->display_buffer[5] = 0xFF;
    } else {
        led_data_p->display_buffer[5] = 0;
    }
}

void LED_Write_Digit_Raw(unsigned char loc, unsigned char bitmask) {
    if (loc > 4) return;
    led_data_p->display_buffer[(loc<<1)+1] = bitmask;
}

void LED_Write_Digit_Num(unsigned char loc, unsigned char num, unsigned char dot) {
    if (loc > 4) return;
    if (loc > 1) loc++;
    LED_Write_Digit_Raw(loc, numbertable[num] | dot << 7);
}

void LED_Write_Digit_Alpha(unsigned char loc, unsigned char alpha, unsigned char dot) {
    if (loc > 4) return;
    if (loc > 1) loc++;
    LED_Write_Digit_Raw(loc, alphatable[alpha] | dot << 7);
}

void LED_Write_Num(unsigned int i) {
    LED_Write_Digit_Num(0, (i%10000)/1000, 0);
    LED_Write_Digit_Num(1, (i%1000)/100, 0);
    LED_Write_Digit_Num(2, (i%100)/10, 0);
    LED_Write_Digit_Num(3, i%10, 0);

    if (i < 10) {
        LED_Write_Digit_Raw(0, 0);
        LED_Write_Digit_Raw(1, 0);
        LED_Write_Digit_Raw(3, 0);
    } else if (i < 100) {
        LED_Write_Digit_Raw(0, 0);
        LED_Write_Digit_Raw(1, 0);
    } else if (i < 1000) {
        LED_Write_Digit_Raw(0, 0);
    }
    LED_Write_Display();
}