Skip to content

Instantly share code, notes, and snippets.

@summivox
Last active June 20, 2021 23:50
Show Gist options
  • Save summivox/fbe21e1f8ccbe1a75a3ed1898f87c9d2 to your computer and use it in GitHub Desktop.
Save summivox/fbe21e1f8ccbe1a75a3ed1898f87c9d2 to your computer and use it in GitHub Desktop.

Revisions

  1. summivox revised this gist Jun 20, 2021. 1 changed file with 24 additions and 13 deletions.
    37 changes: 24 additions & 13 deletions esp32_i2c_helper.hpp
    Original file line number Diff line number Diff line change
    @@ -4,26 +4,27 @@
    #include "esp_err.h"
    #include "freertos/FreeRTOS.h"


    /// Thin C++ wrapper for the C API of ESP-IDF I2C master transactions.
    ///
    /// Features:
    ///
    /// - builder pattern (calls are chainable)
    /// - RTTI (automatic clean up)
    ///
    /// \example
    /// uint8_t data[kMaxLen];
    /// const esp_err_t err = I2cTxn()
    /// .Start()
    /// .WriteByte((i2c_address_7bit << 1) | I2C_MASTER_WRITE)
    /// .WriteByte(0xFF)
    /// .Start()
    /// .WriteByte((i2c_address_7bit << 1) | I2C_MASTER_READ)
    /// .Read(data, len)
    /// .Stop()
    /// .SendTo(i2c_num);
    ///
    class I2cTxn {
    /// @example
    /// ```c++
    /// uint8_t data[len];
    /// const esp_err_t err = I2cTxn()
    /// .StartWrite(i2c_address_7bit)
    /// .WriteByte(0xFF)
    /// .StartRead(i2c_address_7bit)
    /// .Read(data, len)
    /// .Stop()
    /// .SendTo(i2c_num);
    /// ```
    ///

    public:
    I2cTxn() : txn_(i2c_cmd_link_create()) {}
    ~I2cTxn() { i2c_cmd_link_delete(txn_); }
    @@ -39,6 +40,16 @@ class I2cTxn {
    i2c_master_start(txn_);
    return *this;
    }
    I2cTxn& StartWrite(uint8_t i2c_address_7bit, bool ack_en = true) {
    i2c_master_start(txn_);
    i2c_master_write_byte(txn_, (i2c_address_7bit << 1) | I2C_MASTER_WRITE, ack_en);
    return *this;
    }
    I2cTxn& StartRead(uint8_t i2c_address_7bit, bool ack_en = true) {
    i2c_master_start(txn_);
    i2c_master_write_byte(txn_, (i2c_address_7bit << 1) | I2C_MASTER_READ, ack_en);
    return *this;
    }
    I2cTxn& Stop() {
    i2c_master_stop(txn_);
    return *this;
  2. summivox created this gist Jun 20, 2021.
    65 changes: 65 additions & 0 deletions esp32_i2c_helper.hpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #pragma once

    #include "driver/i2c.h"
    #include "esp_err.h"
    #include "freertos/FreeRTOS.h"


    /// Thin C++ wrapper for the C API of ESP-IDF I2C master transactions.
    ///
    /// Features:
    /// - builder pattern (calls are chainable)
    /// - RTTI (automatic clean up)
    ///
    /// \example
    /// uint8_t data[kMaxLen];
    /// const esp_err_t err = I2cTxn()
    /// .Start()
    /// .WriteByte((i2c_address_7bit << 1) | I2C_MASTER_WRITE)
    /// .WriteByte(0xFF)
    /// .Start()
    /// .WriteByte((i2c_address_7bit << 1) | I2C_MASTER_READ)
    /// .Read(data, len)
    /// .Stop()
    /// .SendTo(i2c_num);
    ///
    class I2cTxn {
    public:
    I2cTxn() : txn_(i2c_cmd_link_create()) {}
    ~I2cTxn() { i2c_cmd_link_delete(txn_); }

    // implicit conversion for easier interop with C API
    operator i2c_cmd_handle_t() { return txn_; }

    esp_err_t SendTo(i2c_port_t i2c_num, TickType_t ticks_to_wait = portMAX_DELAY) {
    return i2c_master_cmd_begin(i2c_num, txn_, ticks_to_wait);
    }

    I2cTxn& Start() {
    i2c_master_start(txn_);
    return *this;
    }
    I2cTxn& Stop() {
    i2c_master_stop(txn_);
    return *this;
    }
    I2cTxn& WriteByte(uint8_t data, bool ack_en = true) {
    i2c_master_write_byte(txn_, data, ack_en);
    return *this;
    }
    I2cTxn& ReadByte(uint8_t* data, i2c_ack_type_t ack = I2C_MASTER_ACK) {
    i2c_master_read_byte(txn_, data, ack);
    return *this;
    }
    I2cTxn& Write(const void* data, size_t data_len, bool ack_en = true) {
    i2c_master_write(txn_, reinterpret_cast<const uint8_t*>(data), data_len, ack_en);
    return *this;
    }
    I2cTxn& Read(void* data, size_t data_len, i2c_ack_type_t ack = I2C_MASTER_LAST_NACK) {
    i2c_master_read(txn_, reinterpret_cast<uint8_t*>(data), data_len, ack);
    return *this;
    }

    private:
    i2c_cmd_handle_t txn_;
    };