From e65b830098c2e36bdf5d98b680cb4990ae509bb5 Mon Sep 17 00:00:00 2001 From: Wangjialin Date: Wed, 11 Oct 2017 01:57:24 +0800 Subject: [PATCH] feature(cpp): add cpp code for I2C devices --- components/i2c_devices/i2c_bus/i2c_bus.c | 5 +- .../i2c_devices/i2c_bus/i2c_bus_obj.cpp | 56 +++++++ .../i2c_devices/i2c_bus/include/i2c_bus.h | 57 ++++++- .../others/is31fl3xxx/include/is31fl3218.h | 57 +++++++ .../others/is31fl3xxx/include/is31fl3736.h | 73 +++++++++ .../others/is31fl3xxx/is31fl3218.c | 51 +++--- .../others/is31fl3xxx/is31fl3218_obj.cpp | 52 ++++++ .../others/is31fl3xxx/is31fl3736.c | 14 +- .../others/is31fl3xxx/is31fl3736_obj.cpp | 57 +++++++ .../is31fl3xxx/test/is31fl3218_obj_test.cpp | 104 ++++++++++++ .../others/is31fl3xxx/test/is31fl3218_test.c | 20 +-- .../is31fl3xxx/test/is31fl3736_obj_test.cpp | 154 ++++++++++++++++++ .../others/is31fl3xxx/test/is31fl3736_test.c | 81 ++++----- .../others/is31fl3xxx/test/snake.c | 34 ++-- components/i2c_devices/sensor/bh1750/bh1750.c | 6 +- .../i2c_devices/sensor/bh1750/bh1750_obj.cpp | 64 ++++++++ .../sensor/bh1750/include/bh1750.h | 74 ++++++++- .../sensor/bh1750/test/bh1750_obj_test.cpp | 59 +++++++ .../sensor/bh1750/test/bh1750_test.c | 40 ++--- components/i2c_devices/sensor/hts221/hts221.c | 6 +- .../i2c_devices/sensor/hts221/hts221_obj.cpp | 71 ++++++++ .../sensor/hts221/include/hts221.h | 51 +++++- .../sensor/hts221/test/hts221_obj_test.cpp | 55 +++++++ .../sensor/hts221/test/hts221_test.c | 6 +- .../sensor/lis2dh12/include/lis2dh12.h | 57 ++++++- .../i2c_devices/sensor/lis2dh12/lis2dh12.c | 6 +- .../sensor/lis2dh12/lis2dh12_obj.cpp | 85 ++++++++++ .../lis2dh12/test/lis2dh12_obj_test.cpp | 57 +++++++ .../sensor/lis2dh12/test/lis2dh12_test.c | 8 +- .../sensor/mvh3004d/include/mvh3004d.h | 45 ++++- .../i2c_devices/sensor/mvh3004d/mvh3004d.c | 6 +- .../sensor/mvh3004d/mvh3004d_obj.cpp | 57 +++++++ .../mvh3004d/test/mvh3004d_obj_test.cpp | 53 ++++++ .../sensor/mvh3004d/test/mvh3004d_test.c | 2 +- components/lcd/test/lcd_thermostat.cpp | 4 +- 35 files changed, 1468 insertions(+), 159 deletions(-) create mode 100644 components/i2c_devices/i2c_bus/i2c_bus_obj.cpp create mode 100644 components/i2c_devices/others/is31fl3xxx/is31fl3218_obj.cpp create mode 100644 components/i2c_devices/others/is31fl3xxx/is31fl3736_obj.cpp create mode 100755 components/i2c_devices/others/is31fl3xxx/test/is31fl3218_obj_test.cpp create mode 100755 components/i2c_devices/others/is31fl3xxx/test/is31fl3736_obj_test.cpp create mode 100644 components/i2c_devices/sensor/bh1750/bh1750_obj.cpp create mode 100644 components/i2c_devices/sensor/bh1750/test/bh1750_obj_test.cpp create mode 100644 components/i2c_devices/sensor/hts221/hts221_obj.cpp create mode 100644 components/i2c_devices/sensor/hts221/test/hts221_obj_test.cpp create mode 100644 components/i2c_devices/sensor/lis2dh12/lis2dh12_obj.cpp create mode 100644 components/i2c_devices/sensor/lis2dh12/test/lis2dh12_obj_test.cpp create mode 100644 components/i2c_devices/sensor/mvh3004d/mvh3004d_obj.cpp create mode 100644 components/i2c_devices/sensor/mvh3004d/test/mvh3004d_obj_test.cpp diff --git a/components/i2c_devices/i2c_bus/i2c_bus.c b/components/i2c_devices/i2c_bus/i2c_bus.c index 524c181c2..9d2ae13ee 100644 --- a/components/i2c_devices/i2c_bus/i2c_bus.c +++ b/components/i2c_devices/i2c_bus/i2c_bus.c @@ -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; @@ -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); } diff --git a/components/i2c_devices/i2c_bus/i2c_bus_obj.cpp b/components/i2c_devices/i2c_bus/i2c_bus_obj.cpp new file mode 100644 index 000000000..5501e3a66 --- /dev/null +++ b/components/i2c_devices/i2c_bus/i2c_bus_obj.cpp @@ -0,0 +1,56 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#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; +} diff --git a/components/i2c_devices/i2c_bus/include/i2c_bus.h b/components/i2c_devices/i2c_bus/include/i2c_bus.h index c3f9278b8..992f63e87 100644 --- a/components/i2c_devices/i2c_bus/include/i2c_bus.h +++ b/components/i2c_devices/i2c_bus/include/i2c_bus.h @@ -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 @@ -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 diff --git a/components/i2c_devices/others/is31fl3xxx/include/is31fl3218.h b/components/i2c_devices/others/is31fl3xxx/include/is31fl3218.h index 315ade1f6..64946a22d 100755 --- a/components/i2c_devices/others/is31fl3xxx/include/is31fl3218.h +++ b/components/i2c_devices/others/is31fl3xxx/include/is31fl3218.h @@ -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 diff --git a/components/i2c_devices/others/is31fl3xxx/include/is31fl3736.h b/components/i2c_devices/others/is31fl3xxx/include/is31fl3736.h index 0c786751c..aecbf1d08 100755 --- a/components/i2c_devices/others/is31fl3xxx/include/is31fl3736.h +++ b/components/i2c_devices/others/is31fl3xxx/include/is31fl3736.h @@ -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 diff --git a/components/i2c_devices/others/is31fl3xxx/is31fl3218.c b/components/i2c_devices/others/is31fl3xxx/is31fl3218.c index fe213515c..1d0b67bbf 100755 --- a/components/i2c_devices/others/is31fl3xxx/is31fl3218.c +++ b/components/i2c_devices/others/is31fl3xxx/is31fl3218.c @@ -1,27 +1,27 @@ /* - * ESPRESSIF MIT License - * - * Copyright (c) 2017 - * - * 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 + * + * 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 #include #include "driver/i2c.h" @@ -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; @@ -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); diff --git a/components/i2c_devices/others/is31fl3xxx/is31fl3218_obj.cpp b/components/i2c_devices/others/is31fl3xxx/is31fl3218_obj.cpp new file mode 100644 index 000000000..b486dde16 --- /dev/null +++ b/components/i2c_devices/others/is31fl3xxx/is31fl3218_obj.cpp @@ -0,0 +1,52 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "is31fl3218.h" + +CIs31fl3218::CIs31fl3218(CI2CBus *p_i2c_bus) +{ + bus = p_i2c_bus; + m_led_handle = led_is31fl3218_create(bus->get_bus_handle()); +} + +CIs31fl3218::~CIs31fl3218() +{ + led_is31fl3218_delete(m_led_handle, false); + m_led_handle = NULL; +} + +esp_err_t CIs31fl3218::set_duty(uint32_t ch_bit, uint8_t duty) +{ + return is31fl3218_channel_set(m_led_handle, ch_bit, duty); +} + +esp_err_t CIs31fl3218::write_duty_regs(uint8_t* duty, int len) +{ + return is31fl3218_write_pwm_regs(m_led_handle, duty, len); +} + diff --git a/components/i2c_devices/others/is31fl3xxx/is31fl3736.c b/components/i2c_devices/others/is31fl3xxx/is31fl3736.c index c0466b809..5faae3819 100755 --- a/components/i2c_devices/others/is31fl3xxx/is31fl3736.c +++ b/components/i2c_devices/others/is31fl3xxx/is31fl3736.c @@ -44,16 +44,12 @@ static const char *tag = "IS31FL3736"; typedef struct { i2c_bus_handle_t bus; uint16_t dev_addr; - bool dev_addr_10bit_en; gpio_num_t rst_io; is31fl3736_addr_pin_t addr1; is31fl3736_addr_pin_t addr2; uint8_t cur_val; } is31fl3736_dev_t; -/** - * @brief set software shutdown mode - */ esp_err_t is31fl3736_write_page(is31fl3736_handle_t fxled, uint8_t page_num) { IS31_PARAM_CHECK(page_num < 3); @@ -78,10 +74,6 @@ esp_err_t is31fl3736_write_page(is31fl3736_handle_t fxled, uint8_t page_num) return ret; } -/** - * @brief set software shutdown mode - */ -//xSemaphoreHandle reg_mux = NULL; esp_err_t is31fl3736_write_reg(is31fl3736_handle_t fxled, uint8_t reg_addr, uint8_t *data, uint8_t data_num) { is31fl3736_dev_t* led = (is31fl3736_dev_t*) fxled; @@ -115,9 +107,6 @@ esp_err_t is31fl3736_read_reg(is31fl3736_handle_t fxled, uint8_t reg_addr, uint8 return ret; } -/** - * @brief set software shutdown mode - */ esp_err_t is31fl3736_set_mode(is31fl3736_handle_t fxled, is31fl3736_mode_t mode) { uint8_t reg_val; @@ -345,7 +334,6 @@ is31fl3736_handle_t dev_is31fl3736_create(i2c_bus_handle_t bus, gpio_num_t rst_i is31fl3736_dev_t* fxled = (is31fl3736_dev_t*) calloc(1, sizeof(is31fl3736_dev_t)); fxled->bus = bus; fxled->dev_addr = is31fl3736_get_i2c_addr(addr1, addr2); - fxled->dev_addr_10bit_en = 0; fxled->rst_io = rst_io; fxled->cur_val = cur_val; if (rst_io < GPIO_NUM_MAX) { @@ -366,7 +354,7 @@ esp_err_t dev_is31fl3736_delete(is31fl3736_handle_t fxled, bool del_bus) { is31fl3736_dev_t* led = (is31fl3736_dev_t*) fxled; if (del_bus) { - i2s_bus_delete(led->bus); + i2c_bus_delete(led->bus); led->bus = NULL; } free(led); diff --git a/components/i2c_devices/others/is31fl3xxx/is31fl3736_obj.cpp b/components/i2c_devices/others/is31fl3xxx/is31fl3736_obj.cpp new file mode 100644 index 000000000..122990d26 --- /dev/null +++ b/components/i2c_devices/others/is31fl3xxx/is31fl3736_obj.cpp @@ -0,0 +1,57 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "is31fl3736.h" + +CIs31fl3736::CIs31fl3736(CI2CBus *p_i2c_bus, gpio_num_t rst_io, is31fl3736_addr_pin_t addr1, is31fl3736_addr_pin_t addr2, + uint8_t cur_val) +{ + bus = p_i2c_bus; + m_led_handle = dev_is31fl3736_create(p_i2c_bus->get_bus_handle(), rst_io, addr1, addr2, cur_val); +} + +CIs31fl3736::~CIs31fl3736() +{ + dev_is31fl3736_delete(m_led_handle, false); + m_led_handle = NULL; +} + +esp_err_t CIs31fl3736::fill_matrix(uint8_t duty, uint8_t* buf) +{ + return is31fl3736_fill_buf(m_led_handle, duty, buf); +} + +esp_err_t CIs31fl3736::write_reg(uint8_t reg_addr, uint8_t *data, uint8_t data_num) +{ + return is31fl3736_write_reg(m_led_handle, reg_addr, data, data_num); +} + +esp_err_t CIs31fl3736::set_page(uint8_t page_num) +{ + return is31fl3736_write_page(m_led_handle, page_num); +} diff --git a/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_obj_test.cpp b/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_obj_test.cpp new file mode 100755 index 000000000..4d4fb09c9 --- /dev/null +++ b/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_obj_test.cpp @@ -0,0 +1,104 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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. + * + */ + +#define IS31FL3218_TEST_CODE 1 +#if IS31FL3218_TEST_CODE + +#include +#include "driver/i2c.h" +#include "is31fl3218.h" +#include "is31fl3736.h" +#include "i2c_bus.h" +#include "unity.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +extern "C" void is31f13218_obj_test() +{ + CI2CBus i2c_bus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + CIs31fl3218 is31fl3218(&i2c_bus); + uint8_t i = 0, j = 255 / 10; + is31fl3218.set_duty(IS31FL3218_CH_NUM_MAX_MASK, 0); + int cnt = 0; + uint8_t duty_data[18] = { 0 }; + while (1) { + is31fl3218.write_duty_regs(duty_data, sizeof(duty_data)); + vTaskDelay(20 / portTICK_RATE_MS); + if (++i > IS31FL3218_CH_NUM_MAX - 1) { + i = 0; + if (j == 0) { + j = 255 / 10; + if (cnt++ > 1) { + break; + } + } else { + j = 0; + } + } + duty_data[i] = j; + } + cnt = 0; + uint32_t ch_mask = 0x1f; + int dcnt = 4; + while (1) { + vTaskDelay(10 / portTICK_RATE_MS); + for (int i = 0; i < 18; i++) { + if ((ch_mask >> i) & 0x1) { + if (dcnt >= 4) { + int duty = 5 - (dcnt - i); + is31fl3218.set_duty(IS31FL3218_CH_BIT(i), 0xff * duty / 5); + } else { + int duty = 5 - (dcnt - i); + if (duty < 0) { + duty += 16; + } + is31fl3218.set_duty(IS31FL3218_CH_BIT(i), 0xff * duty / 5); + } + } else { + is31fl3218.set_duty(IS31FL3218_CH_BIT(i), 0); + } + } + + ch_mask = ch_mask << 1; + ch_mask |= (ch_mask >> 18); + dcnt++; + if (dcnt > 17) { + dcnt = 0; + if (cnt++ > 4) { + is31fl3218.set_duty(0xfffff, 0xff * 0 / 5); + break; + } + } + } + printf("heap: %d\n", esp_get_free_heap_size()); +} + +TEST_CASE("I2C led is31f13218 cpp test", "[is31f13218_cpp][iot][fxled]") +{ + is31f13218_obj_test(); +} +#endif + diff --git a/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_test.c b/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_test.c index 3743df515..f2195cfce 100755 --- a/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_test.c +++ b/components/i2c_devices/others/is31fl3xxx/test/is31fl3218_test.c @@ -33,8 +33,8 @@ #include "i2c_bus.h" #include "unity.h" -#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */ -#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */ +#define I2C_MASTER_SCL_IO 21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO 15 /*!< gpio number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ @@ -67,8 +67,10 @@ void fxled_is31fl3218_init() } } -void is31f13218_test_task(void* pvParameters) +void is31f13218_test() { + fxled_is31fl3218_init(); + uint8_t i=0,j=255/10; ESP_ERROR_CHECK( is31fl3218_channel_set(fxled, IS31FL3218_CH_NUM_MAX_MASK, 0) ); int cnt = 0; @@ -182,19 +184,17 @@ void is31f13218_test_task(void* pvParameters) break; } } - printf("dcnt: %d\n", dcnt); } -} -void is31f13xxx_test() -{ - fxled_is31fl3218_init(); - is31f13218_test_task(NULL); + led_is31fl3218_delete(fxled, true); + fxled = NULL; + i2c_bus = NULL; + printf("heap: %d\n", esp_get_free_heap_size()); } TEST_CASE("I2C led is31f13218 test", "[is31f13218][iot][fxled]") { - is31f13xxx_test(); + is31f13218_test(); } #endif diff --git a/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_obj_test.cpp b/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_obj_test.cpp new file mode 100755 index 000000000..09564b3fa --- /dev/null +++ b/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_obj_test.cpp @@ -0,0 +1,154 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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. + * + */ + +#define IS31FL3218_TEST_CODE 1 +#if IS31FL3218_TEST_CODE + +#include +#include "driver/i2c.h" +#include "is31fl3218.h" +#include "is31fl3736.h" +#include "i2c_bus.h" +#include "unity.h" +#include "led_12_8_image.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +CI2CBus *p_i2c_bus = NULL; +CIs31fl3736 *p_is31fl3736 = NULL; +extern "C" void initGame(void*); + +esp_err_t example_build_buf(i2c_port_t i2c_port, uint8_t x, uint8_t y, char *c, uint8_t duty, uint8_t* buf) +{ + uint8_t i; + if (x > 8 || y > 12) { + return ESP_FAIL; + } + switch (*c) { + case 'e': + for (i = 0; i < 12; i++) { + if (image_e[i]) { + if (i + y < 12) { + buf[i + y] = image_e[i]; + buf[i + y] = (buf[i + y] << x) & 0xff; + } + } + } + break; + case 's': + for (i = 0; i < 12; i++) { + if (image_s[i]) { + if (i + y < 12) { + buf[i + y] = image_s[i]; + buf[i + y] = (buf[i + y] << x) & 0xff; + } + } + } + break; + case 'p': + for (i = 0; i < 12; i++) { + if (image_p[i]) { + if (i + y < 12) { + buf[i + y] = image_p[i]; + buf[i + y] = (buf[i + y] << x) & 0xff; + } + } + } + break; + default: + return ESP_FAIL; + break; + } + return ESP_OK; +} + +void fill_pixel(int x, int y, uint8_t duty) +{ + if (x == -1 || y == -1) { + return; + } + uint8_t reg = x * 2 + y * 0x10; + p_is31fl3736->write_reg(reg, &duty, 1); +} + +void is31f13736_obj_test_func() +{ + int i = 11; + char c = 'e'; + static uint8_t dir = 0; + p_is31fl3736->set_page(IS31FL3736_PAGE(1)); + initGame((void*) &fill_pixel); + int cnt = 1; + while (cnt) { + if (dir == 0) { + vTaskDelay(150 / portTICK_RATE_MS); + uint8_t buf[12] = { 0 }; + c = 'e'; + example_build_buf(I2C_MASTER_NUM, 0, i, &c, 50, buf); + c = 's'; + example_build_buf(I2C_MASTER_NUM, 0, i + 4, &c, 50, buf); + c = 'p'; + example_build_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); + p_is31fl3736->fill_matrix(50, buf); + if (--i < 0 - 4) { + dir = 1; + } + } else { + vTaskDelay(50 / portTICK_RATE_MS); + uint8_t buf[12] = { 0 }; + c = 'e'; + example_build_buf(I2C_MASTER_NUM, 0, i, &c, 50, buf); + c = 's'; + example_build_buf(I2C_MASTER_NUM, 0, i + 4, &c, 50, buf); + c = 'p'; + example_build_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); + p_is31fl3736->fill_matrix(50, buf); + if (++i > 12) { + dir = 0; + cnt--; + } + } + } +} + +extern "C" void is31f13736_obj_test() +{ + p_i2c_bus = new CI2CBus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + p_is31fl3736 = new CIs31fl3736(p_i2c_bus, (gpio_num_t) GPIO_NUM_32, (is31fl3736_addr_pin_t) 0, + (is31fl3736_addr_pin_t) 0, 0XCF); + is31f13736_obj_test_func(); + delete (p_is31fl3736); + delete (p_i2c_bus); + p_is31fl3736 = NULL; + p_i2c_bus = NULL; + printf("heap: %d\n", esp_get_free_heap_size()); +} + +TEST_CASE("I2C led is31f13736 cpp test", "[is31f13736_cpp][iot][fxled]") +{ + is31f13736_obj_test(); +} +#endif diff --git a/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_test.c b/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_test.c index 372ff9f9f..685f7f8f5 100755 --- a/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_test.c +++ b/components/i2c_devices/others/is31fl3xxx/test/is31fl3736_test.c @@ -1,27 +1,27 @@ /* - * ESPRESSIF MIT License - * - * Copyright (c) 2017 - * - * 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 + * + * 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. + * + */ + #define IS31FL3XXX_TEST_CODE 1 #if IS31FL3XXX_TEST_CODE @@ -33,7 +33,7 @@ #include "led_12_8_image.h" #define I2C_MASTER_SCL_IO 21 /*!< gpio number for I2C master clock */ -#define I2C_MASTER_SDA_IO 22 /*!< gpio number for I2C master data */ +#define I2C_MASTER_SDA_IO 15 /*!< gpio number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ @@ -41,6 +41,8 @@ static i2c_bus_handle_t i2c_bus = NULL; static is31fl3736_handle_t led3736 = NULL; +void fill_pixel(int x, int y, uint8_t duty); + /** * @brief i2c master initialization */ @@ -111,43 +113,44 @@ esp_err_t is31fl3736_display_buf(i2c_port_t i2c_port, uint8_t x, uint8_t y, char return ESP_OK; } - void is31f13736_test_task(void* pvParameters) { - int i=11; + int i = 11; char c = 'e'; static uint8_t dir = 0; is31fl3736_write_page(led3736, IS31FL3736_PAGE(1)); - extern void initGame(); - initGame(); + extern void initGame(void*); + initGame(&fill_pixel); //xTaskCreate(is32f13xxx_bar_task, "is32f13xxx_bar_task", 2048, NULL, 10, NULL); //xTaskCreate(touch_task, "touch_task", 1024*2, NULL, 11, NULL); - while(1) { + int cnt = 1; + while (cnt) { if (dir == 0) { vTaskDelay(150 / portTICK_RATE_MS); - uint8_t buf[12] = {0}; + uint8_t buf[12] = { 0 }; c = 'e'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i, &c, 50, buf); c = 's'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i+4, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i + 4, &c, 50, buf); c = 'p'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i+8, &c, 50,buf); - is31fl3736_send_buf(I2C_MASTER_NUM, 0, i+8, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); + is31fl3736_send_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); if (--i < 0 - 4) { dir = 1; } } else { vTaskDelay(50 / portTICK_RATE_MS); - uint8_t buf[12] = {0}; + uint8_t buf[12] = { 0 }; c = 'e'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i, &c, 50, buf); c = 's'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i+4, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i + 4, &c, 50, buf); c = 'p'; - is31fl3736_display_buf(I2C_MASTER_NUM, 0, i+8, &c, 50,buf); - is31fl3736_send_buf(I2C_MASTER_NUM, 0, i+8, &c, 50,buf); + is31fl3736_display_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); + is31fl3736_send_buf(I2C_MASTER_NUM, 0, i + 8, &c, 50, buf); if (++i > 12) { dir = 0; + cnt--; } } } diff --git a/components/i2c_devices/others/is31fl3xxx/test/snake.c b/components/i2c_devices/others/is31fl3xxx/test/snake.c index a4e508b64..1fde8f7f7 100644 --- a/components/i2c_devices/others/is31fl3xxx/test/snake.c +++ b/components/i2c_devices/others/is31fl3xxx/test/snake.c @@ -22,6 +22,10 @@ extern void srand (unsigned int seed); extern int random (void); +typedef void (*fill_pixel_func_t)(int x, int y, uint8_t duty); + +fill_pixel_func_t fill_pixel_func = NULL; + typedef struct { int x, y; @@ -41,8 +45,13 @@ MainSnack Ms; #define SNAKE_DUTY (0xff/10) #define FOOD_DUTY (0xff/10) -void initGame();//Initial the variable for the game -extern void fill_pixel(uint8_t x, uint8_t y, uint8_t duty); +#ifdef __cplusplus +extern "C" { +#endif +void initGame(void*);//Initial the variable for the game +#ifdef __cplusplus +} +#endif void disp_pos(curPoint* point) { @@ -70,11 +79,12 @@ bool is_dead() void create_food(curPoint* food) { int found = 0; + int cnt = 0; extern uint64_t system_get_rtc_time(void); srand(system_get_rtc_time()); while (1) { found = 0; - int loop_num = rand() % 10; + int loop_num = (rand()+system_get_rtc_time()) % 10; while (loop_num--) { rand(); } @@ -85,7 +95,7 @@ void create_food(curPoint* food) found = 1; } } - if (found == 0) { + if (found == 0 || cnt++ > 1000) { return; } } @@ -120,14 +130,14 @@ void GameProcess(void *arg) food.x = -1; while(gpio_get_level(0) == 1) { - if (food.y == -1) { + ets_printf("find\n"); create_food(&food); printf("food: "); disp_pos(&food); - fill_pixel(food.x, food.y, FOOD_DUTY); + fill_pixel_func(food.x, food.y, FOOD_DUTY); } - fill_pixel(Ms.body[Ms.len].x, Ms.body[Ms.len].y, 0x0); + fill_pixel_func(Ms.body[Ms.len].x, Ms.body[Ms.len].y, 0x0); //move forward by one step for (i = Ms.len - 1; i >= 1; i--) { Ms.body[i + 1] = Ms.body[i]; @@ -171,15 +181,14 @@ void GameProcess(void *arg) //move and draw for (i = 1; i <= Ms.len; i++) { if (1 == i) { - fill_pixel(Ms.body[i].x, Ms.body[i].y, HEAD_DUTY); + fill_pixel_func(Ms.body[i].x, Ms.body[i].y, HEAD_DUTY); } else { - //fill_pixel(Ms.body[i].x, Ms.body[i].y, SNAKE_DUTY); + //fill_pixel_func(Ms.body[i].x, Ms.body[i].y, SNAKE_DUTY); } } - if (mode == MODE_AUTO) { - vTaskDelay(20 / portTICK_PERIOD_MS); + vTaskDelay(10 / portTICK_PERIOD_MS); } else { vTaskDelay(500 / portTICK_PERIOD_MS); } @@ -218,8 +227,9 @@ void snake_set_dir_right() } } -void initGame() +void initGame(void* fill_pixel_cb) { + fill_pixel_func = (fill_pixel_func_t)fill_pixel_cb; curPoint tmp; tmp.x = MAX_COLS / 2; tmp.y = MAX_LINES / 2; diff --git a/components/i2c_devices/sensor/bh1750/bh1750.c b/components/i2c_devices/sensor/bh1750/bh1750.c index 0c02ad9c4..f11f5e3fb 100644 --- a/components/i2c_devices/sensor/bh1750/bh1750.c +++ b/components/i2c_devices/sensor/bh1750/bh1750.c @@ -42,15 +42,13 @@ typedef struct { i2c_bus_handle_t bus; uint16_t dev_addr; - bool dev_addr_10bit_en; } bh1750_dev_t; -bh1750_handle_t sensor_bh1750_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en) +bh1750_handle_t sensor_bh1750_create(i2c_bus_handle_t bus, uint16_t dev_addr) { bh1750_dev_t* sensor = (bh1750_dev_t*) calloc(1, sizeof(bh1750_dev_t)); sensor->bus = bus; sensor->dev_addr = dev_addr; - sensor->dev_addr_10bit_en = addr_10bit_en; return (bh1750_handle_t) sensor; } @@ -58,7 +56,7 @@ esp_err_t sensor_bh1750_delete(bh1750_handle_t sensor, bool del_bus) { bh1750_dev_t* sens = (bh1750_dev_t*) sensor; if(del_bus) { - i2s_bus_delete(sens->bus); + i2c_bus_delete(sens->bus); sens->bus = NULL; } free(sens); diff --git a/components/i2c_devices/sensor/bh1750/bh1750_obj.cpp b/components/i2c_devices/sensor/bh1750/bh1750_obj.cpp new file mode 100644 index 000000000..93d1733b6 --- /dev/null +++ b/components/i2c_devices/sensor/bh1750/bh1750_obj.cpp @@ -0,0 +1,64 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "bh1750.h" + +CBh1750::CBh1750(CI2CBus *p_i2c_bus, uint8_t addr) +{ + bus = p_i2c_bus; + m_sensor_handle = sensor_bh1750_create(bus->get_bus_handle(), addr); + +} + +CBh1750::~CBh1750() +{ + sensor_bh1750_delete(m_sensor_handle, false); + m_sensor_handle = NULL; +} + +float CBh1750::read() +{ + float val; + bh1750_get_data(m_sensor_handle, &val); + return val; +} + +esp_err_t CBh1750::on() +{ + return bh1750_power_on(m_sensor_handle); +} +esp_err_t CBh1750::off() +{ + return bh1750_power_down(m_sensor_handle); +} + +esp_err_t CBh1750::set_mode(bh1750_cmd_measure_t cmd_measure) +{ + return bh1750_set_measure_mode(m_sensor_handle, cmd_measure); +} + diff --git a/components/i2c_devices/sensor/bh1750/include/bh1750.h b/components/i2c_devices/sensor/bh1750/include/bh1750.h index ddfb117ee..99efd94b1 100644 --- a/components/i2c_devices/sensor/bh1750/include/bh1750.h +++ b/components/i2c_devices/sensor/bh1750/include/bh1750.h @@ -40,6 +40,7 @@ typedef enum{ BH1750_ONETIME_4LX_RES =0x23, /*!< Command to set measure mode as One Time L-Resolution mode*/ }bh1750_cmd_measure_t; +#define BH1750_I2C_ADDRESS_DEFAULT (0x23) typedef void* bh1750_handle_t; /** @@ -135,13 +136,12 @@ esp_err_t bh1750_change_measure_time(bh1750_handle_t sensor, uint8_t measure_tim * * @param bus I2C bus object handle * @param dev_addr I2C device address of sensor - * @param addr_10bit_en To enable 10bit address * * @return * - NULL Fail * - Others Success */ -bh1750_handle_t sensor_bh1750_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en); +bh1750_handle_t sensor_bh1750_create(i2c_bus_handle_t bus, uint16_t dev_addr); /** * @brief Delete and release a sensor object @@ -159,5 +159,75 @@ esp_err_t sensor_bh1750_delete(bh1750_handle_t sensor, bool del_bus); } #endif +#ifdef __cplusplus +/** + * class of BH1750 light sensor + * simple usage: + * CBh1750 *bh1750 = new CBh1750(&i2c_bus); + * bh1750.read(); + * ...... + * delete(bh1750); + */ +class CBh1750 +{ +private: + bh1750_handle_t m_sensor_handle; + CI2CBus *bus; + + /** + * prevent copy constructing + */ + CBh1750(const CBh1750&); + CBh1750& operator = (const CBh1750&); +public: + /** + * @brief constructor of CBh1750 + * + * @param p_i2c_bus pointer to CI2CBus object + * @param addr slave device address + */ + CBh1750(CI2CBus *p_i2c_bus, uint8_t addr = BH1750_I2C_ADDRESS_DEFAULT); + + ~CBh1750(); + /** + * @brief read brightness + * @return brightness value + */ + float read(); + + /** + * @brief turn on the sensor + * @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 on(); + + /** + * @brief turn off the sensor + * @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 off(); + + /** + * @brief set test mode for sensor + * @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_mode(bh1750_cmd_measure_t cmd_measure); +}; +#endif + #endif diff --git a/components/i2c_devices/sensor/bh1750/test/bh1750_obj_test.cpp b/components/i2c_devices/sensor/bh1750/test/bh1750_obj_test.cpp new file mode 100644 index 000000000..5dc218941 --- /dev/null +++ b/components/i2c_devices/sensor/bh1750/test/bh1750_obj_test.cpp @@ -0,0 +1,59 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "unity.h" +#include "bh1750.h" +#include "i2c_bus.h" +#include "esp_log.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +extern "C" void bh1750_obj_test() +{ + CI2CBus i2c_bus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + CBh1750 bh1750(&i2c_bus); + + bh1750.on(); + int cnt = 5; + while(cnt--){ + bh1750.set_mode(BH1750_ONETIME_4LX_RES); + vTaskDelay(30 / portTICK_RATE_MS); + printf("bh1750 val(one time mode): %f\n", bh1750.read()); + bh1750.set_mode(BH1750_CONTINUE_4LX_RES); + vTaskDelay(30 / portTICK_RATE_MS); + printf("bh1750 val(continuously mode): %f\n", bh1750.read()); + vTaskDelay(200 / portTICK_RATE_MS); + } + printf("heap: %d\n", esp_get_free_heap_size()); +} + +TEST_CASE("Sensor BH1750 obj test", "[bh1750_cpp][iot][sensor]") +{ + bh1750_obj_test(); +} diff --git a/components/i2c_devices/sensor/bh1750/test/bh1750_test.c b/components/i2c_devices/sensor/bh1750/test/bh1750_test.c index e205720ab..11b1f56fc 100644 --- a/components/i2c_devices/sensor/bh1750/test/bh1750_test.c +++ b/components/i2c_devices/sensor/bh1750/test/bh1750_test.c @@ -30,15 +30,15 @@ #include "driver/i2c.h" #include "bh1750.h" #include "i2c_bus.h" +#include "esp_log.h" -#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */ -#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */ +#define I2C_MASTER_SCL_IO 21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO 15 /*!< gpio number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ #define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */ - static i2c_bus_handle_t i2c_bus = NULL; static bh1750_handle_t sens = NULL; @@ -60,7 +60,7 @@ void i2c_bus_init() void bh1750_init() { i2c_bus_init(); - sens = sensor_bh1750_create(i2c_bus, 0x23, false); + sens = sensor_bh1750_create(i2c_bus, 0x23); } void bh1750_test_task(void* pvParameters) @@ -68,35 +68,31 @@ void bh1750_test_task(void* pvParameters) int ret; bh1750_cmd_measure_t cmd_measure; float bh1750_data; - while(1){ - uint32_t i; + int cnt = 2; + while (cnt--) { bh1750_power_on(sens); cmd_measure = BH1750_ONETIME_4LX_RES; bh1750_set_measure_mode(sens, cmd_measure); vTaskDelay(30 / portTICK_RATE_MS); - for (i=0; i<10; i++) { - ret = bh1750_get_data(sens, &bh1750_data); - if (ret == ESP_OK) { - printf("bh1750 val(one time mode): %f\n", bh1750_data); - } else { - printf("No ack, sensor not connected...\n"); - } - vTaskDelay(1000 / portTICK_RATE_MS); + ret = bh1750_get_data(sens, &bh1750_data); + if (ret == ESP_OK) { + printf("bh1750 val(one time mode): %f\n", bh1750_data); + } else { + printf("No ack, sensor not connected...\n"); } cmd_measure = BH1750_CONTINUE_4LX_RES; bh1750_set_measure_mode(sens, cmd_measure); vTaskDelay(30 / portTICK_RATE_MS); - for (i=0; i<10; i++) { - ret = bh1750_get_data(sens, &bh1750_data); - if (ret == ESP_OK) { - printf("bh1750 val(continuously mode): %f\n", bh1750_data); - } else { - printf("No ack, sensor not connected...\n"); - } - vTaskDelay(1000 / portTICK_RATE_MS); + ret = bh1750_get_data(sens, &bh1750_data); + if (ret == ESP_OK) { + printf("bh1750 val(continuously mode): %f\n", bh1750_data); + } else { + printf("No ack, sensor not connected...\n"); } vTaskDelay(1000 / portTICK_RATE_MS); } + sensor_bh1750_delete(sens, true); + vTaskDelete(NULL); } void bh1750_test() diff --git a/components/i2c_devices/sensor/hts221/hts221.c b/components/i2c_devices/sensor/hts221/hts221.c index 14a800727..ce4660632 100644 --- a/components/i2c_devices/sensor/hts221/hts221.c +++ b/components/i2c_devices/sensor/hts221/hts221.c @@ -29,7 +29,6 @@ typedef struct { i2c_bus_handle_t bus; uint16_t dev_addr; - bool dev_addr_10bit_en; } hts221_dev_t; esp_err_t hts221_write_one_reg(hts221_handle_t sensor, uint8_t reg_addr, uint8_t data) @@ -384,12 +383,11 @@ esp_err_t hts221_get_temperature(hts221_handle_t sensor, int16_t *temperature) return ESP_OK; } -hts221_handle_t sensor_hts221_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en) +hts221_handle_t sensor_hts221_create(i2c_bus_handle_t bus, uint16_t dev_addr) { hts221_dev_t* sensor = (hts221_dev_t*) calloc(1, sizeof(hts221_dev_t)); sensor->bus = bus; sensor->dev_addr = dev_addr; - sensor->dev_addr_10bit_en = addr_10bit_en; return (hts221_handle_t) sensor; } @@ -397,7 +395,7 @@ esp_err_t sensor_hts221_delete(hts221_handle_t sensor, bool del_bus) { hts221_dev_t* sens = (hts221_dev_t*) sensor; if(del_bus) { - i2s_bus_delete(sens->bus); + i2c_bus_delete(sens->bus); sens->bus = NULL; } free(sens); diff --git a/components/i2c_devices/sensor/hts221/hts221_obj.cpp b/components/i2c_devices/sensor/hts221/hts221_obj.cpp new file mode 100644 index 000000000..e2c1fbb69 --- /dev/null +++ b/components/i2c_devices/sensor/hts221/hts221_obj.cpp @@ -0,0 +1,71 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "hts221.h" + +CHts221::CHts221(CI2CBus *p_i2c_bus, uint8_t addr) +{ + bus = p_i2c_bus; + m_sensor_handle = sensor_hts221_create(bus->get_bus_handle(), addr); + hts221_config_t hts221_config; + hts221_get_config(m_sensor_handle, &hts221_config); + hts221_config.avg_h = HTS221_AVGH_32; + hts221_config.avg_t = HTS221_AVGT_16; + hts221_config.odr = HTS221_ODR_1HZ; + hts221_config.bdu_status = HTS221_DISABLE; + hts221_config.heater_status = HTS221_DISABLE; + hts221_set_config(m_sensor_handle, &hts221_config); + hts221_set_activate(m_sensor_handle); +} + +CHts221::~CHts221() +{ + sensor_hts221_delete(m_sensor_handle, false); + m_sensor_handle = NULL; +} + +float CHts221::read_temperature() +{ + int16_t temperature; + hts221_get_temperature(m_sensor_handle, &temperature); + return (float) temperature / 10; +} + +float CHts221::read_humidity() +{ + int16_t humidity; + hts221_get_humidity(m_sensor_handle, &humidity); + return (float) humidity / 10; +} + +uint8_t CHts221::id() +{ + uint8_t id; + hts221_get_deviceid(m_sensor_handle, &id); + return id; +} diff --git a/components/i2c_devices/sensor/hts221/include/hts221.h b/components/i2c_devices/sensor/hts221/include/hts221.h index 2acfea8dd..dca8e6fa2 100644 --- a/components/i2c_devices/sensor/hts221/include/hts221.h +++ b/components/i2c_devices/sensor/hts221/include/hts221.h @@ -622,13 +622,12 @@ esp_err_t hts221_get_temperature(hts221_handle_t sensor, int16_t *temperature); * * @param bus I2C bus object handle * @param dev_addr I2C device address of sensor - * @param addr_10bit_en To enable 10bit address * * @return * - NULL Fail * - Others Success */ -hts221_handle_t sensor_hts221_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en); +hts221_handle_t sensor_hts221_create(i2c_bus_handle_t bus, uint16_t dev_addr); /** * @brief Delete and release a sensor object @@ -645,5 +644,53 @@ esp_err_t sensor_hts221_delete(hts221_handle_t sensor, bool del_bus); #ifdef __cplusplus } #endif + +#ifdef __cplusplus +/** + * class of HTS221 temperature and humidity sensor + */ +class CHts221 +{ +private: + hts221_handle_t m_sensor_handle; + CI2CBus *bus; + + /** + * prevent copy constructing + */ + CHts221(const CHts221&); + CHts221& operator = (const CHts221&); +public: + /** + * @brief Constructor of CHts221 class + * @param p_i2c_bus pointer to CI2CBus object + * @param addr sensor device address + */ + CHts221(CI2CBus *p_i2c_bus, uint8_t addr = HTS221_I2C_ADDRESS); + + /** + * @brief Destructor function of CHts221 class + */ + ~CHts221(); + + /** + * @brief read temperature + * @return temperature value + */ + float read_temperature(); + + /** + * @brief read humidity + * @return humidity value + */ + float read_humidity(); + + /** + * @brief read device ID + * @return device id + */ + uint8_t id(); +}; +#endif #endif diff --git a/components/i2c_devices/sensor/hts221/test/hts221_obj_test.cpp b/components/i2c_devices/sensor/hts221/test/hts221_obj_test.cpp new file mode 100644 index 000000000..af4200320 --- /dev/null +++ b/components/i2c_devices/sensor/hts221/test/hts221_obj_test.cpp @@ -0,0 +1,55 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "unity.h" +#include "hts221.h" +#include "i2c_bus.h" +#include "esp_log.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +extern "C" void hts221_obj_test() +{ + CI2CBus i2c_bus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + CHts221 hts221(&i2c_bus); + int cnt = 3; + while (cnt--) { + printf("\n********HTS221 HUMIDITY&TEMPERATURE SENSOR********\n"); + printf("humidity value is: %2.2f\n", hts221.read_humidity()); + printf("temperature value is: %2.2f\n", hts221.read_temperature()); + printf("**************************************************\n"); + vTaskDelay(1000 / portTICK_RATE_MS); + printf("heap: %d\n", esp_get_free_heap_size()); + } +} + +TEST_CASE("Sensor hts221 obj test", "[hts221_cpp][iot][sensor]") +{ + hts221_obj_test(); +} diff --git a/components/i2c_devices/sensor/hts221/test/hts221_test.c b/components/i2c_devices/sensor/hts221/test/hts221_test.c index f630a664d..b1bbbdcec 100644 --- a/components/i2c_devices/sensor/hts221/test/hts221_test.c +++ b/components/i2c_devices/sensor/hts221/test/hts221_test.c @@ -32,8 +32,8 @@ #include "hts221.h" #include "esp_system.h" -#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */ -#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */ +#define I2C_MASTER_SCL_IO 21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO 15 /*!< gpio number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ @@ -55,7 +55,7 @@ void i2c_sensor_hts221_init() conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = I2C_MASTER_FREQ_HZ; i2c_bus = i2c_bus_create(i2c_master_port, &conf); - hts221 = sensor_hts221_create(i2c_bus, HTS221_I2C_ADDRESS, false); + hts221 = sensor_hts221_create(i2c_bus, HTS221_I2C_ADDRESS); } void hts221_test_task(void* pvParameters) diff --git a/components/i2c_devices/sensor/lis2dh12/include/lis2dh12.h b/components/i2c_devices/sensor/lis2dh12/include/lis2dh12.h index 438b3a7c5..6ccd6f0d1 100644 --- a/components/i2c_devices/sensor/lis2dh12/include/lis2dh12.h +++ b/components/i2c_devices/sensor/lis2dh12/include/lis2dh12.h @@ -798,13 +798,12 @@ esp_err_t lis2dh12_get_z_acc(lis2dh12_handle_t sensor, uint16_t *z_acc); * * @param bus I2C bus object handle * @param dev_addr I2C device address of sensor - * @param addr_10bit_en To enable 10bit address * * @return * - NULL Fail * - Others Success */ -lis2dh12_handle_t sensor_lis2dh12_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en); +lis2dh12_handle_t sensor_lis2dh12_create(i2c_bus_handle_t bus, uint16_t dev_addr); /** * @brief Delete and release a sensor object @@ -821,4 +820,58 @@ esp_err_t sensor_lis2dh12_delete(lis2dh12_handle_t sensor, bool del_bus); #ifdef __cplusplus } #endif + +#ifdef __cplusplus +/** + * class of lis2dh12 acceleration sensor + */ +class CLis2dh12 +{ +private: + lis2dh12_handle_t m_sensor_handle; + CI2CBus *bus; + + /** + * prevent copy constructing + */ + CLis2dh12(const CLis2dh12&); + CLis2dh12& operator = (const CLis2dh12&); +public: + /** + * @brief Constructor of CLis2dh12 class + * @param p_i2c_bus pointer to CI2CBus object + * @param addr slave device address + */ + CLis2dh12(CI2CBus *p_i2c_bus, uint8_t addr = LIS2DH12_I2C_ADDRESS); + + /** + * @brief Destructor of CLis2dh12 class + */ + ~CLis2dh12(); + + /** + * @brief Read device ID + * @return device ID + */ + uint8_t id(); + + /** + * @brief get acceleration on x-axis + * @return acceleration on x-axis + */ + uint16_t ax(); + + /** + * @brief get acceleration on y-axis + * @return acceleration on y-axis + */ + uint16_t ay(); + + /** + * @brief get acceleration on z-axis + * @return acceleration on z-axis + */ + uint16_t az(); +}; +#endif #endif diff --git a/components/i2c_devices/sensor/lis2dh12/lis2dh12.c b/components/i2c_devices/sensor/lis2dh12/lis2dh12.c index d4d7a361a..432b1bf08 100644 --- a/components/i2c_devices/sensor/lis2dh12/lis2dh12.c +++ b/components/i2c_devices/sensor/lis2dh12/lis2dh12.c @@ -40,7 +40,6 @@ static const char* TAG = "lis2dh12"; typedef struct { i2c_bus_handle_t bus; uint16_t dev_addr; - bool dev_addr_10bit_en; } lis2dh12_dev_t; esp_err_t lis2dh12_write_one_reg(lis2dh12_handle_t sensor, uint8_t reg_addr, uint8_t data) @@ -262,12 +261,11 @@ esp_err_t lis2dh12_get_z_acc(lis2dh12_handle_t sensor, uint16_t *z_acc) return ESP_OK; } -lis2dh12_handle_t sensor_lis2dh12_create(lis2dh12_handle_t bus, uint16_t dev_addr, bool addr_10bit_en) +lis2dh12_handle_t sensor_lis2dh12_create(lis2dh12_handle_t bus, uint16_t dev_addr) { lis2dh12_dev_t* sensor = (lis2dh12_dev_t*) calloc(1, sizeof(lis2dh12_dev_t)); sensor->bus = bus; sensor->dev_addr = dev_addr; - sensor->dev_addr_10bit_en = addr_10bit_en; return (lis2dh12_handle_t) sensor; } @@ -275,7 +273,7 @@ esp_err_t sensor_lis2dh12_delete(lis2dh12_handle_t sensor, bool del_bus) { lis2dh12_dev_t* sens = (lis2dh12_dev_t*) sensor; if(del_bus) { - i2s_bus_delete(sens->bus); + i2c_bus_delete(sens->bus); sens->bus = NULL; } free(sens); diff --git a/components/i2c_devices/sensor/lis2dh12/lis2dh12_obj.cpp b/components/i2c_devices/sensor/lis2dh12/lis2dh12_obj.cpp new file mode 100644 index 000000000..3ea59570e --- /dev/null +++ b/components/i2c_devices/sensor/lis2dh12/lis2dh12_obj.cpp @@ -0,0 +1,85 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "lis2dh12.h" + +CLis2dh12::CLis2dh12(CI2CBus *p_i2c_bus, uint8_t addr) +{ + bus = p_i2c_bus; + m_sensor_handle = sensor_lis2dh12_create(bus->get_bus_handle(), addr); + + lis2dh12_config_t lis2dh12_config; + lis2dh12_get_config(m_sensor_handle, &lis2dh12_config); + lis2dh12_config.temp_enable = LIS2DH12_TEMP_DISABLE; + lis2dh12_config.odr = LIS2DH12_ODR_1HZ; + lis2dh12_config.opt_mode = LIS2DH12_OPT_NORMAL; + lis2dh12_config.z_enable = LIS2DH12_ENABLE; + lis2dh12_config.y_enable = LIS2DH12_ENABLE; + lis2dh12_config.x_enable = LIS2DH12_ENABLE; + lis2dh12_config.bdu_status = LIS2DH12_DISABLE; + lis2dh12_config.fs = LIS2DH12_FS_16G; + lis2dh12_set_config(m_sensor_handle, &lis2dh12_config); +} + +CLis2dh12::~CLis2dh12() +{ + sensor_lis2dh12_delete(m_sensor_handle, false); + m_sensor_handle = NULL; +} + +uint8_t CLis2dh12::id() +{ + uint8_t id; + lis2dh12_get_deviceid(m_sensor_handle, &id); + return id; +} + +uint16_t CLis2dh12::ax() +{ + uint16_t acc_val; + lis2dh12_get_x_acc(m_sensor_handle, &acc_val); + return acc_val; +} + +uint16_t CLis2dh12::ay() +{ + uint16_t acc_val; + lis2dh12_get_y_acc(m_sensor_handle, &acc_val); + return acc_val; +} + +uint16_t CLis2dh12::az() +{ + uint16_t acc_val; + lis2dh12_get_z_acc(m_sensor_handle, &acc_val); + return acc_val; +} + + + + diff --git a/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_obj_test.cpp b/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_obj_test.cpp new file mode 100644 index 000000000..9650a2a09 --- /dev/null +++ b/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_obj_test.cpp @@ -0,0 +1,57 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "unity.h" +#include "lis2dh12.h" +#include "i2c_bus.h" +#include "esp_log.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +extern "C" void lis2dh12_obj_test() +{ + CI2CBus i2c_bus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + CLis2dh12 lis2dh12(&i2c_bus); + + int cnt = 5; + while(cnt--){ + printf("\n******************************************\n"); + printf("X-axis acceleration is: %08x\n", lis2dh12.ax()); + printf("Y-axis acceleration is: %08x\n", lis2dh12.ay()); + printf("Z-axis acceleration is: %08x\n", lis2dh12.az()); + printf("******************************************\n"); + vTaskDelay(500 / portTICK_RATE_MS); + } + printf("heap: %d\n", esp_get_free_heap_size()); +} + +TEST_CASE("Sensor LIS2DH12 obj test", "[lis2dh12_cpp][iot][sensor]") +{ + lis2dh12_obj_test(); +} diff --git a/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_test.c b/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_test.c index 018982fd4..fddf67f90 100644 --- a/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_test.c +++ b/components/i2c_devices/sensor/lis2dh12/test/lis2dh12_test.c @@ -53,7 +53,7 @@ void i2c_master_init() conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = I2C_MASTER_FREQ_HZ; i2c_bus = i2c_bus_create(I2C_MASTER_NUM, &conf); - sens = sensor_lis2dh12_create(i2c_bus, LIS2DH12_I2C_ADDRESS, false); + sens = sensor_lis2dh12_create(i2c_bus, LIS2DH12_I2C_ADDRESS); } void lis2dh12_test_task(void* pvParameters) @@ -64,7 +64,7 @@ void lis2dh12_test_task(void* pvParameters) uint16_t z_acc; lis2dh12_get_deviceid(sens, &deviceid); printf("LIS2DH12 device id is: %02x\n", deviceid); - + lis2dh12_config_t lis2dh12_config; lis2dh12_get_config(sens, &lis2dh12_config); printf("temp_enable is: %02x\n", lis2dh12_config.temp_enable); @@ -74,7 +74,7 @@ void lis2dh12_test_task(void* pvParameters) printf("y_enable status is: %02x\n", lis2dh12_config.y_enable); printf("x_enable status is: %02x\n", lis2dh12_config.x_enable); printf("bdu_status status is: %02x\n", lis2dh12_config.bdu_status); - printf("full scale is: %02x\n", lis2dh12_config.fs); + printf("full scale is: %02x\n", lis2dh12_config.fs); lis2dh12_config.temp_enable = LIS2DH12_TEMP_DISABLE; lis2dh12_config.odr = LIS2DH12_ODR_1HZ; @@ -83,7 +83,7 @@ void lis2dh12_test_task(void* pvParameters) lis2dh12_config.y_enable = LIS2DH12_ENABLE; lis2dh12_config.x_enable = LIS2DH12_ENABLE; lis2dh12_config.bdu_status = LIS2DH12_DISABLE; - lis2dh12_config.fs = LIS2DH12_FS_16G; + lis2dh12_config.fs = LIS2DH12_FS_16G; lis2dh12_set_config(sens, &lis2dh12_config); while(1){ diff --git a/components/i2c_devices/sensor/mvh3004d/include/mvh3004d.h b/components/i2c_devices/sensor/mvh3004d/include/mvh3004d.h index d33709365..8f408aba4 100644 --- a/components/i2c_devices/sensor/mvh3004d/include/mvh3004d.h +++ b/components/i2c_devices/sensor/mvh3004d/include/mvh3004d.h @@ -40,13 +40,12 @@ typedef void* mvh3004d_handle_t; * * @param bus I2C bus object handle * @param dev_addr I2C device address of sensor - * @param addr_10bit_en To enable 10bit address * * @return * - NULL Fail * - Others Success */ -mvh3004d_handle_t sensor_mvh3004d_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en); +mvh3004d_handle_t sensor_mvh3004d_create(i2c_bus_handle_t bus, uint16_t dev_addr); /** * @brief Delete and release a sensor object @@ -95,5 +94,47 @@ esp_err_t mvh3004d_get_temperature(mvh3004d_handle_t sensor, float* tp); } #endif +#ifdef __cplusplus +/** + * class of mvh3004d temperature and humidity sensor + */ +class CMvh3004d +{ +private: + mvh3004d_handle_t m_sensor_handle; + CI2CBus *bus; + + /** + * prevent copy constructing + */ + CMvh3004d(const CMvh3004d&); + CMvh3004d& operator = (const CMvh3004d&); +public: + /** + * @brief Constructor for CMvh3004d class + * @param p_i2c_bus pointer to CI2CBus object + * @param addr slave device address + */ + CMvh3004d(CI2CBus *p_i2c_bus, uint8_t addr = MVH3004D_SLAVE_ADDR); + + /** + * @brief Destructor function for CMvh3004d class + */ + ~CMvh3004d(); + + /** + * @brief read temperature + * @return temperature value + */ + float read_temperature(); + + /** + * @brief read humidity + * @return humidity value + */ + float read_humidity(); +}; +#endif + #endif diff --git a/components/i2c_devices/sensor/mvh3004d/mvh3004d.c b/components/i2c_devices/sensor/mvh3004d/mvh3004d.c index 9487c49ab..924d1b50b 100644 --- a/components/i2c_devices/sensor/mvh3004d/mvh3004d.c +++ b/components/i2c_devices/sensor/mvh3004d/mvh3004d.c @@ -38,15 +38,13 @@ static const char* TAG = "mvh3004d"; typedef struct { i2c_bus_handle_t bus; uint16_t dev_addr; - bool dev_addr_10bit_en; } mvh3004d_dev_t; -mvh3004d_handle_t sensor_mvh3004d_create(i2c_bus_handle_t bus, uint16_t dev_addr, bool addr_10bit_en) +mvh3004d_handle_t sensor_mvh3004d_create(i2c_bus_handle_t bus, uint16_t dev_addr) { mvh3004d_dev_t* sensor = (mvh3004d_dev_t*) calloc(1, sizeof(mvh3004d_dev_t)); sensor->bus = bus; sensor->dev_addr = dev_addr; - sensor->dev_addr_10bit_en = addr_10bit_en; return (mvh3004d_handle_t) sensor; } @@ -54,7 +52,7 @@ esp_err_t sensor_mvh3004d_delete(mvh3004d_handle_t sensor, bool del_bus) { mvh3004d_dev_t* sens = (mvh3004d_dev_t*) sensor; if(del_bus) { - i2s_bus_delete(sens->bus); + i2c_bus_delete(sens->bus); sens->bus = NULL; } free(sens); diff --git a/components/i2c_devices/sensor/mvh3004d/mvh3004d_obj.cpp b/components/i2c_devices/sensor/mvh3004d/mvh3004d_obj.cpp new file mode 100644 index 000000000..93970db2b --- /dev/null +++ b/components/i2c_devices/sensor/mvh3004d/mvh3004d_obj.cpp @@ -0,0 +1,57 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 +#include "esp_log.h" +#include "driver/i2c.h" +#include "i2c_bus.h" +#include "mvh3004d.h" + +CMvh3004d::CMvh3004d(CI2CBus *p_i2c_bus, uint8_t addr) +{ + bus = p_i2c_bus; + m_sensor_handle = sensor_mvh3004d_create(bus->get_bus_handle(), addr); + +} + +CMvh3004d::~CMvh3004d() +{ + sensor_mvh3004d_delete(m_sensor_handle, false); + m_sensor_handle = NULL; +} + +float CMvh3004d::read_temperature() +{ + float val; + mvh3004d_get_temperature(m_sensor_handle, &val); + return val; +} + +float CMvh3004d::read_humidity() +{ + float val; + mvh3004d_get_huminity(m_sensor_handle, &val); + return val; +} + diff --git a/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_obj_test.cpp b/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_obj_test.cpp new file mode 100644 index 000000000..003feba47 --- /dev/null +++ b/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_obj_test.cpp @@ -0,0 +1,53 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2017 + * + * 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "unity.h" +#include "mvh3004d.h" +#include "i2c_bus.h" +#include "esp_log.h" + +#define I2C_MASTER_SCL_IO (gpio_num_t)21 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO (gpio_num_t)15 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */ + +extern "C" void mvh3004d_obj_test() +{ + CI2CBus i2c_bus(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO); + CMvh3004d mvh3004d(&i2c_bus); + + int cnt = 0; + while (cnt < 5) { + printf("test[%d]: tp: %.02f; rh: %.02f %%\n", cnt++, mvh3004d.read_temperature(), mvh3004d.read_humidity()); + vTaskDelay(10 / portTICK_RATE_MS); + } + printf("heap: %d\n", esp_get_free_heap_size()); +} + +TEST_CASE("Sensor MVH3004D obj test", "[mvh3004d_cpp][iot][sensor]") +{ + mvh3004d_obj_test(); +} diff --git a/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_test.c b/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_test.c index e33bfb7ac..67b0bf4ec 100644 --- a/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_test.c +++ b/components/i2c_devices/sensor/mvh3004d/test/mvh3004d_test.c @@ -57,7 +57,7 @@ static void i2c_bus_init() void mvh3004d_init() { i2c_bus_init(); - sens = sensor_mvh3004d_create(i2c_bus, MVH3004D_SLAVE_ADDR, false); + sens = sensor_mvh3004d_create(i2c_bus, MVH3004D_SLAVE_ADDR); } void mvh3004d_test_task(void* pvParameters) diff --git a/components/lcd/test/lcd_thermostat.cpp b/components/lcd/test/lcd_thermostat.cpp index b0cbbfd85..e368982b5 100644 --- a/components/lcd/test/lcd_thermostat.cpp +++ b/components/lcd/test/lcd_thermostat.cpp @@ -212,7 +212,7 @@ void i2c_sensor_init() conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = I2C_MASTER_FREQ_HZ; i2c_bus = i2c_bus_create(I2C_MASTER_NUM, &conf); - hts221 = sensor_hts221_create(i2c_bus, HTS221_I2C_ADDRESS, false); + hts221 = sensor_hts221_create(i2c_bus, HTS221_I2C_ADDRESS); uint8_t hts221_deviceid; hts221_get_deviceid(hts221, &hts221_deviceid); @@ -235,7 +235,7 @@ void i2c_sensor_init() hts221_set_config(hts221, &hts221_config); hts221_set_activate(hts221); - bh1750 = sensor_bh1750_create(i2c_bus, 0x23, false); + bh1750 = sensor_bh1750_create(i2c_bus, 0x23); hts221_test_task(); }