diff --git a/components/ht16k33_alpha/README.md b/components/ht16k33_alpha/README.md index bf87f5e8..2db8f97f 100644 --- a/components/ht16k33_alpha/README.md +++ b/components/ht16k33_alpha/README.md @@ -20,6 +20,7 @@ display: scroll_speed: 250ms scroll_dwell: 2s scroll_delay: 3 + size: 4 lambda: |- it.print("ABCD"); secondary_displays: @@ -40,3 +41,5 @@ display: `secondary_display:` is a list of i2c devices where `address:` is required and `i2c_id:` is optional unless there is more than one i2c bus. +`size:` number of characters in module, default is 4. + diff --git a/components/ht16k33_alpha/display.py b/components/ht16k33_alpha/display.py index 98e31c7e..c11b36b2 100644 --- a/components/ht16k33_alpha/display.py +++ b/components/ht16k33_alpha/display.py @@ -12,8 +12,10 @@ CONF_SCROLL = "scroll" CONF_SCROLL_SPEED = "scroll_speed" CONF_SCROLL_DWELL = "scroll_dwell" +CONF_SWAP_BITS = "swap_bits" CONF_SCROLL_DELAY = "scroll_delay" CONF_SECONDARY_DISPLAYS = "secondary_displays" +CONF_SIZE = "size" CONFIG_SECONDARY = cv.Schema({ cv.GenerateID(): cv.declare_id(i2c.I2CDevice) @@ -23,10 +25,12 @@ cv.GenerateID(): cv.declare_id(HT16K33AlphaDisplay), cv.Optional(CONF_CONTINUOUS, default=False): cv.boolean, cv.Optional(CONF_SCROLL, default=False): cv.boolean, + cv.Optional(CONF_SWAP_BITS, default=False): cv.boolean, cv.Optional(CONF_SCROLL_SPEED, default='250ms'): cv.positive_time_period_milliseconds, cv.Optional(CONF_SCROLL_DWELL, default='2s'): cv.positive_time_period_milliseconds, cv.Optional(CONF_SCROLL_DELAY, default='3'): cv.float_range(min=1), cv.Optional(CONF_SECONDARY_DISPLAYS): cv.ensure_list(CONFIG_SECONDARY), + cv.Optional(CONF_SIZE, default=4): cv.int_range(min=1, max=8) }).extend(cv.polling_component_schema('1s')).extend(i2c.i2c_device_schema(0x70)) async def to_code(config): @@ -42,8 +46,10 @@ async def to_code(config): if config[CONF_SCROLL]: cg.add(var.set_scroll(True)) cg.add(var.set_continuous(config[CONF_CONTINUOUS])) + cg.add(var.set_swap_bits(config[CONF_SWAP_BITS])) cg.add(var.set_scroll_speed(config[CONF_SCROLL_SPEED])) cg.add(var.set_scroll_dwell(config[CONF_SCROLL_DWELL])) + cg.add(var.set_size(config[CONF_SIZE])) cg.add(var.set_scroll_delay(int(config[CONF_SCROLL_DELAY] * config[CONF_SCROLL_SPEED].total_milliseconds))) if CONF_SECONDARY_DISPLAYS in config: for conf in config[CONF_SECONDARY_DISPLAYS]: diff --git a/components/ht16k33_alpha/font.h b/components/ht16k33_alpha/font.h index 4b783f4f..2915755a 100644 --- a/components/ht16k33_alpha/font.h +++ b/components/ht16k33_alpha/font.h @@ -54,7 +54,8 @@ static const uint16_t alphafonttable[] PROGMEM = { 0b0000000011011011, // 2 0b0000000010001111, // 3 0b0000000011100110, // 4 -0b0010000001101001, // 5 +// 0b0010000001101001, // 5 +0b0000000011101101, // 5 fix 0b0000000011111101, // 6 0b0000000000000111, // 7 0b0000000011111111, // 8 diff --git a/components/ht16k33_alpha/ht16k33_display.cpp b/components/ht16k33_alpha/ht16k33_display.cpp index f63fab55..6f83bb45 100644 --- a/components/ht16k33_alpha/ht16k33_display.cpp +++ b/components/ht16k33_alpha/ht16k33_display.cpp @@ -30,7 +30,7 @@ void HT16K33AlphaDisplay::setup() { void HT16K33AlphaDisplay::loop() { unsigned long now = millis(); - int numc = this->displays_.size() * 8; + int numc = this->displays_.size() * this->size_ * 2; int len = this->buffer_.size(); if (!this->scroll_ || (len <= numc)) return; @@ -53,7 +53,7 @@ void HT16K33AlphaDisplay::loop() { float HT16K33AlphaDisplay::get_setup_priority() const { return setup_priority::PROCESSOR; } void HT16K33AlphaDisplay::display_() { - int numc = this->displays_.size() * 8; + int numc = this->displays_.size() * this->size_ * 2; int len = this->buffer_.size(); uint8_t data[numc]; memset(data, 0, numc); @@ -68,7 +68,7 @@ void HT16K33AlphaDisplay::display_() { } pos = 0; for (auto *display : this->displays_) { - display->write_bytes(DISPLAY_COMMAND_SET_DDRAM_ADDR, data + pos, 8); + display->write_bytes(DISPLAY_COMMAND_SET_DDRAM_ADDR, data + pos, this->size_ * 2); pos += 8; } } @@ -107,6 +107,26 @@ float HT16K33AlphaDisplay::get_brightness() { return this->brightness_ / 16.0; } +uint16_t HT16K33AlphaDisplay::swapBits(uint16_t n, uint p1, uint p2) { + // Extract the bit at position p1 + unsigned int bit1 = (n >> p1) & 1; + + // Extract the bit at position p2 + unsigned int bit2 = (n >> p2) & 1; + + // If the bits are different, perform the swap + if (bit1 != bit2) { + // Create a mask to toggle the bits at p1 and p2 + // If bit1 is 0 and bit2 is 1, set bit at p1 and clear bit at p2 + // If bit1 is 1 and bit2 is 0, clear bit at p1 and set bit at p2 + unsigned int xor_mask = (1U << p1) | (1U << p2); + + // XOR the number with the mask to swap the bits + n ^= xor_mask; + } + return n; +} + void HT16K33AlphaDisplay::print(const char *str) { uint16_t fontc = 0; while (*str != '\0') { @@ -120,6 +140,9 @@ void HT16K33AlphaDisplay::print(const char *str) { fontc |= 0x4000; str++; } + if(this->swap_bits_) { + fontc = this->swapBits(fontc, 11, 13); + } this->buffer_.push_back(fontc & 0xff); this->buffer_.push_back(fontc >> 8); } diff --git a/components/ht16k33_alpha/ht16k33_display.h b/components/ht16k33_alpha/ht16k33_display.h index 0ac30c26..ae7b1504 100644 --- a/components/ht16k33_alpha/ht16k33_display.h +++ b/components/ht16k33_alpha/ht16k33_display.h @@ -20,10 +20,12 @@ class HT16K33AlphaDisplay : public PollingComponent, public i2c::I2CDevice { float get_setup_priority() const override; void add_secondary_display(i2c::I2CDevice *display) { this->displays_.push_back(display); } void set_scroll(bool scroll) { this->scroll_ = scroll; } + void set_swap_bits(bool swap_bits) { this->swap_bits_ = swap_bits; } void set_continuous(bool continuous) { this->continuous_ = continuous; } void set_scroll_speed(unsigned long scroll_speed) { this->scroll_speed_ = scroll_speed; } void set_scroll_dwell(unsigned long scroll_dwell) { this->scroll_dwell_ = scroll_dwell; } void set_scroll_delay(unsigned long scroll_delay) { this->scroll_delay_ = scroll_delay; } + void set_size(unsigned long size) { this->size_ = size; } void update() override; //// Clear display void set_brightness(float level); @@ -45,15 +47,18 @@ class HT16K33AlphaDisplay : public PollingComponent, public i2c::I2CDevice { void command_(uint8_t value); void call_writer() { this->writer_(*this); } void display_(); + uint16_t swapBits(uint16_t n, uint p1, uint p2); std::vector displays_ {this}; std::function writer_; bool scroll_ {false}; bool continuous_ {false}; + bool swap_bits_ {false}; unsigned long scroll_speed_ {250}; unsigned long scroll_dwell_ {2000}; unsigned long scroll_delay_ {750}; unsigned long last_scroll_ {0}; + unsigned long size_ {4}; std::vector buffer_; int offset_ {0}; uint8_t brightness_ = 16;