-
Notifications
You must be signed in to change notification settings - Fork 10
/
autoPOT.cpp
42 lines (28 loc) · 1.04 KB
/
autoPOT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <autoPOT.h>
autoPOT::autoPOT(int inPin) {
callback = NULL; // Bacause it ain't
pinNum = inPin; // Because they said.
value = -1; // Because it can't.
window = 0; // Because not now.
}
// Nothing to do here.
autoPOT::~autoPOT(void) { }
// User tells us what to call when we see a change.
void autoPOT::setCallback(void(*funct)(int)) {
callback = funct;
hookup();
}
// Sets a +/- window where changed in value from the analog port are ignored. Get a value
// beyond this 'window" and the change is announced in the callback.
void autoPOT::setWindow(int plusMinus) { window = abs(plusMinus); }
// What we do in the background.
void autoPOT::idle(void) {
int newVal;
if (callback) { // If we have a callback ready?
newVal = analogRead(pinNum); // Read the analog port.
if (newVal>value+window||newVal<value-window) { // If it falls outside our window..
value = newVal; // Save off the new value.
callback(value); // Call our callback with the value.
}
}
}