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