L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
amb_tsl2591.c File Reference
#include "amb_tsl2591.h"
#include "i2c.h"
#include "utils/mydefs.h"
#include "mymems.h"
#include <stdio.h>
Include dependency graph for amb_tsl2591.c:

Go to the source code of this file.

Macros

#define AMBIENT_ADDR   (0x29 << 1)
#define TSL2591_COMMAND   0xA0
#define REG_ENABLE   0x00
#define REG_CONFIG   0x01
#define REG_C0DATAL   0x14
#define REG_C1DATAL   0x16
#define THRESH_MAX   60000
#define THRESH_MIN   500

Enumerations

enum  TSL2591_Gain_t { TSL2591_GAIN_LOW = 0x00 , TSL2591_GAIN_MED = 0x10 , TSL2591_GAIN_HIGH = 0x20 , TSL2591_GAIN_MAX = 0x30 }

Functions

int8_t amb_tsl2591_Is (I2C_HandleTypeDef *hi2c, int8_t tryInit)
 
  • check if light sensor is present

HAL_StatusTypeDef amb_tsl2591_IsOn (I2C_HandleTypeDef *hi2c, uint8_t *onOff)
 check if sensor is turned on or not
HAL_StatusTypeDef amb_tsl2591_On (I2C_HandleTypeDef *hi2c)
 turn on sensor, read value 4x to automatically configure the sensor.
HAL_StatusTypeDef amb_tsl2591_Off (I2C_HandleTypeDef *hi2c)
 turn off sensor
HAL_StatusTypeDef amb_tsl2591_Init (I2C_HandleTypeDef *hi2c)
 initialization of light sensor, and check if sensor is present or not Subsequently the sensor is turned off so it doesn't run unnecessarily
static HAL_StatusTypeDef ambient_AdjustGain (I2C_HandleTypeDef *hi2c, uint16_t rawCH0)
 This function checks the raw channel 0 value. If it is near the 16-bit limit (65535), it drops the gain. If it is too low, it boosts it.
HAL_StatusTypeDef amb_tsl2591_Read (I2C_HandleTypeDef *hi2c)
 When calculating Lux, you must use the actual multiplier corresponding to the currentGain.
void amb_tsl2591_LogData (char *buf)
 log data to buffer

Variables

static TSL2591_Gain_t _currentGain = TSL2591_GAIN_MED
static int8_t _isAmbientSensor = 0
amb_tsl2591_t _amb_tsl2591Data = { }
 Live measurement data from the TSL2591 sensor; updated by amb_tsl2591_Read().
amb_tsl2591_t _bck_amb_tsl2591Data = {}
 Snapshot copy of the last completed TSL2591 measurement; used for LoRaWAN transmission.

Macro Definition Documentation

◆ AMBIENT_ADDR

#define AMBIENT_ADDR   (0x29 << 1)

◆ REG_C0DATAL

#define REG_C0DATAL   0x14

Definition at line 14 of file amb_tsl2591.c.

Referenced by amb_tsl2591_Read().

◆ REG_C1DATAL

#define REG_C1DATAL   0x16

Definition at line 15 of file amb_tsl2591.c.

◆ REG_CONFIG

#define REG_CONFIG   0x01

Definition at line 13 of file amb_tsl2591.c.

Referenced by amb_tsl2591_On(), and ambient_AdjustGain().

◆ REG_ENABLE

#define REG_ENABLE   0x00

Definition at line 12 of file amb_tsl2591.c.

Referenced by amb_tsl2591_IsOn(), amb_tsl2591_Off(), and amb_tsl2591_On().

◆ THRESH_MAX

#define THRESH_MAX   60000

Definition at line 17 of file amb_tsl2591.c.

Referenced by amb_tsl2591_Read(), and ambient_AdjustGain().

◆ THRESH_MIN

#define THRESH_MIN   500

Definition at line 18 of file amb_tsl2591.c.

Referenced by ambient_AdjustGain().

◆ TSL2591_COMMAND

#define TSL2591_COMMAND   0xA0

Enumeration Type Documentation

◆ TSL2591_Gain_t

Enumerator
TSL2591_GAIN_LOW 
TSL2591_GAIN_MED 
TSL2591_GAIN_HIGH 
TSL2591_GAIN_MAX 

Definition at line 21 of file amb_tsl2591.c.

Function Documentation

◆ amb_tsl2591_Init()

HAL_StatusTypeDef amb_tsl2591_Init ( I2C_HandleTypeDef * hi2c)

initialization of light sensor, and check if sensor is present or not Subsequently the sensor is turned off so it doesn't run unnecessarily

Return values
HAL_OK- sensor is present, HAL_ERROR

Definition at line 121 of file amb_tsl2591.c.

References _isAmbientSensor, amb_tsl2591_Off(), amb_tsl2591_On(), AMBIENT_ADDR, and I2C_IsDeviceReadyMT().

Referenced by amb_tsl2591_Is().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ amb_tsl2591_Is()

int8_t amb_tsl2591_Is ( I2C_HandleTypeDef * hi2c,
int8_t tryInit )

  • check if light sensor is present

Parameters
tryInit- in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no
Return values
1- is present, 0 - is not

Definition at line 35 of file amb_tsl2591.c.

References _isAmbientSensor, and amb_tsl2591_Init().

Here is the call graph for this function:

◆ amb_tsl2591_IsOn()

HAL_StatusTypeDef amb_tsl2591_IsOn ( I2C_HandleTypeDef * hi2c,
uint8_t * onOff )

check if sensor is turned on or not

Parameters
onOff- on output contains 1-on 0-off, but only if status is HAL_OK
Return values
HAL_OK- onOff contains sensor state, HAL_ERROR

Definition at line 42 of file amb_tsl2591.c.

References _isAmbientSensor, AMBIENT_ADDR, REG_ENABLE, and TSL2591_COMMAND.

Referenced by amb_tsl2591_Read().

Here is the caller graph for this function:

◆ amb_tsl2591_LogData()

void amb_tsl2591_LogData ( char * buf)

log data to buffer

Definition at line 248 of file amb_tsl2591.c.

References _amb_tsl2591Data, PRIf_02, and PRIf_02D.

◆ amb_tsl2591_Off()

HAL_StatusTypeDef amb_tsl2591_Off ( I2C_HandleTypeDef * hi2c)

turn off sensor

Return values
HAL_OK,HAL_ERROR

Definition at line 101 of file amb_tsl2591.c.

References _isAmbientSensor, AMBIENT_ADDR, REG_ENABLE, and TSL2591_COMMAND.

Referenced by amb_tsl2591_Init().

Here is the caller graph for this function:

◆ amb_tsl2591_On()

HAL_StatusTypeDef amb_tsl2591_On ( I2C_HandleTypeDef * hi2c)

turn on sensor, read value 4x to automatically configure the sensor.

Return values
HAL_OK,HAL_ERROR

Definition at line 57 of file amb_tsl2591.c.

References _amb_tsl2591Data, _currentGain, _isAmbientSensor, _memsMainBlock, amb_tsl2591_Read(), AMBIENT_ADDR, mems_WriteMainBlock(), REG_CONFIG, REG_ENABLE, TSL2591_COMMAND, and TSL2591_GAIN_MED.

Referenced by amb_tsl2591_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ amb_tsl2591_Read()

HAL_StatusTypeDef amb_tsl2591_Read ( I2C_HandleTypeDef * hi2c)

When calculating Lux, you must use the actual multiplier corresponding to the currentGain.

read value from sensor, the value is in _ambientData

Definition at line 179 of file amb_tsl2591.c.

References _amb_tsl2591Data, _currentGain, _isAmbientSensor, amb_tsl2591_IsOn(), AMBIENT_ADDR, ambient_AdjustGain(), REG_C0DATAL, THRESH_MAX, TSL2591_COMMAND, TSL2591_GAIN_HIGH, TSL2591_GAIN_LOW, TSL2591_GAIN_MAX, and TSL2591_GAIN_MED.

Referenced by amb_tsl2591_On().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ambient_AdjustGain()

HAL_StatusTypeDef ambient_AdjustGain ( I2C_HandleTypeDef * hi2c,
uint16_t rawCH0 )
static

This function checks the raw channel 0 value. If it is near the 16-bit limit (65535), it drops the gain. If it is too low, it boosts it.

500)

Definition at line 142 of file amb_tsl2591.c.

References _currentGain, AMBIENT_ADDR, REG_CONFIG, THRESH_MAX, THRESH_MIN, TSL2591_COMMAND, TSL2591_GAIN_HIGH, TSL2591_GAIN_LOW, TSL2591_GAIN_MAX, and TSL2591_GAIN_MED.

Referenced by amb_tsl2591_Read().

Here is the caller graph for this function:

Variable Documentation

◆ _amb_tsl2591Data

amb_tsl2591_t _amb_tsl2591Data = { }

Live measurement data from the TSL2591 sensor; updated by amb_tsl2591_Read().

Definition at line 32 of file amb_tsl2591.c.

Referenced by amb_tsl2591_LogData(), amb_tsl2591_On(), amb_tsl2591_Read(), and sensors_CopyToBck().

◆ _bck_amb_tsl2591Data

amb_tsl2591_t _bck_amb_tsl2591Data = {}

Snapshot copy of the last completed TSL2591 measurement; used for LoRaWAN transmission.

Definition at line 33 of file amb_tsl2591.c.

Referenced by sensors_CayennelFromBckData(), sensors_CopyToBck(), sensors_ReadToBckFromDataBlock(), and sensors_WriteFromBckToDataBlock().

◆ _currentGain

TSL2591_Gain_t _currentGain = TSL2591_GAIN_MED
static

Definition at line 30 of file amb_tsl2591.c.

Referenced by amb_tsl2591_On(), amb_tsl2591_Read(), and ambient_AdjustGain().

◆ _isAmbientSensor

int8_t _isAmbientSensor = 0
static