forked from dfrencham/rad-gate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.cpp
More file actions
110 lines (93 loc) · 2.46 KB
/
Copy pathDisplay.cpp
File metadata and controls
110 lines (93 loc) · 2.46 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
/*****************************************
* RemoteArDuino (RAD) Gate
*
*****************************************/
#include "Display.h"
#include <stdint.h>
#include <Arduino.h>
#include "utility.h"
uint8_t num_digits = 4;
uint8_t msb, nmsb, nlsb,lsb;
uint8_t digit[] = {msb, nmsb, nlsb,lsb};
uint8_t dp;
uint8_t blank = 11; // 0x00
int LED_SEG_TAB[] = {
0xfc, // 0
0x60, // 1
0xda, // 2
0xf2, // 3
0x66, // 4
0xb6, // 5
0xbe, // 6
0xe0, // 7
0xfe, // 8
0xf6, // 9
0x01, // . // 10
0x00, // Off // 11
0x02, // - // 12
0x0a, // r // 13
0xee, // A // 14
0x7a, // d // 15
0x9e, // E // 16
0x1e, // t // 17
};
Display::Display() {}
void Display::displayNumber(uint32_t value, bool leadingZero, uint8_t dp) {
// leadingZero = 1 means show leading zeros
// dp = 0 means no dp (so +0 to LED_SEG_TAB[x])
// dp = 1-> 4 means display dp only for that corresponding digit (so +1 to LED_SEG_TAB[x])
// chop off excess digits as required
// could round first, but is not neccessary
if (value > 999999){
value = value/1000;
} else
if (value > 99999){
value = value/100;
} else
if (value > 9999) {
value = value/10;
}
// break down integer "value" into single digits as msb, nmsb, nlsb, lsb
msb = value / 1000 ;
value = value % 1000;
digit[0] = msb;
nmsb = value / 100;
value = value % 100;
digit[1] = nmsb;
nlsb = value / 10;
value = value % 10;
digit[2] = nlsb;
lsb = value;
digit[3] = lsb;
// progressively remove leading zeros (if required)
if (!leadingZero) {
if (msb==0) {
digit[0] = blank;
}
if (msb==0 && nmsb==0 ) {
digit[1] = blank;
}
if (msb==0 && nmsb==0 && nlsb==0) {
digit[2] = blank;
}
if (msb==0 && nmsb==0 && nlsb==0 && lsb==0) {
digit[3] = blank;
}
}
setDisplay(digit, dp, num_digits);
}
void Display::allOff() { // turns off all segments
digitalWrite(PIN_DISPLAY_LATCH, LOW);
for (int y=0; y<num_digits; y++) {
shiftOut(PIN_DISPLAY_DATA, PIN_DISPLAY_CLOCK, LSBFIRST, 0);
}
digitalWrite(PIN_DISPLAY_LATCH, HIGH);
}
void Display::setDisplay(uint8_t digits[], uint8_t dp, int num_digits) {
// Set the Display from array
digitalWrite(PIN_DISPLAY_LATCH, LOW);
for (int y=num_digits-1; y>=0; y--) {
shiftOut(PIN_DISPLAY_DATA, PIN_DISPLAY_CLOCK, LSBFIRST, LED_SEG_TAB[digits[y]] + (!(dp==0 || (dp-1) != y)));
}
digitalWrite(PIN_DISPLAY_LATCH, HIGH);
}