JSN-SR04_Gen3_RK
Public Types | Public Member Functions | Data Fields
JSN_SR04_Gen3::DistanceAlarm Class Reference

Settings for use with distance alarm mode. More...

#include <JSN-SR04_Gen3_RK.h>

Inheritance diagram for JSN_SR04_Gen3::DistanceAlarm:
JSN_SR04_Gen3::Distance JSN_SR04_Gen3::DistanceAlarmGreaterThan JSN_SR04_Gen3::DistanceAlarmLessThan

Public Types

enum class  Direction : int { LESS_THAN = -1 , GREATER_THAN = +1 }
 This enum specifies whether being in alarm is when the distance is less than or greater than the current distance. More...
 

Public Member Functions

DistanceAlarmwithDistance (Distance distance)
 Sets the distance for the alarm. More...
 
DistanceAlarmwithDirection (Direction direction)
 Direction LESS_THAN or GREATER_THAN. More...
 
DistanceAlarmwithHysteresis (Distance hysteresis)
 Hystersis value (default: 0.5 cm) More...
 
DistanceAlarmwithPeriod (std::chrono::milliseconds period)
 The period to test (default: 500 milliseconds, twice per second) More...
 
bool isValid () const
 Returns true if a distance has been configured. More...
 
- Public Member Functions inherited from JSN_SR04_Gen3::Distance
 Distance ()
 Construct a new Distance object with a distance of 0.
 
 Distance (double valueM)
 Construct a new Distance object with a distance in meters. More...
 
 Distance (const Distance &value)
 Construct a new Distance object from another Distance object. More...
 
Distanceoperator= (const Distance &value)
 Copy the distance value from another Distance object. More...
 
void setDistanceM (double distanceM)
 Set the Distance in meters. More...
 
double getDistanceM () const
 Get the Distance in meters. More...
 
void cm (double cm)
 Set the distance in centimeters. More...
 
double cm () const
 Get the value of the Distance in centimeters. More...
 
void mm (double mm)
 Set the distance in millimeter. More...
 
double mm () const
 Get the value of the Distance in millimeters. More...
 
void inch (double inch)
 Set the distance in inches. More...
 
double inch () const
 Get the value of the Distance in inches. More...
 

Data Fields

Direction direction = Direction::LESS_THAN
 Stores the direction of the test. Default: LESS_THAN.
 
Distance hysteresis = 0.0005
 Stores the distance for hysteresis. Default: 0.5 cm.
 
unsigned long periodMs = 500
 How often check the sensor for alarm condition. More...
 
- Data Fields inherited from JSN_SR04_Gen3::Distance
double distanceM = 0.0
 The value of the distance in meters.
 

Detailed Description

Settings for use with distance alarm mode.

If you need fine control over the alarm mode, use this class directly. For simple use cases, you can use DistanceAlarmLessThan or DistanceAlarmGreaterThan which are easier to set up.

Member Enumeration Documentation

◆ Direction

enum class JSN_SR04_Gen3::DistanceAlarm::Direction : int
strong

This enum specifies whether being in alarm is when the distance is less than or greater than the current distance.

Enumerator
LESS_THAN 

Alarm when less than distance (you are too close)

GREATER_THAN 

Alarm when greater than distance (you are too far away)

Member Function Documentation

◆ isValid()

bool JSN_SR04_Gen3::DistanceAlarm::isValid ( ) const
inline

Returns true if a distance has been configured.

Returns
true
false

◆ withDirection()

DistanceAlarm & JSN_SR04_Gen3::DistanceAlarm::withDirection ( Direction  direction)
inline

Direction LESS_THAN or GREATER_THAN.

Parameters
directionthe direction enum to test
Returns
DistanceAlarm& This object, for chaining options, fluent-style

◆ withDistance()

DistanceAlarm & JSN_SR04_Gen3::DistanceAlarm::withDistance ( Distance  distance)
inline

Sets the distance for the alarm.

Parameters
distanceThe distance value to set
Returns
DistanceAlarm& This object, for chaining options, fluent-style

The Distance object contains a distance in meters, but for convenience, the classes DistanceCm and DistanceInch can make setting the value easy in other units.

◆ withHysteresis()

DistanceAlarm & JSN_SR04_Gen3::DistanceAlarm::withHysteresis ( Distance  hysteresis)
inline

Hystersis value (default: 0.5 cm)

Parameters
hysteresisThe distance value to set
Returns
DistanceAlarm& This object, for chaining options, fluent-style

The Distance object contains a distance in meters, but for convenience, the classes DistanceCm and DistanceInch can make setting the value easy in other units.

When in LESS_THAN mode, you enter alarm mode when the distance is less than the alarm distance. You exit alarm mode when the distance is greater than the alarm distance + hysteresis.

When in GREATER_THAN mode, you enter alarm mode when the distance is greater than the alarm distance. You exit alarm mode when the distance is less than the alarm distance - hysteresis.

◆ withPeriod()

DistanceAlarm & JSN_SR04_Gen3::DistanceAlarm::withPeriod ( std::chrono::milliseconds  period)
inline

The period to test (default: 500 milliseconds, twice per second)

Parameters
periodThe period as a chrono literal
Returns
DistanceAlarm& This object, for chaining options, fluent-style

The default is 500ms, but you can pass constants like 10s for 10 seconds (10000 ms).

Field Documentation

◆ periodMs

unsigned long JSN_SR04_Gen3::DistanceAlarm::periodMs = 500

How often check the sensor for alarm condition.

This probably should be longer than safetyTimeoutMs but can be shorter.


The documentation for this class was generated from the following file: