Skip to content

Commit

Permalink
Merge pull request #15 from AzonInc/dev
Browse files Browse the repository at this point in the history
Fix NPE & GPIO override
  • Loading branch information
AzonInc authored Sep 16, 2024
2 parents 8b3b468 + fcb8225 commit d52f86e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 77 deletions.
173 changes: 96 additions & 77 deletions components/tcs_intercom/tcs_intercom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,32 @@ namespace esphome
if(ver[0] > 0)
{
ESP_LOGI(TAG, "Doorman Hardware detected: V%i.%i.%i", ver[0], ver[1], ver[2]);
if (this->hardware_version_ != nullptr)
{
this->hardware_version_->publish_state("Doorman " + std::to_string(ver[0]) + "." + std::to_string(ver[1]) + "." + std::to_string(ver[2]));
}
}
else
{
if (this->hardware_version_ != nullptr)
this->hardware_version_str_ = "Doorman " + std::to_string(ver[0]) + "." + std::to_string(ver[1]) + "." + std::to_string(ver[2]);

// Override GPIO
if(ver[0] == 1 && (ver[1] == 3 || ver[1] == 4))
{
this->hardware_version_->publish_state("Generic");
esp32::ESP32InternalGPIOPin *gpio_pin_rx_;
gpio_pin_rx_ = new(esp32::ESP32InternalGPIOPin);
gpio_pin_rx_->set_pin(static_cast<gpio_num_t>(9));
gpio_pin_rx_->set_flags(gpio::Flags::FLAG_INPUT);
this->set_rx_pin(gpio_pin_rx_);

esp32::ESP32InternalGPIOPin *gpio_pin_tx_;
gpio_pin_tx_ = new(esp32::ESP32InternalGPIOPin);
gpio_pin_tx_->set_pin(static_cast<gpio_num_t>(8));
gpio_pin_tx_->set_flags(gpio::Flags::FLAG_OUTPUT);
this->set_tx_pin(gpio_pin_tx_);

ESP_LOGD(TAG, "Doorman Hardware GPIO Override: RX (%i), TX (%i)", this->rx_pin_->get_pin(), this->tx_pin_->get_pin());
}
}
#else
#endif

if (this->hardware_version_ != nullptr)
{
this->hardware_version_->publish_state("Generic");
this->hardware_version_->publish_state(this->hardware_version_str_);
}
#endif


this->rx_pin_->setup();
Expand Down Expand Up @@ -105,7 +113,12 @@ namespace esphome
ESP_LOGCONFIG(TAG, " Event: disabled");
}

ESP_LOGCONFIG(TAG, " Hardware: %s", this->hardware_version_->state.c_str());
if (this->hardware_version_ != nullptr)
{
this->hardware_version_->publish_state("Generic");
}

ESP_LOGCONFIG(TAG, " Hardware: %s", this->hardware_version_str_.c_str());
}

void TCSComponent::loop()
Expand Down Expand Up @@ -167,7 +180,11 @@ namespace esphome

uint8_t curBit = 4;

if (timeInUS >= 1000 && timeInUS <= 2999)
if (timeInUS < 1000)
{
curBit = 5; // invalid glitches typical 29ms
}
else if (timeInUS >= 1000 && timeInUS <= 2999)
{
curBit = 0;
}
Expand All @@ -185,42 +202,63 @@ namespace esphome
curPos = 0;
}

if (curPos == 0)
{
if (curBit == 2)
if (curBit != 5) { // skip processing for glitches
if (curPos == 0)
{
curPos++;
}
if (curBit == 2)
{
curPos++;
}

curCMD = 0;
curCRC = 0;
calCRC = 1;
curLength = 0;
}
else if (curBit == 0 || curBit == 1)
{
if (curPos == 1)
{
curLength = curBit;
curPos++;
curCMD = 0;
curCRC = 0;
calCRC = 1;
curLength = 0;
}
else if (curPos >= 2 && curPos <= 17)
else if (curBit == 0 || curBit == 1)
{
if (curBit)
if (curPos == 1)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, (curLength ? 33 : 17) - curPos);
#else
bitSet(curCMD, (curLength ? 33 : 17) - curPos);
#endif
curLength = curBit;
curPos++;
}
else if (curPos >= 2 && curPos <= 17)
{
if (curBit)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, (curLength ? 33 : 17) - curPos);
#else
bitSet(curCMD, (curLength ? 33 : 17) - curPos);
#endif
}

calCRC ^= curBit;
curPos++;
}
else if (curPos == 18)
{
if (curLength)
calCRC ^= curBit;
curPos++;
}
else if (curPos == 18)
{
if (curLength)
{
if (curBit)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, 33 - curPos);
#else
bitSet(curCMD, 33 - curPos);
#endif
}

calCRC ^= curBit;
curPos++;
}
else
{
curCRC = curBit;
cmdIntReady = 1;
}
}
else if (curPos >= 19 && curPos <= 33)
{
if (curBit)
{
Expand All @@ -230,61 +268,42 @@ namespace esphome
bitSet(curCMD, 33 - curPos);
#endif
}

calCRC ^= curBit;
curPos++;
}
else
else if (curPos == 34)
{
curCRC = curBit;
cmdIntReady = 1;
}
}
else if (curPos >= 19 && curPos <= 33)
else
{
if (curBit)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, 33 - curPos);
#else
bitSet(curCMD, 33 - curPos);
#endif
}

calCRC ^= curBit;
curPos++;
curPos = 0;
}
else if (curPos == 34)

if (cmdIntReady)
{
curCRC = curBit;
cmdIntReady = 1;
}
}
else
{
curPos = 0;
}
cmdIntReady = 0;

if (cmdIntReady)
{
cmdIntReady = 0;
if (curCRC == calCRC)
{
arg->s_cmdReady = true;
arg->s_cmd = curCMD;
}

if (curCRC == calCRC)
{
arg->s_cmdReady = true;
arg->s_cmd = curCMD;
curCMD = 0;
curPos = 0;
}

curCMD = 0;
curPos = 0;
}
}

void TCSComponent::publish_command(uint32_t command, bool fire_events)
{
// Convert to HEX
char byte_cmd[9];
sprintf(byte_cmd, "%08x", command);
sprintf(byte_cmd, "%08X", command);

// Publish Command to Sensors
if (this->bus_command_ != nullptr)
Expand Down
2 changes: 2 additions & 0 deletions components/tcs_intercom/tcs_intercom.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace esphome

text_sensor::TextSensor *bus_command_{nullptr};
text_sensor::TextSensor *hardware_version_{nullptr};

std::string hardware_version_str_ = "Generic";
};

template<typename... Ts> class TCSIntercomSendAction : public Action<Ts...>
Expand Down

0 comments on commit d52f86e

Please sign in to comment.