Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
latonita committed May 13, 2024
1 parent 56172e0 commit 71caf26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions esphome/components/husb238/husb238.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "husb238.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"

namespace esphome {
Expand Down Expand Up @@ -235,10 +236,16 @@ bool Husb238Component::command_request_pdo(SrcVoltageSelection voltage) {
}

if (!this->select_pdo_voltage_(voltage)) {
ESP_LOGV(TAG, "Select PDO voltage failed");
return false;
}
delay(5);

return this->send_command_(CommandFunction::REQUEST_PDO);
if (!this->send_command_(CommandFunction::REQUEST_PDO)) {
ESP_LOGV(TAG, "Send REQUEST_PDO failed");
return false;
}
return true;
}

bool Husb238Component::is_attached() {
Expand All @@ -253,42 +260,44 @@ bool Husb238Component::read_all_() {
ESP_LOGE(TAG, "Component not ready");
return false;
}
auto err = !this->read_bytes(static_cast<uint8_t>(CommandRegister::PD_STATUS0), &this->registers_.raw[0], REG_NUM);
if (err) {
auto ok = this->read_bytes(static_cast<uint8_t>(CommandRegister::PD_STATUS0), &this->registers_.raw[0], REG_NUM);
if (!ok) {
ESP_LOGE(TAG, "Error reading HUSB238");
}
return err;
return ok;
}

bool Husb238Component::read_status_() {
if (!this->is_ready()) {
ESP_LOGE(TAG, "Component not ready");
return false;
}
auto err = !this->read_bytes(static_cast<uint8_t>(CommandRegister::PD_STATUS0), &this->registers_.raw[0], 2);
if (err) {
auto ok = this->read_bytes(static_cast<uint8_t>(CommandRegister::PD_STATUS0), &this->registers_.raw[0], 2);
if (!ok) {
ESP_LOGE(TAG, "Error reading HUSB238");
}
return err;
return ok;
}

bool Husb238Component::send_command_(CommandFunction function) {
ESP_LOGV(TAG, "Sending command %u", (uint8_t) function);

if (!this->is_ready()) {
ESP_LOGE(TAG, "Component not ready");
return false;
}
RegGoCommand go_command;
auto err = !this->read_byte(static_cast<uint8_t>(CommandRegister::GO_COMMAND), &go_command.raw);
if (err) {
auto ok = this->read_byte(static_cast<uint8_t>(CommandRegister::GO_COMMAND), &go_command.raw);
if (!ok) {
ESP_LOGE(TAG, "Error reading HUSB238");
return false;
}
go_command.function = function;
err = !this->write_byte(static_cast<uint8_t>(CommandRegister::GO_COMMAND), go_command.raw);
if (err) {
ok = this->write_byte(static_cast<uint8_t>(CommandRegister::GO_COMMAND), go_command.raw);
if (!ok) {
ESP_LOGE(TAG, "Error sending command to HUSB238");
}
return err;
return ok;
}

/*
Expand Down Expand Up @@ -328,17 +337,18 @@ RegSrcPdo Husb238Component::get_detected_current_() {
*/

bool Husb238Component::select_pdo_voltage_(SrcVoltageSelection voltage) {
ESP_LOGV(TAG, "Setting PDO voltage selector to %.0f", voltage_to_float());
if (!this->is_ready()) {
ESP_LOGE(TAG, "Component not ready");
return false;
}

this->registers_.src_pdo_sel.voltage = voltage;
auto err = !this->write_byte(static_cast<uint8_t>(CommandRegister::SRC_PDO), this->registers_.src_pdo_sel.raw);
if (err) {
auto ok = this->write_byte(static_cast<uint8_t>(CommandRegister::SRC_PDO), this->registers_.src_pdo_sel.raw);
if (!ok) {
ESP_LOGE(TAG, "Error setting PDO voltage");
}
return err;
return ok;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/husb238/husb238.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ enum class SrcVoltageSelection : uint8_t {
SRC_PDO_20V = 0b1010,
};

enum class CommandFunction : u_int8_t {
enum class CommandFunction : uint8_t {
REQUEST_PDO = 0b00001,
GET_SRC_CAP = 0b00100,
HARD_RESET = 0b10000,
Expand Down

0 comments on commit 71caf26

Please sign in to comment.