Merge pull request #1531 from esphome/bump-1.16.1

1.16.1
This commit is contained in:
Jesse Hills 2021-02-14 15:04:43 +13:00 committed by GitHub
commit 925a992d1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 248 additions and 115 deletions

View File

@ -16,7 +16,7 @@ ENV USERNAME="" PASSWORD=""
EXPOSE 6052 EXPOSE 6052
# Run healthcheck (heartbeat) # Run healthcheck (heartbeat)
HEALTHCHECK --interval=5m --timeout=3s \ HEALTHCHECK --interval=30s --timeout=30s \
CMD curl --fail http://localhost:6052 || exit 1 CMD curl --fail http://localhost:6052 || exit 1
# The directory the user should mount their configuration files to # The directory the user should mount their configuration files to

View File

@ -79,6 +79,12 @@ void HLW8012Component::update() {
this->power_sensor_->publish_state(power); this->power_sensor_->publish_state(power);
} }
if (this->energy_sensor_ != nullptr) {
cf_total_pulses_ += raw_cf;
float energy = cf_total_pulses_ * power_multiplier_micros / 3600 / 1000000.0f;
this->energy_sensor_->publish_state(energy);
}
if (this->change_mode_at_++ == this->change_mode_every_) { if (this->change_mode_at_++ == this->change_mode_every_) {
this->current_mode_ = !this->current_mode_; this->current_mode_ = !this->current_mode_;
ESP_LOGV(TAG, "Changing mode to %s mode", this->current_mode_ ? "CURRENT" : "VOLTAGE"); ESP_LOGV(TAG, "Changing mode to %s mode", this->current_mode_ ? "CURRENT" : "VOLTAGE");

View File

@ -29,6 +29,7 @@ class HLW8012Component : public PollingComponent {
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; } void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; } void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
protected: protected:
uint32_t nth_value_{0}; uint32_t nth_value_{0};
@ -37,6 +38,7 @@ class HLW8012Component : public PollingComponent {
uint32_t change_mode_every_{8}; uint32_t change_mode_every_{8};
float current_resistor_{0.001}; float current_resistor_{0.001};
float voltage_divider_{2351}; float voltage_divider_{2351};
uint64_t cf_total_pulses_{0};
GPIOPin *sel_pin_; GPIOPin *sel_pin_;
GPIOPin *cf_pin_; GPIOPin *cf_pin_;
pulse_counter::PulseCounterStorage cf_store_; pulse_counter::PulseCounterStorage cf_store_;
@ -45,6 +47,7 @@ class HLW8012Component : public PollingComponent {
sensor::Sensor *voltage_sensor_{nullptr}; sensor::Sensor *voltage_sensor_{nullptr};
sensor::Sensor *current_sensor_{nullptr}; sensor::Sensor *current_sensor_{nullptr};
sensor::Sensor *power_sensor_{nullptr}; sensor::Sensor *power_sensor_{nullptr};
sensor::Sensor *energy_sensor_{nullptr};
}; };
} // namespace hlw8012 } // namespace hlw8012

View File

@ -3,8 +3,8 @@ import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import sensor from esphome.components import sensor
from esphome.const import CONF_CHANGE_MODE_EVERY, CONF_INITIAL_MODE, CONF_CURRENT, \ from esphome.const import CONF_CHANGE_MODE_EVERY, CONF_INITIAL_MODE, CONF_CURRENT, \
CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_SEL_PIN, CONF_VOLTAGE, CONF_VOLTAGE_DIVIDER, \ CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_ENERGY, CONF_SEL_PIN, CONF_VOLTAGE, \
ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT CONF_VOLTAGE_DIVIDER, ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, UNIT_WATT_HOURS
AUTO_LOAD = ['pulse_counter'] AUTO_LOAD = ['pulse_counter']
@ -29,6 +29,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1),
cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2),
cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 1), cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 1),
cv.Optional(CONF_ENERGY): sensor.sensor_schema(UNIT_WATT_HOURS, ICON_FLASH, 1),
cv.Optional(CONF_CURRENT_RESISTOR, default=0.001): cv.resistance, cv.Optional(CONF_CURRENT_RESISTOR, default=0.001): cv.resistance,
cv.Optional(CONF_VOLTAGE_DIVIDER, default=2351): cv.positive_float, cv.Optional(CONF_VOLTAGE_DIVIDER, default=2351): cv.positive_float,
@ -57,6 +58,9 @@ def to_code(config):
if CONF_POWER in config: if CONF_POWER in config:
sens = yield sensor.new_sensor(config[CONF_POWER]) sens = yield sensor.new_sensor(config[CONF_POWER])
cg.add(var.set_power_sensor(sens)) cg.add(var.set_power_sensor(sens))
if CONF_ENERGY in config:
sens = yield sensor.new_sensor(config[CONF_ENERGY])
cg.add(var.set_energy_sensor(sens))
cg.add(var.set_current_resistor(config[CONF_CURRENT_RESISTOR])) cg.add(var.set_current_resistor(config[CONF_CURRENT_RESISTOR]))
cg.add(var.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER])) cg.add(var.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER]))
cg.add(var.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY])) cg.add(var.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY]))

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import i2c from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT
DEPENDENCIES = ['i2c'] DEPENDENCIES = ['i2c']
MULTI_CONF = True MULTI_CONF = True
@ -20,6 +20,7 @@ MCP23008GPIOPin = mcp23008_ns.class_('MCP23008GPIOPin', cg.GPIOPin)
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(MCP23008), cv.Required(CONF_ID): cv.declare_id(MCP23008),
cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))
@ -27,6 +28,7 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)
cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT]))
CONF_MCP23008 = 'mcp23008' CONF_MCP23008 = 'mcp23008'

View File

@ -14,8 +14,10 @@ void MCP23008::setup() {
return; return;
} }
// all pins input if (this->open_drain_ints_) {
this->write_reg_(MCP23008_IODIR, 0xFF); // enable open-drain interrupt pins, 3.3V-safe
this->write_reg_(MCP23008_IOCON, 0x04);
}
} }
bool MCP23008::digital_read(uint8_t pin) { bool MCP23008::digital_read(uint8_t pin) {
uint8_t bit = pin % 8; uint8_t bit = pin % 8;

View File

@ -39,6 +39,8 @@ class MCP23008 : public Component, public i2c::I2CDevice {
void digital_write(uint8_t pin, bool value); void digital_write(uint8_t pin, bool value);
void pin_mode(uint8_t pin, uint8_t mode); void pin_mode(uint8_t pin, uint8_t mode);
void set_open_drain_ints(const bool value) { open_drain_ints_ = value; }
float get_setup_priority() const override; float get_setup_priority() const override;
protected: protected:
@ -50,6 +52,7 @@ class MCP23008 : public Component, public i2c::I2CDevice {
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a); void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a);
uint8_t olat_{0x00}; uint8_t olat_{0x00};
bool open_drain_ints_;
}; };
class MCP23008GPIOPin : public GPIOPin { class MCP23008GPIOPin : public GPIOPin {

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import i2c from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT
DEPENDENCIES = ['i2c'] DEPENDENCIES = ['i2c']
MULTI_CONF = True MULTI_CONF = True
@ -20,6 +20,7 @@ MCP23017GPIOPin = mcp23017_ns.class_('MCP23017GPIOPin', cg.GPIOPin)
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(MCP23017), cv.Required(CONF_ID): cv.declare_id(MCP23017),
cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))
@ -27,6 +28,7 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)
cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT]))
CONF_MCP23017 = 'mcp23017' CONF_MCP23017 = 'mcp23017'

View File

@ -14,9 +14,11 @@ void MCP23017::setup() {
return; return;
} }
// all pins input if (this->open_drain_ints_) {
this->write_reg_(MCP23017_IODIRA, 0xFF); // enable open-drain interrupt pins, 3.3V-safe
this->write_reg_(MCP23017_IODIRB, 0xFF); this->write_reg_(MCP23017_IOCONA, 0x04);
this->write_reg_(MCP23017_IOCONB, 0x04);
}
} }
bool MCP23017::digital_read(uint8_t pin) { bool MCP23017::digital_read(uint8_t pin) {
uint8_t bit = pin % 8; uint8_t bit = pin % 8;

View File

@ -51,6 +51,8 @@ class MCP23017 : public Component, public i2c::I2CDevice {
void digital_write(uint8_t pin, bool value); void digital_write(uint8_t pin, bool value);
void pin_mode(uint8_t pin, uint8_t mode); void pin_mode(uint8_t pin, uint8_t mode);
void set_open_drain_ints(const bool value) { open_drain_ints_ = value; }
float get_setup_priority() const override; float get_setup_priority() const override;
protected: protected:
@ -63,6 +65,7 @@ class MCP23017 : public Component, public i2c::I2CDevice {
uint8_t olat_a_{0x00}; uint8_t olat_a_{0x00};
uint8_t olat_b_{0x00}; uint8_t olat_b_{0x00};
bool open_drain_ints_;
}; };
class MCP23017GPIOPin : public GPIOPin { class MCP23017GPIOPin : public GPIOPin {

View File

@ -22,7 +22,7 @@ void PN532::setup() {
} }
std::vector<uint8_t> version_data; std::vector<uint8_t> version_data;
if (!this->read_response_(PN532_COMMAND_VERSION_DATA, version_data)) { if (!this->read_response(PN532_COMMAND_VERSION_DATA, version_data)) {
ESP_LOGE(TAG, "Error getting version"); ESP_LOGE(TAG, "Error getting version");
this->mark_failed(); this->mark_failed();
return; return;
@ -42,7 +42,7 @@ void PN532::setup() {
} }
std::vector<uint8_t> wakeup_result; std::vector<uint8_t> wakeup_result;
if (!this->read_response_(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) { if (!this->read_response(PN532_COMMAND_SAMCONFIGURATION, wakeup_result)) {
this->error_code_ = WAKEUP_FAILED; this->error_code_ = WAKEUP_FAILED;
this->mark_failed(); this->mark_failed();
return; return;
@ -62,7 +62,7 @@ void PN532::setup() {
} }
std::vector<uint8_t> sam_result; std::vector<uint8_t> sam_result;
if (!this->read_response_(PN532_COMMAND_SAMCONFIGURATION, sam_result)) { if (!this->read_response(PN532_COMMAND_SAMCONFIGURATION, sam_result)) {
ESP_LOGV(TAG, "Invalid SAM result: (%u)", sam_result.size()); // NOLINT ESP_LOGV(TAG, "Invalid SAM result: (%u)", sam_result.size()); // NOLINT
for (uint8_t dat : sam_result) { for (uint8_t dat : sam_result) {
ESP_LOGV(TAG, " 0x%02X", dat); ESP_LOGV(TAG, " 0x%02X", dat);
@ -97,7 +97,7 @@ void PN532::loop() {
return; return;
std::vector<uint8_t> read; std::vector<uint8_t> read;
bool success = this->read_response_(PN532_COMMAND_INLISTPASSIVETARGET, read); bool success = this->read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
this->requested_read_ = false; this->requested_read_ = false;
@ -230,7 +230,7 @@ bool PN532::write_command_(const std::vector<uint8_t> &data) {
} }
bool PN532::read_ack_() { bool PN532::read_ack_() {
ESP_LOGVV(TAG, "Reading ACK..."); ESP_LOGV(TAG, "Reading ACK...");
std::vector<uint8_t> data; std::vector<uint8_t> data;
if (!this->read_data(data, 6)) { if (!this->read_data(data, 6)) {
@ -241,96 +241,18 @@ bool PN532::read_ack_() {
data[2] == 0x00 && // start of packet data[2] == 0x00 && // start of packet
data[3] == 0xFF && data[4] == 0x00 && // ACK packet code data[3] == 0xFF && data[4] == 0x00 && // ACK packet code
data[5] == 0xFF && data[6] == 0x00); // postamble data[5] == 0xFF && data[6] == 0x00); // postamble
ESP_LOGVV(TAG, "ACK valid: %s", YESNO(matches)); ESP_LOGV(TAG, "ACK valid: %s", YESNO(matches));
return matches; return matches;
} }
bool PN532::read_response_(uint8_t command, std::vector<uint8_t> &data) { void PN532::send_nack_() {
ESP_LOGV(TAG, "Reading response"); ESP_LOGV(TAG, "Sending NACK for retransmit");
uint8_t len = this->read_response_length_(); this->write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
if (len == 0) { delay(10);
return false;
}
ESP_LOGV(TAG, "Reading response of length %d", len);
if (!this->read_data(data, 6 + len + 2)) {
ESP_LOGD(TAG, "No response data");
return false;
}
if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
// invalid packet
ESP_LOGV(TAG, "read data invalid preamble!");
return false;
}
bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
data[6] == 0xD5 && // TFI - frame from PN532 to system controller
data[7] == command + 1); // Correct command response
if (!valid_header) {
ESP_LOGV(TAG, "read data invalid header!");
return false;
}
data.erase(data.begin(), data.begin() + 6); // Remove headers
uint8_t checksum = 0;
for (int i = 0; i < len + 1; i++) {
uint8_t dat = data[i];
checksum += dat;
}
checksum = ~checksum + 1;
if (data[len + 1] != checksum) {
ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len], checksum);
return false;
}
if (data[len + 2] != 0x00) {
ESP_LOGV(TAG, "read data invalid postamble!");
return false;
}
data.erase(data.begin(), data.begin() + 2); // Remove TFI and command code
data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
return true;
}
uint8_t PN532::read_response_length_() {
std::vector<uint8_t> data;
if (!this->read_data(data, 6)) {
return 0;
}
if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
// invalid packet
ESP_LOGV(TAG, "read data invalid preamble!");
return 0;
}
bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
data[6] == 0xD5); // TFI - frame from PN532 to system controller
if (!valid_header) {
ESP_LOGV(TAG, "read data invalid header!");
return 0;
}
this->write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00}); // NACK - Retransmit last message
// full length of message, including TFI
uint8_t full_len = data[4];
// length of data, excluding TFI
uint8_t len = full_len - 1;
if (full_len == 0)
len = 0;
return len;
} }
void PN532::turn_off_rf_() { void PN532::turn_off_rf_() {
ESP_LOGVV(TAG, "Turning RF field OFF"); ESP_LOGV(TAG, "Turning RF field OFF");
this->write_command_({ this->write_command_({
PN532_COMMAND_RFCONFIGURATION, PN532_COMMAND_RFCONFIGURATION,
0x01, // RF Field 0x01, // RF Field

View File

@ -46,12 +46,12 @@ class PN532 : public PollingComponent {
protected: protected:
void turn_off_rf_(); void turn_off_rf_();
bool write_command_(const std::vector<uint8_t> &data); bool write_command_(const std::vector<uint8_t> &data);
bool read_response_(uint8_t command, std::vector<uint8_t> &data);
bool read_ack_(); bool read_ack_();
uint8_t read_response_length_(); void send_nack_();
virtual bool write_data(const std::vector<uint8_t> &data) = 0; virtual bool write_data(const std::vector<uint8_t> &data) = 0;
virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0; virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
virtual bool read_response(uint8_t command, std::vector<uint8_t> &data) = 0;
nfc::NfcTag *read_tag_(std::vector<uint8_t> &uid); nfc::NfcTag *read_tag_(std::vector<uint8_t> &uid);

View File

@ -64,7 +64,7 @@ bool PN532::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &
return false; return false;
} }
if (!this->read_response_(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) { if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) {
return false; return false;
} }
data.erase(data.begin()); data.erase(data.begin());
@ -89,7 +89,7 @@ bool PN532::auth_mifare_classic_block_(std::vector<uint8_t> &uid, uint8_t block_
} }
std::vector<uint8_t> response; std::vector<uint8_t> response;
if (!this->read_response_(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) { if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response) || response[0] != 0x00) {
ESP_LOGE(TAG, "Authentication failed - Block 0x%02x", block_num); ESP_LOGE(TAG, "Authentication failed - Block 0x%02x", block_num);
return false; return false;
} }
@ -194,7 +194,7 @@ bool PN532::write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t>
} }
std::vector<uint8_t> response; std::vector<uint8_t> response;
if (!this->read_response_(PN532_COMMAND_INDATAEXCHANGE, response)) { if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
ESP_LOGE(TAG, "Error writing block %d", block_num); ESP_LOGE(TAG, "Error writing block %d", block_num);
return false; return false;
} }

View File

@ -52,7 +52,7 @@ bool PN532::read_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t>
return false; return false;
} }
if (!this->read_response_(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) { if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, data) || data[0] != 0x00) {
return false; return false;
} }
data.erase(data.begin()); data.erase(data.begin());
@ -168,7 +168,7 @@ bool PN532::write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t>
} }
std::vector<uint8_t> response; std::vector<uint8_t> response;
if (!this->read_response_(PN532_COMMAND_INDATAEXCHANGE, response)) { if (!this->read_response(PN532_COMMAND_INDATAEXCHANGE, response)) {
ESP_LOGE(TAG, "Error writing page %d", page_num); ESP_LOGE(TAG, "Error writing page %d", page_num);
return false; return false;
} }

View File

@ -36,6 +36,90 @@ bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
return true; return true;
} }
bool PN532I2C::read_response(uint8_t command, std::vector<uint8_t> &data) {
ESP_LOGV(TAG, "Reading response");
uint8_t len = this->read_response_length_();
if (len == 0) {
return false;
}
ESP_LOGV(TAG, "Reading response of length %d", len);
if (!this->read_data(data, 6 + len + 2)) {
ESP_LOGD(TAG, "No response data");
return false;
}
if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
// invalid packet
ESP_LOGV(TAG, "read data invalid preamble!");
return false;
}
bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
data[6] == 0xD5 && // TFI - frame from PN532 to system controller
data[7] == command + 1); // Correct command response
if (!valid_header) {
ESP_LOGV(TAG, "read data invalid header!");
return false;
}
data.erase(data.begin(), data.begin() + 6); // Remove headers
uint8_t checksum = 0;
for (int i = 0; i < len + 1; i++) {
uint8_t dat = data[i];
checksum += dat;
}
checksum = ~checksum + 1;
if (data[len + 1] != checksum) {
ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len], checksum);
return false;
}
if (data[len + 2] != 0x00) {
ESP_LOGV(TAG, "read data invalid postamble!");
return false;
}
data.erase(data.begin(), data.begin() + 2); // Remove TFI and command code
data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
return true;
}
uint8_t PN532I2C::read_response_length_() {
std::vector<uint8_t> data;
if (!this->read_data(data, 6)) {
return 0;
}
if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
// invalid packet
ESP_LOGV(TAG, "read data invalid preamble!");
return 0;
}
bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
data[6] == 0xD5); // TFI - frame from PN532 to system controller
if (!valid_header) {
ESP_LOGV(TAG, "read data invalid header!");
return 0;
}
this->send_nack_();
// full length of message, including TFI
uint8_t full_len = data[4];
// length of data, excluding TFI
uint8_t len = full_len - 1;
if (full_len == 0)
len = 0;
return len;
}
void PN532I2C::dump_config() { void PN532I2C::dump_config() {
PN532::dump_config(); PN532::dump_config();
LOG_I2C_DEVICE(this); LOG_I2C_DEVICE(this);

View File

@ -14,6 +14,8 @@ class PN532I2C : public pn532::PN532, public i2c::I2CDevice {
protected: protected:
bool write_data(const std::vector<uint8_t> &data) override; bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override; bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;
uint8_t read_response_length_();
}; };
} // namespace pn532_i2c } // namespace pn532_i2c

View File

@ -26,7 +26,7 @@ bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
delay(2); delay(2);
// First byte, communication mode: Write data // First byte, communication mode: Write data
this->write_byte(0x01); this->write_byte(0x01);
ESP_LOGV(TAG, "Writing data: %s", hexencode(data).c_str());
this->write_array(data.data(), data.size()); this->write_array(data.data(), data.size());
this->disable(); this->disable();
@ -34,31 +34,122 @@ bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
} }
bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) { bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
this->enable(); ESP_LOGV(TAG, "Waiting for ready byte...");
// First byte, communication mode: Read state
this->write_byte(0x02);
uint32_t start_time = millis(); uint32_t start_time = millis();
while (true) { while (true) {
if (this->read_byte() & 0x01) this->enable();
// First byte, communication mode: Read state
this->write_byte(0x02);
bool ready = this->read_byte() == 0x01;
this->disable();
if (ready)
break; break;
ESP_LOGV(TAG, "Not ready yet...");
if (millis() - start_time > 100) { if (millis() - start_time > 100) {
this->disable();
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!"); ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false; return false;
} }
yield();
} }
// Read data (transmission from the PN532 to the host) // Read data (transmission from the PN532 to the host)
this->enable();
delay(2);
this->write_byte(0x03); this->write_byte(0x03);
ESP_LOGV(TAG, "Reading data...");
data.resize(len); data.resize(len);
this->read_array(data.data(), len); this->read_array(data.data(), len);
this->disable(); this->disable();
data.insert(data.begin(), 0x01); data.insert(data.begin(), 0x01);
ESP_LOGV(TAG, "Read data: %s", hexencode(data).c_str());
return true; return true;
}; }
bool PN532Spi::read_response(uint8_t command, std::vector<uint8_t> &data) {
ESP_LOGV(TAG, "Reading response");
uint32_t start_time = millis();
while (true) {
this->enable();
// First byte, communication mode: Read state
this->write_byte(0x02);
bool ready = this->read_byte() == 0x01;
this->disable();
if (ready)
break;
ESP_LOGV(TAG, "Not ready yet...");
if (millis() - start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false;
}
yield();
}
this->enable();
delay(2);
this->write_byte(0x03);
std::vector<uint8_t> header(7);
this->read_array(header.data(), 7);
ESP_LOGV(TAG, "Header data: %s", hexencode(header).c_str());
if (header[0] != 0x00 && header[1] != 0x00 && header[2] != 0xFF) {
// invalid packet
ESP_LOGV(TAG, "read data invalid preamble!");
return false;
}
bool valid_header = (static_cast<uint8_t>(header[3] + header[4]) == 0 && // LCS, len + lcs = 0
header[5] == 0xD5 && // TFI - frame from PN532 to system controller
header[6] == command + 1); // Correct command response
if (!valid_header) {
ESP_LOGV(TAG, "read data invalid header!");
return false;
}
// full length of message, including command response
uint8_t full_len = header[3];
// length of data, excluding command response
uint8_t len = full_len - 1;
if (full_len == 0)
len = 0;
ESP_LOGV(TAG, "Reading response of length %d", len);
data.resize(len + 1);
this->read_array(data.data(), len + 1);
this->disable();
ESP_LOGV(TAG, "Response data: %s", hexencode(data).c_str());
uint8_t checksum = header[5] + header[6]; // TFI + Command response code
for (int i = 0; i < len - 1; i++) {
uint8_t dat = data[i];
checksum += dat;
}
checksum = ~checksum + 1;
if (data[len - 1] != checksum) {
ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
return false;
}
if (data[len] != 0x00) {
ESP_LOGV(TAG, "read data invalid postamble!");
return false;
}
data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
return true;
}
void PN532Spi::dump_config() { void PN532Spi::dump_config() {
PN532::dump_config(); PN532::dump_config();

View File

@ -18,6 +18,7 @@ class PN532Spi : public pn532::PN532,
protected: protected:
bool write_data(const std::vector<uint8_t> &data) override; bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override; bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;
}; };
} // namespace pn532_spi } // namespace pn532_spi

View File

@ -407,7 +407,7 @@ void HOT ST7735::senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes) {
this->cs_->digital_write(false); this->cs_->digital_write(false);
this->enable(); this->enable();
for (uint8_t i = 0; i < num_data_bytes; i++) { for (uint8_t i = 0; i < num_data_bytes; i++) {
this->transfer_byte(pgm_read_byte(data_bytes++)); // write byte - SPI library this->write_byte(pgm_read_byte(data_bytes++)); // write byte - SPI library
} }
this->cs_->digital_write(true); this->cs_->digital_write(true);
this->disable(); this->disable();

View File

@ -2,7 +2,7 @@
MAJOR_VERSION = 1 MAJOR_VERSION = 1
MINOR_VERSION = 16 MINOR_VERSION = 16
PATCH_VERSION = '0' PATCH_VERSION = '1'
__short_version__ = f'{MAJOR_VERSION}.{MINOR_VERSION}' __short_version__ = f'{MAJOR_VERSION}.{MINOR_VERSION}'
__version__ = f'{__short_version__}.{PATCH_VERSION}' __version__ = f'{__short_version__}.{PATCH_VERSION}'
@ -357,6 +357,7 @@ CONF_ON_VALUE = 'on_value'
CONF_ON_VALUE_RANGE = 'on_value_range' CONF_ON_VALUE_RANGE = 'on_value_range'
CONF_ONE = 'one' CONF_ONE = 'one'
CONF_OPEN_ACTION = 'open_action' CONF_OPEN_ACTION = 'open_action'
CONF_OPEN_DRAIN_INTERRUPT = 'open_drain_interrupt'
CONF_OPEN_DURATION = 'open_duration' CONF_OPEN_DURATION = 'open_duration'
CONF_OPEN_ENDSTOP = 'open_endstop' CONF_OPEN_ENDSTOP = 'open_endstop'
CONF_OPTIMISTIC = 'optimistic' CONF_OPTIMISTIC = 'optimistic'

View File

@ -434,6 +434,9 @@ sensor:
power: power:
name: 'HLW8012 Power' name: 'HLW8012 Power'
id: hlw8012_power id: hlw8012_power
energy:
name: "HLW8012 Energy"
id: hlw8012_energy
update_interval: 15s update_interval: 15s
current_resistor: 0.001 ohm current_resistor: 0.001 ohm
voltage_divider: 2351 voltage_divider: 2351
@ -1892,10 +1895,12 @@ pcf8574:
mcp23017: mcp23017:
- id: 'mcp23017_hub' - id: 'mcp23017_hub'
open_drain_interrupt: 'true'
mcp23008: mcp23008:
- id: 'mcp23008_hub' - id: 'mcp23008_hub'
address: 0x22 address: 0x22
open_drain_interrupt: 'true'
mcp23016: mcp23016:
- id: 'mcp23016_hub' - id: 'mcp23016_hub'