diff options
| author | jacqueline <me@jacqueline.id.au> | 2022-10-07 10:11:58 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2022-10-07 10:11:58 +1100 |
| commit | 1b2b9182e08973895871d4512bbf027cdc175c0f (patch) | |
| tree | 1952d6b80b549c2dba72cb5c597216ce3234491c /main/gpio-expander.cpp | |
| parent | 4e643baf5f6af1c65e08295bab4ab4f55c3feaf4 (diff) | |
| download | tangara-fw-1b2b9182e08973895871d4512bbf027cdc175c0f.tar.gz | |
Add a little wrapper for I2C things
Diffstat (limited to 'main/gpio-expander.cpp')
| -rw-r--r-- | main/gpio-expander.cpp | 43 |
1 files changed, 16 insertions, 27 deletions
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 <cstdint> namespace gay_ipod { @@ -27,40 +30,26 @@ esp_err_t GpioExpander::Write() { std::pair<uint8_t, uint8_t> 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; } |
