-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplay.cpp
More file actions
112 lines (95 loc) · 2.88 KB
/
Display.cpp
File metadata and controls
112 lines (95 loc) · 2.88 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
#include "Display.h"
extern Logger logger;
void Display::begin()
{
for (int i = 0; i < 8; i++)
{
pinMode(_pinDigits[i], INPUT);
pinMode(_pinSegments[i], INPUT);
}
clear();
}
// decode the digit from the stable mask using the predefined segment patterns
char Display::decodeDigit(uint8_t stableMask) {
uint8_t mask7 = stableMask & 0x7F; // keep only the first 7 bits (A-G segments)
for (int i = 0; i < _nPatterns; i++) {
if (mask7 == (_segmentPatterns[i] & 0x7F)) {
return _segmentToChar[i];
}
}
return '?';
}
void Display::clear() {
for (int i = 0; i < 8; i++) {
_digitChars[i] = ' ';
}
_digitChars[8] = '\0'; // null terminator for string operations
}
// sniff 7‑segment display
void Display::update() {
for (int d = 0; d < 8; d++) {
if (digitalRead(_pinDigits[d]) == LOW) continue;
// start timeout timer
uint32_t startUs = micros();
// Check if digit is stable
const uint32_t minStableUs = 50;
uint32_t t0 = micros();
while (digitalRead(_pinDigits[d]) == HIGH && (micros() - t0) < minStableUs) {
// wait for digit to stabilize
}
// If digit is still HIGH, it means it's stable
if (digitalRead(_pinDigits[d]) == LOW) continue;
uint16_t oneCount[8] = {0};
uint32_t sampleCount = 0;
// sample while digit is HIGH, but bail out if we exceed _maxSampleUs
while (digitalRead(_pinDigits[d]) == HIGH) {
// break on timeout
if (micros() - startUs >= _maxSampleUs) {
//logger.logln("Timeout reading digit " + String(d));
break;
}
// accumulate segment lows
for (int s = 0; s < 8; s++) {
if (digitalRead(_pinSegments[s]) == LOW) oneCount[s]++;
}
sampleCount++;
}
if (sampleCount < 100 || (micros() - startUs) < 1000) {
// logger.logln("Digit " + String(d) +
// ": too few samples (" + String(sampleCount) +
// ") or too short time (" + String(micros() - startUs) + " us)");
continue;
}
// build stable mask
uint8_t stableMask = 0;
for (int s = 0; s < 7; s++) {
if (sampleCount > 0 && oneCount[s] * 3 >= sampleCount) {
stableMask |= (1 << s);
}
}
// decode
char result = decodeDigit(stableMask);
// debug if unrecognized
if (result == '?') {
logger.logln("Unrecognized pattern on digit " + String(d) +
": stableMask=0b" + String(stableMask, BIN) +
" samples=" + String(sampleCount));
continue;
}
_digitChars[d] = result;
}
}
int Display::extractTemperature(int start, int end) {
String tempStr = String(_digitChars).substring(start, end);
tempStr.trim();
return atoi(tempStr.c_str());
}
int Display::getTargetTemp() {
return extractTemperature(4, 8);
}
int Display::getCurrentTemp() {
return extractTemperature(0, 4);
}
String Display::getDisplayString() {
return String(_digitChars);
}