mirror of
https://github.com/esphome/esphome.git
synced 2025-03-16 07:38:17 +00:00
Update ds248x.cpp
check clang
This commit is contained in:
parent
858f5eafcd
commit
d0c7709da9
@ -5,62 +5,62 @@
|
|||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
namespace ds248x {
|
namespace ds248x {
|
||||||
|
|
||||||
static const uint8_t DS2482_800_COMMAND_CHANNEL_SELECTION = 0xC3;
|
static const uint8_t DS2482_800_COMMAND_CHANNEL_SELECTION = 0xC3;
|
||||||
|
|
||||||
static const uint8_t DS248X_COMMAND_RESET = 0xF0;
|
static const uint8_t DS248X_COMMAND_RESET = 0xF0;
|
||||||
static const uint8_t DS248X_COMMAND_SETREADPTR = 0xE1;
|
static const uint8_t DS248X_COMMAND_SETREADPTR = 0xE1;
|
||||||
static const uint8_t DS248X_COMMAND_WRITECONFIG = 0xD2;
|
static const uint8_t DS248X_COMMAND_WRITECONFIG = 0xD2;
|
||||||
static const uint8_t DS248X_COMMAND_RESETWIRE = 0xB4;
|
static const uint8_t DS248X_COMMAND_RESETWIRE = 0xB4;
|
||||||
static const uint8_t DS248X_COMMAND_WRITEBYTE = 0xA5;
|
static const uint8_t DS248X_COMMAND_WRITEBYTE = 0xA5;
|
||||||
static const uint8_t DS248X_COMMAND_READBYTE = 0x96;
|
static const uint8_t DS248X_COMMAND_READBYTE = 0x96;
|
||||||
static const uint8_t DS248X_COMMAND_SINGLEBIT = 0x87;
|
static const uint8_t DS248X_COMMAND_SINGLEBIT = 0x87;
|
||||||
static const uint8_t DS248X_COMMAND_TRIPLET = 0x78;
|
static const uint8_t DS248X_COMMAND_TRIPLET = 0x78;
|
||||||
|
|
||||||
static const uint8_t DS248X_POINTER_STATUS = 0xF0;
|
static const uint8_t DS248X_POINTER_STATUS = 0xF0;
|
||||||
static const uint8_t DS248X_STATUS_BUSY = (1 << 0);
|
static const uint8_t DS248X_STATUS_BUSY = (1 << 0);
|
||||||
static const uint8_t DS248X_STATUS_PPD = (1 << 1);
|
static const uint8_t DS248X_STATUS_PPD = (1 << 1);
|
||||||
static const uint8_t DS248X_STATUS_SD = (1 << 2);
|
static const uint8_t DS248X_STATUS_SD = (1 << 2);
|
||||||
static const uint8_t DS248X_STATUS_LL = (1 << 3);
|
static const uint8_t DS248X_STATUS_LL = (1 << 3);
|
||||||
static const uint8_t DS248X_STATUS_RST = (1 << 4);
|
static const uint8_t DS248X_STATUS_RST = (1 << 4);
|
||||||
static const uint8_t DS248X_STATUS_SBR = (1 << 5);
|
static const uint8_t DS248X_STATUS_SBR = (1 << 5);
|
||||||
static const uint8_t DS248X_STATUS_TSB = (1 << 6);
|
static const uint8_t DS248X_STATUS_TSB = (1 << 6);
|
||||||
static const uint8_t DS248X_STATUS_DIR = (1 << 7);
|
static const uint8_t DS248X_STATUS_DIR = (1 << 7);
|
||||||
|
|
||||||
static const uint8_t DS248X_POINTER_DATA = 0xE1;
|
static const uint8_t DS248X_POINTER_DATA = 0xE1;
|
||||||
|
|
||||||
static const uint8_t DS248X_POINTER_CONFIG = 0xC3;
|
static const uint8_t DS248X_POINTER_CONFIG = 0xC3;
|
||||||
static const uint8_t DS248X_CONFIG_ACTIVE_PULLUP = (1 << 0);
|
static const uint8_t DS248X_CONFIG_ACTIVE_PULLUP = (1 << 0);
|
||||||
static const uint8_t DS248X_CONFIG_POWER_DOWN = (1 << 1);
|
static const uint8_t DS248X_CONFIG_POWER_DOWN = (1 << 1);
|
||||||
static const uint8_t DS248X_CONFIG_STRONG_PULLUP = (1 << 2);
|
static const uint8_t DS248X_CONFIG_STRONG_PULLUP = (1 << 2);
|
||||||
static const uint8_t DS248X_CONFIG_1WIRE_SPEED = (1 << 3);
|
static const uint8_t DS248X_CONFIG_1WIRE_SPEED = (1 << 3);
|
||||||
|
|
||||||
static const uint8_t WIRE_COMMAND_SKIP = 0xCC;
|
static const uint8_t WIRE_COMMAND_SKIP = 0xCC;
|
||||||
static const uint8_t WIRE_COMMAND_SELECT = 0x55;
|
static const uint8_t WIRE_COMMAND_SELECT = 0x55;
|
||||||
static const uint8_t WIRE_COMMAND_SEARCH = 0xF0;
|
static const uint8_t WIRE_COMMAND_SEARCH = 0xF0;
|
||||||
|
|
||||||
static const uint8_t DS248X_ERROR_TIMEOUT = (1 << 0);
|
static const uint8_t DS248X_ERROR_TIMEOUT = (1 << 0);
|
||||||
static const uint8_t DS248X_ERROR_SHORT = (1 << 1);
|
static const uint8_t DS248X_ERROR_SHORT = (1 << 1);
|
||||||
static const uint8_t DS248X_ERROR_CONFIG = (1 << 2);
|
static const uint8_t DS248X_ERROR_CONFIG = (1 << 2);
|
||||||
|
|
||||||
static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
|
static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
|
||||||
static const uint8_t DALLAS_MODEL_DS1822 = 0x22;
|
static const uint8_t DALLAS_MODEL_DS1822 = 0x22;
|
||||||
static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
|
static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
|
||||||
static const uint8_t DALLAS_MODEL_DS1825 = 0x3B;
|
static const uint8_t DALLAS_MODEL_DS1825 = 0x3B;
|
||||||
static const uint8_t DALLAS_MODEL_DS28EA00 = 0x42;
|
static const uint8_t DALLAS_MODEL_DS28EA00 = 0x42;
|
||||||
|
|
||||||
static const uint8_t DALLAS_COMMAND_START_CONVERSION = 0x44;
|
static const uint8_t DALLAS_COMMAND_START_CONVERSION = 0x44;
|
||||||
static const uint8_t DALLAS_COMMAND_READ_SCRATCH_PAD = 0xBE;
|
static const uint8_t DALLAS_COMMAND_READ_SCRATCH_PAD = 0xBE;
|
||||||
static const uint8_t DALLAS_COMMAND_WRITE_SCRATCH_PAD = 0x4E;
|
static const uint8_t DALLAS_COMMAND_WRITE_SCRATCH_PAD = 0x4E;
|
||||||
static const uint8_t DALLAS_COMMAND_SAVE_EEPROM = 0x48;
|
static const uint8_t DALLAS_COMMAND_SAVE_EEPROM = 0x48;
|
||||||
|
|
||||||
static const char *const TAG = "ds248x";
|
static const char *const TAG = "ds248x";
|
||||||
|
|
||||||
static const uint8_t CHANNEL_CODE[8] = { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
|
static const uint8_t CHANNEL_CODE[8] = {0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87};
|
||||||
static const uint8_t READ_CHANNEL_CODE[8] = { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
|
static const uint8_t READ_CHANNEL_CODE[8] = {0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87};
|
||||||
|
|
||||||
void DS248xComponent::setup() {
|
void DS248xComponent::setup() {
|
||||||
uint64_t address = 0;
|
uint64_t address = 0;
|
||||||
uint8_t channel = 0;
|
uint8_t channel = 0;
|
||||||
uint8_t index = 0;
|
uint8_t index = 0;
|
||||||
@ -74,26 +74,26 @@ namespace esphome {
|
|||||||
this->sleep_pin_->pin_mode(esphome::gpio::FLAG_OUTPUT);
|
this->sleep_pin_->pin_mode(esphome::gpio::FLAG_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_100) {
|
if (this->ds248x_type_ == DS248xType::DS2482_100) {
|
||||||
// Reset
|
// Reset
|
||||||
this->reset_hub();
|
this->reset_hub();
|
||||||
address = 0;
|
address = 0;
|
||||||
while(this->search(&address)) {
|
while (this->search(&address)) {
|
||||||
raw_sensors.push_back(address);
|
raw_sensors.push_back(address);
|
||||||
raw_channel_sensors.push_back(0);
|
raw_channel_sensors.push_back(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
for(channel=0; channel<NBR_CHANNELS; channel++) {
|
for (channel = 0; channel < NBR_CHANNELS; channel++) {
|
||||||
// Reset
|
// Reset
|
||||||
this->reset_hub();
|
this->reset_hub();
|
||||||
|
|
||||||
// 1-wire channel selection
|
// 1-wire channel selection
|
||||||
if(this->set_channel(channel)) {
|
if (this->set_channel(channel)) {
|
||||||
// Search 1-wire components
|
// Search 1-wire components
|
||||||
address = 0;
|
address = 0;
|
||||||
while(this->search(&address)) {
|
while (this->search(&address)) {
|
||||||
raw_sensors.push_back(address);
|
raw_sensors.push_back(address);
|
||||||
raw_channel_sensors.push_back(channel);
|
raw_channel_sensors.push_back(channel);
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ namespace esphome {
|
|||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
for (auto &address : raw_sensors) {
|
for (auto &address : raw_sensors) {
|
||||||
channel=raw_channel_sensors[index];
|
channel = raw_channel_sensors[index];
|
||||||
index++;
|
index++;
|
||||||
auto *address8 = reinterpret_cast<uint8_t *>(&address);
|
auto *address8 = reinterpret_cast<uint8_t *>(&address);
|
||||||
if (crc8(address8, 7) != address8[7]) {
|
if (crc8(address8, 7) != address8[7]) {
|
||||||
@ -125,7 +125,7 @@ namespace esphome {
|
|||||||
index = 0;
|
index = 0;
|
||||||
|
|
||||||
// DS2482_100
|
// DS2482_100
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_100) {
|
if (this->ds248x_type_ == DS248xType::DS2482_100) {
|
||||||
for (auto *sensor : this->sensors_) {
|
for (auto *sensor : this->sensors_) {
|
||||||
if (sensor->get_index().has_value()) {
|
if (sensor->get_index().has_value()) {
|
||||||
if (*sensor->get_index() >= this->found_sensors_.size()) {
|
if (*sensor->get_index() >= this->found_sensors_.size()) {
|
||||||
@ -144,8 +144,8 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DS2482_800
|
// DS2482_800
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
for(channel=0; channel<NBR_CHANNELS; channel++) {
|
for (channel = 0; channel < NBR_CHANNELS; channel++) {
|
||||||
for (auto *sensor : this->channel_sensors_[channel]) {
|
for (auto *sensor : this->channel_sensors_[channel]) {
|
||||||
if (sensor->get_index().has_value()) {
|
if (sensor->get_index().has_value()) {
|
||||||
if (*sensor->get_index() >= this->found_sensors_.size()) {
|
if (*sensor->get_index() >= this->found_sensors_.size()) {
|
||||||
@ -163,10 +163,9 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
void DS248xComponent::dump_config() {
|
||||||
|
|
||||||
void DS248xComponent::dump_config() {
|
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
uint8_t channel = 0;
|
uint8_t channel = 0;
|
||||||
ESP_LOGCONFIG(TAG, "DS248x:");
|
ESP_LOGCONFIG(TAG, "DS248x:");
|
||||||
@ -175,7 +174,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
|
|
||||||
switch(this->ds248x_type_) {
|
switch (this->ds248x_type_) {
|
||||||
case DS248xType::DS2482_100:
|
case DS248xType::DS2482_100:
|
||||||
ESP_LOGCONFIG(TAG, " Type: DD2482-100");
|
ESP_LOGCONFIG(TAG, " Type: DD2482-100");
|
||||||
break;
|
break;
|
||||||
@ -199,7 +198,7 @@ namespace esphome {
|
|||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
|
||||||
// DS2482-100
|
// DS2482-100
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_100) {
|
if (this->ds248x_type_ == DS248xType::DS2482_100) {
|
||||||
for (auto *sensor : this->sensors_) {
|
for (auto *sensor : this->sensors_) {
|
||||||
LOG_SENSOR(" ", "Device", sensor);
|
LOG_SENSOR(" ", "Device", sensor);
|
||||||
if (sensor->get_index().has_value()) {
|
if (sensor->get_index().has_value()) {
|
||||||
@ -216,8 +215,8 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DS2482-800
|
// DS2482-800
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
for(channel=0; channel<NBR_CHANNELS; channel++) {
|
for (channel = 0; channel < NBR_CHANNELS; channel++) {
|
||||||
for (auto *sensor : this->channel_sensors_[channel]) {
|
for (auto *sensor : this->channel_sensors_[channel]) {
|
||||||
LOG_SENSOR(" ", "Device", sensor);
|
LOG_SENSOR(" ", "Device", sensor);
|
||||||
if (sensor->get_index().has_value()) {
|
if (sensor->get_index().has_value()) {
|
||||||
@ -233,40 +232,37 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
void DS248xComponent::register_sensor(DS248xTemperatureSensor *sensor) {
|
||||||
|
|
||||||
void DS248xComponent::register_sensor(DS248xTemperatureSensor *sensor) {
|
|
||||||
|
|
||||||
this->sensors_.push_back(sensor);
|
this->sensors_.push_back(sensor);
|
||||||
|
|
||||||
// DS2482-100
|
// DS2482-100
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_100) {
|
if (this->ds248x_type_ == DS248xType::DS2482_100) {
|
||||||
this->channel_sensors_[0].push_back(sensor);
|
this->channel_sensors_[0].push_back(sensor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// DS2482-800
|
// DS2482-800
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
this->channel_sensors_[sensor->get_channel()].push_back(sensor);
|
this->channel_sensors_[sensor->get_channel()].push_back(sensor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DS248xComponent::update() {
|
void DS248xComponent::update() {
|
||||||
uint8_t channel = 0;
|
uint8_t channel = 0;
|
||||||
uint8_t nbr_channels = 1;
|
uint8_t nbr_channels = 1;
|
||||||
int nbr_sensors = 0;
|
int nbr_sensors = 0;
|
||||||
int nbr_sensors_on_channel = 0;
|
int nbr_sensors_on_channel = 0;
|
||||||
|
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
nbr_channels = NBR_CHANNELS;
|
nbr_channels = NBR_CHANNELS;
|
||||||
for(channel=0; channel<nbr_channels; channel++) {
|
for (channel = 0; channel < nbr_channels; channel++) {
|
||||||
nbr_sensors += channel_sensors_[channel].size();
|
nbr_sensors += channel_sensors_[channel].size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(channel=0; channel<nbr_channels; channel++) {
|
for (channel = 0; channel < nbr_channels; channel++) {
|
||||||
|
if (this->ds248x_type_ == DS248xType::DS2482_800) {
|
||||||
if(this->ds248x_type_ == DS248xType::DS2482_800) {
|
|
||||||
ESP_LOGV(TAG, "Channel: %u", channel);
|
ESP_LOGV(TAG, "Channel: %u", channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,8 +271,7 @@ namespace esphome {
|
|||||||
ESP_LOGV(TAG, "Start sensor update for %i sensors", nbr_sensors_on_channel);
|
ESP_LOGV(TAG, "Start sensor update for %i sensors", nbr_sensors_on_channel);
|
||||||
|
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
if(nbr_sensors_on_channel && this->set_channel(channel)) {
|
if (nbr_sensors_on_channel && this->set_channel(channel)) {
|
||||||
|
|
||||||
if (this->enable_bus_sleep_) {
|
if (this->enable_bus_sleep_) {
|
||||||
this->write_config(this->read_config() & ~DS248X_CONFIG_POWER_DOWN);
|
this->write_config(this->read_config() & ~DS248X_CONFIG_POWER_DOWN);
|
||||||
}
|
}
|
||||||
@ -321,7 +316,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
ESP_LOGV(TAG, "Update Sensor idx: %i", readIdx);
|
ESP_LOGV(TAG, "Update Sensor idx: %i", readIdx);
|
||||||
|
|
||||||
DS248xTemperatureSensor* sensor = sensors_[readIdx];
|
DS248xTemperatureSensor *sensor = sensors_[readIdx];
|
||||||
this->set_channel(sensor->get_channel());
|
this->set_channel(sensor->get_channel());
|
||||||
readIdx++;
|
readIdx++;
|
||||||
|
|
||||||
@ -344,13 +339,11 @@ namespace esphome {
|
|||||||
sensor->publish_state(tempc);
|
sensor->publish_state(tempc);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
float DS248xComponent::get_setup_priority() const {
|
float DS248xComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
return setup_priority::DATA;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t DS248xComponent::read_config() {
|
uint8_t DS248xComponent::read_config() {
|
||||||
std::array<uint8_t, 2> cmd;
|
std::array<uint8_t, 2> cmd;
|
||||||
cmd[0] = DS248X_COMMAND_SETREADPTR;
|
cmd[0] = DS248X_COMMAND_SETREADPTR;
|
||||||
cmd[1] = DS248X_POINTER_CONFIG;
|
cmd[1] = DS248X_POINTER_CONFIG;
|
||||||
@ -360,32 +353,31 @@ namespace esphome {
|
|||||||
this->read(&cfg_byte, sizeof(cfg_byte));
|
this->read(&cfg_byte, sizeof(cfg_byte));
|
||||||
|
|
||||||
return cfg_byte;
|
return cfg_byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DS248xComponent::write_config(uint8_t cfg) {
|
void DS248xComponent::write_config(uint8_t cfg) {
|
||||||
std::array<uint8_t, 2> cmd;
|
std::array<uint8_t, 2> cmd;
|
||||||
cmd[0] = DS248X_COMMAND_WRITECONFIG;
|
cmd[0] = DS248X_COMMAND_WRITECONFIG;
|
||||||
cmd[1] = cfg | ((~cfg) << 4);
|
cmd[1] = cfg | ((~cfg) << 4);
|
||||||
this->write(cmd.data(), sizeof(cmd));
|
this->write(cmd.data(), sizeof(cmd));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t DS248xComponent::wait_while_busy() {
|
uint8_t DS248xComponent::wait_while_busy() {
|
||||||
std::array<uint8_t, 2> cmd;
|
std::array<uint8_t, 2> cmd;
|
||||||
cmd[0] = DS248X_COMMAND_SETREADPTR;
|
cmd[0] = DS248X_COMMAND_SETREADPTR;
|
||||||
cmd[1] = DS248X_POINTER_STATUS;
|
cmd[1] = DS248X_POINTER_STATUS;
|
||||||
this->write(cmd.data(), sizeof(cmd));
|
this->write(cmd.data(), sizeof(cmd));
|
||||||
|
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
for(int i=1000; i>0; i--) {
|
for (int i = 1000; i > 0; i--) {
|
||||||
this->read(&status, sizeof(status));
|
this->read(&status, sizeof(status));
|
||||||
if (!(status & DS248X_STATUS_BUSY))
|
if (!(status & DS248X_STATUS_BUSY))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DS248xComponent::reset_hub() {
|
||||||
void DS248xComponent::reset_hub() {
|
|
||||||
if (this->sleep_pin_) {
|
if (this->sleep_pin_) {
|
||||||
this->sleep_pin_->digital_write(true);
|
this->sleep_pin_->digital_write(true);
|
||||||
}
|
}
|
||||||
@ -400,9 +392,9 @@ namespace esphome {
|
|||||||
last_device_found = false;
|
last_device_found = false;
|
||||||
searchAddress = 0;
|
searchAddress = 0;
|
||||||
searchLastDiscrepancy = 0;
|
searchLastDiscrepancy = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DS248xComponent::set_channel(uint8_t channel) {
|
bool DS248xComponent::set_channel(uint8_t channel) {
|
||||||
std::array<uint8_t, 2> cmd;
|
std::array<uint8_t, 2> cmd;
|
||||||
uint8_t data_byte;
|
uint8_t data_byte;
|
||||||
|
|
||||||
@ -421,13 +413,11 @@ namespace esphome {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t DS248xComponent::get_channel() {
|
uint8_t DS248xComponent::get_channel() { return (this->channel); }
|
||||||
return(this->channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DS248xComponent::reset_devices() {
|
bool DS248xComponent::reset_devices() {
|
||||||
auto status = wait_while_busy();
|
auto status = wait_while_busy();
|
||||||
if (status & DS248X_STATUS_BUSY) {
|
if (status & DS248X_STATUS_BUSY) {
|
||||||
ESP_LOGE(TAG, "Master never finished command");
|
ESP_LOGE(TAG, "Master never finished command");
|
||||||
@ -453,9 +443,9 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DS248xComponent::write_command(uint8_t command, uint8_t data) {
|
void DS248xComponent::write_command(uint8_t command, uint8_t data) {
|
||||||
auto status = wait_while_busy();
|
auto status = wait_while_busy();
|
||||||
|
|
||||||
if (status & DS248X_STATUS_BUSY) {
|
if (status & DS248X_STATUS_BUSY) {
|
||||||
@ -466,21 +456,19 @@ namespace esphome {
|
|||||||
cmd[0] = command;
|
cmd[0] = command;
|
||||||
cmd[1] = data;
|
cmd[1] = data;
|
||||||
this->write(cmd.data(), sizeof(cmd));
|
this->write(cmd.data(), sizeof(cmd));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DS248xComponent::select(uint64_t address) {
|
void DS248xComponent::select(uint64_t address) {
|
||||||
this->write_command(DS248X_COMMAND_WRITEBYTE, WIRE_COMMAND_SELECT);
|
this->write_command(DS248X_COMMAND_WRITEBYTE, WIRE_COMMAND_SELECT);
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
this->write_command(DS248X_COMMAND_WRITEBYTE, (address >> (i*8)) & 0xff);
|
this->write_command(DS248X_COMMAND_WRITEBYTE, (address >> (i * 8)) & 0xff);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DS248xComponent::write_to_wire(uint8_t data) {
|
void DS248xComponent::write_to_wire(uint8_t data) { this->write_command(DS248X_COMMAND_WRITEBYTE, data); }
|
||||||
this->write_command(DS248X_COMMAND_WRITEBYTE, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t DS248xComponent::read_from_wire() {
|
uint8_t DS248xComponent::read_from_wire() {
|
||||||
auto status = wait_while_busy();
|
auto status = wait_while_busy();
|
||||||
|
|
||||||
if (status & DS248X_STATUS_BUSY) {
|
if (status & DS248X_STATUS_BUSY) {
|
||||||
@ -505,10 +493,9 @@ namespace esphome {
|
|||||||
this->read(&data_byte, sizeof(data_byte));
|
this->read(&data_byte, sizeof(data_byte));
|
||||||
|
|
||||||
return data_byte;
|
return data_byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DS248xComponent::search(uint64_t* address) {
|
|
||||||
|
|
||||||
|
bool DS248xComponent::search(uint64_t *address) {
|
||||||
if (last_device_found)
|
if (last_device_found)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -523,7 +510,7 @@ namespace esphome {
|
|||||||
|
|
||||||
uint8_t direction;
|
uint8_t direction;
|
||||||
uint8_t last_zero = 0;
|
uint8_t last_zero = 0;
|
||||||
for(uint8_t i=0;i<64;i++) {
|
for (uint8_t i = 0; i < 64; i++) {
|
||||||
uint64_t searchBit = 1LL << i;
|
uint64_t searchBit = 1LL << i;
|
||||||
|
|
||||||
if (i < searchLastDiscrepancy)
|
if (i < searchLastDiscrepancy)
|
||||||
@ -542,15 +529,13 @@ namespace esphome {
|
|||||||
|
|
||||||
if (id && comp_id)
|
if (id && comp_id)
|
||||||
return 0;
|
return 0;
|
||||||
else
|
else if (!id && !comp_id && !direction)
|
||||||
if (!id && !comp_id && !direction)
|
|
||||||
last_zero = i;
|
last_zero = i;
|
||||||
|
|
||||||
if (direction)
|
if (direction)
|
||||||
searchAddress |= searchBit;
|
searchAddress |= searchBit;
|
||||||
else
|
else
|
||||||
searchAddress &= ~searchBit;
|
searchAddress &= ~searchBit;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
searchLastDiscrepancy = last_zero;
|
searchLastDiscrepancy = last_zero;
|
||||||
@ -561,49 +546,33 @@ namespace esphome {
|
|||||||
*address = searchAddress;
|
*address = searchAddress;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DS248xTemperatureSensor::set_address(uint64_t address) {
|
void DS248xTemperatureSensor::set_address(uint64_t address) { this->address_ = address; }
|
||||||
this->address_ = address;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DS248xTemperatureSensor::set_channel(uint8_t channel) {
|
void DS248xTemperatureSensor::set_channel(uint8_t channel) { this->channel_ = channel; }
|
||||||
this->channel_ = channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t DS248xTemperatureSensor::get_channel() const {
|
uint8_t DS248xTemperatureSensor::get_channel() const { return this->channel_; }
|
||||||
return this->channel_;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t DS248xTemperatureSensor::get_resolution() const {
|
uint8_t DS248xTemperatureSensor::get_resolution() const { return this->resolution_; }
|
||||||
return this->resolution_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DS248xTemperatureSensor::set_resolution(uint8_t resolution) {
|
void DS248xTemperatureSensor::set_resolution(uint8_t resolution) { this->resolution_ = resolution; }
|
||||||
this->resolution_ = resolution;
|
|
||||||
}
|
|
||||||
|
|
||||||
optional<uint8_t> DS248xTemperatureSensor::get_index() const {
|
optional<uint8_t> DS248xTemperatureSensor::get_index() const { return this->index_; }
|
||||||
return this->index_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DS248xTemperatureSensor::set_index(uint8_t index) {
|
void DS248xTemperatureSensor::set_index(uint8_t index) { this->index_ = index; }
|
||||||
this->index_ = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *DS248xTemperatureSensor::get_address8() {
|
uint8_t *DS248xTemperatureSensor::get_address8() { return reinterpret_cast<uint8_t *>(&this->address_); }
|
||||||
return reinterpret_cast<uint8_t *>(&this->address_);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string &DS248xTemperatureSensor::get_address_name() {
|
const std::string &DS248xTemperatureSensor::get_address_name() {
|
||||||
if (this->address_name_.empty()) {
|
if (this->address_name_.empty()) {
|
||||||
this->address_name_ = std::string("0x") + format_hex(this->address_);
|
this->address_name_ = std::string("0x") + format_hex(this->address_);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this->address_name_;
|
return this->address_name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t DS248xTemperatureSensor::millis_to_wait_for_conversion() const {
|
uint16_t DS248xTemperatureSensor::millis_to_wait_for_conversion() const {
|
||||||
switch (this->resolution_) {
|
switch (this->resolution_) {
|
||||||
case 9:
|
case 9:
|
||||||
return 94;
|
return 94;
|
||||||
@ -614,9 +583,9 @@ namespace esphome {
|
|||||||
default:
|
default:
|
||||||
return 750;
|
return 750;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IRAM_ATTR DS248xTemperatureSensor::read_scratch_pad() {
|
bool IRAM_ATTR DS248xTemperatureSensor::read_scratch_pad() {
|
||||||
bool result = this->parent_->reset_devices();
|
bool result = this->parent_->reset_devices();
|
||||||
if (!result) {
|
if (!result) {
|
||||||
this->parent_->status_set_warning();
|
this->parent_->status_set_warning();
|
||||||
@ -632,9 +601,9 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DS248xTemperatureSensor::setup_sensor() {
|
bool DS248xTemperatureSensor::setup_sensor() {
|
||||||
bool r = this->read_scratch_pad();
|
bool r = this->read_scratch_pad();
|
||||||
|
|
||||||
if (!r) {
|
if (!r) {
|
||||||
@ -661,7 +630,6 @@ namespace esphome {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (this->scratch_pad_[4] == resolution_register_val)
|
if (this->scratch_pad_[4] == resolution_register_val)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -671,7 +639,6 @@ namespace esphome {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool result = this->parent_->reset_devices();
|
bool result = this->parent_->reset_devices();
|
||||||
if (!result) {
|
if (!result) {
|
||||||
ESP_LOGE(TAG, "Reset failed");
|
ESP_LOGE(TAG, "Reset failed");
|
||||||
@ -701,9 +668,9 @@ namespace esphome {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DS248xTemperatureSensor::check_scratch_pad() {
|
bool DS248xTemperatureSensor::check_scratch_pad() {
|
||||||
bool chksum_validity = (crc8(this->scratch_pad_, 8) == this->scratch_pad_[8]);
|
bool chksum_validity = (crc8(this->scratch_pad_, 8) == this->scratch_pad_[8]);
|
||||||
bool config_validity = false;
|
bool config_validity = false;
|
||||||
|
|
||||||
@ -727,8 +694,8 @@ namespace esphome {
|
|||||||
ESP_LOGW(TAG, "'%s' - Scratch pad config register invalid!", this->get_name().c_str());
|
ESP_LOGW(TAG, "'%s' - Scratch pad config register invalid!", this->get_name().c_str());
|
||||||
}
|
}
|
||||||
return chksum_validity && config_validity;
|
return chksum_validity && config_validity;
|
||||||
}
|
}
|
||||||
float DS248xTemperatureSensor::get_temp_c() {
|
float DS248xTemperatureSensor::get_temp_c() {
|
||||||
int16_t temp = (int16_t(this->scratch_pad_[1]) << 11) | (int16_t(this->scratch_pad_[0]) << 3);
|
int16_t temp = (int16_t(this->scratch_pad_[1]) << 11) | (int16_t(this->scratch_pad_[0]) << 3);
|
||||||
if (this->get_address8()[0] == DALLAS_MODEL_DS18S20) {
|
if (this->get_address8()[0] == DALLAS_MODEL_DS18S20) {
|
||||||
int diff = (this->scratch_pad_[7] - this->scratch_pad_[6]) << 7;
|
int diff = (this->scratch_pad_[7] - this->scratch_pad_[6]) << 7;
|
||||||
@ -736,9 +703,9 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return temp / 128.0f;
|
return temp / 128.0f;
|
||||||
}
|
}
|
||||||
std::string DS248xTemperatureSensor::unique_id() { return "dallas-" + str_lower_case(format_hex(this->address_)); }
|
std::string DS248xTemperatureSensor::unique_id() { return "dallas-" + str_lower_case(format_hex(this->address_)); }
|
||||||
|
|
||||||
} // namespace ds248x
|
} // namespace ds248x
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
Loading…
x
Reference in New Issue
Block a user