This is a library to read out sensor values from Silicon Labs' si70xx-range of relative humidity and temperature sensors.
Dependents: MemLCD-Temperature-Humidity-Demo lab123
Information
All examples in this repo are considered EXPERIMENTAL QUALITY, meaning this code has been created as one-off proof-of-concept and is suitable as a demonstration for experimental purposes only. This code will not be regularly maintained by Silicon Labs and there is no guarantee that these projects will work across all environments, SDK versions and hardware.
Caution
This library targets mbed's asynchronous transfer APIs, so it can only be used in conjunction with platforms supporting these APIs.
The library is currently compatible with Si7013, Si7020 and Si7021 parts.
Usage
Include mbed low-power to use this driver
#include "mbed.h" #include "SILABS_RHT.h" I2C sensorI2C(PD6, PD7); //PD6=SDA, PD7=SCL silabs::SILABS_RHT rhtSensor(&sensorI2C); volatile bool busChecked = false; void respondedCallback( void ) { busChecked = true; } int main() { rhtSensor.check_availability(si7021, respondedCallback); while(busChecked == false) sleep(); busChecked = false; rhtSensor.measure(si7021, respondedCallback); while(busChecked == false) sleep(); if(rhtSensor.get_active()) { printf("Temperature: %d.%03d degC\n", rhtSensor.get_Temperature()/1000, rhtSensor.get_Temperature()%1000); } else { printf("No sensor found\n"); } while(1) sleep(); }
Datasheets
http://www.silabs.com/products/sensors/humidity-sensors/Pages/si7013-20-21.aspx
SILABS_RHT.h@2:83a8b7df827f, 2015-05-05 (annotated)
- Committer:
- stevew817
- Date:
- Tue May 05 14:32:43 2015 +0000
- Revision:
- 2:83a8b7df827f
- Parent:
- 1:f3c25dea392e
CallbackPointer.h got deprecated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Steven Cooreman |
0:9fd18754e0c0 | 1 | /***************************************************************************//** |
Steven Cooreman |
0:9fd18754e0c0 | 2 | * @file SILABS_RHT.h |
Steven Cooreman |
1:f3c25dea392e | 3 | * @brief Driver class for the Silicon Labs si70xx series I2C RHT sensors |
Steven Cooreman |
0:9fd18754e0c0 | 4 | ******************************************************************************* |
Steven Cooreman |
0:9fd18754e0c0 | 5 | * @section License |
Steven Cooreman |
0:9fd18754e0c0 | 6 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
Steven Cooreman |
0:9fd18754e0c0 | 7 | ******************************************************************************* |
Steven Cooreman |
0:9fd18754e0c0 | 8 | * |
Steven Cooreman |
0:9fd18754e0c0 | 9 | * Permission is granted to anyone to use this software for any purpose, |
Steven Cooreman |
0:9fd18754e0c0 | 10 | * including commercial applications, and to alter it and redistribute it |
Steven Cooreman |
0:9fd18754e0c0 | 11 | * freely, subject to the following restrictions: |
Steven Cooreman |
0:9fd18754e0c0 | 12 | * |
Steven Cooreman |
0:9fd18754e0c0 | 13 | * 1. The origin of this software must not be misrepresented; you must not |
Steven Cooreman |
0:9fd18754e0c0 | 14 | * claim that you wrote the original software. |
Steven Cooreman |
0:9fd18754e0c0 | 15 | * 2. Altered source versions must be plainly marked as such, and must not be |
Steven Cooreman |
0:9fd18754e0c0 | 16 | * misrepresented as being the original software. |
Steven Cooreman |
0:9fd18754e0c0 | 17 | * 3. This notice may not be removed or altered from any source distribution. |
Steven Cooreman |
0:9fd18754e0c0 | 18 | * |
Steven Cooreman |
0:9fd18754e0c0 | 19 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
Steven Cooreman |
0:9fd18754e0c0 | 20 | * obligation to support this Software. Silicon Labs is providing the |
Steven Cooreman |
0:9fd18754e0c0 | 21 | * Software "AS IS", with no express or implied warranties of any kind, |
Steven Cooreman |
0:9fd18754e0c0 | 22 | * including, but not limited to, any implied warranties of merchantability |
Steven Cooreman |
0:9fd18754e0c0 | 23 | * or fitness for any particular purpose or warranties against infringement |
Steven Cooreman |
0:9fd18754e0c0 | 24 | * of any proprietary rights of a third party. |
Steven Cooreman |
0:9fd18754e0c0 | 25 | * |
Steven Cooreman |
0:9fd18754e0c0 | 26 | * Silicon Labs will not be liable for any consequential, incidental, or |
Steven Cooreman |
0:9fd18754e0c0 | 27 | * special damages, or any other relief, or for any claim by any third party, |
Steven Cooreman |
0:9fd18754e0c0 | 28 | * arising from your use of this Software. |
Steven Cooreman |
0:9fd18754e0c0 | 29 | * |
Steven Cooreman |
0:9fd18754e0c0 | 30 | ******************************************************************************/ |
Steven Cooreman |
0:9fd18754e0c0 | 31 | |
Steven Cooreman |
0:9fd18754e0c0 | 32 | #ifndef SILABS_RHT_H |
Steven Cooreman |
0:9fd18754e0c0 | 33 | #define SILABS_RHT_H |
Steven Cooreman |
0:9fd18754e0c0 | 34 | |
Steven Cooreman |
0:9fd18754e0c0 | 35 | #include "platform.h" |
Steven Cooreman |
0:9fd18754e0c0 | 36 | #include <mbed.h> |
Steven Cooreman |
0:9fd18754e0c0 | 37 | |
Steven Cooreman |
0:9fd18754e0c0 | 38 | typedef void (*cbptr_t)(void); |
Steven Cooreman |
0:9fd18754e0c0 | 39 | |
Steven Cooreman |
0:9fd18754e0c0 | 40 | #define SILABS_RHT_ERROR_BUSY -1 |
Steven Cooreman |
0:9fd18754e0c0 | 41 | #define SILABS_RHT_ERROR_I2C_BUSY -2 |
Steven Cooreman |
0:9fd18754e0c0 | 42 | #define SILABS_RHT_NO_ACTION -3 |
Steven Cooreman |
0:9fd18754e0c0 | 43 | #define SILABS_RHT_ERROR_ARGUMENT -4 |
Steven Cooreman |
0:9fd18754e0c0 | 44 | #define SILABS_RHT_OK 0 |
Steven Cooreman |
0:9fd18754e0c0 | 45 | |
Steven Cooreman |
0:9fd18754e0c0 | 46 | typedef enum { |
Steven Cooreman |
0:9fd18754e0c0 | 47 | RHT_IDLE, // Object initialized |
Steven Cooreman |
0:9fd18754e0c0 | 48 | RHT_ACTIVE, // Object initialized and sensor available |
Steven Cooreman |
0:9fd18754e0c0 | 49 | RHT_MEASURING, // In the process of getting an RH measurement |
Steven Cooreman |
0:9fd18754e0c0 | 50 | RHT_TEMPERATURE, // In the process of reading temperature measurement |
Steven Cooreman |
0:9fd18754e0c0 | 51 | RHT_PINGING, // In the process of polling the device |
Steven Cooreman |
0:9fd18754e0c0 | 52 | RHT_FWREV // In the process of reading the device's firmware revision |
Steven Cooreman |
0:9fd18754e0c0 | 53 | } SILABS_RHT_state_t; |
Steven Cooreman |
0:9fd18754e0c0 | 54 | |
Steven Cooreman |
0:9fd18754e0c0 | 55 | typedef enum { |
Steven Cooreman |
0:9fd18754e0c0 | 56 | si7013, |
Steven Cooreman |
0:9fd18754e0c0 | 57 | si7020, |
Steven Cooreman |
0:9fd18754e0c0 | 58 | si7021 |
Steven Cooreman |
0:9fd18754e0c0 | 59 | } SILABS_RHT_device_t; |
Steven Cooreman |
0:9fd18754e0c0 | 60 | |
Steven Cooreman |
0:9fd18754e0c0 | 61 | namespace silabs { |
Steven Cooreman |
0:9fd18754e0c0 | 62 | class SILABS_RHT { |
Steven Cooreman |
0:9fd18754e0c0 | 63 | |
Steven Cooreman |
0:9fd18754e0c0 | 64 | public: |
Steven Cooreman |
0:9fd18754e0c0 | 65 | |
Steven Cooreman |
0:9fd18754e0c0 | 66 | SILABS_RHT(I2C * i2c); |
Steven Cooreman |
0:9fd18754e0c0 | 67 | |
Steven Cooreman |
1:f3c25dea392e | 68 | /* |
Steven Cooreman |
0:9fd18754e0c0 | 69 | * Get last measured temperature data |
Steven Cooreman |
1:f3c25dea392e | 70 | * return: int32_t = temperature in millidegrees centigrade |
Steven Cooreman |
0:9fd18754e0c0 | 71 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 72 | int32_t get_temperature(); |
Steven Cooreman |
0:9fd18754e0c0 | 73 | |
Steven Cooreman |
1:f3c25dea392e | 74 | /* |
Steven Cooreman |
0:9fd18754e0c0 | 75 | * Get last measured relative humidity data |
Steven Cooreman |
1:f3c25dea392e | 76 | * return: uint32_t = relative humidity value in milli-percent |
Steven Cooreman |
0:9fd18754e0c0 | 77 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 78 | uint32_t get_humidity(); |
Steven Cooreman |
0:9fd18754e0c0 | 79 | |
Steven Cooreman |
1:f3c25dea392e | 80 | /* |
Steven Cooreman |
0:9fd18754e0c0 | 81 | * Get current state of the sensor, active or inactive. Need to call check_availability first after initialization. |
Steven Cooreman |
1:f3c25dea392e | 82 | * return: true if the last operation (check_active or measure) was a success, false if not (device did not respond). |
Steven Cooreman |
0:9fd18754e0c0 | 83 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 84 | bool get_active(); |
Steven Cooreman |
0:9fd18754e0c0 | 85 | |
Steven Cooreman |
1:f3c25dea392e | 86 | /* |
Steven Cooreman |
1:f3c25dea392e | 87 | * Perform measurement. |
Steven Cooreman |
1:f3c25dea392e | 88 | * Asynchronous callback can be provided (type void (*)(void)). |
Steven Cooreman |
1:f3c25dea392e | 89 | * return: 0 if successful, else one of the defined error codes. |
Steven Cooreman |
0:9fd18754e0c0 | 90 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 91 | int measure(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL); |
Steven Cooreman |
0:9fd18754e0c0 | 92 | |
Steven Cooreman |
1:f3c25dea392e | 93 | /* |
Steven Cooreman |
0:9fd18754e0c0 | 94 | * Check if the sensor is active and responding. This will update the get_active value. |
Steven Cooreman |
1:f3c25dea392e | 95 | * Asynchronous callback can be provided (type void (*)(void)). |
Steven Cooreman |
1:f3c25dea392e | 96 | * return: 0 if successful, else one of the defined error codes. |
Steven Cooreman |
0:9fd18754e0c0 | 97 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 98 | int check_availability(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL); |
Steven Cooreman |
0:9fd18754e0c0 | 99 | |
Steven Cooreman |
0:9fd18754e0c0 | 100 | protected: |
Steven Cooreman |
0:9fd18754e0c0 | 101 | mbed::I2C *_i2c; |
Steven Cooreman |
0:9fd18754e0c0 | 102 | |
Steven Cooreman |
1:f3c25dea392e | 103 | event_callback_t _internalCallback; |
Steven Cooreman |
0:9fd18754e0c0 | 104 | SILABS_RHT_state_t _state; |
Steven Cooreman |
0:9fd18754e0c0 | 105 | cbptr_t _completionCallbackPtr; |
Steven Cooreman |
0:9fd18754e0c0 | 106 | |
Steven Cooreman |
0:9fd18754e0c0 | 107 | uint8_t _address; |
Steven Cooreman |
0:9fd18754e0c0 | 108 | uint8_t _rx_buf[8]; |
Steven Cooreman |
0:9fd18754e0c0 | 109 | uint8_t _tx_buf[2]; |
Steven Cooreman |
0:9fd18754e0c0 | 110 | |
Steven Cooreman |
0:9fd18754e0c0 | 111 | uint32_t _rhData; |
Steven Cooreman |
0:9fd18754e0c0 | 112 | int32_t _tData; |
Steven Cooreman |
0:9fd18754e0c0 | 113 | |
Steven Cooreman |
0:9fd18754e0c0 | 114 | |
Steven Cooreman |
0:9fd18754e0c0 | 115 | |
Steven Cooreman |
1:f3c25dea392e | 116 | /** |
Steven Cooreman |
0:9fd18754e0c0 | 117 | * Callback handler for internal I2C transfers. |
Steven Cooreman |
0:9fd18754e0c0 | 118 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 119 | void _cbHandler( int event ); |
Steven Cooreman |
0:9fd18754e0c0 | 120 | |
Steven Cooreman |
1:f3c25dea392e | 121 | /** |
Steven Cooreman |
0:9fd18754e0c0 | 122 | * Callback handler for internal I2C transfers triggered by timeout. |
Steven Cooreman |
0:9fd18754e0c0 | 123 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 124 | void _cbHandlerTimeout( void ); |
Steven Cooreman |
0:9fd18754e0c0 | 125 | |
Steven Cooreman |
1:f3c25dea392e | 126 | /** |
Steven Cooreman |
0:9fd18754e0c0 | 127 | * Internal lookup table for I2C address by device type. |
Steven Cooreman |
0:9fd18754e0c0 | 128 | */ |
Steven Cooreman |
0:9fd18754e0c0 | 129 | uint8_t get_address(SILABS_RHT_device_t device); |
Steven Cooreman |
0:9fd18754e0c0 | 130 | }; |
Steven Cooreman |
0:9fd18754e0c0 | 131 | |
Steven Cooreman |
0:9fd18754e0c0 | 132 | } // namespace silabs |
Steven Cooreman |
0:9fd18754e0c0 | 133 | |
Steven Cooreman |
0:9fd18754e0c0 | 134 | #endif //SILABS_RHT_H |