|
| 1 | + |
| 2 | +// ======== Display constants. |
| 3 | +#define _A_ 0x80 |
| 4 | +#define _B_ 0x40 |
| 5 | +#define _C_ 0x20 |
| 6 | +#define _D_ 0x10 |
| 7 | +#define _E_ 0x08 |
| 8 | +#define _F_ 0x04 |
| 9 | +#define _G_ 0x02 |
| 10 | +#define _DP_ 0x01 |
| 11 | + |
| 12 | + |
| 13 | +#define DATA 0 |
| 14 | +#define CLOCK 1 |
| 15 | +#define BLANK 2 |
| 16 | + |
| 17 | +const char segments[] = { |
| 18 | + _A_|_B_|_C_|_D_|_E_|_F_, // 0 |
| 19 | + _B_|_C_, // 1 |
| 20 | + _A_|_B_|_G_|_D_|_E_, // 2 |
| 21 | + _A_|_B_|_C_|_D_|_G_, // 3 |
| 22 | + _B_|_C_|_F_|_G_, // 4 |
| 23 | + _A_|_C_|_D_|_F_|_G_, // 5 |
| 24 | + _A_|_C_|_D_|_E_|_F_|_G_, // 6 |
| 25 | + _A_|_B_|_C_, // 7 |
| 26 | + _A_|_B_|_C_|_D_|_E_|_F_|_G_, // 8 |
| 27 | + _A_|_B_|_C_|_D_|_F_|_G_, // 9 |
| 28 | + |
| 29 | + _A_|_B_|_C_|_E_|_F_|_G_, // A |
| 30 | + _C_|_D_|_E_|_F_|_G_, // b |
| 31 | + _D_|_E_|_G_, // c |
| 32 | + _B_|_C_|_D_|_E_|_G_, // d |
| 33 | + _A_|_D_|_E_|_F_|_G_, //e |
| 34 | + _A_|_E_|_F_|_G_, // f |
| 35 | + |
| 36 | + _A_|_B_|_E_|_F_|_G_, // P [0x10] |
| 37 | + |
| 38 | + _G_, // - [0x11] |
| 39 | + |
| 40 | + _A_, _B_, _C_, _D_, _E_, _F_, // 0x12 - 0x17 |
| 41 | + |
| 42 | + 0, // 0x18 |
| 43 | +}; |
| 44 | + |
| 45 | +char digits[4]; |
| 46 | +char decimals; |
| 47 | + |
| 48 | +#define PULSE digitalWrite(CLOCK, LOW); delayMicroseconds(10) ; digitalWrite(CLOCK, HIGH); delayMicroseconds(10) ; |
| 49 | + |
| 50 | +void clear() { |
| 51 | + char i; |
| 52 | + digitalWrite(DATA, LOW); |
| 53 | + for (i = 0 ; i < 32 ; i++) { |
| 54 | + PULSE; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void display() { |
| 59 | + char i, d, digit; |
| 60 | + |
| 61 | + digitalWrite(BLANK, LOW); |
| 62 | + |
| 63 | + clear(); |
| 64 | + for (d = 0 ; d < 4 ; d++) { |
| 65 | + digit = segments[digits[d]]; |
| 66 | + |
| 67 | + if ((decimals >> d & 0x1) == 0x1) digit |= _DP_; |
| 68 | + |
| 69 | + for (i = 0 ; i < 8 ; i++) { |
| 70 | + digitalWrite(DATA, digit & 0x1 ? HIGH : LOW); |
| 71 | + digit >>= 1; |
| 72 | + PULSE; |
| 73 | + } |
| 74 | + } |
| 75 | + digitalWrite(BLANK, HIGH); |
| 76 | +} |
| 77 | + |
| 78 | +void displayAP() { |
| 79 | + digits[0] = 0x10; |
| 80 | + digits[1] = 0xA; |
| 81 | + digits[2] = 0x18; |
| 82 | + digits[3] = 0x18; |
| 83 | + display(); |
| 84 | +} |
| 85 | + |
| 86 | +void displayDash() { |
| 87 | + digits[0] = digits[1] = digits[2] = digits[3] = 0x11; |
| 88 | + display(); |
| 89 | +} |
| 90 | + |
| 91 | +void clearDigits() { |
| 92 | + digits[0] = digits[1] = digits[2] = digits[3] = 0x18; |
| 93 | +} |
| 94 | + |
| 95 | +// Twirler handler. |
| 96 | +Ticker ticker; |
| 97 | + |
| 98 | +volatile char busySegment = 0x12; |
| 99 | +volatile char busyDigit; |
| 100 | + |
| 101 | +void displayBusy(char digit) { |
| 102 | + busyDigit = digit; |
| 103 | + ticker.attach(0.1, _displayBusy); |
| 104 | +} |
| 105 | + |
| 106 | +void stopDisplayBusy() { |
| 107 | + ticker.detach(); |
| 108 | +} |
| 109 | + |
| 110 | +void _displayBusy() { |
| 111 | + if (busySegment > 0x17) { |
| 112 | + busySegment = 0x12; |
| 113 | + } |
| 114 | + clearDigits(); |
| 115 | + digits[busyDigit] = busySegment++; |
| 116 | + display(); |
| 117 | +} |
| 118 | + |
| 119 | +// End twirler handler. |
| 120 | + |
| 121 | +// IP Display handler. |
| 122 | +volatile signed char dispOctet = -1; |
| 123 | + |
| 124 | +char displayIP() { |
| 125 | + if (dispOctet > -1) { |
| 126 | + return 1; |
| 127 | + } |
| 128 | + if (digitalRead(SETUP_PIN) == 1) return 0; |
| 129 | + dispOctet = 0; |
| 130 | + ticker.attach(1.0, _displayIP); |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +void _displayIP() { |
| 135 | + if (dispOctet > 3) { |
| 136 | + ticker.detach(); |
| 137 | + dispOctet = -1; |
| 138 | + clockMode == MODE_CLOCK ? displayClock() : displayAP(); |
| 139 | + return; |
| 140 | + } |
| 141 | + clearDigits(); |
| 142 | + uint8_t octet = (uint32_t(clockMode == MODE_CLOCK ? WiFi.localIP() : WiFi.softAPIP()) >> (8 * dispOctet++)) & 0xff; |
| 143 | + uint8_t d = 0; |
| 144 | + for (; octet > 99 ; octet -= 100) d++; |
| 145 | + digits[2] = d; |
| 146 | + d = 0; |
| 147 | + for (; octet > 9 ; octet -= 10) d++; |
| 148 | + digits[1] = d; |
| 149 | + digits[0] = octet; |
| 150 | + decimals = 0x1; |
| 151 | + display(); |
| 152 | +} |
| 153 | + |
| 154 | +// end Ip display handler. |
| 155 | + |
| 156 | +void displayClock() { |
| 157 | + int h = hour(); |
| 158 | + int m = minute(); |
| 159 | + digits[0] = digits[1] = digits[2] = digits[3] = decimals = 0; |
| 160 | + |
| 161 | + if (h > 19) digits[3] = 2; |
| 162 | + else if (h > 9) digits[3] = 1; |
| 163 | + digits[2] = h % 10; |
| 164 | + |
| 165 | + if (m > 49) digits[1] = 5; |
| 166 | + else if (m > 39) digits[1] = 4; |
| 167 | + else if (m > 29) digits[1] = 3; |
| 168 | + else if (m > 19) digits[1] = 2; |
| 169 | + else if (m > 9) digits[1] = 1; |
| 170 | + |
| 171 | + digits[0] = m % 10; |
| 172 | + |
| 173 | + if (second() & 0x1) decimals = 0x4; |
| 174 | + if (timeStatus() != timeSet) decimals |= 0x1; |
| 175 | + display(); |
| 176 | +} |
| 177 | + |
| 178 | +void setupDisplay() { |
| 179 | + pinMode(DATA, OUTPUT); |
| 180 | + pinMode(CLOCK, OUTPUT); |
| 181 | + pinMode(BLANK, OUTPUT); |
| 182 | + displayDash(); |
| 183 | +} |
| 184 | + |
0 commit comments