switch between intercoms
This commit is contained in:
parent
ae46832d88
commit
324d97c0ce
@ -244,6 +244,8 @@ void initRoutes() {
|
||||
server.on("/api/v1/networkSettings", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, networkSettingsUpdate);
|
||||
|
||||
server.on("/api/v1/intercomStatus", intercomStatus);
|
||||
//server.on("/api/v1/intercomSettings", HTTP_GET, intercomSettingsRead);
|
||||
//server.on("/api/v1/intercomSettings", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, intercomSettingsUpdate);
|
||||
|
||||
server.on("/api/v1/mqttStatus", mqttStatus);
|
||||
server.on("/api/v1/mqttSettings", HTTP_GET, mqttSettingsRead);
|
||||
|
@ -24,7 +24,7 @@ void resetCounters() {
|
||||
}
|
||||
|
||||
void writeState(char* message) {
|
||||
//println(message);
|
||||
println(message);
|
||||
publishToMQTT(STATE_MQTT_TOPIC, message);
|
||||
}
|
||||
|
27
src/domain/stateMachineController.cpp
Normal file
27
src/domain/stateMachineController.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "domain/stateMachineController.h"
|
||||
|
||||
StateMachineController::StateMachineController() : _strategy(nullptr) {}
|
||||
|
||||
void StateMachineController::setStrategy(StateMachineStrategy* newStrategy) {
|
||||
if (_strategy) {
|
||||
delete _strategy;
|
||||
}
|
||||
|
||||
_strategy = newStrategy;
|
||||
if (_strategy) {
|
||||
_strategy->setup();
|
||||
}
|
||||
}
|
||||
|
||||
void StateMachineController::updateStateMachine(int data) {
|
||||
if (_strategy) {
|
||||
_strategy->updateStateMachine(data);
|
||||
}
|
||||
}
|
||||
|
||||
StateMachineController::~StateMachineController() {
|
||||
// Cleanup the strategy in the destructor
|
||||
if (_strategy) {
|
||||
delete _strategy;
|
||||
}
|
||||
}
|
14
src/domain/stateMachineController.h
Normal file
14
src/domain/stateMachineController.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "domain/stateMachineStrategy.h"
|
||||
|
||||
class StateMachineController {
|
||||
private:
|
||||
StateMachineStrategy* _strategy;
|
||||
|
||||
public:
|
||||
StateMachineController();
|
||||
void setStrategy(StateMachineStrategy* newStrategy);
|
||||
void updateStateMachine(int data);
|
||||
~StateMachineController();
|
||||
};
|
8
src/domain/stateMachineStrategy.h
Normal file
8
src/domain/stateMachineStrategy.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
class StateMachineStrategy {
|
||||
public:
|
||||
virtual void setup() = 0;
|
||||
virtual void updateStateMachine(int data) = 0;
|
||||
virtual ~StateMachineStrategy() {}
|
||||
};
|
141
src/domain/strategies/cyfralStrategy.cpp
Normal file
141
src/domain/strategies/cyfralStrategy.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
#include "domain/strategies/cyfralStrategy.h"
|
||||
|
||||
CyfralStrategy::CyfralStrategy() {}
|
||||
|
||||
void CyfralStrategy::setup() {
|
||||
_initStateMachine();
|
||||
}
|
||||
|
||||
void CyfralStrategy::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) {
|
||||
_flatReceived();
|
||||
_changeState(DATA_RECEIVED);
|
||||
}
|
||||
} else if (data == HIGH) {
|
||||
_countZeros = 0;
|
||||
_countOnes++;
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case DATA_RECEIVED:
|
||||
if (data == LOW) {
|
||||
_countZeros++;
|
||||
if (_countZeros >= CALL_ENDED_THRESHOLD) {
|
||||
_changeState(CALL_ENDED);
|
||||
}
|
||||
} else if (data == HIGH) {
|
||||
_countOnes++;
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CALL_ENDED:
|
||||
if (data == LOW) {
|
||||
_countZeros++;
|
||||
if (_countZeros >= NOT_CONNECTED_THRESHOLD) {
|
||||
_changeState(NOT_CONNECTED);
|
||||
}
|
||||
} else if (data == HIGH) {
|
||||
_countOnes++;
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CyfralStrategy::_resetCounters() {
|
||||
_countZeros = 0;
|
||||
_countOnes = 0;
|
||||
|
||||
_previousData = 0;
|
||||
_dataLength = 0;
|
||||
_signalDuration = 0;
|
||||
}
|
||||
|
||||
void CyfralStrategy::_writeState(char* message) {
|
||||
println(message);
|
||||
publishToMQTT(STATE_MQTT_TOPIC, message);
|
||||
}
|
||||
|
||||
void CyfralStrategy::_receiveData(int data) {
|
||||
if (data != _previousData) {
|
||||
if (_previousData == HIGH) {
|
||||
//println("AAAA ", dataLength, " ", signalDuration);
|
||||
_dataLength++;
|
||||
}
|
||||
_signalDuration = 0;
|
||||
} else {
|
||||
_signalDuration++;
|
||||
}
|
||||
_previousData = data;
|
||||
}
|
||||
|
||||
void CyfralStrategy::_changeState(State state, bool resetCountersFlag) {
|
||||
_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 CyfralStrategy::_flatReceived() {
|
||||
int flat = _dataLength/2;
|
||||
|
||||
if (flat < 1)
|
||||
return;
|
||||
|
||||
println("| data length: ", _dataLength);
|
||||
println("| flat: ", flat);
|
||||
publishToMQTT(FLAT_NUMBER_MQTT_TOPIC, flat);
|
||||
}
|
||||
|
||||
void CyfralStrategy::_initStateMachine() {
|
||||
_changeState(CONNECTED);
|
||||
}
|
42
src/domain/strategies/cyfralStrategy.h
Normal file
42
src/domain/strategies/cyfralStrategy.h
Normal file
@ -0,0 +1,42 @@
|
||||
#include "utils/print.h"
|
||||
#include "config/config.h"
|
||||
#include "infra/mqtt.h"
|
||||
#include "infra/led.h"
|
||||
#include "domain/stateMachineStrategy.h"
|
||||
|
||||
class CyfralStrategy : public StateMachineStrategy {
|
||||
private:
|
||||
enum State {
|
||||
NOT_CONNECTED,
|
||||
CONNECTED,
|
||||
RECEIVING_DATA,
|
||||
DATA_RECEIVED,
|
||||
CALL_ENDED
|
||||
};
|
||||
|
||||
const int CONNECTED_THRESHOLD = 50000;
|
||||
const int NOT_CONNECTED_THRESHOLD = 50000;
|
||||
const int INITIALIZING_CALL_THRESHOLD = 15000;
|
||||
const int DATA_RECEIVED_THESHOLD = 30000;
|
||||
const int CALL_ENDED_THRESHOLD = 10000;
|
||||
|
||||
State _currentState = NOT_CONNECTED;
|
||||
int _countZeros = 0;
|
||||
int _countOnes = 0;
|
||||
|
||||
int _previousData = 0;
|
||||
int _dataLength = 0;
|
||||
int _signalDuration = 0;
|
||||
|
||||
void _resetCounters();
|
||||
void _writeState(char* message);
|
||||
void _receiveData(int data);
|
||||
void _changeState(State state, bool resetCountersFlag = true);
|
||||
void _flatReceived();
|
||||
void _initStateMachine();
|
||||
|
||||
public:
|
||||
CyfralStrategy();
|
||||
void setup() override;
|
||||
void updateStateMachine(int data) override;
|
||||
};
|
171
src/domain/strategies/vizitStrategy.cpp
Normal file
171
src/domain/strategies/vizitStrategy.cpp
Normal file
@ -0,0 +1,171 @@
|
||||
#include "domain/strategies/vizitStrategy.h"
|
||||
|
||||
VizitStrategy::VizitStrategy() {}
|
||||
|
||||
void VizitStrategy::setup() {
|
||||
_initStateMachine();
|
||||
}
|
||||
|
||||
void VizitStrategy::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_FIRST_DIGIT);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RECEIVING_FIRST_DIGIT:
|
||||
_receiveDigit(data);
|
||||
|
||||
if (data == LOW) {
|
||||
_countOnes = 0;
|
||||
_countZeros++;
|
||||
} else if (data == HIGH) {
|
||||
_countZeros = 0;
|
||||
_countOnes++;
|
||||
if (_countOnes >= DATA_RECEIVED_THESHOLD) {
|
||||
_firstDigitReceived();
|
||||
_changeState(RECEIVING_SECOND_DIGIT);
|
||||
}
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RECEIVING_SECOND_DIGIT:
|
||||
_receiveDigit(data);
|
||||
|
||||
if (data == LOW) {
|
||||
_countOnes = 0;
|
||||
_countZeros++;
|
||||
} else if (data == HIGH) {
|
||||
_countZeros = 0;
|
||||
_countOnes++;
|
||||
if (_countOnes >= DATA_RECEIVED_THESHOLD) {
|
||||
_secondDigitReceived();
|
||||
_changeState(DATA_RECEIVED);
|
||||
}
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case DATA_RECEIVED:
|
||||
if (data == LOW) {
|
||||
_countZeros++;
|
||||
if (_countZeros >= CALL_ENDED_THRESHOLD) {
|
||||
_changeState(CALL_ENDED);
|
||||
}
|
||||
} else if (data == HIGH) {
|
||||
_countOnes++;
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CALL_ENDED:
|
||||
if (data == LOW) {
|
||||
_countZeros++;
|
||||
if (_countZeros >= NOT_CONNECTED_THRESHOLD) {
|
||||
_changeState(NOT_CONNECTED);
|
||||
}
|
||||
} else if (data == HIGH) {
|
||||
_countOnes++;
|
||||
if (_countOnes >= CONNECTED_THRESHOLD) {
|
||||
_changeState(CONNECTED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VizitStrategy::_resetCounters() {
|
||||
_countZeros = 0;
|
||||
_countOnes = 0;
|
||||
|
||||
_previousData = 0;
|
||||
_dataLength = 1;
|
||||
_signalDuration = 0;
|
||||
}
|
||||
|
||||
void VizitStrategy::_writeState(char* message) {
|
||||
println(message);
|
||||
publishToMQTT(STATE_MQTT_TOPIC, message);
|
||||
}
|
||||
|
||||
void VizitStrategy::_receiveDigit(int data) {
|
||||
if (data != _previousData) {
|
||||
if (_previousData == HIGH) {
|
||||
//println("AAAA ", dataLength, " ", signalDuration);
|
||||
_dataLength++;
|
||||
}
|
||||
_signalDuration = 0;
|
||||
} else {
|
||||
_signalDuration++;
|
||||
}
|
||||
_previousData = data;
|
||||
}
|
||||
|
||||
void VizitStrategy::_changeState(State state, bool resetCountersFlag) {
|
||||
_currentState = state;
|
||||
|
||||
switch (state) {
|
||||
case NOT_CONNECTED:
|
||||
ledTurnOff();
|
||||
_writeState("not connected");
|
||||
break;
|
||||
case CONNECTED:
|
||||
ledTurnOn();
|
||||
_writeState("connected");
|
||||
break;
|
||||
case RECEIVING_FIRST_DIGIT:
|
||||
_writeState("receiving data");
|
||||
break;
|
||||
}
|
||||
|
||||
if (resetCountersFlag)
|
||||
_resetCounters();
|
||||
}
|
||||
|
||||
void VizitStrategy::_firstDigitReceived() {
|
||||
println("| 1 data length: ", _dataLength);
|
||||
_flat = _dataLength*10;
|
||||
}
|
||||
|
||||
void VizitStrategy::_secondDigitReceived() {
|
||||
if (_dataLength == 11) {
|
||||
_dataLength = 1;
|
||||
}
|
||||
|
||||
_flat += _dataLength;
|
||||
_flat -= 1;
|
||||
|
||||
if (_flat > 100 && _flat < 110) {
|
||||
_flat -= 100;
|
||||
}
|
||||
|
||||
println("| 2 data length: ", _dataLength);
|
||||
println("| flat: ", _flat);
|
||||
publishToMQTT(FLAT_NUMBER_MQTT_TOPIC, _flat);
|
||||
}
|
||||
|
||||
void VizitStrategy::_initStateMachine() {
|
||||
_changeState(CONNECTED);
|
||||
}
|
46
src/domain/strategies/vizitStrategy.h
Normal file
46
src/domain/strategies/vizitStrategy.h
Normal file
@ -0,0 +1,46 @@
|
||||
#include "utils/print.h"
|
||||
#include "config/config.h"
|
||||
#include "infra/mqtt.h"
|
||||
#include "infra/led.h"
|
||||
#include "domain/stateMachineStrategy.h"
|
||||
|
||||
class VizitStrategy : public StateMachineStrategy {
|
||||
private:
|
||||
enum State {
|
||||
NOT_CONNECTED,
|
||||
CONNECTED,
|
||||
RECEIVING_FIRST_DIGIT,
|
||||
RECEIVING_SECOND_DIGIT,
|
||||
DATA_RECEIVED,
|
||||
CALL_ENDED
|
||||
};
|
||||
|
||||
const int CONNECTED_THRESHOLD = 50000;
|
||||
const int NOT_CONNECTED_THRESHOLD = 50000;
|
||||
const int INITIALIZING_CALL_THRESHOLD = 45;
|
||||
const int DATA_RECEIVED_THESHOLD = 5000;
|
||||
const int CALL_ENDED_THRESHOLD = 10000;
|
||||
|
||||
State _currentState = NOT_CONNECTED;
|
||||
int _countZeros = 0;
|
||||
int _countOnes = 0;
|
||||
|
||||
int _previousData = 0;
|
||||
int _dataLength = 0;
|
||||
int _signalDuration = 0;
|
||||
|
||||
int _flat = 0;
|
||||
|
||||
void _resetCounters();
|
||||
void _writeState(char* message);
|
||||
void _receiveDigit(int data);
|
||||
void _changeState(State state, bool resetCountersFlag=true);
|
||||
void _firstDigitReceived();
|
||||
void _secondDigitReceived();
|
||||
void _initStateMachine();
|
||||
|
||||
public:
|
||||
VizitStrategy();
|
||||
void setup() override;
|
||||
void updateStateMachine(int data) override;
|
||||
};
|
17
src/main.cpp
17
src/main.cpp
@ -16,22 +16,33 @@
|
||||
#include "infra/fs.h"
|
||||
#include "infra/httpServer.h"
|
||||
|
||||
#include "domain/stateMachine.h"
|
||||
#include "app/routes.h"
|
||||
|
||||
#include "domain/stateMachineController.h"
|
||||
#include "domain/strategies/cyfralStrategy.h"
|
||||
|
||||
uint32_t lastMillis;
|
||||
uint64_t lastMicros;
|
||||
|
||||
int data = 0;
|
||||
|
||||
bool flag = false;
|
||||
int zeros = 0;
|
||||
int ones = 0;
|
||||
|
||||
StateMachineController controller;
|
||||
CyfralStrategy strategy;
|
||||
|
||||
void IRAM_ATTR one() {
|
||||
flag = true;
|
||||
//zeros = 0;
|
||||
ones += 1;
|
||||
}
|
||||
|
||||
void IRAM_ATTR zero() {
|
||||
flag = false;
|
||||
//ones = 0;
|
||||
zeros += 1;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@ -60,12 +71,14 @@ void setup() {
|
||||
initMQTT();
|
||||
initRoutes();
|
||||
initHttpServer();
|
||||
|
||||
controller.setStrategy(&strategy);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (lastMicros < micros()) {
|
||||
data = digitalRead(DATA_PIN);
|
||||
updateStateMachine(data);
|
||||
controller.updateStateMachine(data);
|
||||
|
||||
if (PRINT_RAW_SIGNAL_FLAG)
|
||||
printf("{}", data);
|
||||
|
Loading…
x
Reference in New Issue
Block a user