mirror of
https://github.com/esphome/esphome.git
synced 2026-05-23 03:06:05 +08:00
[tm1638][rp2040_pio_led_strip][atm90e32] Fix bounds checks and off-by-one (#14559)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -619,7 +619,7 @@ void ATM90E32Component::run_gain_calibrations() {
|
||||
ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
|
||||
phase_labels[phase]);
|
||||
} else {
|
||||
uint32_t new_voltage_gain = static_cast<uint16_t>((ref_voltage / measured_voltage) * current_voltage_gain);
|
||||
uint32_t new_voltage_gain = static_cast<uint32_t>((ref_voltage / measured_voltage) * current_voltage_gain);
|
||||
if (new_voltage_gain == 0) {
|
||||
ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
|
||||
phase_labels[phase]);
|
||||
@@ -644,7 +644,7 @@ void ATM90E32Component::run_gain_calibrations() {
|
||||
ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
|
||||
phase_labels[phase]);
|
||||
} else {
|
||||
uint32_t new_current_gain = static_cast<uint16_t>((ref_current / measured_current) * current_current_gain);
|
||||
uint32_t new_current_gain = static_cast<uint32_t>((ref_current / measured_current) * current_current_gain);
|
||||
if (new_current_gain == 0) {
|
||||
ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
|
||||
phase_labels[phase]);
|
||||
|
||||
@@ -70,7 +70,7 @@ void RP2040PIOLEDStripLightOutput::setup() {
|
||||
// but there are only 4 state machines on each PIO so we can only have 4 strips per PIO
|
||||
uint offset = 0;
|
||||
|
||||
if (RP2040PIOLEDStripLightOutput::num_instance_[this->pio_ == pio0 ? 0 : 1] > 4) {
|
||||
if (RP2040PIOLEDStripLightOutput::num_instance_[this->pio_ == pio0 ? 0 : 1] >= 4) {
|
||||
ESP_LOGE(TAG, "Too many instances of PIO program");
|
||||
this->mark_failed();
|
||||
return;
|
||||
|
||||
@@ -147,35 +147,38 @@ void TM1638Component::set_intensity(uint8_t brightness_level) {
|
||||
|
||||
uint8_t TM1638Component::print(uint8_t start_pos, const char *str) {
|
||||
uint8_t pos = start_pos;
|
||||
|
||||
bool last_was_dot = false;
|
||||
|
||||
for (; *str != '\0'; str++) {
|
||||
uint8_t data = TM1638_UNKNOWN_CHAR;
|
||||
|
||||
if (*str >= ' ' && *str <= '~') {
|
||||
data = progmem_read_byte(&TM1638Translation::SEVEN_SEG[*str - 32]); // subract 32 to account for ASCII offset
|
||||
} else if (data == TM1638_UNKNOWN_CHAR) {
|
||||
// Subtract 32 to account for ASCII offset
|
||||
data = progmem_read_byte(&TM1638Translation::SEVEN_SEG[*str - 32]);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Encountered character '%c' with no TM1638 representation while translating string!", *str);
|
||||
}
|
||||
|
||||
if (*str == '.') // handle dots
|
||||
{
|
||||
if (pos != start_pos &&
|
||||
!last_was_dot) // if we are not at the first position, backup by one unless last char was a dot
|
||||
{
|
||||
if (*str == '.') {
|
||||
// Merge dot onto previous character unless we're at the start or last was also a dot
|
||||
if (pos != start_pos && !last_was_dot) {
|
||||
pos--;
|
||||
}
|
||||
this->buffer_[pos] |= 0b10000000; // turn on the dot on the previous position
|
||||
last_was_dot = true; // set a bit in case the next chracter is also a dot
|
||||
} else // if not a dot, then just write the character to display
|
||||
{
|
||||
if (pos >= 8) {
|
||||
ESP_LOGI(TAG, "TM1638 String is too long for the display!");
|
||||
break;
|
||||
}
|
||||
// Turn on the dot on the previous position
|
||||
this->buffer_[pos] |= 0b10000000;
|
||||
last_was_dot = true;
|
||||
} else {
|
||||
// Not a dot, write the character to display
|
||||
if (pos >= 8) {
|
||||
ESP_LOGI(TAG, "TM1638 String is too long for the display!");
|
||||
break;
|
||||
}
|
||||
this->buffer_[pos] = data;
|
||||
last_was_dot = false; // clear dot tracking bit
|
||||
last_was_dot = false;
|
||||
}
|
||||
|
||||
pos++;
|
||||
|
||||
Reference in New Issue
Block a user