ESPHome  2024.11.1
max6956.cpp
Go to the documentation of this file.
1 #include "max6956.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace max6956 {
6 
7 static const char *const TAG = "max6956";
8 
10 const uint32_t MASK_TRANSITION_DETECTION = 0x80;
11 const uint32_t MASK_INDIVIDUAL_CURRENT = 0x40;
12 const uint32_t MASK_NORMAL_OPERATION = 0x01;
13 
14 const uint32_t MASK_1PORT_VALUE = 0x03;
15 const uint32_t MASK_PORT_CONFIG = 0x03;
16 const uint8_t MASK_CONFIG_CURRENT = 0x40;
17 const uint8_t MASK_CURRENT_PIN = 0x0F;
18 
19 /**************************************
20  * MAX6956 *
21  **************************************/
23  ESP_LOGCONFIG(TAG, "Setting up MAX6956...");
24  uint8_t configuration;
25  if (!this->read_reg_(MAX6956_CONFIGURATION, &configuration)) {
26  this->mark_failed();
27  return;
28  }
29 
32 
35  this->read_reg_(MAX6956_CONFIGURATION, &configuration);
36  ESP_LOGD(TAG, "Initial reg[0x%.2X]=0x%.2X", MAX6956_CONFIGURATION, configuration);
37  configuration = configuration | MASK_NORMAL_OPERATION;
38  this->write_reg_(MAX6956_CONFIGURATION, configuration);
39 
40  ESP_LOGCONFIG(TAG, "Enabling normal operation");
41  ESP_LOGD(TAG, "setup reg[0x%.2X]=0x%.2X", MAX6956_CONFIGURATION, configuration);
42 }
43 
44 bool MAX6956::digital_read(uint8_t pin) {
45  uint8_t reg_addr = MAX6956_1PORT_VALUE_START + pin;
46  uint8_t value = 0;
47  this->read_reg_(reg_addr, &value);
48  return (value & MASK_1PORT_VALUE);
49 }
50 
51 void MAX6956::digital_write(uint8_t pin, bool value) {
52  uint8_t reg_addr = MAX6956_1PORT_VALUE_START + pin;
53  this->write_reg_(reg_addr, value);
54 }
55 
56 void MAX6956::pin_mode(uint8_t pin, gpio::Flags flags) {
57  uint8_t reg_addr = MAX6956_PORT_CONFIG_START + (pin - MAX6956_MIN) / 4;
58  uint8_t config = 0;
59  uint8_t shift = 2 * (pin % 4);
61 
62  if (flags == gpio::FLAG_INPUT) {
64  } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
66  } else if (flags == gpio::FLAG_OUTPUT) {
68  }
69 
70  this->read_reg_(reg_addr, &config);
71  config &= ~(MASK_PORT_CONFIG << shift);
72  config |= (mode << shift);
73  this->write_reg_(reg_addr, config);
74 }
75 
77  uint8_t reg_addr = MAX6956_PORT_CONFIG_START + (pin - MAX6956_MIN) / 4;
78  uint8_t config = 0;
79  uint8_t shift = 2 * (pin % 4);
81 
82  if (flags == max6956::FLAG_LED) {
84  }
85 
86  this->read_reg_(reg_addr, &config);
87  config &= ~(MASK_PORT_CONFIG << shift);
88  config |= (mode << shift);
89  this->write_reg_(reg_addr, config);
90 }
91 
92 void MAX6956::set_brightness_global(uint8_t current) {
93  if (current > 15) {
94  ESP_LOGE(TAG, "Global brightness out off range (%u)", current);
95  return;
96  }
97  global_brightness_ = current;
98 }
99 
101 
102 void MAX6956::set_brightness_mode(max6956::MAX6956CURRENTMODE brightness_mode) { brightness_mode_ = brightness_mode; };
103 
105  uint8_t reg_addr = MAX6956_CONFIGURATION;
106  uint8_t config = 0;
107 
108  this->read_reg_(reg_addr, &config);
109  config &= ~MASK_CONFIG_CURRENT;
110  config |= brightness_mode_ << 6;
111  this->write_reg_(reg_addr, config);
112 }
113 
114 void MAX6956::set_pin_brightness(uint8_t pin, float brightness) {
115  uint8_t reg_addr = MAX6956_CURRENT_START + (pin - MAX6956_MIN) / 2;
116  uint8_t config = 0;
117  uint8_t shift = 4 * (pin % 2);
118  uint8_t bright = roundf(brightness * 15);
119 
120  if (prev_bright_[pin - MAX6956_MIN] == bright)
121  return;
122 
123  prev_bright_[pin - MAX6956_MIN] = bright;
124 
125  this->read_reg_(reg_addr, &config);
126  config &= ~(MASK_CURRENT_PIN << shift);
127  config |= (bright << shift);
128  this->write_reg_(reg_addr, config);
129 }
130 
131 bool MAX6956::read_reg_(uint8_t reg, uint8_t *value) {
132  if (this->is_failed())
133  return false;
134 
135  return this->read_byte(reg, value);
136 }
137 
138 bool MAX6956::write_reg_(uint8_t reg, uint8_t value) {
139  if (this->is_failed())
140  return false;
141 
142  return this->write_byte(reg, value);
143 }
144 
146  ESP_LOGCONFIG(TAG, "MAX6956");
147 
149  ESP_LOGCONFIG(TAG, "current mode: global");
150  ESP_LOGCONFIG(TAG, "global brightness: %u", global_brightness_);
151  } else {
152  ESP_LOGCONFIG(TAG, "current mode: segment");
153  }
154 }
155 
156 /**************************************
157  * MAX6956GPIOPin *
158  **************************************/
159 void MAX6956GPIOPin::setup() { pin_mode(flags_); }
160 void MAX6956GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
161 bool MAX6956GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
162 void MAX6956GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
163 std::string MAX6956GPIOPin::dump_summary() const {
164  char buffer[32];
165  snprintf(buffer, sizeof(buffer), "%u via Max6956", pin_);
166  return buffer;
167 }
168 
169 } // namespace max6956
170 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
max6956::MAX6956CURRENTMODE brightness_mode_
Definition: max6956.h:66
bool is_failed() const
Definition: component.cpp:143
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition: max6956.cpp:56
void dump_config() override
Definition: max6956.cpp:145
const uint8_t MASK_CONFIG_CURRENT
Definition: max6956.cpp:16
const uint32_t MASK_TRANSITION_DETECTION
Masks for MAX6956 Configuration register.
Definition: max6956.cpp:10
MAX6956GPIOMode
Modes for MAX6956 pins.
Definition: max6956.h:11
void set_brightness_mode(max6956::MAX6956CURRENTMODE brightness_mode)
Definition: max6956.cpp:102
const uint32_t MASK_NORMAL_OPERATION
Definition: max6956.cpp:12
bool digital_read(uint8_t pin)
Definition: max6956.cpp:44
void digital_write(uint8_t pin, bool value)
Definition: max6956.cpp:51
bool write_reg_(uint8_t reg, uint8_t value)
Definition: max6956.cpp:138
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
const uint8_t MASK_CURRENT_PIN
Definition: max6956.cpp:17
void setup() override
Definition: max6956.cpp:22
std::string dump_summary() const override
Definition: max6956.cpp:163
const uint32_t flags
Definition: stm32flash.h:85
void pin_mode(gpio::Flags flags) override
Definition: max6956.cpp:160
void set_brightness_global(uint8_t current)
Definition: max6956.cpp:92
const uint32_t MASK_INDIVIDUAL_CURRENT
Definition: max6956.cpp:11
bool read_reg_(uint8_t reg, uint8_t *value)
Definition: max6956.cpp:131
void digital_write(bool value) override
Definition: max6956.cpp:162
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t global_brightness_
Definition: max6956.h:67
const uint32_t MASK_PORT_CONFIG
Definition: max6956.cpp:15
void set_pin_brightness(uint8_t pin, float brightness)
Definition: max6956.cpp:114
const uint32_t MASK_1PORT_VALUE
Definition: max6956.cpp:14