GY-63_MS5611/libraries/AnalogPin/AnalogPin.h

60 lines
1.6 KiB
C
Raw Normal View History

2020-11-27 05:10:47 -05:00
#pragma once
//
// FILE: AnalogPin.h
// AUTHOR: Rob Tillaart
2021-10-17 13:20:48 -04:00
// VERSION: 0.2.5
2020-11-27 05:10:47 -05:00
// DATE: 2013-09-09
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
// URL: https://github.com/RobTillaart/AnalogPin
//
2021-10-17 13:20:48 -04:00
#include "Arduino.h"
2021-10-17 13:20:48 -04:00
#define ANALOGPIN_LIB_VERSION (F("0.2.5"))
class AnalogPin
{
public:
2021-01-29 06:31:58 -05:00
explicit AnalogPin(const uint8_t pin);
2020-11-27 05:10:47 -05:00
2021-01-29 06:31:58 -05:00
// prescale = { 2..7 }, imho 2 is bad, 3 is pretty noisy, 4 and 5 are acceptable, 6 and 7 are good. Depends on project!!!
// time indication per analogRead for different prescale values on UNO
// 2 => 14 uSec 5 => 38 uSec
// 3 => 18 uSec 6 => 63 uSec
// 4 => 24 uSec 7 => 120 uSec (default/normal)
2021-10-17 13:20:48 -04:00
void setPrescaler(const uint8_t prescale = 7);
2021-01-29 06:31:58 -05:00
inline uint8_t getPrescaler(void) const { return _prescale; };
2021-01-29 06:31:58 -05:00
// noise 0..255; in practice only small values are used (0..10).
inline void setNoiseThreshold(const uint8_t noise = 0) { _noise = noise; };
inline uint8_t getNoiseThreshold(void) const { return _noise; };
2020-11-27 05:10:47 -05:00
2021-01-29 06:31:58 -05:00
// alpha 0..31;
2021-10-17 13:20:48 -04:00
void setSmoothWeight(const uint8_t alpha = 0);
2021-01-29 06:31:58 -05:00
inline uint8_t getSmoothWeight(void) const { return _alpha; };
2021-01-29 06:31:58 -05:00
// set twice to true to do analogRead twice to reduce noise too
int read(const bool twice = false);
int readSmoothed();
2020-11-27 05:10:47 -05:00
2021-01-29 06:31:58 -05:00
// expose internal data as that might be useful.
inline int readPrevious(void) const { return _prevValue; }
inline int readLast(void) const { return _value; }
2020-11-27 05:10:47 -05:00
2021-10-17 13:20:48 -04:00
private:
void _rawRead();
2020-11-27 05:10:47 -05:00
2021-01-29 06:31:58 -05:00
uint8_t _pin;
uint8_t _alpha;
uint8_t _noise;
uint8_t _prescale;
int _value;
int _prevValue;
};
2020-11-27 05:10:47 -05:00
// -- END OF FILE --