+ version 0.1.04

+ smaller footprint
This commit is contained in:
rob tillaart 2015-03-06 14:26:17 +01:00
parent 411126e5a9
commit 677aaeb463
2 changed files with 14 additions and 9 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: AnalogPin.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// VERSION: 0.1.04
// DATE: 2014-10-05
// PURPOSE: wrapper class for analogRead
//
@ -9,22 +9,24 @@
// 0.1.00 - 2013-09-09 initial version
// 0.1.01 - 2013-11-09 added some comments
// 0.1.02 - 2014-10-05 changed signatures datatypes
// 0.1.03 - 2014-12-07 some refactor
// 0.1.04 - 2015-03-06 refactor smaller footprint
//
// Released to the public domain
//
#include "AnalogPin.h"
AnalogPin::AnalogPin(uint8_t pin)
AnalogPin::AnalogPin(const uint8_t pin)
{
_pin = pin;
_prevValue = analogRead(pin);
}
int AnalogPin::read(uint8_t noise)
int AnalogPin::read(const uint8_t noise)
{
int value = analogRead(_pin);
if (noise == 0 || abs(value - _prevValue) > noise)
if (noise == 0 || ((value - _prevValue) & 0x7FFF) > noise)
{
_prevValue = value;
}
@ -33,9 +35,12 @@ int AnalogPin::read(uint8_t noise)
int AnalogPin::readSmoothed(uint8_t alpha)
{
alpha = constrain(alpha, 0, 31);
if (alpha > 31) alpha = 31;
int value = analogRead(_pin);
value = (alpha*_prevValue + (32-alpha)*value)/32;
if (alpha > 0)
{
value = value + (alpha * (_prevValue - value)) / 32;
}
_prevValue = value;
return value;
}

View File

@ -1,7 +1,7 @@
//
// FILE: AnalogPin.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// VERSION: 0.1.04
// DATE: 2014-10-05
// PURPOSE: wrapper class for analogRead
// URL:
@ -18,12 +18,12 @@
#include "WProgram.h"
#endif
#define ANALOGPIN_LIB_VERSION "0.1.02"
#define ANALOGPIN_LIB_VERSION "0.1.04"
class AnalogPin
{
public:
AnalogPin(uint8_t);
AnalogPin(const uint8_t);
// noise 0..255; in practice 0..4
int read(uint8_t noise = 0);