summaryrefslogtreecommitdiff
path: root/main/i2c.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-10-07 10:11:58 +1100
committerjacqueline <me@jacqueline.id.au>2022-10-07 10:11:58 +1100
commit1b2b9182e08973895871d4512bbf027cdc175c0f (patch)
tree1952d6b80b549c2dba72cb5c597216ce3234491c /main/i2c.cpp
parent4e643baf5f6af1c65e08295bab4ab4f55c3feaf4 (diff)
downloadtangara-fw-1b2b9182e08973895871d4512bbf027cdc175c0f.tar.gz
Add a little wrapper for I2C things
Diffstat (limited to 'main/i2c.cpp')
-rw-r--r--main/i2c.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/main/i2c.cpp b/main/i2c.cpp
new file mode 100644
index 00000000..2cb8420f
--- /dev/null
+++ b/main/i2c.cpp
@@ -0,0 +1,44 @@
+#include "i2c.h"
+#include "assert.h"
+
+namespace gay_ipod {
+
+I2CTransaction::I2CTransaction() {
+ handle_ = i2c_cmd_link_create();
+ assert(handle_ != NULL && "failed to create command link");
+}
+
+I2CTransaction::~I2CTransaction() {
+ i2c_cmd_link_delete(handle_);
+}
+
+esp_err_t I2CTransaction::Execute() {
+ return i2c_master_cmd_begin(I2C_NUM_0, handle_, kI2CTimeout);
+}
+
+I2CTransaction& I2CTransaction::start() {
+ ESP_ERROR_CHECK(i2c_master_start(handle_));
+ return *this;
+}
+
+I2CTransaction& I2CTransaction::stop() {
+ ESP_ERROR_CHECK(i2c_master_stop(handle_));
+ return *this;
+}
+
+I2CTransaction& I2CTransaction::write_addr(uint8_t addr, uint8_t op) {
+ write_ack(addr << 1 | op);
+ return *this;
+}
+
+I2CTransaction& I2CTransaction::write_ack(uint8_t data) {
+ ESP_ERROR_CHECK(i2c_master_write_byte(handle_, data, true));
+ return *this;
+}
+
+I2CTransaction& I2CTransaction::read(uint8_t *dest, i2c_ack_type_t ack) {
+ ESP_ERROR_CHECK(i2c_master_read_byte(handle_, dest, ack));
+ return *this;
+}
+
+} // namespace gay_ipod