diff --git a/external_components/hoermann_door/hciemulator.cpp b/external_components/hoermann_door/hciemulator.cpp index a432d0b..39b5e2a 100644 --- a/external_components/hoermann_door/hciemulator.cpp +++ b/external_components/hoermann_door/hciemulator.cpp @@ -1,5 +1,4 @@ #include "hciemulator.h" -#include #define CHECKCHANGEDSET(Target, Value, Flag) \ if ((Target) != (Value)) \ @@ -56,6 +55,12 @@ void HCIEmulator::setLogLevel(int level) hciloglevel = level; } +//#ifdef USE_ESP_IDF + uint16_t HCIEmulator::word(uint8_t high, uint8_t low){ + return (uint16_t) ((high << 8) | low); + } +//#endif + // modbus crc calculation borrowed from: // https://github.com/yaacov/ArduinoModbusSlave #define MODBUS_CRC_LENGTH 2 @@ -129,7 +134,13 @@ void HCIEmulator::poll() if (m_port->available() > 0) { // Serial.println("got data"); - m_rxlen += m_port->read_array((u_int8_t *)(m_rxbuffer + m_rxlen), _min((int)(255 - m_rxlen), m_port->available())); + int bytesToRead = std::min((int)(255 - m_rxlen), m_port->available()); + if(m_port->read_array((uint8_t *)(m_rxbuffer + m_rxlen), bytesToRead)) { + m_rxlen += bytesToRead; + } else { + Log(LL_ERROR, "Error reading from UART"); + //m_port->flush(); + } if (m_rxlen > 254) { Log(LL_ERROR, "RX Bufferoverflow, skip next Frame"); @@ -137,16 +148,16 @@ void HCIEmulator::poll() m_rxlen = 0; m_skipFrame = true; } - m_recvTime = micros(); + m_recvTime = esphome::micros(); } // Serial.printf("Data % x\n", m_txbuffer); // check frame, process frame - if (m_rxlen > 0 && (micros() - m_recvTime > T3_5)) + if (m_rxlen > 0 && (esphome::micros() - m_recvTime > T3_5)) { // Serial.printf("Act on it % x\n", m_txbuffer); // check last action timeout -> reset > then 2sec - if (m_statemachine != WAITING && m_lastStateTime + 2000 < millis()) + if (m_statemachine != WAITING && m_lastStateTime + 2000 < esphome::millis()) { m_statemachine = WAITING; } @@ -165,17 +176,17 @@ void HCIEmulator::poll() m_txbuffer[(m_txlen - MODBUS_CRC_LENGTH) + 1] = crc >> 8; // send data - m_lastSendTime = micros() - m_recvTime; + m_lastSendTime = esphome::micros() - m_recvTime; // Log(LL_DEBUG, ("ST:"+String(m_lastSendTime)).c_str()); - m_pin->digital_write(HIGH); + m_pin->digital_write(true); // Log3(LL_DEBUG, "write data: "); m_port->write_array(m_txbuffer, m_txlen); Log3(LL_DEBUG, "Response: ", m_txbuffer, m_txlen); - delayMicroseconds(m_txlen * 9 * 22); // 8 bits + par * Bittime 18 micros on 57600 bauds - m_pin->digital_write(LOW); + esphome::delayMicroseconds(m_txlen * 9 * 22); // 8 bits + par * Bittime 18 micros on 57600 bauds + m_pin->digital_write(false); m_txlen = 0; } } @@ -282,10 +293,10 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x02; m_txbuffer[8] = 0x10; m_statemachine = STARTOPENDOOR_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTOPENDOOR_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x01; m_txbuffer[8] = 0x10; @@ -298,10 +309,10 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x02; m_txbuffer[8] = 0x20; m_statemachine = STARTCLOSEDOOR_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTCLOSEDOOR_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x01; m_txbuffer[8] = 0x20; @@ -314,10 +325,10 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x02; m_txbuffer[8] = 0x40; m_statemachine = STARTSTOPDOOR_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTSTOPDOOR_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x01; m_txbuffer[8] = 0x40; @@ -330,10 +341,10 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x02; m_txbuffer[9] = 0x40; m_statemachine = STARTVENTPOSITION_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTVENTPOSITION_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x01; m_txbuffer[9] = 0x40; @@ -346,11 +357,11 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x02; m_txbuffer[9] = 0x04; m_statemachine = STARTOPENDOORHALF_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTOPENDOORHALF_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x01; m_txbuffer[9] = 0x04; @@ -363,10 +374,10 @@ void HCIEmulator::processDeviceStatusFrame() m_txbuffer[7] = 0x10; m_txbuffer[9] = 0x02; m_statemachine = STARTTOGGLELAMP_RELEASE; - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); break; case STARTTOGGLELAMP_RELEASE: - if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < millis()) + if (m_lastStateTime + SIMULATEKEYPRESSDELAYMS < esphome::millis()) { m_txbuffer[7] = 0x08; m_txbuffer[9] = 0x02; @@ -440,7 +451,7 @@ void HCIEmulator::openDoor() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTOPENDOOR; } @@ -450,7 +461,7 @@ void HCIEmulator::openDoorHalf() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTOPENDOORHALF; } @@ -460,7 +471,7 @@ void HCIEmulator::closeDoor() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTCLOSEDOOR; } @@ -470,7 +481,7 @@ void HCIEmulator::stopDoor() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTSTOPDOOR; } @@ -480,7 +491,7 @@ void HCIEmulator::toggleLamp() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTTOGGLELAMP; } @@ -490,7 +501,7 @@ void HCIEmulator::ventilationPosition() { return; } - m_lastStateTime = millis(); + m_lastStateTime = esphome::millis(); m_statemachine = STARTVENTPOSITION; } diff --git a/external_components/hoermann_door/hciemulator.h b/external_components/hoermann_door/hciemulator.h index 34ef0d3..bf313f4 100644 --- a/external_components/hoermann_door/hciemulator.h +++ b/external_components/hoermann_door/hciemulator.h @@ -2,10 +2,9 @@ #define __hciemulator_h #ifdef USE_ESP_IDF - #include - #include - #include - #include "esp_idf.h" + //#include + //#include + //#include #endif #include "esphome/components/uart/uart.h" @@ -95,7 +94,7 @@ public: const SHCIState &getState() { - if (micros() - m_recvTime > 2000000) + if (esphome::micros() - m_recvTime > 2000000) { // 2 sec without statusmessage m_state.valid = false; @@ -105,7 +104,7 @@ public: unsigned long getMessageAge() { - return micros() - m_recvTime; + return esphome::micros() - m_recvTime; } int getLogLevel(); @@ -118,6 +117,9 @@ protected: void processDeviceStatusFrame(); void processDeviceBusScanFrame(); void processBroadcastStatusFrame(); + //#ifdef USE_ESP_IDF + uint16_t word(uint8_t high, uint8_t low); + //#endif private: callback_function_t m_statusCallback; @@ -135,30 +137,14 @@ private: size_t m_rxlen; size_t m_txlen; - unsigned char m_rxbuffer[255] = { + uint8_t m_rxbuffer[255] = { 0, }; - unsigned char m_txbuffer[255] = { + uint8_t m_txbuffer[255] = { 0, }; bool m_skipFrame; }; -// Now, let's just define micros and millis and delay and delayMicros just so we know -#ifdef USE_ESP_IDF - unsigned long micros(){ - return (unsigned long) esp_timer_get_time(); - } - unsigned long millis(){ - return (unsigned long) esp_timer_get_time() / 1000; - } - void delay(uint32 duration){ - - } - void delayMicroseconds(uint32 duration){ - - } -#endif - #endif diff --git a/external_components/hoermann_door/hoermann.h b/external_components/hoermann_door/hoermann.h index 056176f..138f336 100644 --- a/external_components/hoermann_door/hoermann.h +++ b/external_components/hoermann_door/hoermann.h @@ -1,5 +1,7 @@ #pragma once +#include + class HoermannMainComponent; #include "hciemulator.h" #include "esphome/components/uart/uart.h" @@ -40,7 +42,7 @@ class HoermannMainComponent: public Component{ this->_tx_on->setup(); this->emulator = new HCIEmulator(this->_tx_on, this->_uart); - this->_tx_on->digital_write(LOW); + this->_tx_on->digital_write(false); this->set_continue_ = true; xTaskCreatePinnedToCore( dispatcherFn, // Function to implement the task @@ -53,11 +55,6 @@ class HoermannMainComponent: public Component{ 1 // Core where the task should run ); } - - void stop_polling(){ - this->_tx_on->digital_write(HIGH); - this->set_continue_ = false; - } void modBusPolling(void *parameter) { @@ -65,7 +62,7 @@ class HoermannMainComponent: public Component{ { if (lastCall > 0) { - maxPeriod = _max(micros() - lastCall, maxPeriod); + maxPeriod = std::max((micros() - lastCall), (unsigned long)maxPeriod); } lastCall = micros(); emulator->poll(); diff --git a/garage1.yaml b/garage1.yaml index 92e67a9..1363431 100644 --- a/garage1.yaml +++ b/garage1.yaml @@ -32,15 +32,19 @@ external_components: components: [ hoermann_door ] cover: - - platform: hoermann_door - name: door_${garageSide} - hoermann_controller: door_controll_internal + - platform: hoermann_door + name: door_${garageSide} + hoermann_controller: door_controll_internal uart: rx_pin: 16 tx_pin: 17 baud_rate: 57600 id: hm_connection + data_bits: 8 # Standard + parity: EVEN # NON Standard, None would be standard + stop_bits: 1 # Standard + hoermann_door: id: door_controll_internal