Skip to content

File syn_sensor.h

FileList > sensor > syn_sensor.h

Go to the source code of this file

Sensor polling framework — periodic read → filter → threshold → event. More...

  • #include "../common/syn_defs.h"
  • #include "../dsp/syn_filter.h"
  • #include "../dsp/syn_signal.h"
  • #include "../port/syn_port_system.h"
  • #include "../util/syn_hysteresis.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_Sensor
Sensor descriptor — owns the full read→filter→threshold pipeline.

Public Types

Type Name
enum SYN_SensorFilterType
Filter type selector for sensor pipeline.
typedef int16_t(* SYN_SensorReadFunc
Read function — returns the raw sensor value.
typedef void(* SYN_SensorThresholdCallback
Threshold callback — called when threshold is crossed.

Public Functions

Type Name
void syn_sensor_clear_filter (SYN_Sensor * sensor)
Remove any attached filter.
void syn_sensor_clear_threshold (SYN_Sensor * sensor)
Disable threshold monitoring.
void syn_sensor_enable (SYN_Sensor * sensor, bool enable)
Enable or disable polling.
void syn_sensor_init (SYN_Sensor * sensor, const char * name, SYN_SensorReadFunc read_func, void * ctx)
Initialize a sensor.
int16_t syn_sensor_read_now (SYN_Sensor * sensor)
Force an immediate reading (ignoring interval).
void syn_sensor_service (SYN_Sensor * sensors, size_t count)
Service an array of sensors (calls syn_sensor_update on each).
void syn_sensor_set_filter_ema (SYN_Sensor * sensor, SYN_FilterEMA * f)
Attach an exponential moving-average filter.
void syn_sensor_set_filter_ma (SYN_Sensor * sensor, SYN_FilterMA * f)
Attach a moving-average filter.
void syn_sensor_set_filter_median (SYN_Sensor * sensor, SYN_FilterMedian * f)
Attach a median filter.
void syn_sensor_set_interval (SYN_Sensor * sensor, uint32_t interval_ms)
Set polling interval in milliseconds.
void syn_sensor_set_stats (SYN_Sensor * sensor, SYN_Signal * stats)
Attach a signal statistics window.
void syn_sensor_set_threshold (SYN_Sensor * sensor, int32_t threshold, int32_t band, SYN_SensorThresholdCallback on_high, SYN_SensorThresholdCallback on_low, void * ctx)
Set a threshold with hysteresis and callbacks.
bool syn_sensor_update (SYN_Sensor * sensor)
Update the sensor — poll if interval elapsed, filter, check threshold.

Public Static Functions

Type Name
int16_t syn_sensor_raw (const SYN_Sensor * sensor)
Get the last raw value.
int16_t syn_sensor_value (const SYN_Sensor * sensor)
Get the last filtered value.

Detailed Description

Wires up the full sensor pipeline: poll at an interval, filter the reading, compare against thresholds (with hysteresis), and fire callbacks or set event flags.

** **

static SYN_Sensor temp_sensor;
static SYN_FilterEMA temp_filter;
syn_filter_ema_init(&temp_filter, 64);

syn_sensor_init(&temp_sensor, "temp", read_temperature, NULL);
syn_sensor_set_interval(&temp_sensor, 1000);      // poll every 1s
syn_sensor_set_filter_ema(&temp_sensor, &temp_filter);
syn_sensor_set_threshold(&temp_sensor, 8000, 500,  // 80.00°C ± 5°C
                           on_temp_high, on_temp_low, NULL);

// In main loop:
syn_sensor_update(&temp_sensor);

Public Types Documentation

enum SYN_SensorFilterType

Filter type selector for sensor pipeline.

enum SYN_SensorFilterType {
    SYN_SENSOR_FILTER_NONE = 0,
    SYN_SENSOR_FILTER_MA = 1,
    SYN_SENSOR_FILTER_EMA = 2,
    SYN_SENSOR_FILTER_MEDIAN = 3
};


typedef SYN_SensorReadFunc

Read function — returns the raw sensor value.

typedef int16_t(* SYN_SensorReadFunc) (void *ctx);

Parameters:

  • ctx User context pointer.

Returns:

Raw sensor reading.


typedef SYN_SensorThresholdCallback

Threshold callback — called when threshold is crossed.

typedef void(* SYN_SensorThresholdCallback) (struct SYN_Sensor *sensor, int16_t value, void *ctx);

Parameters:

  • sensor The sensor that fired.
  • value Current filtered value.
  • ctx User context pointer.

Public Functions Documentation

function syn_sensor_clear_filter

Remove any attached filter.

void syn_sensor_clear_filter (
    SYN_Sensor * sensor
) 

Parameters:

  • sensor Sensor.

function syn_sensor_clear_threshold

Disable threshold monitoring.

void syn_sensor_clear_threshold (
    SYN_Sensor * sensor
) 

Parameters:

  • sensor Sensor.

function syn_sensor_enable

Enable or disable polling.

void syn_sensor_enable (
    SYN_Sensor * sensor,
    bool enable
) 

Parameters:

  • sensor Sensor.
  • enable true to enable, false to disable.

function syn_sensor_init

Initialize a sensor.

void syn_sensor_init (
    SYN_Sensor * sensor,
    const char * name,
    SYN_SensorReadFunc read_func,
    void * ctx
) 

Parameters:

  • sensor Sensor instance.
  • name Human-readable name (for logging).
  • read_func Function that reads the sensor hardware.
  • ctx Context passed to read_func.

function syn_sensor_read_now

Force an immediate reading (ignoring interval).

int16_t syn_sensor_read_now (
    SYN_Sensor * sensor
) 

Parameters:

  • sensor Sensor.

Returns:

The filtered value.


function syn_sensor_service

Service an array of sensors (calls syn_sensor_update on each).

void syn_sensor_service (
    SYN_Sensor * sensors,
    size_t count
) 

Parameters:

  • sensors Array of sensors.
  • count Number of sensors.

function syn_sensor_set_filter_ema

Attach an exponential moving-average filter.

void syn_sensor_set_filter_ema (
    SYN_Sensor * sensor,
    SYN_FilterEMA * f
) 

Parameters:

  • sensor Sensor.
  • f Initialized EMA filter.

function syn_sensor_set_filter_ma

Attach a moving-average filter.

void syn_sensor_set_filter_ma (
    SYN_Sensor * sensor,
    SYN_FilterMA * f
) 

Parameters:

  • sensor Sensor.
  • f Initialized MA filter.

function syn_sensor_set_filter_median

Attach a median filter.

void syn_sensor_set_filter_median (
    SYN_Sensor * sensor,
    SYN_FilterMedian * f
) 

Parameters:

  • sensor Sensor.
  • f Initialized median filter.

function syn_sensor_set_interval

Set polling interval in milliseconds.

void syn_sensor_set_interval (
    SYN_Sensor * sensor,
    uint32_t interval_ms
) 

Parameters:

  • sensor Sensor.
  • interval_ms Poll interval.

function syn_sensor_set_stats

Attach a signal statistics window.

void syn_sensor_set_stats (
    SYN_Sensor * sensor,
    SYN_Signal * stats
) 

Each update pushes the filtered value into the signal window, giving you min/max/mean/variance/delta automatically.

Parameters:

  • sensor Sensor.
  • stats Initialized SYN_Signal instance, or NULL to detach.

function syn_sensor_set_threshold

Set a threshold with hysteresis and callbacks.

void syn_sensor_set_threshold (
    SYN_Sensor * sensor,
    int32_t threshold,
    int32_t band,
    SYN_SensorThresholdCallback on_high,
    SYN_SensorThresholdCallback on_low,
    void * ctx
) 

Parameters:

  • sensor Sensor.
  • threshold Center threshold value.
  • band Hysteresis half-width.
  • on_high Called when value crosses above (threshold + band).
  • on_low Called when value drops below (threshold - band).
  • ctx Context for callbacks.

function syn_sensor_update

Update the sensor — poll if interval elapsed, filter, check threshold.

bool syn_sensor_update (
    SYN_Sensor * sensor
) 

Call from your main loop or scheduler task.

Parameters:

  • sensor Sensor.

Returns:

true if a new reading was taken this call.


Public Static Functions Documentation

function syn_sensor_raw

Get the last raw value.

static inline int16_t syn_sensor_raw (
    const SYN_Sensor * sensor
) 

Parameters:

  • sensor Sensor.

Returns:

Raw reading.


function syn_sensor_value

Get the last filtered value.

static inline int16_t syn_sensor_value (
    const SYN_Sensor * sensor
) 

Parameters:

  • sensor Sensor.

Returns:

Filtered reading.



The documentation for this class was generated from the following file src/syntropic/sensor/syn_sensor.h