-
Notifications
You must be signed in to change notification settings - Fork 34
/
proximity-sensor.cpp
52 lines (42 loc) · 1.77 KB
/
proximity-sensor.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
48
49
50
51
52
#include "proximity-sensor.h"
void ProximitySensor::attach(uint8_t sdaPin, uint8_t sclPin, uint8_t rangeThreshold) {
Serial.println(F("ProximitySensor: Initializing..."));
_sensor = SparkFun_APDS9960();
Serial.println(F("ProximitySensor: Starting I2C..."));
Wire.begin(sdaPin, sclPin);
Serial.println(F("ProximitySensor: Starting I2C...DONE"));
Serial.println(F("ProximitySensor: Configuring I2C and initial values..."));
if (_sensor.init()) {
Serial.println(F("ProximitySensor: Configuring I2C and initial values...DONE"));
} else {
Serial.println(F("ProximitySensor: Configuring I2C and initial values...FAILED!"));
}
Serial.println(F("ProximitySensor: Adjusting the sensor gain..."));
if (_sensor.setProximityGain(PGAIN_2X)) {
Serial.println(F("ProximitySensor: Adjusting the sensor gain...DONE"));
} else {
Serial.println(F("ProximitySensor: Adjusting the sensor gain...FAILED!"));
}
Serial.println(F("ProximitySensor: Starting the sensor..."));
bool enableInterrupts = false;
if (_sensor.enableProximitySensor(enableInterrupts)) {
Serial.println(F("ProximitySensor: Starting the sensor...DONE"));
} else {
Serial.println(F("ProximitySensor: Starting the sensor...FAILED!"));
}
_rangeThreshold = rangeThreshold;
Serial.println(F("ProximitySensor: Initializing...DONE"));
}
uint8_t ProximitySensor::getProximity() {
if ( _sensor.readProximity(_lastProximityValue) ) {
Serial.print(F("ProximitySensor: Proximity value: "));
Serial.println(_lastProximityValue);
return _lastProximityValue;
} else {
Serial.println(F("ProximitySensor: Reading proximity value FAILED!"));
}
}
bool ProximitySensor::isInRange() {
uint8_t currentProximityValue = getProximity();
return currentProximityValue > _rangeThreshold;
}