RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::HCSR501 Class Reference

An HC-SR501 PIR motion sensor. More...

#include <HCSR501.h>

Inheritance diagram for RumPi::HCSR501:
[legend]
Collaboration diagram for RumPi::HCSR501:
[legend]

Public Member Functions

 HCSR501 (unsigned int inputPinNumber)
 Builds an HC-SR501 motion sensor on the given input pin, with no motion detected yet. More...
 
virtual ~HCSR501 ()
 Destroys the motion sensor. More...
 
virtual void TurnOn () override
 Turns the sensor on: sets its pin as a digital input and enables the pull-down resistor so the line idles LOW and reads HIGH only on motion. More...
 
virtual void TurnOff () override
 Turns the sensor off. More...
 
virtual void ProcessRawValues () override
 Reads the sensor's pin (HIGH = motion) and latches any detection so a brief pulse between snapshots is not lost. More...
 
virtual std::string ToString () const override
 Serializes the sensor's current motion state to a string. More...
 
bool GetIsMotionDetected () const
 Gets whether motion was detected on the most recent read. More...
 
bool ConsumeMotionSinceLastRead ()
 Returns whether motion was seen since the last call, then clears the latch so the next window starts fresh. More...
 
- Public Member Functions inherited from RumPi::BaseComponent
 BaseComponent ()
 Builds a base component with an unknown type and an empty name. More...
 
virtual ~BaseComponent ()
 Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More...
 
virtual void TurnOn ()=0
 Turns the component on (e.g. More...
 
virtual void TurnOff ()=0
 Turns the component off. More...
 
virtual std::string ToString () const =0
 Serializes the component's current readings to a string (usually JSON). More...
 
virtual void ProcessRawValues ()=0
 Reads and processes the component's latest raw values (called once per poll). More...
 
virtual EComponentType GetComponentType () const
 Gets the component's type. More...
 
const std::string & GetName () const
 Gets the component's unique per-instance name. More...
 
void SetName (const std::string &name)
 Sets the component's unique per-instance name. More...
 

Private Attributes

bool myIsMotionDetected
 Whether motion was detected on the most recent read. More...
 
unsigned int myInputPinNumber
 The GPIO input pin the sensor is read from. More...
 
bool myMotionLatch
 Latched "motion since the last read": ProcessRawValues raises it whenever the pin reads HIGH, and the snapshot consumes it via ConsumeMotionSinceLastRead(). More...
 

Additional Inherited Members

- Static Public Member Functions inherited from RumPi::BaseComponent
static std::string ComponentTypeToString (const EComponentType theComponentTypeToConvert)
 Maps an EComponentType to its string name. More...
 
static EComponentType StringToComponentType (const std::string &theComponentTypeName)
 Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More...
 
- Protected Attributes inherited from RumPi::BaseComponent
EComponentType myComponentType
 The kind of component this is (set by each concrete subclass). More...
 
std::string myName
 A unique per-instance name, assigned when the component is added to the manager. More...
 

Detailed Description

An HC-SR501 PIR motion sensor.

Fast-sampled each poll; a brief motion pulse is latched so it is not lost between the less-frequent snapshots.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ HCSR501()

RumPi::HCSR501::HCSR501 ( unsigned int  inputPinNumber)

Builds an HC-SR501 motion sensor on the given input pin, with no motion detected yet.

Author
Eddie O'Hagan
Date
8/13/2026
motion.TurnOn();
HCSR501(unsigned int inputPinNumber)
Builds an HC-SR501 motion sensor on the given input pin, with no motion detected yet.
Definition: HCSR501.cpp:18
constexpr unsigned int PIN_GPIO_18
BCM GPIO pin 18.
Definition: Common.h:44
Parameters
[in]inputPinNumberThe BCM GPIO input pin the sensor is read from.

◆ ~HCSR501()

RumPi::HCSR501::~HCSR501 ( )
virtual

Destroys the motion sensor.

Nothing to clean up beyond the base class.

Author
Eddie O'Hagan
Date
8/13/2026
HCSR501* motion = new HCSR501(18);
delete motion;

Member Function Documentation

◆ ConsumeMotionSinceLastRead()

bool RumPi::HCSR501::ConsumeMotionSinceLastRead ( )

Returns whether motion was seen since the last call, then clears the latch so the next window starts fresh.

Used by the dashboard snapshot so a brief motion pulse between snapshots is still reported once.

Author
Eddie O'Hagan
Date
8/13/2026
bool moved = motion.ConsumeMotionSinceLastRead();
Returns
true if motion occurred since the previous call; false otherwise.

◆ GetIsMotionDetected()

bool RumPi::HCSR501::GetIsMotionDetected ( ) const

Gets whether motion was detected on the most recent read.

Author
Eddie O'Hagan
Date
8/13/2026
if (motion.GetIsMotionDetected() == true)
{
//something moved on the last poll...
}
Returns
true if motion was detected on the most recent read; false otherwise.

◆ ProcessRawValues()

void RumPi::HCSR501::ProcessRawValues ( )
overridevirtual

Reads the sensor's pin (HIGH = motion) and latches any detection so a brief pulse between snapshots is not lost.

Fast-sampling this sensor (~100ms) is what makes the short HIGH reliably land in one of these reads.

Author
Eddie O'Hagan
Date
8/13/2026
motion.ProcessRawValues(); //call each poll to refresh and latch motion.

Implements RumPi::BaseComponent.

◆ ToString()

std::string RumPi::HCSR501::ToString ( ) const
overridevirtual

Serializes the sensor's current motion state to a string.

Author
Eddie O'Hagan
Date
8/13/2026
std::string data = motion.ToString(); //e.g. "Motion Sensor HCSR501: Detected motion!\n"
Returns
A string describing whether motion is currently detected.

Implements RumPi::BaseComponent.

◆ TurnOff()

void RumPi::HCSR501::TurnOff ( )
overridevirtual

Turns the sensor off.

The input pin needs no active teardown, so this is effectively a no-op.

Author
Eddie O'Hagan
Date
8/13/2026
motion.TurnOff();

Implements RumPi::BaseComponent.

◆ TurnOn()

void RumPi::HCSR501::TurnOn ( )
overridevirtual

Turns the sensor on: sets its pin as a digital input and enables the pull-down resistor so the line idles LOW and reads HIGH only on motion.

Author
Eddie O'Hagan
Date
8/13/2026
motion.TurnOn();

Implements RumPi::BaseComponent.

Member Data Documentation

◆ myInputPinNumber

unsigned int RumPi::HCSR501::myInputPinNumber
private

The GPIO input pin the sensor is read from.

◆ myIsMotionDetected

bool RumPi::HCSR501::myIsMotionDetected
private

Whether motion was detected on the most recent read.

◆ myMotionLatch

bool RumPi::HCSR501::myMotionLatch
private

Latched "motion since the last read": ProcessRawValues raises it whenever the pin reads HIGH, and the snapshot consumes it via ConsumeMotionSinceLastRead().

Fast-sampling the sensor (~every 100ms) is what makes a brief motion pulse land in one of these reads even though we only build the snapshot every couple of seconds. Only the worker thread touches it, so a plain bool is enough.


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