Final cleanup for hoerman esp-idf ready! Compiles agian, works again. esp-idf might need some trickyness to get running.

This commit is contained in:
2025-09-30 00:40:21 +02:00
parent b8581dcf31
commit 31861e7785
4 changed files with 59 additions and 61 deletions

View File

@@ -1,5 +1,4 @@
#include "hciemulator.h"
#include <string>
#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;
}