Files
espHome-NBS-files/external_components/hoermann_door/hoermann.h

99 lines
2.3 KiB
C++

#pragma once
#include <algorithm>
namespace esphome {
namespace hoermann_door {
class HoermannMainComponent;
}
}
#include "hciemulator.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace hoermann_door {
static const char *const COMP_TAG = "Hoermann";
void dispatcherFn(void *arg);
class HoermannMainComponent: public Component{
protected:
HCIEmulator* emulator;
TaskHandle_t modBusTask;
int log_level = 3; // Defaults to INFO
uart::UARTComponent* _uart;
InternalGPIOPin* _tx_on;
volatile bool set_continue_ = true;
volatile unsigned long lastCall = 0;
volatile unsigned long maxPeriod = 0;
public:
void set_seriel_connection(uart::UARTComponent* uart){
this->_uart = uart;
}
void set_tx_on_pin(InternalGPIOPin* pin){
this->_tx_on = pin;
}
void set_log_level(int level){
this->log_level = level;
}
HCIEmulator* getEmulator(){
return emulator;
}
void setup() override {
this->_tx_on->setup();
this->emulator = new HCIEmulator(this->_tx_on, this->_uart);
this->emulator->setLogLevel(this->log_level);
this->_tx_on->digital_write(false);
this->set_continue_ = true;
xTaskCreatePinnedToCore(
dispatcherFn, // Function to implement the task
"ModBusTask", // Name of the task
10000, // Stack size in words
this, // Task input parameter
// 1, // Priority of the task
configMAX_PRIORITIES - 1,
&modBusTask, // Task handle.
1 // Core where the task should run
);
}
void modBusPolling(void *parameter)
{
while (set_continue_)
{
if (lastCall > 0)
{
maxPeriod = std::max((micros() - lastCall), (unsigned long)maxPeriod);
}
lastCall = micros();
emulator->poll();
vTaskDelay(1);
}
vTaskDelete(NULL);
}
void dump_config() override {
ESP_LOGCONFIG(COMP_TAG, "hoermann_door_component:");
ESP_LOGCONFIG(COMP_TAG, " UART: %d", this->_uart->get_baud_rate());
//LOG_PIN(TAG, this->_tx_on);
}
};
void dispatcherFn(void *arg)
{
HoermannMainComponent *x = (HoermannMainComponent *)arg;
x->modBusPolling(arg);
}
}
}