From 19fbf250a938c01e526962ded7e3d398ab7c85c8 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 14 Oct 2025 17:50:53 +0300 Subject: [PATCH] Add readme --- I2Csensors/CMakeLists.txt | 4 +- I2Csensors/Readme.md | 156 +++++++++++++++++++++++++++- I2Csensors/common_table | 2 + I2Csensors/examples/logmany.c | 11 +- I2Csensors/examples/single_sensor.c | 2 +- I2Csensors/i2csensorsPTH.pc.in | 2 +- 6 files changed, 166 insertions(+), 11 deletions(-) diff --git a/I2Csensors/CMakeLists.txt b/I2Csensors/CMakeLists.txt index d3aaa17..5208a25 100644 --- a/I2Csensors/CMakeLists.txt +++ b/I2Csensors/CMakeLists.txt @@ -8,9 +8,9 @@ set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}") project(${PROJ} VERSION ${VERSION} LANGUAGES C) # default flags -set(CMAKE_C_FLAGS "${CFLAGS} -O2") +set(CMAKE_C_FLAGS "${CFLAGS} -O2 -Wno-address-of-packed-member") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS}") -set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W -Wno-address-of-packed-member") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W") set(CMAKE_COLOR_MAKEFILE ON) option(DEBUG "Compile in debug mode" OFF) diff --git a/I2Csensors/Readme.md b/I2Csensors/Readme.md index c540232..d756c9b 100644 --- a/I2Csensors/Readme.md +++ b/I2Csensors/Readme.md @@ -1,6 +1,158 @@ Reading information from different T/P/H sensors ================================================ -Supported sensors: AHT10, AHT15, AHT21b, BMP180, BMP280, BME280, SI7005 +Supported sensors: AHT10, AHT15, AHT21b, BMP180, BMP280, BME280, SHT3x, SI7005 + + + + +| Sensor | Precision H/T/P | Address | Max speed | Comment | +|---------|-----------------|---------------|-----------|---------| +| AHT10 | 2%/0.3°/- | 0x38/0x39 | 400k | ADDR selects lowest address bit | +| AHT15 | 2%/0.3°/- | 0x38 | 400k | what is the difference from AHT10? | +| AHT21b | 3%/0.5°/- | 0x38 | 400k | | +| BMP180 | -/1°/12Pa | 0x77 | 3.4M | could also works by SPI | +| BME280 | 3%/1°/0.2Pa | 0x76/77 | 3.4M | SDO allows to select lowest I2C address bit; supports SPI | +| SHT30 | 3%/0.3°/- | 0x44/0x45 | 1M | SHT31 have higher humidity precision (2%); ADDR selects address lowest bit; hav ALERT pin | +| SI7005 | 4.5%/1°/- | 0x40 | 400k | ~CS can select sensor if several are on bus | +------------------------------------------------------------------- + + +## Install library + +1. Download: `git clone` or other way. +2. Create building directory: `mkdir mk`. Go into it: `cd mk`. +3. Run `cmake`: `cmake ..`. +4. Build and install: `make && su -c "make install"`. + +### Cmake options + +Marked options are ON by default: + +- [ ] DEBUG - compile in debug mode; +- [x] EXAMPLES - build also examples (they won't be installed, you can use them just in build dir). + +## How to use + +After installing library you can use it including `i2csensorsPTH.h` into your code and linking with `-l i2csensorsPTH`. +Also you can use `pkg-config` after installing library: + +``` +pkg-config --libs --cflags i2csensorsPTH + +``` + + +### Base types + +#### `sensor_status_t` + +Status of given sensor. `SENS_NOTINIT` means that you should init device; also if you get `SENS_ERR` you should try to reinit it. +Receiving error on init function means that there's troubles on the bus or with sensor. + +``` +typedef enum{ + SENS_NOTINIT, // wasn't inited + SENS_BUSY, // measurement in progress + SENS_ERR, // error occured + SENS_RELAX, // do nothing + SENS_RDY, // data ready - can get it +} sensor_status_t; +``` + +#### `sensor_props_t` + +Properties: if the corresponding field sets, the device have this ability. `flags` allows to use all together as bit fields. + +``` +typedef union{ + struct{ + uint8_t T : 1; // can temperature (degC) + uint8_t H : 1; // can humidity (percent) + uint8_t P : 1; // can pressure (hPa) + uint8_t htr : 1; // have heater + }; + uint32_t flags; +} sensor_props_t; +``` + +#### `sensor_data_t` + +Gathered data. The fields that are zeroed in sensor's properties are undefined. + +``` +typedef struct{ + double T; // temperature, degC + double H; // humidity, percents + double P; // pressure, hPa +} sensor_data_t; +``` + +### Functions + +#### `int sensors_open(const char *dev)` + +Open I2C device by path `dev`. Returns `TRUE` if all OK. + +#### `void sensors_close()` + +Close I2C device. + +#### `char *sensors_list()` + +Returns allocated string with comma-separated names of all supported sensors. Don't forget to `free` it later. + +#### `sensor_t* sensor_new(const char *name)` + +Search `name` in list of supported sensors and, if found, returns pointer to sensors structure. Returns `NULL` if sensor not failed or some error oqqured. + +#### `void sensor_delete(sensor_t **s)` + +Delete all memory, allocated for given sensor. + +#### `sensor_props_t sensor_properties(sensor_t *s)` + +Allows to check sensor's properties. + +#### `int sensor_init(sensor_t *s, uint8_t address)` + +Try to find given sensor on the bus and run initial procedures (like calibration and so on). The `address` argument shoul be zero to use default I2C address or non-zero for custom. +Returns `TRUE` if all OK. + +#### `int sensor_heater(sensor_t *s, int on)` + +Turn on (`on == 1`) or off (`on == 0`) sensor's heater (if sensor supported it). Returns `FALSE` if sensor don't support heater or some error occured during operations. + +#### `int sensor_start(sensor_t *s)` + +Start measurement process. While measuring, you should poll sensor until data would be ready (or you get timeout error). + +#### `sensor_status_t sensor_process(sensor_t *s)` + +Polling sensor and gathering all data in simple finite-state machine. Checks if sensor is still busy and asks for data portion on each measuring stage. +Returns current sensor's state. If you get `SENS_RDY`, you can ask for data. + +#### `int sensor_getdata(sensor_t *s, sensor_data_t *d)` + +Get data into your variable `d`. Returns `FALSE` if data isn't ready (e.g. you didn't run `start` or sensor is still measuring). + +### I2C functions + +Of course, you can wish to work with I2C directly (e.g. to switch multiplexer's channel and so on), so here are some usefull functions. + +#### `int sensor_writeI2C(uint8_t addr, uint8_t *data, int len)` + +Write `data` array with len of `len` bytes with device address `addr`. Returns `FALSE` if failed. + +#### `int sensor_readI2C(uint8_t addr, uint8_t *data, int len)` + +Read `len` bytes of `data` from address `addr`. Returns `FALSE` if failed. + +#### `int sensor_readI2Cregs(uint8_t addr, uint8_t regaddr, uint16_t N, uint8_t *data)` + +Read content of `N` 8-bit registers starting from `regaddr` to array `data`. Returns `FALSE` if failed. + +#### `int sensor_writeI2Creg(uint8_t addr, uint8_t regaddr, uint8_t data)` + +Write `data` to single register `regaddr`. Returns `FALSE` if failed. -TODO: write at least shallow doc! \ No newline at end of file diff --git a/I2Csensors/common_table b/I2Csensors/common_table index e2c8ab4..4fc6b73 100644 --- a/I2Csensors/common_table +++ b/I2Csensors/common_table @@ -12,3 +12,5 @@ HTU32d = 2%/0.2 SHT30 = 3%/0.3œ/- = 0x44/0x45 = 1M = Õ SHT31 ÚÁÑ×ÌÅÎÎÁÑ ÔÏÞÎÏÓÔØ ÐÏ ×ÌÁÖÎÏÓÔÉ: 2%; ÍÌÁÄÛÉÊ ÂÉÔ ÁÄÒÅÓÁ ×ÙÂÉÒÁÅÔÓÑ ÎÏÇÏÊ ADDR; ÅÓÔØ ÐÒÏÇÒÁÍÍÉÒÕÅÍÁÑ ÎÏÇÁ ALERT! SHT4x = * = 0x44/0x45** (*) 40: 2-4%/>0.2-0.4œ, 41: 2-2.5%/0.2-0.4œ, 45: 1-2%/0.1-0.3œ; (**) ÁÄÒÅÓ ÚÁ×ÉÓÉÔ ÏÔ ÍÁÒËÉÒÏ×ËÉ (A/B); ÅÓÔØ ÎÁÇÒÅ×ÁÔÅÌØ; ÅÓÔØ ËÏÍÁÎÄÁ reset (0x06) ÐÏ ÁÄÒÅÓÕ 0 SI7005 = 4.5%/1œ/- = 0x40 = 400k = ×ÏÚÍÏÖÅÎ ×ÙÂÏÒ Ó ÐÏÍÏÝØÀ ÎÏÇÉ ~CS + +SHT85? \ No newline at end of file diff --git a/I2Csensors/examples/logmany.c b/I2Csensors/examples/logmany.c index 564b6d3..5a6e1e3 100644 --- a/I2Csensors/examples/logmany.c +++ b/I2Csensors/examples/logmany.c @@ -79,7 +79,7 @@ typedef struct{ } sd_t; // amount of all sensors connected -#define SENSORS_AMOUNT 7 +#define SENSORS_AMOUNT 8 // list of sensors - must be sorted by channel number static sd_t all_sensors[SENSORS_AMOUNT] = { @@ -87,7 +87,8 @@ static sd_t all_sensors[SENSORS_AMOUNT] = { {.name = "SI7005", .type = "SI7005", .nch = 0}, {.name = "AHT10", .type = "AHT10", .nch = 1}, {.name = "BMP180", .type = "BMP180", .nch = 1}, - {.name = "BME280", .type = "BME280", .nch = 1}, + {.name = "BME280a", .type = "BME280", .nch = 1}, + {.name = "BME280b", .type = "BME280", .nch = 2}, {.name = "AHT21b", .type = "AHT21", .nch = 2}, {.name = "SHT30", .type = "SHT3x", .nch = 2}, }; @@ -154,9 +155,9 @@ static void writedata(uint8_t *got){ if(sp.P){ ++NP; fprintf(plogf, "%.2f\t", got[i] ? (G.presmm ? D.P * 0.750062 : D.P) : nan); } } DBG("Measured: %d T, %d H and %d P", NT, NH, NP); - if(NT) fprintf(tlogf, "\n"); - if(NH) fprintf(hlogf, "\n"); - if(NP) fprintf(plogf, "\n"); + if(NT){ fprintf(tlogf, "\n"); fflush(tlogf); } + if(NH){ fprintf(hlogf, "\n"); fflush(hlogf); } + if(NP){ fprintf(plogf, "\n"); fflush(plogf); } } static void startlogs(){ diff --git a/I2Csensors/examples/single_sensor.c b/I2Csensors/examples/single_sensor.c index 4428e91..8e4a60a 100644 --- a/I2Csensors/examples/single_sensor.c +++ b/I2Csensors/examples/single_sensor.c @@ -83,7 +83,7 @@ int main(int argc, char **argv){ if(G.list){ char *l = sensors_list(); green("\nSupported sensors:\n"); - printf(l); printf("\n\n"); + printf("%s\n\n", l); FREE(l); return 0; } diff --git a/I2Csensors/i2csensorsPTH.pc.in b/I2Csensors/i2csensorsPTH.pc.in index 78ca3c3..d3b4ab9 100644 --- a/I2Csensors/i2csensorsPTH.pc.in +++ b/I2Csensors/i2csensorsPTH.pc.in @@ -6,5 +6,5 @@ includedir=${prefix}/include Name: @PROJ@ Description: I2C library working with pressure/temperature/humidity sensors Version: @VERSION@ -Libs: -L${libdir} -l@PROJLIB@ +Libs: -L${libdir} -l@PROJ@ Cflags: -I${includedir}