ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
c7222::SafeLed Class Reference

LED wrapper that prevents simultaneous control from multiple threads. More...

#include <safe_led.hpp>

Inheritance diagram for c7222::SafeLed:
Inheritance graph
Collaboration diagram for c7222::SafeLed:
Collaboration graph

Public Member Functions

 SafeLed (Led &led)
 Construct a SafeLed wrapper around an existing Led instance.
 
 SafeLed (PicoWBoard::LedId id)
 Construct a SafeLed wrapper for a board LED by logical ID.
 
void Acquire ()
 Acquire exclusive control of the LED (blocks until available).
 
bool AcquireFor (uint32_t timeout)
 Acquire exclusive control with a timeout.
 
void Release ()
 Release exclusive control of the LED.
 
bool IsHeld () const
 Return true if this object currently holds the lock.
 
void Set (bool on)
 Set the LED state (requires Acquire()).
 
void On ()
 Turn the LED on (requires Acquire()).
 
void Off ()
 Turn the LED off (requires Acquire()).
 
void Toggle ()
 Toggle the LED (requires Acquire()).
 
- Public Member Functions inherited from c7222::NonCopyable
 NonCopyable (const NonCopyable &)=delete
 
NonCopyableoperator= (const NonCopyable &)=delete
 
 NonCopyable (NonCopyable &&)=default
 
NonCopyableoperator= (NonCopyable &&)=default
 
- Public Member Functions inherited from c7222::NonMovable
 NonMovable (const NonMovable &)=default
 
NonMovableoperator= (const NonMovable &)=default
 
 NonMovable (NonMovable &&)=delete
 
NonMovableoperator= (NonMovable &&)=delete
 

Additional Inherited Members

- Protected Member Functions inherited from c7222::NonCopyableNonMovable
 NonCopyableNonMovable ()=default
 
 ~NonCopyableNonMovable ()=default
 
- Protected Member Functions inherited from c7222::NonCopyable
 NonCopyable ()=default
 
 ~NonCopyable ()=default
 
- Protected Member Functions inherited from c7222::NonMovable
 NonMovable ()=default
 
 ~NonMovable ()=default
 

Detailed Description

LED wrapper that prevents simultaneous control from multiple threads.

Usage pattern:

  1. Call Acquire() to obtain exclusive control.
  2. Perform one or more LED operations.
  3. Call Release() when done.

All LED operations assert that the caller has acquired the lock.

Synchronization model and purpose:

  • Uses a std::mutex to guard ownership state (locked_) across multiple tasks that may try to control the LED.
  • Uses a std::condition_variable to block waiters in Acquire()/AcquireFor() until the LED becomes available, avoiding busy-waiting.

RAII considerations:

  • RAII is intentionally not used to represent LED ownership because ownership spans multiple calls and can be transferred between tasks over time. A scoped guard would tie ownership to a single scope and thread, which does not match the intended usage pattern (press/release workflows).
  • RAII is used internally for mutex management: Acquire()/AcquireFor() use std::unique_lock because std::condition_variable requires it for atomic unlock-and-wait semantics. The lock is released on all exit paths.

Constructor & Destructor Documentation

◆ SafeLed() [1/2]

c7222::SafeLed::SafeLed ( Led led)
explicit

Construct a SafeLed wrapper around an existing Led instance.

◆ SafeLed() [2/2]

c7222::SafeLed::SafeLed ( PicoWBoard::LedId  id)
inlineexplicit

Construct a SafeLed wrapper for a board LED by logical ID.

This is a convenience constructor that looks up the LED from the Platform singleton. It assumes the Platform and PicoWBoard have been initialized and that the specified LED exists.

Member Function Documentation

◆ Acquire()

void c7222::SafeLed::Acquire ( )

Acquire exclusive control of the LED (blocks until available).

◆ AcquireFor()

bool c7222::SafeLed::AcquireFor ( uint32_t  timeout)

Acquire exclusive control with a timeout.

Parameters
timeoutMaximum time to wait.
Returns
true if the lock was acquired, false on timeout.
Here is the caller graph for this function:

◆ IsHeld()

bool c7222::SafeLed::IsHeld ( ) const

Return true if this object currently holds the lock.

◆ Off()

void c7222::SafeLed::Off ( )

Turn the LED off (requires Acquire()).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ On()

void c7222::SafeLed::On ( )

Turn the LED on (requires Acquire()).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Release()

void c7222::SafeLed::Release ( )

Release exclusive control of the LED.

Here is the caller graph for this function:

◆ Set()

void c7222::SafeLed::Set ( bool  on)

Set the LED state (requires Acquire()).

Here is the call graph for this function:

◆ Toggle()

void c7222::SafeLed::Toggle ( )

Toggle the LED (requires Acquire()).

Here is the call graph for this function:

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