Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
158 Kevin 1
#include "display_led_HT16K33.h"
2
#include "base_I2C.h"
155 Kevin 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_p;
27
 
28
void LED_Init(LED_DATA *data) {
29
    led_data_p = data;
30
 
31
    led_data_p->i2c_address = HT16K33_ADDRESS;
32
}
33
 
34
void LED_Start() {
35
    char c = 0x21;     // Cmd to turn on oscillator
36
 
37
    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
38
    char result = I2C_Get_Status();
39
    while (!result) {
40
        result = I2C_Get_Status();
41
    }
42
 
43
    LED_Blink_Rate(HT16K33_BLINK_OFF);
44
    LED_Set_Brightness(15);  // Max brightness
45
    LED_Clear();
46
    LED_Write_Display();
47
}
48
 
49
void LED_Set_Brightness(char c) {
50
    if (c > 15) c = 15;
51
    c |= 0xE0;
52
 
53
    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
54
    char result = I2C_Get_Status();
55
    while (!result) {
56
        result = I2C_Get_Status();
57
    }
58
}
59
 
60
void LED_Blink_Rate(char c) {
61
    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_p->i2c_address, 1, &buffer);
68
    buffer = I2C_Get_Status();
69
    while (!buffer) {
70
        buffer = I2C_Get_Status();
71
    }
72
}
73
 
74
void LED_Write_Display() {
75
    led_data_p->display_buffer[0] = 0x00;  // Start at address 0x00
76
    I2C_Master_Send(led_data_p->i2c_address, 17, led_data_p->display_buffer);
77
 
78
    char result = I2C_Get_Status();
79
    while (!result) {
80
        result = I2C_Get_Status();
81
    }
82
}
83
 
84
void LED_Clear() {
85
    for (char c = 0; c < 17; c++) {
86
        led_data_p->display_buffer[c] = 0;
87
    }
88
}
89
 
90
void LED_Draw_Colon(char c) {
91
    if (c) {
92
        led_data_p->display_buffer[5] = 0xFF;
93
    } else {
94
        led_data_p->display_buffer[5] = 0;
95
    }
96
}
97
 
98
void LED_Write_Digit_Raw(char loc, char bitmask) {
99
    if (loc > 4) return;
100
    led_data_p->display_buffer[(loc<<1)+1] = bitmask;
101
}
102
 
103
void LED_Write_Digit_Num(char loc, char num, char dot) {
104
    if (loc > 4) return;
105
    if (loc > 1) loc++;
106
    LED_Write_Digit_Raw(loc, numbertable[num] | dot << 7);
107
}
108
 
109
void LED_Write_Digit_Alpha(char loc, char alpha, char dot) {
110
    if (loc > 4) return;
111
    if (loc > 1) loc++;
112
    LED_Write_Digit_Raw(loc, alphatable[alpha] | dot << 7);
113
}
114
 
115
void LED_Write_Num(int i) {
116
    LED_Write_Digit_Num(0, (i%10000)/1000, 0);
117
    LED_Write_Digit_Num(1, (i%1000)/100, 0);
118
    LED_Write_Digit_Num(2, (i%100)/10, 0);
119
    LED_Write_Digit_Num(3, i%10, 0);
120
 
121
    if (i < 10) {
122
        LED_Write_Digit_Raw(0, 0);
123
        LED_Write_Digit_Raw(1, 0);
124
        LED_Write_Digit_Raw(3, 0);
125
    } else if (i < 100) {
126
        LED_Write_Digit_Raw(0, 0);
127
        LED_Write_Digit_Raw(1, 0);
128
    } else if (i < 1000) {
129
        LED_Write_Digit_Raw(0, 0);
130
    }
131
    LED_Write_Display();
132
}