ESPHome 2026.11.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
7
8ESPHOME_LOG_TAG(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 this->mark_failed(LOG_STR("RESET failed"));
69 return;
70 }
71
72 // Auto increment registers, and respond to all-call address
73 if (!this->write_byte(TLC59208F_REG_MODE1, TLC59208F_MODE1_AI2 | TLC59208F_MODE1_ALLCALL)) {
74 this->mark_failed(LOG_STR("MODE1 failed"));
75 return;
76 }
77 if (!this->write_byte(TLC59208F_REG_MODE2, this->mode_)) {
78 this->mark_failed(LOG_STR("MODE2 failed"));
79 return;
80 }
81 // Set all 3 outputs to be individually controlled
82 // TODO: think of a way to support group dimming
83 if (!this->write_byte(TLC59208F_REG_LEDOUT0, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
84 this->mark_failed(LOG_STR("LEDOUT0 failed"));
85 return;
86 }
87 if (!this->write_byte(TLC59208F_REG_LEDOUT1, (LDR_PWM << 6) | (LDR_PWM << 4) | (LDR_PWM << 2) | (LDR_PWM << 0))) {
88 this->mark_failed(LOG_STR("LEDOUT1 failed"));
89 return;
90 }
92
93 this->loop();
94}
95
97 ESP_LOGCONFIG(TAG,
98 "TLC59208F:\n"
99 " Mode: 0x%02X",
100 this->mode_);
101}
102
104 if (this->min_channel_ == 0xFF || !this->update_)
105 return;
106
107 for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
108 uint8_t pwm = this->pwm_amounts_[channel];
109 ESP_LOGVV(TAG, "Channel %02u: pwm=%04u ", channel, pwm);
110
111 uint8_t reg = TLC59208F_REG_PWM0 + channel;
112 if (!this->write_byte(reg, pwm)) {
113 this->status_set_warning();
114 return;
115 }
116 }
117
118 this->status_clear_warning();
119 this->update_ = false;
120}
121
123 auto c = channel->channel_;
124 this->min_channel_ = std::min(this->min_channel_, c);
125 this->max_channel_ = std::max(this->max_channel_, c);
126 channel->set_parent(this);
127}
128
130 const uint8_t max_duty = 255;
131 const float duty_rounded = roundf(state * max_duty);
132 auto duty = static_cast<uint8_t>(duty_rounded);
133 this->parent_->set_channel_value_(this->channel_, duty);
134}
135
136} // namespace esphome::tlc59208f
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:2081
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:325
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
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:15
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
ESPHOME_LOG_TAG(TAG, "tlc59208f")
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