ESPHome 2026.5.1
Loading...
Searching...
No Matches
tlc59208f_output.cpp
Go to the documentation of this file.
1#include "tlc59208f_output.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "tlc59208f";
9
10// * marks register defaults
11// 0*: Register auto increment disabled, 1: Register auto increment enabled
12const uint8_t TLC59208F_MODE1_AI2 = (1 << 7);
13// 0*: don't auto increment bit 1, 1: auto increment bit 1
14const uint8_t TLC59208F_MODE1_AI1 = (1 << 6);
15// 0*: don't auto increment bit 0, 1: auto increment bit 0
16const uint8_t TLC59208F_MODE1_AI0 = (1 << 5);
17// 0: normal mode, 1*: low power mode, osc off
18const uint8_t TLC59208F_MODE1_SLEEP = (1 << 4);
19// 0*: device doesn't respond to i2c bus sub-address 1, 1: responds
20const uint8_t TLC59208F_MODE1_SUB1 = (1 << 3);
21// 0*: device doesn't respond to i2c bus sub-address 2, 1: responds
22const uint8_t TLC59208F_MODE1_SUB2 = (1 << 2);
23// 0*: device doesn't respond to i2c bus sub-address 3, 1: responds
24const uint8_t TLC59208F_MODE1_SUB3 = (1 << 1);
25// 0: device doesn't respond to i2c all-call 3, 1*: responds to all-call
26const uint8_t TLC59208F_MODE1_ALLCALL = (1 << 0);
27
28// TLC59208F MODE2 constants are now inline constexpr in tlc59208f_output.h
29
30// --- Special function ---
31// Call address to perform software reset, no devices will ACK
32const uint8_t TLC59208F_SWRST_ADDR = 0x96; //(0x4b 7-bit addr + ~W)
33const uint8_t TLC59208F_SWRST_SEQ[2] = {0xa5, 0x5a};
34
35// --- Registers ---2
36// Mode register 1
37const uint8_t TLC59208F_REG_MODE1 = 0x00;
38// Mode register 2
39const uint8_t TLC59208F_REG_MODE2 = 0x01;
40// PWM0
41const uint8_t TLC59208F_REG_PWM0 = 0x02;
42// Group PWM
43const uint8_t TLC59208F_REG_GROUPPWM = 0x0a;
44// Group Freq
45const uint8_t TLC59208F_REG_GROUPFREQ = 0x0b;
46// LEDOUTx registers
47const uint8_t TLC59208F_REG_LEDOUT0 = 0x0c;
48const uint8_t TLC59208F_REG_LEDOUT1 = 0x0d;
49// Sub-address registers
50const uint8_t TLC59208F_REG_SUBADR1 = 0x0e; // default: 0x92 (8-bit addr)
51const uint8_t TLC59208F_REG_SUBADR2 = 0x0f; // default: 0x94 (8-bit addr)
52const uint8_t TLC59208F_REG_SUBADR3 = 0x10; // default: 0x98 (8-bit addr)
53// All call address register
54const uint8_t TLC59208F_REG_ALLCALLADR = 0x11; // default: 0xd0 (8-bit addr)
55
56// --- Output modes ---
57static const uint8_t LDR_OFF = 0x00;
58static const uint8_t LDR_ON = 0x01;
59static const uint8_t LDR_PWM = 0x02;
60static const uint8_t LDR_GRPPWM = 0x03;
61
63 ESP_LOGV(TAG, " Resetting all devices on the bus");
64
65 // Reset all devices on the bus
66 if (this->bus_->write_readv(TLC59208F_SWRST_ADDR >> 1, TLC59208F_SWRST_SEQ, sizeof TLC59208F_SWRST_SEQ, nullptr, 0) !=
68 ESP_LOGE(TAG, "RESET failed");
69 this->mark_failed();
70 return;
71 }
72
73 // Auto increment registers, and respond to all-call address
74 if (!this->write_byte(TLC59208F_REG_MODE1, TLC59208F_MODE1_AI2 | TLC59208F_MODE1_ALLCALL)) {
75 ESP_LOGE(TAG, "MODE1 failed");
76 this->mark_failed();
77 return;
78 }
79 if (!this->write_byte(TLC59208F_REG_MODE2, this->mode_)) {
80 ESP_LOGE(TAG, "MODE2 failed");
81 this->mark_failed();
82 return;
83 }
84 // Set all 3 outputs to be individually controlled
85 // TODO: think of a way to support group dimming
86 if (!this->write_byte(TLC59208F_REG_LEDOUT0, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
87 ESP_LOGE(TAG, "LEDOUT0 failed");
88 this->mark_failed();
89 return;
90 }
91 if (!this->write_byte(TLC59208F_REG_LEDOUT1, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
92 ESP_LOGE(TAG, "LEDOUT1 failed");
93 this->mark_failed();
94 return;
95 }
97
98 this->loop();
99}
100
102 ESP_LOGCONFIG(TAG,
103 "TLC59208F:\n"
104 " Mode: 0x%02X",
105 this->mode_);
106
107 if (this->is_failed()) {
108 ESP_LOGE(TAG, "Setting up TLC59208F failed!");
109 }
110}
111
113 if (this->min_channel_ == 0xFF || !this->update_)
114 return;
115
116 for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
117 uint8_t pwm = this->pwm_amounts_[channel];
118 ESP_LOGVV(TAG, "Channel %02u: pwm=%04u ", channel, pwm);
119
120 uint8_t reg = TLC59208F_REG_PWM0 + channel;
121 if (!this->write_byte(reg, pwm)) {
122 this->status_set_warning();
123 return;
124 }
125 }
126
127 this->status_clear_warning();
128 this->update_ = false;
129}
130
132 auto c = channel->channel_;
133 this->min_channel_ = std::min(this->min_channel_, c);
134 this->max_channel_ = std::max(this->max_channel_, c);
135 channel->set_parent(this);
136}
137
139 const uint8_t max_duty = 255;
140 const float duty_rounded = roundf(state * max_duty);
141 auto duty = static_cast<uint8_t>(duty_rounded);
142 this->parent_->set_channel_value_(this->channel_, duty);
143}
144
145} // namespace esphome::tlc59208f
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_warning()
Definition component.h:306
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:1869
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:271
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
void write_state(float state) override
void register_channel(TLC59208FChannel *channel)
bool state
Definition fan.h:2
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
const uint8_t TLC59208F_REG_GROUPFREQ
const uint8_t TLC59208F_REG_LEDOUT0
const uint8_t TLC59208F_REG_MODE1
const uint8_t TLC59208F_SWRST_SEQ[2]
const uint8_t TLC59208F_REG_SUBADR1
const uint8_t TLC59208F_REG_MODE2
const uint8_t TLC59208F_SWRST_ADDR
const uint8_t TLC59208F_REG_SUBADR2
const uint8_t TLC59208F_MODE1_SLEEP
const uint8_t TLC59208F_MODE1_AI0
const uint8_t TLC59208F_REG_LEDOUT1
const uint8_t TLC59208F_MODE1_AI1
const uint8_t TLC59208F_MODE1_SUB1
const uint8_t TLC59208F_MODE1_SUB2
const uint8_t TLC59208F_REG_PWM0
const uint8_t TLC59208F_MODE1_SUB3
const uint8_t TLC59208F_MODE1_AI2
const uint8_t TLC59208F_REG_ALLCALLADR
const uint8_t TLC59208F_REG_SUBADR3
const uint8_t TLC59208F_REG_GROUPPWM
const uint8_t TLC59208F_MODE1_ALLCALL
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48