diff --git a/README.md b/README.md index 7328f88..10fff1e 100644 --- a/README.md +++ b/README.md @@ -39,17 +39,12 @@ Supports sensors features: **Connections** The Sensor uses I2C for communication's. Data is outputted (eg to a PC) via a USB. -Can be set up for any I2C interface and speed. By default I2C0, GPIO16(SDATA) and GPIO17(SCLK) & 100kHz. -If you want to use the other I2C port (I2C1) in addition to changing in the main.ccp, -The user must change it in library header as well, could not find a way around this. - -* i2c_inst_t *i2c = i2c0 to i2c_inst_t *i2c = i2c1 - +Can be set up for any I2C interface and bus speed. By default I2C0, GPIO16(SDATA) and GPIO17(SCLK) & 100kHz. **Files** The main.cpp contains tests showing library functions -There is also an library (ahtxx.cpp and ahtxx.h), +There is also an library (ahtxx.cpp and ahtxx.hpp), **Output** diff --git a/extra/doc/CHANGELOG.md b/extra/doc/CHANGELOG.md index 7407fff..0eac9d1 100644 --- a/extra/doc/CHANGELOG.md +++ b/extra/doc/CHANGELOG.md @@ -2,5 +2,7 @@ * version 1.0.0 November 2022 * first release +* version 1.0.1 December 2022 + * Change so user can pick any I2C interface (IC20 or IC21) diff --git a/include/ahtxx/ahtxx.hpp b/include/ahtxx/ahtxx.hpp index 0439e8d..5ea1678 100644 --- a/include/ahtxx/ahtxx.hpp +++ b/include/ahtxx/ahtxx.hpp @@ -52,8 +52,11 @@ typedef enum ASAIR_I2C_SENSOR_e{ class LIB_AHTXX { private: - i2c_inst_t *i2c = i2c0; + i2c_inst_t *i2c; uint8_t _address; + uint8_t _SDataPin; + uint8_t _SClkPin; + uint16_t _CLKSpeed = 100; //I2C bus speed in khz typically 100-400 ASAIR_I2C_SENSOR_e _sensorName; uint8_t _rawDataBuffer[6] = {AHT10_ERROR, 0, 0, 0, 0, 0}; int16_t returnValue = 0; @@ -61,10 +64,10 @@ class LIB_AHTXX public: // Constructor - LIB_AHTXX(uint8_t , ASAIR_I2C_SENSOR_e); + LIB_AHTXX(uint8_t address, i2c_inst_t* i2c_type, uint8_t sdata , uint8_t sclk ,uint16_t clockspeed); - void AHT10_InitI2C(i2c_inst_t* i2c_type, uint8_t sdata , uint8_t sclk ,uint16_t clockspeed); - void AHT10_DeInit(i2c_inst_t* i2c_type); + void AHT10_InitI2C(ASAIR_I2C_SENSOR_e sensorName); + void AHT10_DeInit(); bool AHT10_begin(); uint8_t AHT10_readRawData(); float AHT10_readTemperature(bool); diff --git a/src/ahtxx/ahtxx.cpp b/src/ahtxx/ahtxx.cpp index a3a47de..20aff55 100644 --- a/src/ahtxx/ahtxx.cpp +++ b/src/ahtxx/ahtxx.cpp @@ -10,29 +10,32 @@ #include "../include/ahtxx/ahtxx.hpp" // Constructor Desc init the sensor data types call before begin() -// Param 1 I2C address -// Param 2 Enum with Sensor types +// Param 1 : I2C address +// Param 2 : IC2 interface , ic20 or i2c1 +// Param 3 : Data pin I2C +// Param 4 : Clock pin I2C +// Param 5 : I2C Clock speed in Khz 0-400Khz -LIB_AHTXX::LIB_AHTXX(uint8_t address, ASAIR_I2C_SENSOR_e sensorName) { +LIB_AHTXX::LIB_AHTXX(uint8_t address, i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed) { _address = address; - _sensorName = sensorName; + i2c = i2c_type; + _SClkPin = SCLKpin; + _SDataPin = SDApin; + _CLKSpeed = CLKspeed; } // Function Desc Initialise the I2C -// Param 1 : IC2 interface ic20 i2c1 etc -// Param 2 : Data pin I2C -// Param 3 : Clock pin I2C -// Param 4 : I2C Clock speed in Khz 0-400Khz +// Param 1 :: Enum with Sensor types // NOTE :: call before begin method -void LIB_AHTXX::AHT10_InitI2C(i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed) { - i2c_inst_t *i2c = i2c_type; +void LIB_AHTXX::AHT10_InitI2C(ASAIR_I2C_SENSOR_e sensorName) { + _sensorName = sensorName; //init I2C - i2c_init(i2c, CLKspeed * 1000); - gpio_set_function(SDApin, GPIO_FUNC_I2C); - gpio_set_function(SCLKpin, GPIO_FUNC_I2C); - gpio_pull_up(SDApin); - gpio_pull_up(SCLKpin); + gpio_set_function(_SDataPin, GPIO_FUNC_I2C); + gpio_set_function(_SClkPin, GPIO_FUNC_I2C); + gpio_pull_up(_SDataPin); + gpio_pull_up(_SClkPin); + i2c_init(i2c, _CLKSpeed * 1000); } // Function desc: Initialize I2C & configure the sensor, call this function before @@ -288,9 +291,10 @@ uint8_t LIB_AHTXX::AHT10_getBusyBit(bool readI2C) { } -void LIB_AHTXX::AHT10_DeInit(i2c_inst_t* i2c_type) +void LIB_AHTXX::AHT10_DeInit() { - i2c_inst_t *i2c = i2c_type; + gpio_set_function(_SDataPin, GPIO_FUNC_NULL); + gpio_set_function(_SClkPin, GPIO_FUNC_NULL); i2c_deinit(i2c); } diff --git a/src/main.cpp b/src/main.cpp index c924ccc..ecf103f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,16 +13,16 @@ #include "pico/stdlib.h" #include "ahtxx/ahtxx.hpp" -LIB_AHTXX myAHT10(AHT10_ADDRESS_0X38, AHT10_SENSOR); +LIB_AHTXX myAHT10(AHT10_ADDRESS_0X38, i2c0, 16, 17, 100); int main(void) { float AHT10_temperature; stdio_init_all(); // Initialize chosen serial port, 38400 default baudrate. - busy_wait_ms(500); + busy_wait_ms(1000); printf("AHT10 Start \r\n"); - myAHT10.AHT10_InitI2C(i2c0, 16, 17, 100); + myAHT10.AHT10_InitI2C(AHT10_SENSOR); busy_wait_ms(500); // Start the sensor comms