-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLED_7segment_bitbang.h
More file actions
134 lines (119 loc) · 3.39 KB
/
LED_7segment_bitbang.h
File metadata and controls
134 lines (119 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef __LED_7segment_bitbang_included__
#define __LED_7segment_bitbang_included__
#define private public
class LED_7segment_bitbang final {
public:
// Singleton instance;
// use "LED_7segment_bitbang &myname = LED_7segment_bitbang::instance"
// to access the functions more easily
static LED_7segment_bitbang instance; // Singleton instance
// start timer, blank the display
void init();
// stop timer, blank the display
static void done();
// output bitmasks
// be _very_ careful while addressing these,
// only assign l_, la ... lg
// or use l_ as a bitmask
static uint16_t volatile bits[3]; // only set la..lg
// constants for turning on LEDs
// negative logic, since these pins drive grounding pins
// (lb & lc
static const uint16_t l_ = 0x1ea8; // blank
static const uint16_t la=l_ & ~(B00010000 << 8); // top
static const uint16_t lb=l_ & ~(B00001000 << 8); // top left
static const uint16_t lc=l_ & ~(B00000100 << 8); // top right
static const uint16_t ld=l_ & ~(B00000010 << 8); // middle
static const uint16_t le=l_ & ~(B00100000); // bottm left
static const uint16_t lf=l_ & ~(B00001000); // bottom right
static const uint16_t lg=l_ & ~(B10000000); // bottom
// digits 0..9, a..f, blank, "r", "-"
// 0..f are fixed, the other values may still change for the
// next version of the library
static const uint16_t v[];
// blank display
void blank(){
bits[0] = bits[1] = bits[2] = v[16];
}
// write decimal or error if n>199
void writede(int n){
if (n > 199) {
write_err();
return;
}
writed(n);
}
// write decimal
void writed(int n){
byte c = n%10;
n/=10;
bits[2] = v[c];
if (!n) goto blank1;
bits[1] = v[n%10];
n/=10;
if (!n) goto blank2;
bits[0] = v[n & 0x0f]; // prevent access of outside array, faster than %10
return;
blank1:
bits[1] = v[16];
blank2:
bits[0] = v[16];
}
// write hex values
void writeh(int n){
byte c=n&0x0f; n>>=4;
bits[2] = v[c];
if (!n) goto blank1;
bits[1] = v[n&0x0f];
n>>=4;
if (!n) goto blank2;
bits[0] = v[n & 0x0f]; // prevent access of outside array
return;
blank1:
bits[1] = v[16];
blank2:
bits[0] = v[16];
}
// write "Er"
void write_err(){
bits[0] = v[16]; //blank
bits[1] = v[0xe]; //"e"
bits[2] = v[17]; //"r"
}
// write "--"
void write_minus(){
bits[0] = v[16]; //blank
bits[1] = v[18]; //"-"
bits[2] = v[18]; //"-"
}
// write -9 .. 199
void writei(int i){
if (i < -9) {
write_err();
return;
}
if (i < 0) {
bits[0] = v[16]; //blank
bits[1] = v[18]; //"-"
bits[2] = v[-i]; //digit
}
writede(i);
}
// alias for blank()
void write_blank(){
blank();
}
// display next digit; internal function, but it expects to be called from
// interrupt so it has to be public
void next();
private:
LED_7segment_bitbang(); // Singleton constructor
static volatile char current_d; // current digit
static const char digit_pin[]; // pins corresponding to digits
static const byte PD=B11111000; // GPIO-register D mask
static const byte PB=B00011111; // GPIO-register B mask
static const byte PD_blank=B10101000; // value for blank output
static const byte PB_blank=B00011110; // dito
static void set_blank_voltages();
};
#endif //__LED_7segment_bitbang_included__