Smartcar Shield
Loading...
Searching...
No Matches
GP2Y0A02.cpp
Go to the documentation of this file.
1#include "GP2Y0A02.hpp"
2
3namespace
4{
5const auto kMinDistance = 25; // GP2Y0A02's minimum distance
6const auto kMaxDistance = 120; // GP2Y0A02's maximum distance
7} // namespace
8
9GP2Y0A02::GP2Y0A02(Runtime& runtime, uint8_t pin)
10 : InfraredAnalogSensor(runtime)
11 , kPin{ pin }
12 , mRuntime(runtime)
13{
14}
15
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>(9462 / (analogReading - 16.92));
22
23 return (result >= kMinDistance && result <= kMaxDistance ? result : 0);
24}
unsigned int getDistance() override
Gets the distance measured by the sensor in centimeters.
Definition GP2Y0A02.cpp:16
GP2Y0A02(Runtime &runtime, uint8_t pin)
Constructs a GP2Y0A02 sensor.
Definition GP2Y0A02.cpp:9
An abstract class to represent the various infrared analog sensors.
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
Definition Runtime.hpp:36
virtual int getAnalogPinState(uint8_t pin)=0
Get pin's analog (ADC) reading, equivalent of analogRead in Arduino.