perif::USDist class

Ultrasonic distance sensor peripheral. Tested on HC-SR04

Base classes

class GPIOPerif

Constructors, destructors, conversion operators

USDist(uint8_t trigPin = TRIG_PIN, uint8_t echoPin = ECHO_PIN) explicit

Public functions

auto begin() -> bool override
auto isConnected() -> bool override
auto setPin(uint8_t pin, gpio_mode_t mode = GPIO_MODE_INPUT_OUTPUT, bool pullUp = false, bool pullDown = false) -> bool
auto setIntrPin(uint8_t pin, gpioFunc&& handler, uint8_t isrFlags = 0, gpio_int_type_t intrType = GPIO_INTR_ANYEDGE, gpio_mode_t mode = GPIO_MODE_INPUT_OUTPUT, bool pullUp = false, bool pullDown = false) -> bool
void update()
void registerHook(function<void(DevMap&)>&& hook)

Public variables

DevMap data
list<function<void(DevMap&)>> hooks

Protected functions

auto getTimer() -> Timer&

Protected variables

Timer timer

Private functions

void updateData() override
void representData() override

Function documentation

perif::USDist::USDist(uint8_t trigPin = TRIG_PIN, uint8_t echoPin = ECHO_PIN) explicit

Parameters
trigPin the GPIO trigger pin
echoPin the GPIO echo pin

Explicit value constructor.

bool perif::USDist::begin() override

Returns whether the peripheral initialization succeeded

Performs initialization of a peripheral. Necessary to avoid initialization in the constructor which causes many side effects.

bool perif::USDist::isConnected() override

Returns if the sensor is connected

Returns whether the sensor is connected or not.

bool perif::USDist::setPin(uint8_t pin, gpio_mode_t mode = GPIO_MODE_INPUT_OUTPUT, bool pullUp = false, bool pullDown = false)

Parameters
pin the GPIO pin number
mode the GPIO pin mode
pullUp whether the pin is a pullup
pullDown whether the pin is a pulldown
Returns if the pin was setup successfully

Sets up a pin for GPIO operations with interrupts disabled.

bool perif::USDist::setIntrPin(uint8_t pin, gpioFunc&& handler, uint8_t isrFlags = 0, gpio_int_type_t intrType = GPIO_INTR_ANYEDGE, gpio_mode_t mode = GPIO_MODE_INPUT_OUTPUT, bool pullUp = false, bool pullDown = false)

Parameters
pin the pin number
handler the interrupt handler
isrFlags any interrupt configuration flags
intrType the interrupt type
mode the GPIO pin mode
pullUp whether the pin is a pullup
pullDown whether the pin is a pulldown
Returns if the interrupt pin was setup successfully

Specify a pin to use as an interrupt.

void perif::USDist::update()

Performs a manual update of peripheral data and notifies observers.

void perif::USDist::registerHook(function<void(DevMap&)>&& hook)

Parameters
hook the observer function

Registers a hook for obtaining sensor data on update in Json format.

Timer& perif::USDist::getTimer() protected

Returns a timer reference

Obtains a reference to this peripheral's timer.

void perif::USDist::updateData() override private

Updates sensor data. Subclasses should maintain a reference to the data obtained from this function so it can be provided to observers.

void perif::USDist::representData() override private

The representation of this peripheral's data in a meaningful way for debugging, caching, and database operations.

Variable documentation

DevMap perif::USDist::data

The key-value mapping of peripheral data.

list<function<void(DevMap&)>> perif::USDist::hooks

Hooks registered to this peripheral so observers can be notified of any necessary changes.

Timer perif::USDist::timer protected

A timer for managing peripheral updates.