mirror of
https://github.com/esphome/esphome.git
synced 2026-10-20 04:56:48 +00:00
[mpu6050][mpu6886][tas2780][sx1509][cap1188][as7341][ufire_ec][ufire_ise] Use I2CDevice::update_register
This commit is contained in:
@@ -250,17 +250,11 @@ bool AS7341Component::write_register_bit(uint8_t address, bool value, uint8_t bi
|
||||
}
|
||||
|
||||
bool AS7341Component::set_register_bit(uint8_t address, uint8_t bit_position) {
|
||||
uint8_t data;
|
||||
this->read_byte(address, &data);
|
||||
data |= (1 << bit_position);
|
||||
return this->write_byte(address, data);
|
||||
return this->update_register(address, 1 << bit_position, 1 << bit_position);
|
||||
}
|
||||
|
||||
bool AS7341Component::clear_register_bit(uint8_t address, uint8_t bit_position) {
|
||||
uint8_t data;
|
||||
this->read_byte(address, &data);
|
||||
data &= ~(1 << bit_position);
|
||||
return this->write_byte(address, data);
|
||||
return this->update_register(address, 1 << bit_position, 0);
|
||||
}
|
||||
|
||||
uint16_t AS7341Component::swap_bytes(uint16_t data) { return (data >> 8) | (data << 8); }
|
||||
|
||||
@@ -43,10 +43,8 @@ void CAP1188Component::finish_setup_() {
|
||||
}
|
||||
|
||||
// Set sensitivity
|
||||
uint8_t sensitivity = 0;
|
||||
this->read_byte(CAP1188_SENSITVITY, &sensitivity);
|
||||
sensitivity = sensitivity & 0x0f;
|
||||
this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
|
||||
// The threshold replaces the upper nibble and is ORed into the lower one, as before
|
||||
this->update_register(CAP1188_SENSITVITY, 0xF0 | this->touch_threshold_, this->touch_threshold_);
|
||||
|
||||
// Allow multiple touches
|
||||
this->write_byte(CAP1188_MULTI_TOUCH, this->allow_multiple_touches_);
|
||||
|
||||
@@ -28,54 +28,24 @@ void MPU6050Component::setup() {
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Power Management");
|
||||
// Setup power management
|
||||
uint8_t power_management;
|
||||
if (!this->read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
|
||||
// Set clock source - X-Gyro
|
||||
power_management &= 0b11111000;
|
||||
power_management |= MPU6050_CLOCK_SOURCE_X_GYRO;
|
||||
// Disable sleep
|
||||
power_management &= ~(1 << MPU6050_BIT_SLEEP_ENABLED);
|
||||
// Enable temperature
|
||||
power_management &= ~(1 << MPU6050_BIT_TEMPERATURE_DISABLED);
|
||||
ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
|
||||
if (!this->write_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, power_management)) {
|
||||
// Clock source X-Gyro, sleep off, temperature on
|
||||
if (!this->update_register(MPU6050_REGISTER_POWER_MANAGEMENT_1,
|
||||
0b111 | (1 << MPU6050_BIT_SLEEP_ENABLED) | (1 << MPU6050_BIT_TEMPERATURE_DISABLED),
|
||||
MPU6050_CLOCK_SOURCE_X_GYRO)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Gyro Config");
|
||||
// Set scale - 2000DPS
|
||||
uint8_t gyro_config;
|
||||
if (!this->read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
|
||||
gyro_config &= 0b11100111;
|
||||
gyro_config |= MPU6050_SCALE_2000_DPS << 3;
|
||||
ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
|
||||
if (!this->write_byte(MPU6050_REGISTER_GYRO_CONFIG, gyro_config)) {
|
||||
if (!this->update_register(MPU6050_REGISTER_GYRO_CONFIG, 0b00011000, MPU6050_SCALE_2000_DPS << 3)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Accel Config");
|
||||
// Set range - 2G
|
||||
uint8_t accel_config;
|
||||
if (!this->read_byte(MPU6050_REGISTER_ACCEL_CONFIG, &accel_config)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
|
||||
accel_config &= 0b11100111;
|
||||
accel_config |= (MPU6050_RANGE_2G << 3);
|
||||
ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
|
||||
if (!this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config)) {
|
||||
if (!this->update_register(MPU6050_REGISTER_ACCEL_CONFIG, 0b00011000, MPU6050_RANGE_2G << 3)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -32,54 +32,24 @@ void MPU6886Component::setup() {
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Power Management");
|
||||
// Setup power management
|
||||
uint8_t power_management;
|
||||
if (!this->read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
|
||||
// Set clock source - X-Gyro
|
||||
power_management &= 0b11111000;
|
||||
power_management |= MPU6886_CLOCK_SOURCE_X_GYRO;
|
||||
// Disable sleep
|
||||
power_management &= ~(1 << MPU6886_BIT_SLEEP_ENABLED);
|
||||
// Enable temperature
|
||||
power_management &= ~(1 << MPU6886_BIT_TEMPERATURE_DISABLED);
|
||||
ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
|
||||
if (!this->write_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, power_management)) {
|
||||
// Clock source X-Gyro, sleep off, temperature on
|
||||
if (!this->update_register(MPU6886_REGISTER_POWER_MANAGEMENT_1,
|
||||
0b111 | (1 << MPU6886_BIT_SLEEP_ENABLED) | (1 << MPU6886_BIT_TEMPERATURE_DISABLED),
|
||||
MPU6886_CLOCK_SOURCE_X_GYRO)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Gyroscope Config");
|
||||
// Set scale - 2000DPS
|
||||
uint8_t gyro_config;
|
||||
if (!this->read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input gyroscope_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
|
||||
gyro_config &= 0b11100111;
|
||||
gyro_config |= MPU6886_SCALE_2000_DPS << 3;
|
||||
ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
|
||||
if (!this->write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
|
||||
if (!this->update_register(MPU6886_REGISTER_GYRO_CONFIG, 0b00011000, MPU6886_SCALE_2000_DPS << 3)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, " Setting up Accelerometer Config");
|
||||
// Set range - 2G
|
||||
uint8_t accel_config;
|
||||
if (!this->read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, " Input accelerometer_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
|
||||
accel_config &= 0b11100111;
|
||||
accel_config |= (MPU6886_RANGE_2G << 3);
|
||||
ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
|
||||
if (!this->write_byte(MPU6886_REGISTER_ACCEL_CONFIG, accel_config)) {
|
||||
if (!this->update_register(MPU6886_REGISTER_ACCEL_CONFIG, 0b00011000, MPU6886_RANGE_2G << 3)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -175,7 +175,6 @@ void SX1509Component::pin_mode(uint8_t pin, gpio::Flags flags) {
|
||||
|
||||
void SX1509Component::setup_led_driver(uint8_t pin) {
|
||||
uint16_t temp_word = 0;
|
||||
uint8_t temp_byte = 0;
|
||||
|
||||
this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
|
||||
temp_word |= (1 << pin);
|
||||
@@ -184,16 +183,11 @@ void SX1509Component::setup_led_driver(uint8_t pin) {
|
||||
this->ddr_mask_ &= ~(1 << pin); // 0=output
|
||||
this->write_byte_16(REG_DIR_B, this->ddr_mask_);
|
||||
|
||||
this->read_byte(REG_CLOCK, &temp_byte);
|
||||
temp_byte |= (1 << 6); // Internal 2MHz oscillator part 1 (set bit 6)
|
||||
temp_byte &= ~(1 << 5); // Internal 2MHz oscillator part 2 (clear bit 5)
|
||||
this->write_byte(REG_CLOCK, temp_byte);
|
||||
// Internal 2MHz oscillator: set bit 6, clear bit 5
|
||||
this->update_register(REG_CLOCK, (1 << 6) | (1 << 5), 1 << 6);
|
||||
|
||||
this->read_byte(REG_MISC, &temp_byte);
|
||||
temp_byte &= ~(1 << 7); // set linear mode bank B
|
||||
temp_byte &= ~(1 << 3); // set linear mode bank A
|
||||
temp_byte |= 0x70; // Frequency of the LED Driver clock ClkX of all IOs:
|
||||
this->write_byte(REG_MISC, temp_byte);
|
||||
// Linear mode for banks A (bit 3) and B (bit 7); LED driver clock ClkX of all IOs (bits 6:4)
|
||||
this->update_register(REG_MISC, (1 << 7) | (1 << 3) | 0x70, 0x70);
|
||||
|
||||
this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &temp_word);
|
||||
temp_word |= (1 << pin);
|
||||
@@ -216,11 +210,7 @@ void SX1509Component::clock_(uint8_t osc_source, uint8_t osc_pin_function, uint8
|
||||
this->clk_x_ = 2000000;
|
||||
osc_divider = (osc_divider & 0b111) << 4; // 3-bit value, bits 6:4
|
||||
|
||||
uint8_t reg_misc = 0;
|
||||
this->read_byte(REG_MISC, ®_misc);
|
||||
reg_misc &= ~(0b111 << 4);
|
||||
reg_misc |= osc_divider;
|
||||
this->write_byte(REG_MISC, reg_misc);
|
||||
this->update_register(REG_MISC, 0b111 << 4, osc_divider);
|
||||
}
|
||||
|
||||
void SX1509Component::setup_keypad_() {
|
||||
@@ -234,15 +224,11 @@ void SX1509Component::setup_keypad_() {
|
||||
this->ddr_mask_ |= (1 << i);
|
||||
this->write_byte_16(REG_DIR_B, this->ddr_mask_);
|
||||
|
||||
this->read_byte(REG_OPEN_DRAIN_A, &temp_byte);
|
||||
for (int i = 0; i < this->rows_; i++)
|
||||
temp_byte |= (1 << i);
|
||||
this->write_byte(REG_OPEN_DRAIN_A, temp_byte);
|
||||
const uint8_t row_bits = (1 << this->rows_) - 1;
|
||||
this->update_register(REG_OPEN_DRAIN_A, row_bits, row_bits);
|
||||
|
||||
this->read_byte(REG_PULL_UP_B, &temp_byte);
|
||||
for (int i = 0; i < this->cols_; i++)
|
||||
temp_byte |= (1 << i);
|
||||
this->write_byte(REG_PULL_UP_B, temp_byte);
|
||||
const uint8_t col_bits = (1 << this->cols_) - 1;
|
||||
this->update_register(REG_PULL_UP_B, col_bits, col_bits);
|
||||
|
||||
if (debounce_time_ >= scan_time_) {
|
||||
debounce_time_ = scan_time_ >> 1; // Force debounce_time to be less than scan_time
|
||||
@@ -271,13 +257,8 @@ uint16_t SX1509Component::read_key_data() {
|
||||
|
||||
void SX1509Component::set_debounce_config_(uint8_t config_value) {
|
||||
// First make sure clock is configured
|
||||
uint8_t temp_byte = 0;
|
||||
this->read_byte(REG_MISC, &temp_byte);
|
||||
temp_byte |= (1 << 4); // Just default to no divider if not set
|
||||
this->write_byte(REG_MISC, temp_byte);
|
||||
this->read_byte(REG_CLOCK, &temp_byte);
|
||||
temp_byte |= (1 << 6); // default to internal osc.
|
||||
this->write_byte(REG_CLOCK, temp_byte);
|
||||
this->update_register(REG_MISC, 1 << 4, 1 << 4); // Just default to no divider if not set
|
||||
this->update_register(REG_CLOCK, 1 << 6, 1 << 6); // default to internal osc.
|
||||
|
||||
config_value &= 0b111; // 3-bit value
|
||||
this->write_byte(REG_DEBOUNCE_CONFIG, config_value);
|
||||
|
||||
@@ -159,12 +159,10 @@ bool TAS2780::select_page_(uint8_t page) {
|
||||
}
|
||||
|
||||
bool TAS2780::update_bits_(uint8_t reg, uint8_t mask, uint8_t value) {
|
||||
uint8_t current;
|
||||
if (!this->select_page_(TAS2780_PAGE_0) || !this->read_byte(reg, ¤t)) {
|
||||
ESP_LOGE(TAG, "Failed to read register 0x%02X", reg);
|
||||
return false;
|
||||
}
|
||||
return this->write_byte(reg, (current & ~mask) | (value & mask));
|
||||
if (this->select_page_(TAS2780_PAGE_0) && this->update_register(reg, mask, value))
|
||||
return true;
|
||||
ESP_LOGW(TAG, "Failed to update register 0x%02X", reg);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TAS2780::init_() {
|
||||
|
||||
@@ -17,14 +17,8 @@ void UFireECComponent::setup() {
|
||||
ESP_LOGI(TAG, "Found ufire_ec board version 0x%02X", version);
|
||||
|
||||
// Write option for temperature adjustments
|
||||
uint8_t config;
|
||||
this->read_byte(REGISTER_CONFIG, &config);
|
||||
if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
|
||||
config &= ~CONFIG_TEMP_COMPENSATION;
|
||||
} else {
|
||||
config |= CONFIG_TEMP_COMPENSATION;
|
||||
}
|
||||
this->write_byte(REGISTER_CONFIG, config);
|
||||
const bool compensate = this->temperature_sensor_ != nullptr || this->temperature_sensor_external_ != nullptr;
|
||||
this->update_register(REGISTER_CONFIG, CONFIG_TEMP_COMPENSATION, compensate ? CONFIG_TEMP_COMPENSATION : 0);
|
||||
|
||||
// Update temperature compensation
|
||||
this->set_compensation_(this->temperature_compensation_);
|
||||
|
||||
@@ -18,14 +18,8 @@ void UFireISEComponent::setup() {
|
||||
ESP_LOGI(TAG, "Found uFire_ise board version 0x%02X", version);
|
||||
|
||||
// Write option for temperature adjustments
|
||||
uint8_t config;
|
||||
this->read_byte(REGISTER_CONFIG, &config);
|
||||
if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
|
||||
config &= ~CONFIG_TEMP_COMPENSATION;
|
||||
} else {
|
||||
config |= CONFIG_TEMP_COMPENSATION;
|
||||
}
|
||||
this->write_byte(REGISTER_CONFIG, config);
|
||||
const bool compensate = this->temperature_sensor_ != nullptr || this->temperature_sensor_external_ != nullptr;
|
||||
this->update_register(REGISTER_CONFIG, CONFIG_TEMP_COMPENSATION, compensate ? CONFIG_TEMP_COMPENSATION : 0);
|
||||
}
|
||||
|
||||
void UFireISEComponent::update() {
|
||||
|
||||
Reference in New Issue
Block a user