Skip to content

File syn_hysteresis.h

File List > src > syntropic > util > syn_hysteresis.h

Go to the documentation of this file

#ifndef SYN_HYSTERESIS_H
#define SYN_HYSTERESIS_H

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    int32_t threshold; 
    int32_t band;      
    bool state;        
} SYN_Hysteresis;

static inline void syn_hyst_init(SYN_Hysteresis *h, int32_t threshold, int32_t band, bool initial)
{
    h->threshold = threshold;
    h->band = band;
    h->state = initial;
}

static inline bool syn_hyst_update(SYN_Hysteresis *h, int32_t value)
{
    if (h->state) {
        /* Currently high — switch low only if value drops below (threshold - band) */
        if (value < h->threshold - h->band) {
            h->state = false;
        }
    } else {
        /* Currently low — switch high only if value rises above (threshold + band) */
        if (value > h->threshold + h->band) {
            h->state = true;
        }
    }
    return h->state;
}

static inline bool syn_hyst_state(const SYN_Hysteresis *h)
{
    return h->state;
}

static inline void syn_hyst_set(SYN_Hysteresis *h, int32_t threshold, int32_t band)
{
    h->threshold = threshold;
    h->band = band;
}

#ifdef __cplusplus
}
#endif

#endif /* SYN_HYSTERESIS_H */