From 5f1849caf1d5b97fa2d85121b115f9378967eb49 Mon Sep 17 00:00:00 2001
From: Svante Kaiser <svantekaiser@example.com>
Date: Thu, 2 Nov 2023 16:52:57 +0300
Subject: [PATCH] led added

---
 src/config/config.h         |   2 +-
 src/domain/stateMachine.cpp | 116 +++++++++++++++++++++---------------
 src/infra/led.cpp           |  12 ++++
 src/infra/led.h             |   4 ++
 src/main.cpp                |  12 ++--
 5 files changed, 92 insertions(+), 54 deletions(-)
 create mode 100644 src/infra/led.cpp
 create mode 100644 src/infra/led.h

diff --git a/src/config/config.h b/src/config/config.h
index 3f5e9d1..03e9cad 100644
--- a/src/config/config.h
+++ b/src/config/config.h
@@ -12,7 +12,7 @@
 #define FLAT_NUMBER_MQTT_TOPIC "/digitum/intercom_bridge4823/out/flat_number"
 #define STATE_MQTT_TOPIC "/digitum/intercom_bridge4823/out/state"
 
-#define LEDS_PIN 32
+#define LED_PIN 32
 #define DRY_CONT_PIN 15
 #define DOOR_SENS_PIN 114
 
diff --git a/src/domain/stateMachine.cpp b/src/domain/stateMachine.cpp
index c8d79d8..07b9748 100644
--- a/src/domain/stateMachine.cpp
+++ b/src/domain/stateMachine.cpp
@@ -1,6 +1,7 @@
 #include "utils/print.h"
 #include "config/config.h"
 #include "infra/mqtt.h"
+#include "infra/led.h"
 #include "domain/stateMachine.h"
 
 State currentState = NOT_CONNECTED;
@@ -25,98 +26,115 @@ void writeState(char* message) {
   publishToMQTT(STATE_MQTT_TOPIC, message);
 }
 
+void receiveData(int data) {
+  if (data != previousData) {
+    if (previousData == HIGH) {
+      dataLength++;
+    }
+    signalDuration = 0;
+  } else {
+    signalDuration++;
+  }
+  previousData = data;
+}
+
+void changeState(State state, bool resetCountersFlag=true) {
+  currentState = state;
+
+  switch (state) {
+    case NOT_CONNECTED:
+      ledTurnOff();
+      writeState("not connected");
+      break;
+    case CONNECTED:
+      ledTurnOn();
+      writeState("connected");
+      break;
+    case RECEIVING_DATA:
+      writeState("receiving data");
+      break;
+  }
+
+  if (resetCountersFlag)
+    resetCounters();
+}
+
+void flatReceived() {
+  int flat = dataLength/2;
+
+  if (flat < 1)
+    return;
+
+  println("| data length: ", dataLength);
+  println("| flat: ", flat);
+  publishToMQTT(FLAT_NUMBER_MQTT_TOPIC, flat);
+}
+
 void updateStateMachine(int data) {
   switch (currentState) {
     case NOT_CONNECTED:
-      if (data == 0) {
-        // Stay in the NOT_CONNECTED state
-      } else if (data == 1) {
-        currentState = CONNECTED;
-        writeState("connected");
+      if (data == LOW) {} 
+      else if (data == HIGH) {
+        changeState(CONNECTED);
       }
       break;
 
     case CONNECTED:
-      if (data == 0) {
+      if (data == LOW) {
         countZeros++;
         if (countZeros >= NOT_CONNECTED_THRESHOLD) {
-          currentState = NOT_CONNECTED;
-          writeState("not connected");
-          resetCounters();
+          changeState(NOT_CONNECTED);
         }
-      } else if (data == 1) {
+      } else if (data == HIGH) {
         if (countZeros >= INITIALIZING_CALL_THRESHOLD) {
-          currentState = RECEIVING_DATA;
-          writeState("receiving data");
-          resetCounters();
+          changeState(RECEIVING_DATA);
         }
       }
       break;
 
     case RECEIVING_DATA:
-      if (data != previousData) {
-        if (previousData == HIGH) {
-          dataLength++;
-        }
-        signalDuration = 0;
-      } else {
-        signalDuration++;
-      }
-      previousData = data;
-      if (data == 0) {
+      receiveData(data);
+
+      if (data == LOW) {
         countOnes = 0;
         countZeros++;
         if (countZeros >= DATA_RECEIVED_THESHOLD) {
-          println("| data length: ", dataLength);
-          println("| flat: ", dataLength/2);
-          publishToMQTT(FLAT_NUMBER_MQTT_TOPIC, dataLength/2);
-          
-          currentState = DATA_RECEIVED;
-          resetCounters();
+          flatReceived();
+          changeState(DATA_RECEIVED);
         }
-      } else if (data == 1) {
+      } else if (data == HIGH) {
         countZeros = 0;
         countOnes++;
         if (countOnes >= CONNECTED_THRESHOLD) {
-          currentState = CONNECTED;
-          writeState("connected");
-          resetCounters();
+          changeState(CONNECTED);
         }
       }
       break;
 
     case DATA_RECEIVED:
-      if (data == 0) {
+      if (data == LOW) {
         countZeros++;
         if (countZeros >= CALL_ENDED_THRESHOLD) {
-          currentState = CALL_ENDED;
-          writeState("call ended");
-          resetCounters();
+          changeState(CALL_ENDED);
         }
-      } else if (data == 1) {
+      } else if (data == HIGH) {
         countOnes++;
         if (countOnes >= CONNECTED_THRESHOLD) {
-          currentState = CONNECTED;
-          writeState("connected");
-          resetCounters();
+          changeState(CONNECTED);
         }
       break;
       }
 
     case CALL_ENDED:
-      if (data == 0) {
+      if (data == LOW) {
         countZeros++;
         if (countZeros >= NOT_CONNECTED_THRESHOLD) {
-          currentState = NOT_CONNECTED;
-          writeState("not connected");
-          resetCounters();
+          changeState(NOT_CONNECTED);
         }
-      } else if (data == 1) {
+      } else if (data == HIGH) {
         countOnes++;
         if (countOnes >= CONNECTED_THRESHOLD) {
-          currentState = CONNECTED;
-          writeState("connected");
-          resetCounters();
+          changeState(CONNECTED);
         }
       break;
       }
diff --git a/src/infra/led.cpp b/src/infra/led.cpp
new file mode 100644
index 0000000..6ac6e08
--- /dev/null
+++ b/src/infra/led.cpp
@@ -0,0 +1,12 @@
+#include <Arduino.h>
+
+#include "config/config.h"
+#include "infra/led.h"
+
+void ledTurnOn() {
+  digitalWrite(LED_PIN, LOW);
+}
+
+void ledTurnOff() {
+  digitalWrite(LED_PIN, HIGH);
+}
\ No newline at end of file
diff --git a/src/infra/led.h b/src/infra/led.h
new file mode 100644
index 0000000..d02c1e0
--- /dev/null
+++ b/src/infra/led.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void ledTurnOn();
+void ledTurnOff();
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 7173026..08e4f1d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -10,6 +10,7 @@
 #include "config/config.h"
 #include "infra/eth.h"
 #include "infra/mqtt.h"
+#include "infra/led.h"
 #include "domain/stateMachine.h"
 
 uint32_t lastMillis;
@@ -28,13 +29,16 @@ void setup() {
     initMQTT();
 
     pinMode(DATA_PIN, INPUT);
-
+    pinMode(LED_PIN, OUTPUT);
     pinMode(DRY_CONT_PIN, OUTPUT);
-    digitalWrite(DRY_CONT_PIN, 0);
+
+    digitalWrite(LED_PIN, HIGH);
+
+    digitalWrite(DRY_CONT_PIN, LOW);
     delay(2000);
-    digitalWrite(DRY_CONT_PIN, 1);
+    digitalWrite(DRY_CONT_PIN, HIGH);
     delay(2000);
-    digitalWrite(DRY_CONT_PIN, 0);
+    digitalWrite(DRY_CONT_PIN, LOW);
     delay(2000);
 }