ESPHome 2026.3.0-dev
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
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// TLC59208F MODE2 constants are now inline constexpr in tlc59208f_output.h
30
31// --- Special function ---
32// Call address to perform software reset, no devices will ACK
33const uint8_t TLC59208F_SWRST_ADDR = 0x96; //(0x4b 7-bit addr + ~W)
34const uint8_t TLC59208F_SWRST_SEQ[2] = {0xa5, 0x5a};
35
36// --- Registers ---2
37// Mode register 1
38const uint8_t TLC59208F_REG_MODE1 = 0x00;
39// Mode register 2
40const uint8_t TLC59208F_REG_MODE2 = 0x01;
41// PWM0
42const uint8_t TLC59208F_REG_PWM0 = 0x02;
43// Group PWM
44const uint8_t TLC59208F_REG_GROUPPWM = 0x0a;
45// Group Freq
46const uint8_t TLC59208F_REG_GROUPFREQ = 0x0b;
47// LEDOUTx registers
48const uint8_t TLC59208F_REG_LEDOUT0 = 0x0c;
49const uint8_t TLC59208F_REG_LEDOUT1 = 0x0d;
50// Sub-address registers
51const uint8_t TLC59208F_REG_SUBADR1 = 0x0e; // default: 0x92 (8-bit addr)
52const uint8_t TLC59208F_REG_SUBADR2 = 0x0f; // default: 0x94 (8-bit addr)
53const uint8_t TLC59208F_REG_SUBADR3 = 0x10; // default: 0x98 (8-bit addr)
54// All call address register
55const uint8_t TLC59208F_REG_ALLCALLADR = 0x11; // default: 0xd0 (8-bit addr)
56
57// --- Output modes ---
58static const uint8_t LDR_OFF = 0x00;
59static const uint8_t LDR_ON = 0x01;
60static const uint8_t LDR_PWM = 0x02;
61static const uint8_t LDR_GRPPWM = 0x03;
62
64 ESP_LOGV(TAG, " Resetting all devices on the bus");
65
66 // Reset all devices on the bus
67 if (this->bus_->write_readv(TLC59208F_SWRST_ADDR >> 1, TLC59208F_SWRST_SEQ, sizeof TLC59208F_SWRST_SEQ, nullptr, 0) !=
69 ESP_LOGE(TAG, "RESET failed");
70 this->mark_failed();
71 return;
72 }
73
74 // Auto increment registers, and respond to all-call address
75 if (!this->write_byte(TLC59208F_REG_MODE1, TLC59208F_MODE1_AI2 | TLC59208F_MODE1_ALLCALL)) {
76 ESP_LOGE(TAG, "MODE1 failed");
77 this->mark_failed();
78 return;
79 }
80 if (!this->write_byte(TLC59208F_REG_MODE2, this->mode_)) {
81 ESP_LOGE(TAG, "MODE2 failed");
82 this->mark_failed();
83 return;
84 }
85 // Set all 3 outputs to be individually controlled
86 // TODO: think of a way to support group dimming
87 if (!this->write_byte(TLC59208F_REG_LEDOUT0, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
88 ESP_LOGE(TAG, "LEDOUT0 failed");
89 this->mark_failed();
90 return;
91 }
92 if (!this->write_byte(TLC59208F_REG_LEDOUT1, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
93 ESP_LOGE(TAG, "LEDOUT1 failed");
94 this->mark_failed();
95 return;
96 }
98
99 this->loop();
100}
101
103 ESP_LOGCONFIG(TAG,
104 "TLC59208F:\n"
105 " Mode: 0x%02X",
106 this->mode_);
107
108 if (this->is_failed()) {
109 ESP_LOGE(TAG, "Setting up TLC59208F failed!");
110 }
111}
112
114 if (this->min_channel_ == 0xFF || !this->update_)
115 return;
116
117 for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
118 uint8_t pwm = this->pwm_amounts_[channel];
119 ESP_LOGVV(TAG, "Channel %02u: pwm=%04u ", channel, pwm);
120
121 uint8_t reg = TLC59208F_REG_PWM0 + channel;
122 if (!this->write_byte(reg, pwm)) {
123 this->status_set_warning();
124 return;
125 }
126 }
127
128 this->status_clear_warning();
129 this->update_ = false;
130}
131
133 auto c = channel->channel_;
134 this->min_channel_ = std::min(this->min_channel_, c);
135 this->max_channel_ = std::max(this->max_channel_, c);
136 channel->set_parent(this);
137}
138
140 const uint8_t max_duty = 255;
141 const float duty_rounded = roundf(state * max_duty);
142 auto duty = static_cast<uint8_t>(duty_rounded);
143 this->parent_->set_channel_value_(this->channel_, duty);
144}
145
146} // namespace tlc59208f
147} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:1626
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
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:29