Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
More changes for 1.12 (#552)
Browse files Browse the repository at this point in the history
* More 1.12 updates

* Fix log

* Lint

* Lint

* Simplify

* Update api_server.cpp

* Update rotary_encoder.cpp

* Fix global var array

* Lint
  • Loading branch information
OttoWinter committed Mar 17, 2019
1 parent bb2c69b commit 8608b20
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 65 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ platform = espressif8266@1.8.0
board = nodemcuv2
framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
build_flags = ${common.build_flags} -DESPHOME_LOG_LEVEL=6
src_filter = ${common.src_filter} +<examples/livingroom8266/livingroom8266.cpp>

[env:custombmp180]
Expand Down
8 changes: 8 additions & 0 deletions src/esphome/api/api_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ void APIServer::loop() {
ESP_LOGE(TAG, "No client connected to API. Rebooting...");
reboot("api");
}
this->status_set_warning();
} else {
this->last_connected_ = now;
this->status_clear_warning();
}
}
}
Expand Down Expand Up @@ -714,6 +716,12 @@ bool APIConnection::send_buffer(APIMessageType type) {
}

void APIConnection::loop() {
if (!network_is_connected()) {
// when network is disconnected force disconnect immediately
// don't wait for timeout
this->fatal_error_();
return;
}
if (this->client_->disconnected()) {
// failsafe for disconnect logic
this->on_disconnect_();
Expand Down
8 changes: 8 additions & 0 deletions src/esphome/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ class Application {
template<typename T> GlobalVariableComponent<T> *make_global_variable();

template<typename T> GlobalVariableComponent<T> *make_global_variable(T initial_value);
template<typename T>
GlobalVariableComponent<T> *make_global_variable(
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value);

/* _ _ _______ ____ __ __ _______ _____ ____ _ _
* /\ | | | |__ __/ __ \| \/ | /\|__ __|_ _/ __ \| \ | |
Expand Down Expand Up @@ -1233,6 +1236,11 @@ template<typename T> GlobalVariableComponent<T> *Application::make_global_variab
template<typename T> GlobalVariableComponent<T> *Application::make_global_variable(T initial_value) {
return this->register_component(new GlobalVariableComponent<T>(initial_value));
}
template<typename T>
GlobalVariableComponent<T> *Application::make_global_variable(
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
return this->register_component(new GlobalVariableComponent<T>(initial_value));
}

#ifdef USE_NEO_PIXEL_BUS_LIGHT
template<typename T_METHOD, typename T_COLOR_FEATURE>
Expand Down
2 changes: 2 additions & 0 deletions src/esphome/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ template<typename T> class GlobalVariableComponent : public Component {
public:
explicit GlobalVariableComponent();
explicit GlobalVariableComponent(T initial_value);
explicit GlobalVariableComponent(
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value);

T &value();

Expand Down
5 changes: 5 additions & 0 deletions src/esphome/automation.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ template<typename... Ts> ScriptStopAction<Ts...> *Script::make_stop_action() {

template<typename T> GlobalVariableComponent<T>::GlobalVariableComponent() {}
template<typename T> GlobalVariableComponent<T>::GlobalVariableComponent(T initial_value) : value_(initial_value) {}
template<typename T>
GlobalVariableComponent<T>::GlobalVariableComponent(
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
memcpy(this->value_, initial_value.data(), sizeof(T));
}
template<typename T> T &GlobalVariableComponent<T>::value() { return this->value_; }
template<typename T> void GlobalVariableComponent<T>::setup() {
if (this->restore_value_) {
Expand Down
6 changes: 3 additions & 3 deletions src/esphome/binary_sensor/binary_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ void MultiClickTrigger::on_state_(bool state) {
MultiClickTriggerEvent evt = this->timing_[*this->at_index_];

if (evt.max_length != 4294967294UL) {
ESP_LOGV(TAG, "A i=%u min=%u max=%u", *this->at_index_, evt.min_length, evt.max_length);
ESP_LOGV(TAG, "A i=%u min=%u max=%u", *this->at_index_, evt.min_length, evt.max_length); // NOLINT
this->schedule_is_valid_(evt.min_length);
this->schedule_is_not_valid_(evt.max_length);
} else if (*this->at_index_ + 1 != this->timing_.size()) {
ESP_LOGV(TAG, "B i=%u min=%u", *this->at_index_, evt.min_length);
ESP_LOGV(TAG, "B i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
this->cancel_timeout("is_not_valid");
this->schedule_is_valid_(evt.min_length);
} else {
ESP_LOGV(TAG, "C i=%u min=%u", *this->at_index_, evt.min_length);
ESP_LOGV(TAG, "C i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
this->is_valid_ = false;
this->cancel_timeout("is_not_valid");
this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
Expand Down
5 changes: 2 additions & 3 deletions src/esphome/esppreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool ESPPreferenceObject::load_() {

bool valid = this->data_[this->length_words_] == this->calculate_crc_();

ESP_LOGVV(TAG, "LOAD %u: valid=%s, 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, YESNO(valid),
ESP_LOGVV(TAG, "LOAD %zu: valid=%s, 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, YESNO(valid),
this->data_[0], this->data_[1], this->type_, this->calculate_crc_());
return valid;
}
Expand All @@ -45,7 +45,7 @@ bool ESPPreferenceObject::save_() {
this->data_[this->length_words_] = this->calculate_crc_();
if (!this->save_internal_())
return false;
ESP_LOGVV(TAG, "SAVE %u: 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, this->data_[0], this->data_[1],
ESP_LOGVV(TAG, "SAVE %zu: 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, this->data_[0], this->data_[1],
this->type_, this->calculate_crc_());
return true;
}
Expand Down Expand Up @@ -80,7 +80,6 @@ static inline bool esp_rtc_user_mem_write(uint32_t index, uint32_t value) {
auto *ptr = &ESP_RTC_USER_MEM[index];
#ifdef USE_ESP8266_PREFERENCES_FLASH
if (*ptr != value) {
ESP_LOGV(TAG, "RTC flash is dirty...");
esp8266_preferences_modified = true;
}
#endif
Expand Down
31 changes: 23 additions & 8 deletions src/esphome/light/light_color_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,33 @@ void LightColorValues::normalize_color(const LightTraits &traits) {
float max_value = fmaxf(this->get_red(), fmaxf(this->get_green(), this->get_blue()));
if (traits.has_rgb_white_value()) {
max_value = fmaxf(max_value, this->get_white());
this->set_white(this->get_white() / max_value);
if (max_value == 0.0f) {
this->set_white(1.0f);
} else {
this->set_white(this->get_white() / max_value);
}
}
if (max_value == 0.0f) {
this->set_red(1.0f);
this->set_green(1.0f);
this->set_blue(1.0f);
} else {
this->set_red(this->get_red() / max_value);
this->set_green(this->get_green() / max_value);
this->set_blue(this->get_blue() / max_value);
}
this->set_red(this->get_red() / max_value);
this->set_green(this->get_green() / max_value);
this->set_blue(this->get_blue() / max_value);
}

if (traits.has_brightness() && this->get_brightness() == 0.0f) {
// 0% brightness means off
this->set_state(false);
// reset brightness to 100% (0% brightness is not allowed)
this->set_brightness(1.0f);
if (traits.has_rgb_white_value()) {
// 0% brightness for RGBW[W] means no RGB channel, but white channel on.
// do nothing
} else {
// 0% brightness means off
this->set_state(false);
// reset brightness to 100%
this->set_brightness(1.0f);
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/esphome/light/light_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ void LightState::dump_json(JsonObject &root) {
}

struct LightStateRTCState {
bool state;
float brightness;
float red;
float green;
float blue;
float white;
float color_temp;
uint32_t effect;
bool state{false};
float brightness{1.0f};
float red{1.0f};
float green{1.0f};
float blue{1.0f};
float white{1.0f};
float color_temp{1.0f};
uint32_t effect{0};
};

void LightState::setup() {
Expand All @@ -143,9 +143,9 @@ void LightState::setup() {
}

this->rtc_ = global_preferences.make_preference<LightStateRTCState>(this->get_object_id_hash());
LightStateRTCState recovered;
if (!this->rtc_.load(&recovered))
return;
LightStateRTCState recovered{};
// Attempt to load from preferences, else fall back to default values from struct
this->rtc_.load(&recovered);

auto call = this->make_call();
call.set_state(recovered.state);
Expand Down Expand Up @@ -438,7 +438,7 @@ void LightState::StateCall::perform() const {
ESP_LOGD(TAG, " Color Temperature: %.1f mireds", v.get_color_temperature());
}

v.normalize_color(this->state_->output_->get_traits());
v.normalize_color(traits);

if (traits.has_rgb() && (this->red_.has_value() || this->green_.has_value() || this->blue_.has_value())) {
if (traits.has_rgb_white_value() && this->white_.has_value()) {
Expand Down
8 changes: 4 additions & 4 deletions src/esphome/remote/remote_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ void RemoteReceiverComponent::loop() {
// TODO: Handle case when loop() is not called quickly enough to catch idle
return;

ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", this->buffer_read_at_, write_at, dist, now,
this->buffer_[write_at]);
ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", s.buffer_read_at, write_at, dist, now,
s.buffer[write_at]);

// Skip first value, it's from the previous idle level
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
Expand All @@ -345,8 +345,8 @@ void RemoteReceiverComponent::loop() {
break;
}

ESP_LOGVV(TAG, " i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, this->buffer_read_at_,
this->buffer_[this->buffer_read_at_], prev, this->buffer_[prev], multiplier * delta);
ESP_LOGVV(TAG, " i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, s.buffer_read_at, s.buffer[s.buffer_read_at], prev,
s.buffer[prev], multiplier * delta);
this->temp_.push_back(multiplier * delta);
prev = s.buffer_read_at;
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
Expand Down
54 changes: 23 additions & 31 deletions src/esphome/sensor/cse7766.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,8 @@ bool CSE7766Component::check_byte_() {
uint8_t index = this->raw_data_index_;
uint8_t byte = this->raw_data_[index];
if (index == 0) {
// Header - 0x55
// These messages are verbose because the CSE7766
// reports these when no load is attached. If the checksum doesn't match
// though, then we should print a warning.
if (byte == 0xAA) {
ESP_LOGV(TAG, "CSE7766 not calibrated!");
return false;
}
if ((byte & 0xF0) == 0xF0) {
ESP_LOGV(TAG, "CSE7766 reports abnormal hardware: (0x%02X)", byte);
if ((byte >> 3) & 1) {
ESP_LOGV(TAG, " Voltage cycle exceeds range.");
}
if ((byte >> 2) & 1) {
ESP_LOGV(TAG, " Current cycle exceeds range.");
}
if ((byte >> 1) & 1) {
ESP_LOGV(TAG, " Power cycle exceeds range.");
}
if ((byte >> 0) & 1) {
ESP_LOGV(TAG, " Coefficient storage area is abnormal.");
}
return false;
}
if (byte != 0x55) {
ESP_LOGV(TAG, "Invalid Header Start from CSE7766: 0x%02X!", byte);
return false;
}
// Header, usually 0x55, contains data about calibration etc.
// this is validated in parse_data_
return true;
}

Expand Down Expand Up @@ -103,6 +77,18 @@ void CSE7766Component::parse_data_() {
ESP_LOGVV(TAG, " i=%u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", i, BYTE_TO_BINARY(this->raw_data_[i]),
this->raw_data_[i]);
}

uint8_t header1 = this->raw_data_[0];
if (header1 == 0xAA) {
ESP_LOGW(TAG, "CSE7766 not calibrated!");
return;
}
if ((header1 & 0xF0) == 0xF0 && ((header1 >> 0) & 1) == 1) {
ESP_LOGW(TAG, "CSE7766 reports abnormal hardware: (0x%02X)", header1);
ESP_LOGW(TAG, " Coefficient storage area is abnormal.");
return;
}

const uint32_t now = micros();
const float d = (now - this->last_reading_) / 1000.0f;
this->last_reading_ = now;
Expand All @@ -116,19 +102,25 @@ void CSE7766Component::parse_data_() {

uint8_t adj = this->raw_data_[20];

if ((adj >> 6 != 0) && voltage_cycle != 0) {
if ((adj >> 6 != 0) && voltage_cycle != 0 &&
// voltage cycle exceeds range
((header1 >> 3) & 1) == 0) {
// voltage cycle of serial port outputted is a complete cycle;
float voltage = voltage_calib / float(voltage_cycle);
this->voltage_acc_ += voltage * d;
}

if ((adj >> 5 != 0) && power_cycle != 0 && current_cycle != 0) {
if ((adj >> 5 != 0) && power_cycle != 0 && current_cycle != 0 &&
// current cycle exceeds range
((header1 >> 2) & 1) == 0) {
// indicates current cycle of serial port outputted is a complete cycle;
float current = current_calib / float(current_cycle);
this->current_acc_ += current * d;
}

if ((adj >> 4 != 0) && power_cycle != 0) {
if ((adj >> 4 != 0) && power_cycle != 0 &&
// power cycle exceeds range
((header1 >> 1) & 1) == 0) {
// power cycle of serial port outputted is a complete cycle;
float active_power = power_calib / float(power_cycle);
this->power_acc_ += active_power * d;
Expand Down
2 changes: 1 addition & 1 deletion src/esphome/sensor/rotary_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void ICACHE_RAM_ATTR HOT RotaryEncoderSensorStore::gpio_intr(RotaryEncoderSensor
if (arg->pin_b->digital_read())
input_state |= STATE_PIN_B_HIGH;

uint8_t new_state = STATE_LOOKUP_TABLE[input_state];
uint16_t new_state = STATE_LOOKUP_TABLE[input_state];
if ((new_state & arg->resolution & STATE_HAS_INCREMENTED) != 0) {
if (arg->counter < arg->max_value)
arg->counter++;
Expand Down
11 changes: 9 additions & 2 deletions src/esphome/wifi_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
} else {
ESP_LOGV(TAG, " BSSID: Not Set");
}
ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password());
ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password().c_str());
if (ap.get_channel().has_value()) {
ESP_LOGV(TAG, " Channel: %u", *ap.get_channel());
} else {
Expand Down Expand Up @@ -779,6 +779,12 @@ const char *get_disconnect_reason_str(uint8_t reason) {
}

void WiFiComponent::wifi_event_callback(System_Event_t *event) {
#ifdef ESPHOME_LOG_HAS_VERBOSE
// TODO: this callback is called while in cont context, so delay will fail
// We need to defer the log messages until we're out of this context
// only affects verbose log level
// reproducible by enabling verbose log level and letting the ESP disconnect and
// then reconnect to WiFi.
switch (event->event) {
case EVENT_STAMODE_CONNECTED: {
auto it = event->event_info.connected;
Expand All @@ -794,7 +800,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
char buf[33];
memcpy(buf, it.ssid, it.ssid_len);
buf[it.ssid_len] = '\0';
ESP_LOGW(TAG, "Event: Disconnected ssid='%s' bssid=%s reason='%s'", buf, format_mac_addr(it.bssid).c_str(),
ESP_LOGV(TAG, "Event: Disconnected ssid='%s' bssid=%s reason='%s'", buf, format_mac_addr(it.bssid).c_str(),
get_disconnect_reason_str(it.reason));
break;
}
Expand Down Expand Up @@ -844,6 +850,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
default:
break;
}
#endif

if (event->event == EVENT_STAMODE_DISCONNECTED) {
global_wifi_component->error_from_callback_ = true;
Expand Down

0 comments on commit 8608b20

Please sign in to comment.