Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
121 Kevin 1
#include "led_backpack.h"
2
#include "i2c.h"
3
 
4
static const char numbertable[] = {
5
    0x3F /* 0 */,
6
    0x06 /* 1 */,
7
    0x5B /* 2 */,
8
    0x4F /* 3 */,
9
    0x66 /* 4 */,
10
    0x6D /* 5 */,
11
    0x7D, /* 6 */
12
    0x07, /* 7 */
13
    0x7F, /* 8 */
14
    0x6F, /* 9 */
15
};
16
 
17
static const char alphatable[] = {
18
    0x77, /* a */
19
    0x7C, /* b */
20
    0x39, /* C */
21
    0x5E, /* d */
22
    0x79, /* E */
23
    0x71, /* F */
24
};
25
 
26
static LED_DATA led_data;
27
 
28
void LED_Init() {
29
    led_data.i2c_address = 0x70;
30
}
31
 
32
void LED_Start() {
33
    unsigned char result;
34
    unsigned char c = 0x21;     // Cmd to turn on oscillator
35
 
36
    I2C_Master_Send(led_data.i2c_address, 1, &c);
37
    result = I2C_Get_Status();
38
    while (!result) {
39
        result = I2C_Get_Status();
40
    }
41
 
42
    LED_blinkRate(HT16K33_BLINK_OFF);
43
    LED_setBrightness(15);  // Max brightness
44
    LED_clear();
45
}
46
 
47
void LED_setBrightness(unsigned char c) {
48
    unsigned char result;
49
 
50
    if (c > 15) c = 15;
51
    c |= 0xE0;
52
 
53
    I2C_Master_Send(led_data.i2c_address, 1, &c);
54
    result = I2C_Get_Status();
55
    while (!result) {
56
        result = I2C_Get_Status();
57
    }
58
}
59
 
60
void LED_blinkRate(unsigned char c) {
61
    unsigned char buffer;
62
 
63
    if (c > 3) c = 0;
64
 
65
    buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (c << 1);
66
 
67
    I2C_Master_Send(led_data.i2c_address, 1, &buffer);
68
    buffer = I2C_Get_Status();
69
    while (!buffer) {
70
        buffer = I2C_Get_Status();
71
    }
72
}
73
 
74
void LED_writeDisplay() {
75
    unsigned char result;
76
 
77
    led_data.display_buffer[0] = 0x00;  // Start at address 0x00
78
    I2C_Master_Send(led_data.i2c_address, 17, led_data.display_buffer);
79
 
80
    result = I2C_Get_Status();
81
    while (!result) {
82
        result = I2C_Get_Status();
83
    }
84
}
85
 
86
void LED_clear() {
87
    unsigned char c;
88
    for (c = 0; c < 17; c++) {
89
        led_data.display_buffer[c] = 0;
90
    }
91
}
92
 
93
void LED_drawColon(unsigned char c) {
94
    if (c) {
95
        led_data.display_buffer[5] = 0xFF;
96
    } else {
97
        led_data.display_buffer[5] = 0;
98
    }
99
}
100
 
101
void LED_writeDigitRaw(unsigned char loc, unsigned char bitmask) {
102
    if (loc > 4) return;
103
    led_data.display_buffer[(loc<<1)+1] = bitmask;
104
}
105
 
106
void LED_writeDigitNum(unsigned char loc, unsigned char num, unsigned char dot) {
107
    if (loc > 4) return;
108
    if (loc > 1) loc++;
109
    LED_writeDigitRaw(loc, numbertable[num] | dot << 7);
110
}
111
 
112
void LED_writeDigitAlpha(unsigned char loc, unsigned char alpha, unsigned char dot) {
113
    if (loc > 4) return;
114
    if (loc > 1) loc++;
115
    LED_writeDigitRaw(loc, alphatable[alpha] | dot << 7);
116
}
117
 
118
void LED_writeNum(unsigned int i) {
119
    LED_writeDigitNum(0, (i%10000)/1000, 0);
120
    LED_writeDigitNum(1, (i%1000)/100, 0);
121
    LED_writeDigitNum(2, (i%100)/10, 0);
122
    LED_writeDigitNum(3, i%10, 0);
123
 
124
    if (i < 10) {
125
        LED_writeDigitRaw(0, 0);
126
        LED_writeDigitRaw(1, 0);
127
        LED_writeDigitRaw(3, 0);
128
    } else if (i < 100) {
129
        LED_writeDigitRaw(0, 0);
130
        LED_writeDigitRaw(1, 0);
131
    } else if (i < 1000) {
132
        LED_writeDigitRaw(0, 0);
133
    }
134
    LED_writeDisplay();
135
}