refactor: split of seven-segment stuff into its own class
This commit is contained in:
parent
43ff8671a9
commit
3ff0c02874
3 changed files with 140 additions and 99 deletions
|
@ -6,6 +6,8 @@
|
||||||
#include <ESP32_C3_TimerInterrupt.h>
|
#include <ESP32_C3_TimerInterrupt.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
|
#include "seven.h"
|
||||||
|
|
||||||
#define BRIGHTNESS_HIGH 255
|
#define BRIGHTNESS_HIGH 255
|
||||||
#define BRIGHTNESS_LOW 64
|
#define BRIGHTNESS_LOW 64
|
||||||
|
|
||||||
|
@ -21,62 +23,14 @@
|
||||||
#define SW_PLUS D0
|
#define SW_PLUS D0
|
||||||
#define SW_MINUS D2
|
#define SW_MINUS D2
|
||||||
|
|
||||||
|
|
||||||
#define I_E 10
|
|
||||||
#define I_DASH 11
|
|
||||||
#define E_RANGE 0
|
|
||||||
#define E_OTA 1
|
#define E_OTA 1
|
||||||
|
|
||||||
uint8_t dot01 = 0b00000010;
|
|
||||||
uint8_t digits01[] = {
|
|
||||||
//⁻┌┐-└_.┘
|
|
||||||
0b11101101, // 0
|
|
||||||
0b00100001, // 1
|
|
||||||
0b10111100, // 2
|
|
||||||
0b10110101, // 3
|
|
||||||
0b01110001, // 4
|
|
||||||
0b11010101, // 5
|
|
||||||
0b11011101, // 6
|
|
||||||
0b10100001, // 7
|
|
||||||
0b11111101, // 8
|
|
||||||
0b11110101, // 9
|
|
||||||
0b11011100, // E
|
|
||||||
0b00010000, // -
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t dot10 = 0b00010000;
|
|
||||||
uint8_t digits10[] = {
|
|
||||||
//_┘└.┐-⁻┌
|
|
||||||
0b11101011, // 0
|
|
||||||
0b01001000, // 1
|
|
||||||
0b10101110, // 2
|
|
||||||
0b11001110, // 3
|
|
||||||
0b01001101, // 4
|
|
||||||
0b11000111, // 5
|
|
||||||
0b11100111, // 6
|
|
||||||
0b01001010, // 7
|
|
||||||
0b11101111, // 8
|
|
||||||
0b11001111, // 9
|
|
||||||
0b10100111, // E
|
|
||||||
0b00000100, // -
|
|
||||||
};
|
|
||||||
|
|
||||||
uint16_t spinner[] = {
|
|
||||||
0b0000001100000000, // ┌
|
|
||||||
0b0000001010000000, // ⁻
|
|
||||||
0b0000000010100000, // ┐
|
|
||||||
0b0000000000100001, // |
|
|
||||||
0b0000000000000101, // ┘
|
|
||||||
0b1000000000000100, // _
|
|
||||||
0b1010000000000000, // └
|
|
||||||
0b0010000100000000, // |
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client(wifi, "10.20.13.12", 8086);
|
HttpClient client(wifi, "10.20.13.12", 8086);
|
||||||
ESP32Timer ITimer0(0);
|
ESP32Timer ITimer0(0);
|
||||||
|
Seven seven(DISP_SER, DISP_CLK, DISP_PWM, MSBFIRST);
|
||||||
|
|
||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
@ -146,55 +100,10 @@ bool isr_timer0(void *data) {
|
||||||
request_update = true;
|
request_update = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBrightness(uint8_t value) {
|
|
||||||
analogWrite(DISP_PWM, 255-value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void showError(uint8_t error, uint8_t bright = 255) {
|
|
||||||
setBrightness(0);
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[E_RANGE]);
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_E] | dot10);
|
|
||||||
digitalWrite(DISP_CLK, HIGH);
|
|
||||||
digitalWrite(DISP_CLK, LOW);
|
|
||||||
setBrightness(bright);
|
|
||||||
}
|
|
||||||
|
|
||||||
void showNumber(int8_t number, uint8_t bright = 255, bool show_dot01 = false, bool show_dot10 = false) {
|
|
||||||
if (number < -9 || number > 99) {
|
|
||||||
showError(E_RANGE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setBrightness(0);
|
|
||||||
if (number < 0) {
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[-number] | dot01 * show_dot01);
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_DASH] | dot10 * show_dot10);
|
|
||||||
} else if (number < 10) {
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[number] | dot01 * show_dot01);
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, 0 | dot10 * show_dot10);
|
|
||||||
} else {
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[number%10] | dot01 * show_dot01);
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[number/10] | dot10 * show_dot10);
|
|
||||||
}
|
|
||||||
digitalWrite(DISP_CLK, HIGH);
|
|
||||||
digitalWrite(DISP_CLK, LOW);
|
|
||||||
setBrightness(bright);
|
|
||||||
}
|
|
||||||
|
|
||||||
void showSpinner(uint8_t bright = 255) {
|
|
||||||
setBrightness(0);
|
|
||||||
uint8_t spinner_i = (millis() / 50) % 8;
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, (uint8_t) (spinner[spinner_i] & 0xff));
|
|
||||||
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, (uint8_t) (spinner[spinner_i] >> 8));
|
|
||||||
digitalWrite(DISP_CLK, HIGH);
|
|
||||||
digitalWrite(DISP_CLK, LOW);
|
|
||||||
setBrightness(bright);
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw(uint8_t bright = 255, bool show_dot01 = false) {
|
void draw(uint8_t bright = 255, bool show_dot01 = false) {
|
||||||
analogWrite(LED_OPEN, bright * space_open);
|
analogWrite(LED_OPEN, bright * space_open);
|
||||||
analogWrite(LED_CLOSE, bright * !space_open);
|
analogWrite(LED_CLOSE, bright * !space_open);
|
||||||
showNumber(counter, bright, show_dot01);
|
seven.showNumber(counter, bright, show_dot01);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
|
@ -202,7 +111,7 @@ void reconnect() {
|
||||||
Serial.print("Reconnecting to WiFi");
|
Serial.print("Reconnecting to WiFi");
|
||||||
WiFi.reconnect();
|
WiFi.reconnect();
|
||||||
for (uint16_t i = 0; i < 1000 && WiFi.status() != WL_CONNECTED; ++i) {
|
for (uint16_t i = 0; i < 1000 && WiFi.status() != WL_CONNECTED; ++i) {
|
||||||
showSpinner(BRIGHTNESS_HIGH);
|
seven.showSpinner(BRIGHTNESS_HIGH);
|
||||||
delay(50);
|
delay(50);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
@ -241,7 +150,7 @@ void setup() {
|
||||||
pinMode(DISP_CLK, OUTPUT);
|
pinMode(DISP_CLK, OUTPUT);
|
||||||
digitalWrite(DISP_PWM, HIGH);
|
digitalWrite(DISP_PWM, HIGH);
|
||||||
|
|
||||||
showNumber(88, BRIGHTNESS_HIGH, true, true);
|
seven.showNumber(88, BRIGHTNESS_HIGH, true, true);
|
||||||
analogWrite(LED_OPEN, BRIGHTNESS_HIGH);
|
analogWrite(LED_OPEN, BRIGHTNESS_HIGH);
|
||||||
analogWrite(LED_CLOSE, BRIGHTNESS_HIGH);
|
analogWrite(LED_CLOSE, BRIGHTNESS_HIGH);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
@ -274,10 +183,10 @@ void setup() {
|
||||||
if (percentage > 99) {
|
if (percentage > 99) {
|
||||||
percentage = 99;
|
percentage = 99;
|
||||||
}
|
}
|
||||||
showNumber(percentage, BRIGHTNESS_HIGH, true, true);
|
seven.showNumber(percentage, BRIGHTNESS_HIGH, true, true);
|
||||||
})
|
})
|
||||||
.onError([](ota_error_t error) {
|
.onError([](ota_error_t error) {
|
||||||
showError(E_OTA, BRIGHTNESS_HIGH);
|
seven.showError(E_OTA, BRIGHTNESS_HIGH);
|
||||||
Serial.printf("Error[%u]: ", error);
|
Serial.printf("Error[%u]: ", error);
|
||||||
if (error == OTA_AUTH_ERROR) {
|
if (error == OTA_AUTH_ERROR) {
|
||||||
Serial.println("Auth Failed");
|
Serial.println("Auth Failed");
|
||||||
|
|
58
esp32/src/seven.cpp
Normal file
58
esp32/src/seven.cpp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "seven.h"
|
||||||
|
|
||||||
|
Seven::Seven(uint8_t serPin, uint8_t clkPin, uint8_t pwmPin, uint8_t dir) {
|
||||||
|
this->serPin = serPin;
|
||||||
|
this->clkPin = clkPin;
|
||||||
|
this->pwmPin = pwmPin;
|
||||||
|
this->dir = dir;
|
||||||
|
pinMode(this->serPin, OUTPUT);
|
||||||
|
pinMode(this->clkPin, OUTPUT);
|
||||||
|
pinMode(this->pwmPin, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Seven::setBrightness(uint8_t value) {
|
||||||
|
analogWrite(this->pwmPin, 255-value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Seven::showError(uint8_t error, uint8_t bright) {
|
||||||
|
this->setBrightness(0);
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS01[Seven::E_RANGE]);
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS10[Seven::I_E] | Seven::DOT10);
|
||||||
|
digitalWrite(this->clkPin, HIGH);
|
||||||
|
digitalWrite(this->clkPin, LOW);
|
||||||
|
this->setBrightness(bright);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Seven::showNumber(int8_t number, uint8_t bright, bool show_dot01, bool show_dot10) {
|
||||||
|
if (number < -9 || number > 99) {
|
||||||
|
this->showError(E_RANGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->setBrightness(0);
|
||||||
|
if (number < 0) {
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS01[-number] | Seven::DOT01 * show_dot01);
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS10[Seven::I_DASH] | Seven::DOT10 * show_dot10);
|
||||||
|
} else if (number < 10) {
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS01[number] | Seven::DOT01 * show_dot01);
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, 0 | Seven::DOT10 * show_dot10);
|
||||||
|
} else {
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS01[number%10] | Seven::DOT01 * show_dot01);
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, Seven::DIGITS10[number/10] | Seven::DOT10 * show_dot10);
|
||||||
|
}
|
||||||
|
digitalWrite(this->clkPin, HIGH);
|
||||||
|
digitalWrite(this->clkPin, LOW);
|
||||||
|
this->setBrightness(bright);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Seven::showSpinner(uint8_t bright) {
|
||||||
|
this->setBrightness(0);
|
||||||
|
uint8_t spinner_i = (millis() / 50) % 8;
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, (uint8_t) (Seven::SPINNER[spinner_i] & 0xff));
|
||||||
|
shiftOut(this->serPin, this->clkPin, this->dir, (uint8_t) (Seven::SPINNER[spinner_i] >> 8));
|
||||||
|
digitalWrite(this->clkPin, HIGH);
|
||||||
|
digitalWrite(this->clkPin, LOW);
|
||||||
|
this->setBrightness(bright);
|
||||||
|
}
|
||||||
|
|
74
esp32/src/seven.h
Normal file
74
esp32/src/seven.h
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
|
||||||
|
#ifndef _SEVEN_H_
|
||||||
|
#define _SEVEN_H_
|
||||||
|
|
||||||
|
class Seven {
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t serPin;
|
||||||
|
uint8_t clkPin;
|
||||||
|
uint8_t pwmPin;
|
||||||
|
uint8_t dir;
|
||||||
|
|
||||||
|
const uint8_t I_E = 10;
|
||||||
|
const uint8_t I_DASH = 11;
|
||||||
|
const uint8_t E_RANGE = 0;
|
||||||
|
|
||||||
|
const uint8_t DOT01 = 0b00000010;
|
||||||
|
const uint8_t DIGITS01[12] = {
|
||||||
|
//⁻┌┐-└_.┘
|
||||||
|
0b11101101, // 0
|
||||||
|
0b00100001, // 1
|
||||||
|
0b10111100, // 2
|
||||||
|
0b10110101, // 3
|
||||||
|
0b01110001, // 4
|
||||||
|
0b11010101, // 5
|
||||||
|
0b11011101, // 6
|
||||||
|
0b10100001, // 7
|
||||||
|
0b11111101, // 8
|
||||||
|
0b11110101, // 9
|
||||||
|
0b11011100, // E
|
||||||
|
0b00010000, // -
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t DOT10 = 0b00010000;
|
||||||
|
const uint8_t DIGITS10[12] = {
|
||||||
|
//_┘└.┐-⁻┌
|
||||||
|
0b11101011, // 0
|
||||||
|
0b01001000, // 1
|
||||||
|
0b10101110, // 2
|
||||||
|
0b11001110, // 3
|
||||||
|
0b01001101, // 4
|
||||||
|
0b11000111, // 5
|
||||||
|
0b11100111, // 6
|
||||||
|
0b01001010, // 7
|
||||||
|
0b11101111, // 8
|
||||||
|
0b11001111, // 9
|
||||||
|
0b10100111, // E
|
||||||
|
0b00000100, // -
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint16_t SPINNER[8] = {
|
||||||
|
0b0000001100000000, // ┌
|
||||||
|
0b0000001010000000, // ⁻
|
||||||
|
0b0000000010100000, // ┐
|
||||||
|
0b0000000000100001, // |
|
||||||
|
0b0000000000000101, // ┘
|
||||||
|
0b1000000000000100, // _
|
||||||
|
0b1010000000000000, // └
|
||||||
|
0b0010000100000000, // |
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Seven(uint8_t serPin, uint8_t clkPin, uint8_t pwmPin, uint8_t dir);
|
||||||
|
|
||||||
|
void setBrightness(uint8_t value);
|
||||||
|
void showError(uint8_t error, uint8_t bright = 255);
|
||||||
|
void showNumber(int8_t number, uint8_t bright = 255, bool show_dot01 = false, bool show_dot10 = false);
|
||||||
|
void showSpinner(uint8_t bright = 255);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _SEVEN_H_
|
Loading…
Reference in a new issue