espHome-NBS-files/external_components/hoermann_door/door_singleton.h

79 lines
1.8 KiB
C++

#pragma once
#include "esphome.h"
#include "Arduino.h"
#include "hciemulator.h"
//#include "cover.h"
#define RS485 Serial2
#define TX_ON 25
//#define configMAX_PRIORITIES 25
#define TAG "hoermann_door"
void modBusPolling(void *parameter);
class HoermannSingleton {
public:
HoermannSingleton() {
emulator;
}
static HoermannSingleton* instance_;
HCIEmulator emulator;
TaskHandle_t modBusTask;
bool hasBeenInitialized = false;
public:
static HoermannSingleton* getInstance(){
if(instance_ == nullptr){
instance_ = new HoermannSingleton();
}
return instance_;
}
void initializeEmulator(){
if(hasBeenInitialized) return;
hasBeenInitialized = true;
RS485.begin(57600, SERIAL_8E1, 16, 17);
pinMode(TX_ON, OUTPUT);
digitalWrite(TX_ON, LOW);
xTaskCreatePinnedToCore(
modBusPolling, // Function to implement the task
"ModBusTask", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
// 1, // Priority of the task
configMAX_PRIORITIES - 1,
&modBusTask, // Task handle.
1 // Core where the task should run
);
}
HCIEmulator *getEmulator(){
return &emulator;
}
};
HoermannSingleton* HoermannSingleton::instance_ = nullptr;
//DoorManager *DoorManager::getInstance()
volatile unsigned long lastCall = 0;
volatile unsigned long maxPeriod = 0;
void modBusPolling(void *parameter)
{
auto emulator = HoermannSingleton::getInstance()->getEmulator();
while (true)
{
if (lastCall > 0)
{
maxPeriod = _max(micros() - lastCall, maxPeriod);
}
lastCall = micros();
emulator->poll();
vTaskDelay(1);
}
vTaskDelete(NULL);
}