2012-07-28 12:12:35 +02:00
|
|
|
/*
|
|
|
|
* Analogue multi-position switch with hysteresis.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Analogue multi-position switch with hysteresis.
|
|
|
|
*/
|
2013-12-27 21:35:37 +01:00
|
|
|
class Switch {
|
|
|
|
public:
|
|
|
|
struct Fixed {
|
2012-07-28 12:12:35 +02:00
|
|
|
/**
|
|
|
|
* The mid point for each position. Returns the closest.
|
|
|
|
* Add fake out of range values at zero and N-1 for out of
|
|
|
|
* range detection.
|
|
|
|
*/
|
|
|
|
int16_t levels[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
Switch();
|
|
|
|
|
|
|
|
/** Update the switch level, returning the current position. */
|
|
|
|
int update(const Fixed& fixed, uint16_t level);
|
|
|
|
|
|
|
|
/** Returns the current switch position. */
|
|
|
|
int position() const { return position_; }
|
|
|
|
|
2013-12-27 21:35:37 +01:00
|
|
|
private:
|
2012-07-28 12:12:35 +02:00
|
|
|
static const int Hysteresis = 200;
|
|
|
|
|
|
|
|
int8_t position_;
|
|
|
|
};
|