Subversion Repositories Code-Repo

Rev

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_blinkRate(HT16K33_BLINK_OFF);
    LED_setBrightness(15);  // Max brightness
    LED_clear();
    LED_writeDisplay();
}

void LED_setBrightness(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_blinkRate(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_writeDisplay() {
    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_drawColon(unsigned char c) {
    if (c) {
        led_data_p->display_buffer[5] = 0xFF;
    } else {
        led_data_p->display_buffer[5] = 0;
    }
}

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

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

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

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

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