led added

This commit is contained in:
Svante Kaiser 2023-11-02 16:52:57 +03:00
parent 4bdc98bd7b
commit 5f1849caf1
5 changed files with 92 additions and 54 deletions

View File

@ -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

View File

@ -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,35 +26,7 @@ void writeState(char* message) {
publishToMQTT(STATE_MQTT_TOPIC, message);
}
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");
}
break;
case CONNECTED:
if (data == 0) {
countZeros++;
if (countZeros >= NOT_CONNECTED_THRESHOLD) {
currentState = NOT_CONNECTED;
writeState("not connected");
resetCounters();
}
} else if (data == 1) {
if (countZeros >= INITIALIZING_CALL_THRESHOLD) {
currentState = RECEIVING_DATA;
writeState("receiving data");
resetCounters();
}
}
break;
case RECEIVING_DATA:
void receiveData(int data) {
if (data != previousData) {
if (previousData == HIGH) {
dataLength++;
@ -63,60 +36,105 @@ void updateStateMachine(int data) {
signalDuration++;
}
previousData = data;
if (data == 0) {
}
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 == LOW) {}
else if (data == HIGH) {
changeState(CONNECTED);
}
break;
case CONNECTED:
if (data == LOW) {
countZeros++;
if (countZeros >= NOT_CONNECTED_THRESHOLD) {
changeState(NOT_CONNECTED);
}
} else if (data == HIGH) {
if (countZeros >= INITIALIZING_CALL_THRESHOLD) {
changeState(RECEIVING_DATA);
}
}
break;
case RECEIVING_DATA:
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;
}

12
src/infra/led.cpp Normal file
View File

@ -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);
}

4
src/infra/led.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void ledTurnOn();
void ledTurnOff();

View File

@ -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);
}