Smartcar Shield
src
sensors
distance
infrared
analog
sharp
GP2D120.cpp
Go to the documentation of this file.
1
#include "
GP2D120.hpp
"
2
3
namespace
4
{
5
const
auto
kMinDistance = 5;
// GP2D120's minimum distance
6
const
auto
kMaxDistance = 25;
// GP2D120's maximum distance
7
}
// namespace
8
9
GP2D120::GP2D120
(
Runtime
& runtime, uint8_t pin)
10
:
InfraredAnalogSensor
(runtime)
11
, kPin{ pin }
12
, mRuntime(runtime)
13
{
14
}
15
16
unsigned
int
GP2D120::getDistance
()
17
{
18
auto
analogReading = mRuntime.
getAnalogPinState
(kPin);
19
// It's OK to surpress this clang-tidy warning since this is part of a magic (!) formula
20
// NOLINTNEXTLINE(readability-magic-numbers)
21
auto
result =
static_cast<
unsigned
int
>
((2914 / (analogReading + 5)) - 1);
22
23
return
(result >= kMinDistance && result <= kMaxDistance ? result : 0);
24
}
GP2D120::GP2D120
GP2D120(Runtime &runtime, uint8_t pin)
Constructs a GP2D120 sensor.
Definition:
GP2D120.cpp:9
Runtime
Definition:
Runtime.hpp:35
InfraredAnalogSensor
Definition:
InfraredAnalogSensor.hpp:25
GP2D120.hpp
GP2D120::getDistance
unsigned int getDistance() override
Gets the distance measured by the sensor in centimeters.
Definition:
GP2D120.cpp:16
Runtime::getAnalogPinState
virtual int getAnalogPinState(uint8_t pin)=0
Get pin's analog (ADC) reading, equivalent of analogRead in Arduino.
Generated by
1.8.17