From 1b2b9182e08973895871d4512bbf027cdc175c0f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 7 Oct 2022 10:11:58 +1100 Subject: Add a little wrapper for I2C things --- main/gpio-expander.cpp | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'main/gpio-expander.cpp') diff --git a/main/gpio-expander.cpp b/main/gpio-expander.cpp index 1320057e..389fb333 100644 --- a/main/gpio-expander.cpp +++ b/main/gpio-expander.cpp @@ -1,4 +1,7 @@ #include "gpio-expander.h" + +#include "i2c.h" + #include namespace gay_ipod { @@ -27,40 +30,26 @@ esp_err_t GpioExpander::Write() { std::pair ports_ab = unpack(ports()); - // Technically enqueuing these commands could fail, but we don't worry about - // it because that would indicate some really very badly wrong more generally. - i2c_master_start(handle); - i2c_master_write_byte(handle, (kPca8575Address << 1 | I2C_MASTER_WRITE), true); - i2c_master_write_byte(handle, ports_ab.first, true); - i2c_master_write_byte(handle, ports_ab.second, true); - i2c_master_stop(handle); - - esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, handle, kPca8575Timeout); + I2CTransaction transaction; + transaction.start() + .write_addr(kPca8575Address, I2C_MASTER_WRITE) + .write_ack(ports_ab.first, ports_ab.second) + .stop(); - i2c_cmd_link_delete(handle); - return ret; + return transaction.Execute(); } esp_err_t GpioExpander::Read() { - i2c_cmd_handle_t handle = i2c_cmd_link_create(); - if (handle == NULL) { - return ESP_ERR_NO_MEM; - } - uint8_t input_a, input_b; - // Technically enqueuing these commands could fail, but we don't worry about - // it because that would indicate some really very badly wrong more generally. - i2c_master_start(handle); - i2c_master_write_byte(handle, (kPca8575Address << 1 | I2C_MASTER_READ), true); - i2c_master_read_byte(handle, &input_a, I2C_MASTER_ACK); - i2c_master_read_byte(handle, &input_b, I2C_MASTER_LAST_NACK); - i2c_master_stop(handle); - - esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, handle, kPca8575Timeout); - - i2c_cmd_link_delete(handle); + I2CTransaction transaction; + transaction.start() + .write_addr(kPca8575Address, I2C_MASTER_READ) + .read(&input_a, I2C_MASTER_ACK) + .read(&input_b, I2C_MASTER_LAST_NACK) + .stop(); + esp_err_t ret = transaction.Execute(); inputs_ = pack(input_a, input_b); return ret; } -- cgit v1.2.3