led added
This commit is contained in:
parent
4bdc98bd7b
commit
5f1849caf1
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
12
src/infra/led.cpp
Normal file
12
src/infra/led.cpp
Normal 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
4
src/infra/led.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
void ledTurnOn();
|
||||
void ledTurnOff();
|
12
src/main.cpp
12
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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user