#pragma once #include class HoermannMainComponent; #include "hciemulator.h" #include "esphome/components/uart/uart.h" namespace esphome { namespace hoermann_door { static const char *const TAG = "Hoermann"; void dispatcherFn(void *arg); class HoermannMainComponent: public Component{ protected: HCIEmulator* emulator; TaskHandle_t modBusTask; 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; } HCIEmulator* getEmulator(){ return emulator; } void setup() override { this->_tx_on->setup(); this->emulator = new HCIEmulator(this->_tx_on, this->_uart); 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(TAG, "hoermann_door_component:"); ESP_LOGCONFIG(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); } } }