Skip to content

Commit 5625713

Browse files
committed
0.2.5 AnalogPin
1 parent 695c60b commit 5625713

File tree

7 files changed

+90
-48
lines changed

7 files changed

+90
-48
lines changed

libraries/AnalogPin/.arduino-ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ compile:
22
# Choosing to run compilation tests on 2 different Arduino platforms
33
platforms:
44
- uno
5-
- leonardo
6-
- due
7-
- zero
5+
# - due
6+
# - zero
7+
# - leonardo
8+
- m4
9+
- esp32
10+
# - esp8266
11+
# - mega2560

libraries/AnalogPin/.github/workflows/arduino_test_runner.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ name: Arduino CI
44
on: [push, pull_request]
55

66
jobs:
7-
arduino_ci:
7+
runTest:
88
runs-on: ubuntu-latest
99

1010
steps:
1111
- uses: actions/checkout@v2
12-
- uses: Arduino-CI/action@master
13-
# Arduino-CI/[email protected]
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb

libraries/AnalogPin/AnalogPin.cpp

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,68 @@
11
//
22
// FILE: AnalogPin.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.4
4+
// VERSION: 0.2.5
55
// DATE: 2013-09-09
66
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
77
//
8-
// HISTORY:
9-
// 0.1.00 - 2013-09-09 initial version
10-
// 0.1.01 - 2013-11-09 added some comments
11-
// 0.1.02 - 2014-10-05 changed signatures datatypes
12-
// 0.1.03 - 2014-12-07 some refactor
13-
// 0.1.04 - 2015-03-06 refactor smaller footprint
14-
// 0.2.00 - 2015-05-14 added prescale support
15-
// 0.2.01 - 2015-12-19 breaking interface; big refactor;
16-
// 0.2.2 2020-03-25 refactor AVR specific code; bugfix
17-
// 0.2.3 2020-05-27 update library.json
18-
// 0.2.4 2020-12-10 add Arduino-ci
19-
//
20-
// Released to the public domain
21-
//
8+
// HISTORY:
9+
// 0.1.00 2013-09-09 initial version
10+
// 0.1.01 2013-11-09 added some comments
11+
// 0.1.02 2014-10-05 changed signatures data types
12+
// 0.1.03 2014-12-07 some refactor
13+
// 0.1.04 2015-03-06 refactor smaller footprint
14+
// 0.2.00 2015-05-14 added pre-scale support
15+
// 0.2.01 2015-12-19 breaking interface; big refactor;
16+
// 0.2.2 2020-03-25 refactor AVR specific code; bugfix
17+
// 0.2.3 2020-05-27 update library.json
18+
// 0.2.4 2020-12-10 add Arduino-ci
19+
// 0.2.5 2021-10-17 update Arduino-CI
20+
2221

2322
#include "AnalogPin.h"
2423

24+
2525
AnalogPin::AnalogPin(const uint8_t pin)
2626
{
2727
_pin = pin;
2828
_prescale = 7;
2929
_alpha = 0;
3030
_noise = 0;
31-
rawRead();
31+
_rawRead();
3232
_prevValue = _value;
3333
}
3434

35+
36+
void AnalogPin::setPrescaler(const uint8_t prescale)
37+
{
38+
_prescale = prescale;
39+
if (_prescale < 2) _prescale = 2;
40+
else if (_prescale > 7) _prescale = 7;
41+
};
42+
43+
44+
void AnalogPin::setSmoothWeight(const uint8_t alpha)
45+
{
46+
_alpha = alpha;
47+
if (_alpha > 31) _alpha = 31;
48+
};
49+
50+
3551
int AnalogPin::read(const bool twice)
3652
{
37-
if (twice) rawRead();
38-
rawRead();
53+
if (twice) _rawRead();
54+
_rawRead();
3955
if ( (_noise == 0) || (((_value - _prevValue) & 0x7FFF) > _noise) )
4056
{
4157
_prevValue = _value;
4258
}
4359
return _prevValue;
4460
}
4561

62+
4663
int AnalogPin::readSmoothed()
4764
{
48-
rawRead();
65+
_rawRead();
4966
if (_alpha > 0)
5067
{
5168
_value = _value + (_alpha * (_prevValue - _value)) / 32;
@@ -54,7 +71,8 @@ int AnalogPin::readSmoothed()
5471
return _value;
5572
}
5673

57-
void AnalogPin::rawRead()
74+
75+
void AnalogPin::_rawRead()
5876
{
5977
#if defined(ARDUINO_ARCH_AVR)
6078
// remember old register value

libraries/AnalogPin/AnalogPin.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
//
33
// FILE: AnalogPin.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.4
5+
// VERSION: 0.2.5
66
// DATE: 2013-09-09
77
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
88
// URL: https://github.com/RobTillaart/AnalogPin
99
//
1010

11+
1112
#include "Arduino.h"
1213

13-
#define ANALOGPIN_LIB_VERSION "0.2.4"
14+
#define ANALOGPIN_LIB_VERSION (F("0.2.5"))
15+
1416

1517
class AnalogPin
1618
{
@@ -23,15 +25,15 @@ class AnalogPin
2325
// 2 => 14 uSec 5 => 38 uSec
2426
// 3 => 18 uSec 6 => 63 uSec
2527
// 4 => 24 uSec 7 => 120 uSec (default/normal)
26-
void setPrescaler(const uint8_t prescale = 7) { _prescale = constrain(prescale, 2, 7); };
28+
void setPrescaler(const uint8_t prescale = 7);
2729
inline uint8_t getPrescaler(void) const { return _prescale; };
2830

2931
// noise 0..255; in practice only small values are used (0..10).
3032
inline void setNoiseThreshold(const uint8_t noise = 0) { _noise = noise; };
3133
inline uint8_t getNoiseThreshold(void) const { return _noise; };
3234

3335
// alpha 0..31;
34-
void setSmoothWeight(const uint8_t alpha = 0) { _alpha = min(alpha, 31); };
36+
void setSmoothWeight(const uint8_t alpha = 0);
3537
inline uint8_t getSmoothWeight(void) const { return _alpha; };
3638

3739
// set twice to true to do analogRead twice to reduce noise too
@@ -42,9 +44,9 @@ class AnalogPin
4244
inline int readPrevious(void) const { return _prevValue; }
4345
inline int readLast(void) const { return _value; }
4446

45-
private:
4647

47-
void rawRead();
48+
private:
49+
void _rawRead();
4850

4951
uint8_t _pin;
5052
uint8_t _alpha;

libraries/AnalogPin/README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
[![Arduino CI](https://github.com/RobTillaart/AnalogPin/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![Arduino-lint](https://github.com/RobTillaart/AnalogPin/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AnalogPin/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/AnalogPin/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AnalogPin/actions/workflows/jsoncheck.yml)
35
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AnalogPin/blob/master/LICENSE)
46
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AnalogPin.svg?maxAge=3600)](https://github.com/RobTillaart/AnalogPin/releases)
57

8+
69
# AnalogPin
710

811
Arduino library to add functionality on top of analogRead()
@@ -12,27 +15,37 @@ Arduino library to add functionality on top of analogRead()
1215

1316
AnalogPin is an Arduino class that adds noise filtering and smoothing
1417
to analogRead().
15-
Furthermore it can speed up the analogRead() function by tuning the prescaler.
18+
Furthermore it can speed up the analogRead() function by tuning the pre-scaler.
1619
This latter is AVR only.
1720

1821

19-
## Operation
22+
## Interface
23+
24+
25+
- **AnalogPin(uint8_t pin)** constructor with analogue pin as parameter.
26+
- **void setPrescaler(uint8_t prescale = 7)** AVR only pre-scaler.
27+
- **uint8_t getPrescaler()** return pre-scaler set.
28+
- **void setNoiseThreshold(uint8_t noise = 0)** set noise level that should be ignored. Typical 0..2.
29+
- **uint8_t getNoiseThreshold()** return set value.
30+
- **void setSmoothWeight(uint8_t alpha = 0)** alpha = 0..31, parameter for low pass filter.
31+
- **uint8_t getSmoothWeight(void)** returns set alpha.
32+
- **int read(bool twice = false)** read function, optional read twice to stabilize.
33+
- **int readSmoothed()** read version that uses low pass filter.
34+
- **int readPrevious()** returns previous read value.
35+
- **int readLast()** returns last read value without reading a new one.
2036

21-
**readLast()** returns the last read value without reading a new one.
2237

23-
**get/setPrescaler(prescale)** can be used to speed up analogRead().
38+
## Operation
2439

25-
The effect is that both the accuracy and precission are affected.
40+
**get/setPrescaler(prescale)** can be used to speed up analogRead().
41+
The effect is that both the accuracy and precision are affected.
2642
You should verify if this is acceptable for your project.
2743
***Works only for AVR based boards***
2844

45+
**get/setNoiseThreshold(noise)** is used to set the noise threshold to be used by the **read()** function.
2946

30-
**get/setNoiseThreshold(noise)** is used to set the noise threshold to be used by
31-
the **read()** function.
32-
33-
**read(twice)** implements an **analogRead()** that supresses small noise fluctuations.
34-
The parameter twice is used to force analogRead() to be executed twice to reduce noise
35-
from the multiplexing.
47+
**read(twice)** implements an **analogRead()** that suppresses small noise fluctuations.
48+
The parameter twice is used to force analogRead() to be executed twice to reduce noise from the multiplexing.
3649

3750
Example: if the previous read has the value 300 and you
3851
want to interpret all subsequent readings between 290
@@ -56,8 +69,9 @@ This can be used to suppress noise too.
5669

5770
**readSmoothed()** implements an analogRead with a running average build in.
5871

59-
Two functions that expose information that might sometimes be useful.
60-
**readPrevious()** returns the previous value read.
6172

62-
**readLast()** returns the last value read.
73+
## Future
74+
75+
- more examples
76+
6377

libraries/AnalogPin/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/AnalogPin"
1717
},
18-
"version": "0.2.4",
18+
"version": "0.2.5",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*"

libraries/AnalogPin/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=AnalogPin
2-
version=0.2.4
2+
version=0.2.5
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino Library for AnalogPin

0 commit comments

Comments
 (0)