|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
LED wrapper that prevents simultaneous control from multiple threads. More...
#include <safe_led.hpp>


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 | |
| NonCopyable & | operator= (const NonCopyable &)=delete |
| NonCopyable (NonCopyable &&)=default | |
| NonCopyable & | operator= (NonCopyable &&)=default |
Public Member Functions inherited from c7222::NonMovable | |
| NonMovable (const NonMovable &)=default | |
| NonMovable & | operator= (const NonMovable &)=default |
| NonMovable (NonMovable &&)=delete | |
| NonMovable & | operator= (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 | |
LED wrapper that prevents simultaneous control from multiple threads.
Usage pattern:
Acquire() to obtain exclusive control.Release() when done.All LED operations assert that the caller has acquired the lock.
Synchronization model and purpose:
std::mutex to guard ownership state (locked_) across multiple tasks that may try to control the LED.std::condition_variable to block waiters in Acquire()/AcquireFor() until the LED becomes available, avoiding busy-waiting.RAII considerations:
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.
|
explicit |
|
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.
| void c7222::SafeLed::Acquire | ( | ) |
Acquire exclusive control of the LED (blocks until available).
| bool c7222::SafeLed::AcquireFor | ( | uint32_t | timeout | ) |
Acquire exclusive control with a timeout.
| timeout | Maximum time to wait. |

| bool c7222::SafeLed::IsHeld | ( | ) | const |
Return true if this object currently holds the lock.
| void c7222::SafeLed::Off | ( | ) |
Turn the LED off (requires Acquire()).


| void c7222::SafeLed::On | ( | ) |
Turn the LED on (requires Acquire()).


| void c7222::SafeLed::Release | ( | ) |
Release exclusive control of the LED.

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