mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Support inverted tm1637 display (#2878)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
		| @@ -8,6 +8,8 @@ from esphome.const import ( | |||||||
|     CONF_ID, |     CONF_ID, | ||||||
|     CONF_LAMBDA, |     CONF_LAMBDA, | ||||||
|     CONF_INTENSITY, |     CONF_INTENSITY, | ||||||
|  |     CONF_INVERTED, | ||||||
|  |     CONF_LENGTH, | ||||||
| ) | ) | ||||||
|  |  | ||||||
| CODEOWNERS = ["@glmnet"] | CODEOWNERS = ["@glmnet"] | ||||||
| @@ -22,6 +24,8 @@ CONFIG_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend( | |||||||
|         cv.Optional(CONF_INTENSITY, default=7): cv.All( |         cv.Optional(CONF_INTENSITY, default=7): cv.All( | ||||||
|             cv.uint8_t, cv.Range(min=0, max=7) |             cv.uint8_t, cv.Range(min=0, max=7) | ||||||
|         ), |         ), | ||||||
|  |         cv.Optional(CONF_INVERTED, default=False): cv.boolean, | ||||||
|  |         cv.Optional(CONF_LENGTH, default=6): cv.All(cv.uint8_t, cv.Range(min=1, max=6)), | ||||||
|         cv.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema, |         cv.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema, | ||||||
|         cv.Required(CONF_DIO_PIN): pins.gpio_output_pin_schema, |         cv.Required(CONF_DIO_PIN): pins.gpio_output_pin_schema, | ||||||
|     } |     } | ||||||
| @@ -39,6 +43,8 @@ async def to_code(config): | |||||||
|     cg.add(var.set_dio_pin(dio)) |     cg.add(var.set_dio_pin(dio)) | ||||||
|  |  | ||||||
|     cg.add(var.set_intensity(config[CONF_INTENSITY])) |     cg.add(var.set_intensity(config[CONF_INTENSITY])) | ||||||
|  |     cg.add(var.set_inverted(config[CONF_INVERTED])) | ||||||
|  |     cg.add(var.set_length(config[CONF_LENGTH])) | ||||||
|  |  | ||||||
|     if CONF_LAMBDA in config: |     if CONF_LAMBDA in config: | ||||||
|         lambda_ = await cg.process_lambda( |         lambda_ = await cg.process_lambda( | ||||||
|   | |||||||
| @@ -130,7 +130,9 @@ void TM1637Display::setup() { | |||||||
| } | } | ||||||
| void TM1637Display::dump_config() { | void TM1637Display::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "TM1637:"); |   ESP_LOGCONFIG(TAG, "TM1637:"); | ||||||
|   ESP_LOGCONFIG(TAG, "  INTENSITY: %d", this->intensity_); |   ESP_LOGCONFIG(TAG, "  Intensity: %d", this->intensity_); | ||||||
|  |   ESP_LOGCONFIG(TAG, "  Inverted: %d", this->inverted_); | ||||||
|  |   ESP_LOGCONFIG(TAG, "  Length: %d", this->length_); | ||||||
|   LOG_PIN("  CLK Pin: ", this->clk_pin_); |   LOG_PIN("  CLK Pin: ", this->clk_pin_); | ||||||
|   LOG_PIN("  DIO Pin: ", this->dio_pin_); |   LOG_PIN("  DIO Pin: ", this->dio_pin_); | ||||||
|   LOG_UPDATE_INTERVAL(this); |   LOG_UPDATE_INTERVAL(this); | ||||||
| @@ -173,8 +175,14 @@ void TM1637Display::display() { | |||||||
|   this->send_byte_(TM1637_I2C_COMM2); |   this->send_byte_(TM1637_I2C_COMM2); | ||||||
|  |  | ||||||
|   // Write the data bytes |   // Write the data bytes | ||||||
|   for (auto b : this->buffer_) { |   if (this->inverted_) { | ||||||
|     this->send_byte_(b); |     for (uint8_t i = this->length_ - 1; i >= 0; i--) { | ||||||
|  |       this->send_byte_(this->buffer_[i]); | ||||||
|  |     } | ||||||
|  |   } else { | ||||||
|  |     for (auto b : this->buffer_) { | ||||||
|  |       this->send_byte_(b); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   this->stop_(); |   this->stop_(); | ||||||
| @@ -241,14 +249,27 @@ uint8_t TM1637Display::print(uint8_t start_pos, const char *str) { | |||||||
|     } |     } | ||||||
|     // Remap segments, for compatibility with MAX7219 segment definition which is |     // Remap segments, for compatibility with MAX7219 segment definition which is | ||||||
|     // XABCDEFG, but TM1637 is // XGFEDCBA |     // XABCDEFG, but TM1637 is // XGFEDCBA | ||||||
|     data = ((data & 0x80) ? 0x80 : 0) |  // no move X |     if (this->inverted_) { | ||||||
|            ((data & 0x40) ? 0x1 : 0) |   // A |       // XABCDEFG > XGCBAFED | ||||||
|            ((data & 0x20) ? 0x2 : 0) |   // B |       data = ((data & 0x80) ? 0x80 : 0) |  // no move X | ||||||
|            ((data & 0x10) ? 0x4 : 0) |   // C |              ((data & 0x40) ? 0x8 : 0) |   // A | ||||||
|            ((data & 0x8) ? 0x8 : 0) |    // D |              ((data & 0x20) ? 0x10 : 0) |  // B | ||||||
|            ((data & 0x4) ? 0x10 : 0) |   // E |              ((data & 0x10) ? 0x20 : 0) |  // C | ||||||
|            ((data & 0x2) ? 0x20 : 0) |   // F |              ((data & 0x8) ? 0x1 : 0) |    // D | ||||||
|            ((data & 0x1) ? 0x40 : 0);    // G |              ((data & 0x4) ? 0x2 : 0) |    // E | ||||||
|  |              ((data & 0x2) ? 0x4 : 0) |    // F | ||||||
|  |              ((data & 0x1) ? 0x40 : 0);    // G | ||||||
|  |     } else { | ||||||
|  |       // XABCDEFG > XGFEDCBA | ||||||
|  |       data = ((data & 0x80) ? 0x80 : 0) |  // no move X | ||||||
|  |              ((data & 0x40) ? 0x1 : 0) |   // A | ||||||
|  |              ((data & 0x20) ? 0x2 : 0) |   // B | ||||||
|  |              ((data & 0x10) ? 0x4 : 0) |   // C | ||||||
|  |              ((data & 0x8) ? 0x8 : 0) |    // D | ||||||
|  |              ((data & 0x4) ? 0x10 : 0) |   // E | ||||||
|  |              ((data & 0x2) ? 0x20 : 0) |   // F | ||||||
|  |              ((data & 0x1) ? 0x40 : 0);    // G | ||||||
|  |     } | ||||||
|     if (*str == '.') { |     if (*str == '.') { | ||||||
|       if (pos != start_pos) |       if (pos != start_pos) | ||||||
|         pos--; |         pos--; | ||||||
|   | |||||||
| @@ -41,6 +41,8 @@ class TM1637Display : public PollingComponent { | |||||||
|   uint8_t print(const char *str); |   uint8_t print(const char *str); | ||||||
|  |  | ||||||
|   void set_intensity(uint8_t intensity) { this->intensity_ = intensity; } |   void set_intensity(uint8_t intensity) { this->intensity_ = intensity; } | ||||||
|  |   void set_inverted(bool inverted) { this->inverted_ = inverted; } | ||||||
|  |   void set_length(uint8_t length) { this->length_ = length; } | ||||||
|  |  | ||||||
|   void display(); |   void display(); | ||||||
|  |  | ||||||
| @@ -62,6 +64,8 @@ class TM1637Display : public PollingComponent { | |||||||
|   GPIOPin *dio_pin_; |   GPIOPin *dio_pin_; | ||||||
|   GPIOPin *clk_pin_; |   GPIOPin *clk_pin_; | ||||||
|   uint8_t intensity_; |   uint8_t intensity_; | ||||||
|  |   uint8_t length_; | ||||||
|  |   bool inverted_; | ||||||
|   optional<tm1637_writer_t> writer_{}; |   optional<tm1637_writer_t> writer_{}; | ||||||
|   uint8_t buffer_[6] = {0}; |   uint8_t buffer_[6] = {0}; | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -2119,6 +2119,8 @@ display: | |||||||
|       mcp23xxx: mcp23017_hub |       mcp23xxx: mcp23017_hub | ||||||
|       number: 2 |       number: 2 | ||||||
|     intensity: 3 |     intensity: 3 | ||||||
|  |     inverted: true | ||||||
|  |     length: 4 | ||||||
|     lambda: |- |     lambda: |- | ||||||
|       it.print("1234"); |       it.print("1234"); | ||||||
|   - platform: pcd8544 |   - platform: pcd8544 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user