-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImpactSensor.cpp
47 lines (39 loc) · 1.02 KB
/
ImpactSensor.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
43
44
45
46
47
#include "Particle.h";
#include "ImpactSensor.h";
ImpactSensor::ImpactSensor(int pin, int lpin, int thold, int hitDelayMs, std::function<void(int)> cb) {
sensorPin = pin;
ledPin = lpin;
impactThreshold = thold;
hitProcessing = false;
callback = cb;
hitDelayer = new Timer(hitDelayMs, &ImpactSensor::endProcessHit, *this);
}
void ImpactSensor::tick() {
if(isHitProcessing()) {
return;
}
checkHitSensor();
}
bool ImpactSensor::isHitProcessing() {
return hitProcessing;
}
void ImpactSensor::checkHitSensor() {
int impactSensorReading = analogRead(sensorPin);
if(isHit(impactSensorReading)) {
processHit(impactSensorReading);
}
}
bool ImpactSensor::isHit(int impactReading) {
return impactReading >= impactThreshold;
}
void ImpactSensor::processHit(int sensorReading) {
Log.trace("Processing Hit");
hitProcessing = true;
hitDelayer->reset();
digitalWrite(ledPin, HIGH);
callback(sensorReading);
}
void ImpactSensor::endProcessHit() {
hitProcessing = false;
digitalWrite(ledPin, LOW);
}