Skip to content

Commit

Permalink
feature(cpp): add cpp code for I2C devices
Browse files Browse the repository at this point in the history
  • Loading branch information
costaud committed Oct 10, 2017
1 parent b551b7b commit e65b830
Show file tree
Hide file tree
Showing 35 changed files with 1,468 additions and 159 deletions.
5 changes: 2 additions & 3 deletions components/i2c_devices/i2c_bus/i2c_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t* conf)
return NULL;
}

esp_err_t i2s_bus_delete(i2c_bus_handle_t bus)
esp_err_t i2c_bus_delete(i2c_bus_handle_t bus)
{
I2C_BUS_CHECK(bus != NULL, "Handle error", ESP_FAIL);
i2c_bus_t* i2c_bus = (i2c_bus_t*) bus;
Expand All @@ -78,6 +78,5 @@ esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE
I2C_BUS_CHECK(bus != NULL, "Handle error", ESP_FAIL);
I2C_BUS_CHECK(cmd != NULL, "I2C cmd error", ESP_FAIL);
i2c_bus_t* i2c_bus = (i2c_bus_t*) bus;
esp_err_t ret = i2c_master_cmd_begin(i2c_bus->i2c_port, cmd, ticks_to_wait);
return ret;
return i2c_master_cmd_begin(i2c_bus->i2c_port, cmd, ticks_to_wait);
}
56 changes: 56 additions & 0 deletions components/i2c_devices/i2c_bus/i2c_bus_obj.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "i2c_bus.h"

CI2CBus::CI2CBus(i2c_port_t i2c_port, gpio_num_t scl_io, gpio_num_t sda_io, i2c_mode_t i2c_mode, int clk_hz)
{
i2c_config_t conf;
conf.mode = i2c_mode;
conf.scl_io_num = scl_io;
conf.sda_io_num = sda_io;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = clk_hz;
m_i2c_bus_handle = i2c_bus_create(i2c_port, &conf);
}

CI2CBus::~CI2CBus()
{
i2c_bus_delete(m_i2c_bus_handle);
m_i2c_bus_handle = NULL;
}

esp_err_t CI2CBus::send(i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait)
{
return i2c_bus_cmd_begin(m_i2c_bus_handle, cmd, ticks_to_wait);
}

i2c_bus_handle_t CI2CBus::get_bus_handle()
{
return m_i2c_bus_handle;
}
57 changes: 56 additions & 1 deletion components/i2c_devices/i2c_bus/include/i2c_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t* conf);
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t i2s_bus_delete(i2c_bus_handle_t bus);
esp_err_t i2c_bus_delete(i2c_bus_handle_t bus);

/**
* @brief I2C start sending buffered commands
Expand All @@ -69,5 +69,60 @@ esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE
#ifdef __cplusplus
}
#endif


#ifdef __cplusplus
/**
* class of I2c bus
*/
class CI2CBus
{
private:
i2c_bus_handle_t m_i2c_bus_handle;

/**
* prevent copy constructing
*/
CI2CBus(const CI2CBus&);
CI2CBus& operator = (const CI2CBus&);
public:
/**
* @brief Constructor for CI2CBus class
* @param i2c_port I2C hardware port
* @param scl_io gpio index for slc pin
* @param sda_io gpio index for sda pin
* @param i2c_mode mode for I2C bus
* @param clk_hz I2C clock frequency
*
*/
CI2CBus(i2c_port_t i2c_port, gpio_num_t scl_io, gpio_num_t sda_io, i2c_mode_t i2c_mode = I2C_MODE_MASTER, int clk_hz = 100000);

/**
* @brief Destructor function of CI2CBus class
*/
~CI2CBus();

/**
* @brief Send command and data to I2C bus
* @param cmd pointor to command link
* @ticks_to_wait max block time
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t send(i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait);

/**
* @brief Get bus handle
* @return bus handle
*/
i2c_bus_handle_t get_bus_handle();
};
#endif


#endif

57 changes: 57 additions & 0 deletions components/i2c_devices/others/is31fl3xxx/include/is31fl3218.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,62 @@ esp_err_t led_is31fl3218_delete(is31fl3218_handle_t fxled, bool del_bus);
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
/**
* class of is31fl3218 fxled driver
*/
class CIs31fl3218
{
private:
is31fl3218_handle_t m_led_handle;
CI2CBus *bus;

/**
* prevent copy constructing
*/
CIs31fl3218(const CIs31fl3218&);
CIs31fl3218& operator = (const CIs31fl3218&);
public:
/**
* @brief Constructor for CIs31fl3218 class
* @param p_i2c_bus pointer to CI2CBus object
*/
CIs31fl3218(CI2CBus *p_i2c_bus);

/**
* @brief Destructor of CIs31fl3218 class
*/
~CIs31fl3218();

/**
* @brief Set pwm duty cycle for different channels
* @param ch_bit 18bit value, to mask the channels
* @param duty duty cycle value, from 0 to 0xff
* return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t set_duty(uint32_t ch_bit, uint8_t duty);

/**
* @brief Set pwm duty cycle from buffer, fill buffer values to pwm duty registers
* @param duty buffer pointer of duty values
* @param len buffer length
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t write_duty_regs(uint8_t* duty, int len);


};
#endif
#endif

73 changes: 73 additions & 0 deletions components/i2c_devices/others/is31fl3xxx/include/is31fl3736.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,77 @@ esp_err_t is31fl3736_fill_buf(is31fl3736_handle_t fxled, uint8_t duty, uint8_t*
}
#endif

#ifdef __cplusplus
/**
* class of is31fl3736 fxled driver
*/
class CIs31fl3736
{
private:
is31fl3736_handle_t m_led_handle;
CI2CBus *bus;

/**
* prevent copy constructing
*/
CIs31fl3736(const CIs31fl3736&);
CIs31fl3736& operator = (const CIs31fl3736&);
public:
/**
* @brief Constructor function of CIs31fl3736 class
* @param p_i2c_bus pointer to CI2CBus object
* @param rst_io gpio index for reset pin
* @param addr1 connection of addr pin1
* @param addr2 connection of addr pin2
* @param cur_val global current value for led driver
*/
CIs31fl3736(CI2CBus *p_i2c_bus, gpio_num_t rst_io, is31fl3736_addr_pin_t addr1, is31fl3736_addr_pin_t addr2,
uint8_t cur_val);

/**
* @brief Destructor function of CIs31fl3736 class
*/
~CIs31fl3736();

/**
* @brief write slave device register
* @param reg_addr address for slave register
* @param data pointer to data buffer
* @param data_num data length to write
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t write_reg(uint8_t reg_addr, uint8_t *data, uint8_t data_num);

/**
* @brief fill led matrix
* @param duty duty cycle value
* @param buf buffer that save the bit mask of raws in led matrix
* return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t fill_matrix(uint8_t duty, uint8_t* buf);

/**
* @brief set page number to operate
* @param page_num page number
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
* - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
* - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
*/
esp_err_t set_page(uint8_t page_num);
};
#endif

#endif
51 changes: 26 additions & 25 deletions components/i2c_devices/others/is31fl3xxx/is31fl3218.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include <stdio.h>
#include <string.h>
#include "driver/i2c.h"
Expand All @@ -40,7 +40,8 @@ static const char* tag = "IS31";
#define IS31FL3218_READ_BIT 0x01
#define IS31FL3218_ACK_CHECK_EN 1

typedef struct {
typedef struct
{
i2c_bus_handle_t bus;
uint16_t dev_addr;
} is31fl3218_dev_t;
Expand Down Expand Up @@ -176,7 +177,7 @@ esp_err_t led_is31fl3218_delete(is31fl3218_handle_t fxled, bool del_bus)
{
is31fl3218_dev_t* led = (is31fl3218_dev_t*) fxled;
if (del_bus) {
i2s_bus_delete(led->bus);
i2c_bus_delete(led->bus);
led->bus = NULL;
}
free(led);
Expand Down
Loading

0 comments on commit e65b830

Please sign in to comment.