summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2020-12-19 06:40:05 +0100
committerFlorian Baumann <derflob@derflob.de>2020-12-19 06:40:05 +0100
commitca8225bd0f2c10f6f5b16169e3b96f05079616b5 (patch)
treeedaac0ef7f3fed0a6c4207d53c5da816627dc281
parentba808a30e30e0c2bab85c85fa6d11b3a3b15f6fd (diff)
downloadRJ45EEPROMKeyHolder-ca8225bd0f2c10f6f5b16169e3b96f05079616b5.tar.gz
RJ45EEPROMKeyHolder-ca8225bd0f2c10f6f5b16169e3b96f05079616b5.tar.bz2
dim status led
-rw-r--r--include/pins.h2
-rw-r--r--src/main.cpp103
2 files changed, 58 insertions, 47 deletions
diff --git a/include/pins.h b/include/pins.h
index dfb6647..fee33a1 100644
--- a/include/pins.h
+++ b/include/pins.h
@@ -9,6 +9,8 @@
#define LED_REG2 27
#define LED_STATUS 32
+#define LED_STATUS_PWM_CHANNEL 0
+#define LED_STATUS_DUTY_CYCLE 32
#define SPI_SCK 18
#define SPI_MISO 19
diff --git a/src/main.cpp b/src/main.cpp
index 3fee7e2..d00d6ed 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -30,6 +30,7 @@ volatile enum kh_states {
KH_ERROR,
} state;
+void update_led_status();
void update_leds_sockets();
void mqtt_callback(char* topic, byte* payload, unsigned int length);
@@ -67,27 +68,63 @@ void setup_mqtt()
void led_task(void *parameter)
{
while (1) {
- switch (state) {
- case KH_SETUP:
- case KH_CONNECTING_WIFI:
- case KH_CONNECTING_MQTT:
- digitalWrite(LED_STATUS, (millis() % 1000 < 500) ? HIGH : LOW);
- break;
- case KH_PUBLISHING_CONFIG:
- digitalWrite(LED_STATUS, (millis() % 500 < 250) ? HIGH : LOW);
- break;
- case KH_UPDATING_KEY_STATE:
- digitalWrite(LED_STATUS, HIGH);
- break;
- case KH_ERROR:
- default:
- digitalWrite(LED_STATUS, (millis() % 100 < 50) ? HIGH : LOW);
- }
+ update_led_status();
update_leds_sockets();
vTaskDelay(25 / portTICK_PERIOD_MS);
}
}
+void update_led_status()
+{
+ switch (state) {
+ case KH_SETUP:
+ case KH_CONNECTING_WIFI:
+ case KH_CONNECTING_MQTT:
+ ledcWrite(LED_STATUS_PWM_CHANNEL, (millis() % 1000 < 500) ? LED_STATUS_DUTY_CYCLE : 0);
+ break;
+ case KH_PUBLISHING_CONFIG:
+ ledcWrite(LED_STATUS_PWM_CHANNEL, (millis() % 500 < 250) ? LED_STATUS_DUTY_CYCLE : 0);
+ break;
+ case KH_UPDATING_KEY_STATE:
+ ledcWrite(LED_STATUS_PWM_CHANNEL, LED_STATUS_DUTY_CYCLE);
+ break;
+ case KH_ERROR:
+ default:
+ ledcWrite(LED_STATUS_PWM_CHANNEL, (millis() % 100 < 50) ? LED_STATUS_DUTY_CYCLE : 0);
+ }
+}
+
+void update_leds_sockets()
+{
+ auto status = (millis() % 1000 < 800) ? 85 : 170;
+ static uint8_t led_mapping[8] = {7, 0, 1, 2, 3, 6, 5, 4};
+
+ for (int8_t i = 7; i >= 0; i--) {
+ uint8_t led_offset = led_mapping[i];
+
+ if (led_offset % 2 == 0) {
+ // yellow LEDs
+ digitalWrite(LED_REG0, ((status >> led_offset) & 1) & holder[led_offset / 2].flashing);
+ digitalWrite(LED_REG1, ((status >> led_offset) & 1) & holder[4 + led_offset / 2].flashing);
+ digitalWrite(LED_REG2, ((status >> led_offset) & 1) & holder[8 + led_offset / 2].flashing);
+ } else {
+ // green LEDs
+ digitalWrite(LED_REG0, holder[led_offset / 2].plugged);
+ digitalWrite(LED_REG1, holder[4 + led_offset / 2].plugged);
+ digitalWrite(LED_REG2, holder[8 + led_offset / 2].plugged);
+ }
+
+ digitalWrite(LED_REG_SCLK, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(LED_REG_SCLK, LOW);
+ }
+
+ digitalWrite(LED_REG_RCLK, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(LED_REG_RCLK, LOW);
+
+}
+
void update_key_status(bool send_discovery = false)
{
eeprom_key_socket_t *socket = NULL;
@@ -131,37 +168,6 @@ void update_key_status(bool send_discovery = false)
}
}
-void update_leds_sockets()
-{
- auto status = (millis() % 1000 < 800) ? 85 : 170;
- static uint8_t led_mapping[8] = {7, 0, 1, 2, 3, 6, 5, 4};
-
- for (int8_t i = 7; i >= 0; i--) {
- uint8_t led_offset = led_mapping[i];
-
- if (led_offset % 2 == 0) {
- // yellow LEDs
- digitalWrite(LED_REG0, ((status >> led_offset) & 1) & holder[led_offset / 2].flashing);
- digitalWrite(LED_REG1, ((status >> led_offset) & 1) & holder[4 + led_offset / 2].flashing);
- digitalWrite(LED_REG2, ((status >> led_offset) & 1) & holder[8 + led_offset / 2].flashing);
- } else {
- // green LEDs
- digitalWrite(LED_REG0, holder[led_offset / 2].plugged);
- digitalWrite(LED_REG1, holder[4 + led_offset / 2].plugged);
- digitalWrite(LED_REG2, holder[8 + led_offset / 2].plugged);
- }
-
- digitalWrite(LED_REG_SCLK, HIGH);
- delayMicroseconds(10);
- digitalWrite(LED_REG_SCLK, LOW);
- }
-
- digitalWrite(LED_REG_RCLK, HIGH);
- delayMicroseconds(10);
- digitalWrite(LED_REG_RCLK, LOW);
-
-}
-
eeprom_key_socket_t *match_key_id(String key_id)
{
eui48_t identifier = {};
@@ -273,7 +279,10 @@ void setup()
digitalWrite(CS_SEL2, LOW);
digitalWrite(CS_SEL3, LOW);
+ ledcSetup(LED_STATUS_PWM_CHANNEL, 4096, 8);
+ ledcWrite(LED_STATUS_PWM_CHANNEL, 0);
pinMode(LED_STATUS, OUTPUT);
+ ledcAttachPin(LED_STATUS, LED_STATUS_PWM_CHANNEL);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
digitalWrite(SPI_CS, HIGH);