Skip to content

Commit

Permalink
Add function to return pointer to readings array.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Aug 19, 2020
1 parent 858ab21 commit bde0db6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,23 @@ None.
```c++
mySensor.reset();
```

### getReadings()
##### Description
Returns a pointer to the integer array containing the collected data points.
##### Syntax
`getReadings();`
##### Parameters
None.
##### Returns
Pointer to an integer array. *(int\*)*
##### Example
```c++
movingAvg foo(6);
...
int* myData;
myData = foo.getReadings();
Serial.println(myData[0]); // first data point
Serial.println(myData[1]); // second data point
// etc.
```
36 changes: 36 additions & 0 deletions examples/GetReadings/GetReadings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Arduino Moving Average Library
// https://github.com/JChristensen/movingAvg
// Copyright (C) 2020 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Example sketch to demonstrate retrieving the readings
// from a movingAvg object.

#include <movingAvg.h> // https://github.com/JChristensen/movingAvg

movingAvg foo(6);

void setup()
{
Serial.begin(115200);
foo.begin();
foo.reading(1); // generate some sample data
foo.reading(2);
foo.reading(3);
foo.reading(4);
//foo.reading(5);
//foo.reading(6);

int *readings = foo.getReadings(); // returns a pointer to the readings
int n = foo.getCount(); // returns the number of readings
Serial.print("There are ");
Serial.print(n);
Serial.println(" readings:");
for (int i=0; i<n; i++) {
Serial.println(*readings++);
}
}

void loop()
{
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ reading KEYWORD2
getAvg KEYWORD2
getCount KEYWORD2
reset KEYWORD2
getReadings KEYWORD2
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=movingAvg
version=2.1.1
version=2.2.0
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=A simple Arduino library for calculating moving averages.
Expand Down
1 change: 1 addition & 0 deletions src/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class movingAvg
int getAvg();
int getCount() {return m_nbrReadings;}
void reset();
int* getReadings() {return m_readings;}

private:
int m_interval; // number of data points for the moving average
Expand Down

0 comments on commit bde0db6

Please sign in to comment.