-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyDisplay.cpp
165 lines (132 loc) · 4.73 KB
/
myDisplay.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "myDisplay.h"
#include "myTime.h"
#include "myConfig.h"
extern Config config;
unsigned long displayMtime = 0;
void syncDisplayScheduler() {
if(millis()-displayMtime >= 500 ){ // update display twice in a second
updateDisplay();
displayMtime = millis();
}
}
void storeBit(byte bit) {
if (bit == 1) {
digitalWrite(SR_SER, HIGH);
} else {
digitalWrite(SR_SER, LOW);
}
digitalWrite(SR_SRCLK, HIGH);
digitalWrite(SR_SRCLK, LOW);
}
void allDigitsToStorageRegisters() {
digitalWrite(SR_RCLK, HIGH);
digitalWrite(SR_RCLK, LOW);
}
/*
Gives back value of manual brightness.
Value can be used directly on the HW so it is already inverted.
*/
int sensorBridghtness() {
int sensorValue = analogRead(A0);
int brightness_level = sensorValue / 4;
brightness_level = brightness_level > auto_brightness_minimum_light ? auto_brightness_minimum_light : brightness_level; // max 245 to maintain a minimum brightness
//Serial.printf("sensorValue=%d - brightness_level=%d\n",sensorValue,brightness_level);
return brightness_level;
}
void enableDisplay(bool enabled) {
int brightness_level = 255-config.brightness; // manual brightness by default
if (config.auto_brightness) {
brightness_level = sensorBridghtness();
}
if (enabled) {
//digitalWrite(SR_OE, LOW); // inverted
analogWrite(SR_OE, brightness_level); // 0..255 - The higher value the softer brightness
} else {
//digitalWrite(SR_OE, HIGH); // inverted
analogWrite(SR_OE, 255); // 0..255 - The higher value the softer brightness
}
}
void convert(int n, byte &hi, byte &lo) {
// Input validation: max 2 digits
if (n > 99 || n < 0) {
lo = 255;
hi = 255;
return;
}
lo = n % 10;
hi = n / 10;
}
void sendByte(byte digitn, bool second, bool leadingDigit, bool clever) {
// Serial.printf("%d --- ", digitn);
byte data = 0;
// map actual digit according to the physical wiring of the display
if (digitn > 9 || digitn < 0) {
// invalid input
//Serial.print(" INVALID ");
data = mapping[DIGIT_DASH];
} else {
if (clever) {
data = mappingClever[digitn]; // map it a bit differently somewhere
} else {
data = mapping[digitn]; // map it in a standard way
}
}
// remove leading zeros in case of need
if (leadingDigit) {
if (digitn == 0 && !config.leading_zeros) {
data = mapping[DIGIT_OFF]; // turn off given digit to eliminate leading zero
}
}
// deal with leds of second
if (second) { // zeroing most significant bit if second led needs to be on. Second led controlled via digit4 and digit3 (two leds)
const byte mask = 0b01111111;
data = data & mask;
}
// send out data bit by bit in a serial manner (think of the four shift registers)
//Serial.printf(" ; %x ;", data);
int mask = 1;
for (int i = 0; i < 8; i++) {
byte bit = ((data & mask) == mask) ? 1 : 0;
//Serial.printf("%d(%d) ", bit, mask);
mask = mask << 1;
storeBit(bit);
}
//Serial.println();
}
void show(byte digit1, byte digit2, byte digit3, byte digit4, bool second, bool clever) {
//Serial.printf("Writing to display: %d%d:%d%d (SECOND %s)\n", digit1, digit2, digit3, digit4, second ? "ON" : "OFF");
bool doItSmart1 = (clever && (digit1 == 1) && (digit2 != 1));
bool doItSmart2 = (clever && (digit3 == 1) && (digit4 != 1));
//Serial.printf("clever1: %d, clever2: %d\n", doItSmart1, doItSmart2);
sendByte(digit1, second, true, doItSmart1);
sendByte(digit2, second, false, false);
sendByte(digit3, second, false, doItSmart2);
sendByte(digit4, second, false, false);
allDigitsToStorageRegisters();
enableDisplay(true); // TODO this might not the best place to keep the display on especially because of the brightness control (to be implemented)
}
void updateDisplay() {
//Serial.println("update display");
Time localTime = getLocalTime();
//Serial.printf("%d-%d-%d %d:%d:%d\n", localTime.year, localTime.month, localTime.day, localTime.hour, localTime.minute, localTime.second);
bool secondLedsOn = true;
if (config.second_blinking) {
secondLedsOn = (millis() % 1000) > 500; // // turn second leds on and off in every seconds
}
bool clever = config.clever;
byte hourHi, hourLo, minuteHi, minuteLo;
convert(localTime.hour, hourHi, hourLo);
convert(localTime.minute, minuteHi, minuteLo);
show(hourHi, hourLo, minuteHi, minuteLo, secondLedsOn, clever);
}
void displaySetup() {
pinMode(SR_OE, OUTPUT);
digitalWrite(SR_OE, HIGH); // inverted
pinMode(SR_RCLK, OUTPUT);
digitalWrite(SR_RCLK, LOW);
pinMode(SR_SRCLK, OUTPUT);
digitalWrite(SR_SRCLK, LOW);
pinMode(SR_SER, OUTPUT);
digitalWrite(SR_SER, LOW);
show(2,0,2,0,true, false); // init screen to 2020 and second leds also on
}