Envorimental Monitoring
Loading...
Searching...
No Matches
i2c_core.h File Reference

Sensor-agnostic I2C read abstraction layer. More...

#include "stdint.h"
#include "stm32f4xx_hal.h"
#include "main.h"

Go to the source code of this file.

Enumerations

enum  sensor_t { HUMIDITY_SENSOR , PRESSURE_SENSOR , LIGHT_SENSOR , TEMPERATURE_SENSOR }
 Identifies the physical quantity to be read from a sensor. More...

Functions

float i2c_sensor_read (uint8_t device_address, sensor_t sensor_type, void *p_handle)
 Reads a single measurement from the specified I2C sensor.
uint8_t I2C_ScanDeviceAddress (I2C_HandleTypeDef *hi2c)
 Scans the I2C bus for the first responding device.

Detailed Description

Sensor-agnostic I2C read abstraction layer.

Provides a unified interface for reading from the three supported I2C sensors (AHT20, BMP180, BH1750) through a single function call. The caller identifies the desired measurement with a sensor_t tag; the implementation casts the generic handle pointer to the correct sensor driver type and invokes the appropriate driver read function.

Also exposes a bus-scan helper that can locate the first responding device on an I2C bus — useful during bring-up and debugging.

Author
Şule Nur Demirdaş
Date
April 2026

Enumeration Type Documentation

◆ sensor_t

enum sensor_t

Identifies the physical quantity to be read from a sensor.

Passed to i2c_sensor_read() together with the matching sensor handle so that the function can dispatch to the correct driver and extract the right field from the result.

Enumerator
HUMIDITY_SENSOR 

Relative humidity in % RH (AHT20).

PRESSURE_SENSOR 

Atmospheric pressure in Pa (BMP180).

LIGHT_SENSOR 

Illuminance in lux (BH1750).

TEMPERATURE_SENSOR 

Ambient temperature in °C (AHT20).

Function Documentation

◆ I2C_ScanDeviceAddress()

uint8_t I2C_ScanDeviceAddress ( I2C_HandleTypeDef * hi2c)

Scans the I2C bus for the first responding device.

Iterates through all 128 possible 7-bit addresses and returns the first one that acknowledges HAL_I2C_IsDeviceReady(). Intended as a diagnostic helper during hardware bring-up.

Parameters
[in]hi2cPointer to the initialised I2C peripheral handle.
Returns
7-bit address (0x00–0x7F) of the first responding device, or 0xFF if no device is found.

◆ i2c_sensor_read()

float i2c_sensor_read ( uint8_t device_address,
sensor_t sensor_type,
void * p_handle )

Reads a single measurement from the specified I2C sensor.

The function triggers a sensor read via the appropriate driver, then returns the requested physical quantity as a float. On error -1.0f is returned.

Parameters
[in]device_address8-bit I2C address of the target device (left-shifted by 1 as required by HAL).
[in]sensor_typeThe physical quantity to retrieve; must match the sensor pointed to by p_handle.
[in]p_handleVoid pointer to the sensor driver handle (AHT20_HandleTypeDef*, BMP180_HandleTypeDef*, or BH1750_HandleTypeDef*).
Returns
Measured value as a float, or -1.0f on failure.