diff --git a/components/spi_74hc595_display/display.py b/components/spi_74hc595_display/display.py index 4b63684..09f5560 100644 --- a/components/spi_74hc595_display/display.py +++ b/components/spi_74hc595_display/display.py @@ -12,6 +12,7 @@ SPI_74HC595_DISPLAYComponent = spi_74hc595_display_ns.class_( SPI_74HC595_DISPLAYComponentRef = SPI_74HC595_DISPLAYComponent.operator("ref") CONF_REVERSE = "reverse" +CONF_REVERSE_CHAIN = "reverse_chain" CONF_SEGMENT_FIRST = "segment_first" CONF_COMMON_CATHODE = "common_cathode" @@ -21,7 +22,8 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(SPI_74HC595_DISPLAYComponent), cv.Optional(CONF_NUM_CHIPS, default=1): cv.int_range(min=1, max=255), cv.Optional(CONF_REVERSE, default=False): cv.boolean, - cv.Optional(CONF_SEGMENT_FIRST, default=False): cv.boolean, + cv.Optional(CONF_REVERSE_CHAIN, default=False): cv.boolean, + cv.Optional(CONF_SEGMENT_FIRST, default=True): cv.boolean, cv.Optional(CONF_COMMON_CATHODE, default=False): cv.boolean, } ) @@ -37,6 +39,7 @@ async def to_code(config): cg.add(var.set_num_chips(config[CONF_NUM_CHIPS])) cg.add(var.set_reverse(config[CONF_REVERSE])) + cg.add(var.set_reverse_chain(config[CONF_REVERSE_CHAIN])) cg.add(var.set_segment_first(config[CONF_SEGMENT_FIRST])) cg.add(var.set_common_cathode(config[CONF_COMMON_CATHODE])) diff --git a/components/spi_74hc595_display/spi_74hc595_display.cpp b/components/spi_74hc595_display/spi_74hc595_display.cpp index 11be3dd..b2405d7 100644 --- a/components/spi_74hc595_display/spi_74hc595_display.cpp +++ b/components/spi_74hc595_display/spi_74hc595_display.cpp @@ -116,7 +116,13 @@ void SPI_74HC595_DISPLAYComponent::setup() { this->spi_setup(); this->buffer_ = new uint8_t[this->num_chips_ * 8]; // NOLINT for (uint8_t i = 0; i < this->num_chips_ * 8; i++) + { this->buffer_[i] = 0; + } + if (this->common_cathode_){ + this->flip_digit_=0xff; + this->flip_segment_= ~this->flip_digit; + } } void SPI_74HC595_DISPLAYComponent::dump_config() { @@ -128,8 +134,6 @@ void SPI_74HC595_DISPLAYComponent::dump_config() { void SPI_74HC595_DISPLAYComponent::display() { uint32_t delay = static_cast(this->get_update_interval())*1000 / 8; - uint8_t flip_digit=(this->common_cathode_)?0xFF:0x00; - uint8_t flip_segment = ~flip_digit; for (uint8_t i = 0; i < 8; i++) { this->enable(); for (uint8_t j = 0; j < this->num_chips_; j++) { diff --git a/components/spi_74hc595_display/spi_74hc595_display.h b/components/spi_74hc595_display/spi_74hc595_display.h index d68bfa1..a1713d8 100644 --- a/components/spi_74hc595_display/spi_74hc595_display.h +++ b/components/spi_74hc595_display/spi_74hc595_display.h @@ -56,9 +56,15 @@ class SPI_74HC595_DISPLAYComponent : public PollingComponent, uint8_t num_chips_{1}; uint8_t *buffer_; bool reverse_{false}; - bool segment_first_{false}; + bool reverse_chain_{false}; + bool segment_first_{true}; bool common_cathode_{false}; optional writer_{}; + + uint8_t flip_digit_{0}; + uint8_t flip_segment_{0xFF}; + uint8_t digit_mask[8]{0x08,0x04,0x02,0x01,0x80,0x40,0x20,0x10}; + }; } // namespace spi_74hc595_display diff --git a/esp32-clock-template.yaml b/esp32-clock-template.yaml index 8e7e8b7..37a9328 100644 --- a/esp32-clock-template.yaml +++ b/esp32-clock-template.yaml @@ -71,9 +71,9 @@ display: data_rate: 1MHz #10MHz num_chips: 1 #3 update_interval: 4s #16ms - common_cathode: false - segment_first: false - reverse: false + # common_cathode: false + # segment_first: true + # reverse: false lambda: |- static uint32_t clock_frames=0; static uint32_t old_clock=0;