L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
amb_tsl2591.h
Go to the documentation of this file.
1/*
2 * MT 23.12.2025
3 * Ambient21 click uses chip TSL2591 (TSL2591.pdf), https://cdn-shop.adafruit.com/datasheets/TSL25911_Datasheet_EN_v1.pdf
4 * Direct reading from I2C, code generated using Gemini: https://gemini.google.com/share/132a2f796ac4
5 * I won't use the library from microE, this seems easier
6 * I2C address 0x29
7 *
8 * Reading from the light sensor happens progressively, there is so-called auto-calibration of the sensor. If too large value is read
9 * the reading time is shortened. Conversely, in a dark environment the reading time is extended to provide information in LUX.
10 * During the time until calibration is complete, ambient_ReadLux returns HAL_BUSY, and must wait for next reading.
11 *
12 * The sensor needs to be explicitly turned on and then off to save power consumption
13 *
14 */
15
16#ifndef __AMBIENT_21__
17#define __AMBIENT_21__
18
19#include "mysensors_def.h"
20
21#ifdef SENSOR_AMB_TSL2591
22
23
24#include "stm32wlxx_hal.h"
25
26#pragma pack(1)
27/**
28 * @brief Measurement data produced by the TSL2591 ambient light sensor.
29 * Populated by amb_tsl2591_Read(); check IsDataValid before using Lux.
30 */
31typedef struct
32{
33 float Lux; /**< Illuminance in lux */
34 int8_t IsDataValid; /**< 1 – value is valid; 0 – last read failed or sensor not yet calibrated */
36#pragma pack()
37
38/**
39 * @brief Live measurement data from the TSL2591 sensor; updated by amb_tsl2591_Read().
40 */
42/**
43 * @brief Snapshot copy of the last completed TSL2591 measurement; used for LoRaWAN transmission.
44 */
46
47/**
48 * @brief - check if light sensor is present
49 * @param tryInit - in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no
50 * @retval 1 - is present, 0 - is not
51 */
52int8_t amb_tsl2591_Is(I2C_HandleTypeDef *hi2c, int8_t tryInit);
53
54/**
55 * @brief initialization of light sensor, and check if sensor is present or not
56 * Subsequently the sensor is turned off so it doesn't run unnecessarily
57 * @retval HAL_OK - sensor is present, HAL_ERROR
58 */
59HAL_StatusTypeDef amb_tsl2591_Init(I2C_HandleTypeDef *hi2c);
60
61/**
62 * @brief check if sensor is turned on or not
63 * @param onOff - on output contains 1-on 0-off, but only if status is HAL_OK
64 * @retval HAL_OK - onOff contains sensor state, HAL_ERROR
65 */
66HAL_StatusTypeDef amb_tsl2591_IsOn(I2C_HandleTypeDef *hi2c, uint8_t *onOff);
67
68/**
69 * @brief turn on sensor, read value 4x to automatically configure the sensor.
70 * @retval HAL_OK, HAL_ERROR
71 */
72HAL_StatusTypeDef amb_tsl2591_On(I2C_HandleTypeDef *hi2c);
73
74/**
75 * @brief turn off sensor
76 * @retval HAL_OK, HAL_ERROR
77 */
78HAL_StatusTypeDef amb_tsl2591_Off(I2C_HandleTypeDef *hi2c);
79
80/**
81 * @brief read value from sensor, the value is in _ambientData
82 * @retval
83 * HAL_OK - have data,
84 * HAL_BUSY - next reading in progress,
85 * HAL_TIMEOUT - sensor is not turned on
86 * HAL_ERROR - error
87 */
88HAL_StatusTypeDef amb_tsl2591_Read(I2C_HandleTypeDef *hi2c);
89
90/**
91 * @brief log data to buffer
92 */
93void amb_tsl2591_LogData(char* buf);
94
95#endif
96
97#endif
void amb_tsl2591_LogData(char *buf)
log data to buffer
amb_tsl2591_t _bck_amb_tsl2591Data
Snapshot copy of the last completed TSL2591 measurement; used for LoRaWAN transmission.
Definition amb_tsl2591.c:33
amb_tsl2591_t _amb_tsl2591Data
Live measurement data from the TSL2591 sensor; updated by amb_tsl2591_Read().
Definition amb_tsl2591.c:32
int8_t amb_tsl2591_Is(I2C_HandleTypeDef *hi2c, int8_t tryInit)
check if light sensor is present
Definition amb_tsl2591.c:35
HAL_StatusTypeDef amb_tsl2591_Read(I2C_HandleTypeDef *hi2c)
read value from sensor, the value is in _ambientData
HAL_StatusTypeDef amb_tsl2591_Off(I2C_HandleTypeDef *hi2c)
turn off sensor
HAL_StatusTypeDef amb_tsl2591_On(I2C_HandleTypeDef *hi2c)
turn on sensor, read value 4x to automatically configure the sensor.
Definition amb_tsl2591.c:57
HAL_StatusTypeDef amb_tsl2591_Init(I2C_HandleTypeDef *hi2c)
initialization of light sensor, and check if sensor is present or not Subsequently the sensor is turn...
HAL_StatusTypeDef amb_tsl2591_IsOn(I2C_HandleTypeDef *hi2c, uint8_t *onOff)
check if sensor is turned on or not
Definition amb_tsl2591.c:42
Measurement data produced by the TSL2591 ambient light sensor. Populated by amb_tsl2591_Read(); check...
Definition amb_tsl2591.h:32
int8_t IsDataValid
Definition amb_tsl2591.h:34