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

Singleton access to platform-specific devices and initialization. More...

#include <platform.hpp>

Inheritance diagram for c7222::Platform:
Inheritance graph
Collaboration diagram for c7222::Platform:
Collaboration graph

Public Member Functions

bool Initialize ()
 Initialize platform-specific hardware.
 
bool EnsureArchInitialized ()
 Ensure the platform architecture is initialized.
 
OnBoardLEDGetOnBoardLed ()
 Access the on-board LED.
 
OnChipTemperatureSensorGetOnChipTemperatureSensor ()
 Access the on-chip temperature sensor.
 
PicoWBoardGetPicoWBoard ()
 Access the PicoWBoard singleton.
 
LedGetLed (PicoWBoard::LedId id)
 Get a board LED by logical ID.
 
ButtonGetButton (PicoWBoard::ButtonId id)
 Get a board button by logical ID.
 
std::unique_ptr< PwmOutCreateLedPwm (PicoWBoard::LedId id, uint8_t dim)
 Create a PWM output for a board LED pin.
 
void EnableButtonIrq (PicoWBoard::ButtonId id, GpioInputEvent events, std::function< void(uint32_t)> handler)
 Enable IRQs for a board button.
 
void DisableButtonIrq (PicoWBoard::ButtonId id)
 Disable IRQs for a board button.
 
bool IsButtonPressed (PicoWBoard::ButtonId id)
 Read the current state of a board button (true if pressed).
 
void ToggleLed (PicoWBoard::LedId id)
 Toggle a board LED.
 
void LedOn (PicoWBoard::LedId id)
 Turn a board LED on.
 
void LedOff (PicoWBoard::LedId id)
 Turn a board LED off.
 
bool IsInitialized () const
 Return true if platform initialization has completed.
 
- 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
 

Static Public Member Functions

static PlatformGetInstance ()
 Get the singleton instance.
 
Timing helpers

These static wrappers provide access to common Pico SDK timing utilities for main-loop control and low-power waiting. Callers may use these without a Platform instance.

static void SleepMs (uint32_t ms)
 Sleep for a number of milliseconds.
 
static void SleepUs (uint64_t us)
 Sleep for a number of microseconds.
 
static void SleepUntil (std::chrono::steady_clock::time_point target)
 Sleep until a steady-clock time point.
 
static void TightLoopContents ()
 Body for tight polling loops (no-op hook).
 

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

Singleton access to platform-specific devices and initialization.

Purpose:

  • Centralizes platform initialization (Pico SDK / CYW43 setup) so application code does not need to call platform-specific init routines.
  • Provides convenience accessors for common board devices (LEDs and buttons) while still allowing direct use of the dedicated device classes (OnBoardLED, OnChipTemperatureSensor, PicoWBoard, Led, Button).
  • Exposes timing helpers for main-loop management (sleep and tight-loop utilities) in a consistent, platform-owned API.

Dependencies:

Pico platform initialization:

  • Initialize() calls EnsureArchInitialized(), which initializes the Pico SDK architecture layer (including CYW43 on Pico W).
  • Device classes remain explicit-init so callers stay in control of when hardware is configured.

Proper usage:

  • Always call Platform::Initialize() once early.
  • Then explicitly initialize devices you intend to use (LED and temperature sensor).
  • For board LEDs/buttons, use the convenience helpers or access PicoWBoard directly.

Typical usage:

platform->Initialize(); // required before using any hardware-backed APIs
bool Initialize()
Initialize platform-specific hardware.
static Platform * GetInstance()
Get the singleton instance.
static c7222::Platform * platform
Platform abstraction (initializes CYW43/BTstack).
Definition main_ble_gap.cpp:33

On-board LED (explicit init required):

auto* led = platform->GetOnBoardLed();
led->Initialize();
led->On();
bool Initialize()
Initialize the on-board LED hardware.
OnBoardLED * GetOnBoardLed()
Access the on-board LED.
Definition platform.hpp:191

Temperature sensor (explicit init required):

sensor->Initialize();
float temp_c = sensor->GetCelsius();
bool Initialize()
Initialize the ADC and temperature sensor.
OnChipTemperatureSensor * GetOnChipTemperatureSensor()
Access the on-chip temperature sensor.
Definition platform.hpp:213

PicoWBoard LEDs and buttons:

// LEDs
// PWM LED dimming (active-low board LEDs)
// Use pwm while it owns the pin; call pwm->Enable(false) to release GPIO.
// Buttons
[](uint32_t){ / * handle press * / });
void EnableButtonIrq(PicoWBoard::ButtonId id, GpioInputEvent events, std::function< void(uint32_t)> handler)
Enable IRQs for a board button.
Definition platform.hpp:255
std::unique_ptr< PwmOut > CreateLedPwm(PicoWBoard::LedId id, uint8_t dim)
Create a PWM output for a board LED pin.
void ToggleLed(PicoWBoard::LedId id)
Toggle a board LED.
Definition platform.hpp:278
void LedOn(PicoWBoard::LedId id)
Turn a board LED on.
Definition platform.hpp:285
bool IsButtonPressed(PicoWBoard::ButtonId id)
Read the current state of a board button (true if pressed).
Definition platform.hpp:271
@ FallingEdge
Trigger on high-to-low edge.

Timing helpers:

// Sleep for 250 ms between loop iterations.
while (true) {
// ... do work ...
}
static void SleepMs(uint32_t ms)
Sleep for a number of milliseconds.

Member Function Documentation

◆ CreateLedPwm()

std::unique_ptr< PwmOut > c7222::Platform::CreateLedPwm ( PicoWBoard::LedId  id,
uint8_t  dim 
)

Create a PWM output for a board LED pin.

Parameters
idBoard LED identifier.
dimBrightness 0-255 (0 = off, 255 = fully on).
Returns
Unique pointer to a PWM output configured for the LED pin.

Notes:

  • Board LEDs are active-low, so the PWM output is configured as active-low.
  • Do not drive the same pin with Led and PwmOut at the same time. If you previously used Led, stop using it and allow PWM to take over.
Here is the caller graph for this function:

◆ DisableButtonIrq()

void c7222::Platform::DisableButtonIrq ( PicoWBoard::ButtonId  id)
inline

Disable IRQs for a board button.

Here is the call graph for this function:

◆ EnableButtonIrq()

void c7222::Platform::EnableButtonIrq ( PicoWBoard::ButtonId  id,
GpioInputEvent  events,
std::function< void(uint32_t)>  handler 
)
inline

Enable IRQs for a board button.

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

◆ EnsureArchInitialized()

bool c7222::Platform::EnsureArchInitialized ( )

Ensure the platform architecture is initialized.

Returns
true when architecture init succeeds.

◆ GetButton()

Button & c7222::Platform::GetButton ( PicoWBoard::ButtonId  id)
inline

Get a board button by logical ID.

Here is the call graph for this function:

◆ GetInstance()

static Platform * c7222::Platform::GetInstance ( )
static

Get the singleton instance.

Here is the caller graph for this function:

◆ GetLed()

Led & c7222::Platform::GetLed ( PicoWBoard::LedId  id)
inline

Get a board LED by logical ID.

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

◆ GetOnBoardLed()

OnBoardLED * c7222::Platform::GetOnBoardLed ( )
inline

Access the on-board LED.

Note: Call Initialize() first. The LED's own Initialize() must be called explicitly by the user; Platform does not auto-initialize it.

Example:

auto* led = platform->GetOnBoardLed();
led->Initialize();
led->On();

Access the on-board LED singleton.

Here is the call graph for this function:

◆ GetOnChipTemperatureSensor()

OnChipTemperatureSensor * c7222::Platform::GetOnChipTemperatureSensor ( )
inline

Access the on-chip temperature sensor.

Note: Call Initialize() first. The sensor's own Initialize() must be called explicitly by the user; Platform does not auto-initialize it.

Example:

sensor->Initialize();
float temp_c = sensor->GetCelsius();

Access the on-chip temperature sensor singleton.

Here is the call graph for this function:

◆ GetPicoWBoard()

PicoWBoard * c7222::Platform::GetPicoWBoard ( )
inline

Access the PicoWBoard singleton.

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

◆ Initialize()

bool c7222::Platform::Initialize ( )

Initialize platform-specific hardware.

This performs architecture-level initialization (e.g., CYW43 on Pico W). It does not automatically initialize the on-board LED or temperature sensor; those must be initialized explicitly by the user.

Returns
true when initialization succeeds.
Here is the caller graph for this function:

◆ IsButtonPressed()

bool c7222::Platform::IsButtonPressed ( PicoWBoard::ButtonId  id)
inline

Read the current state of a board button (true if pressed).

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

◆ IsInitialized()

bool c7222::Platform::IsInitialized ( ) const
inline

Return true if platform initialization has completed.

◆ LedOff()

void c7222::Platform::LedOff ( PicoWBoard::LedId  id)
inline

Turn a board LED off.

Here is the call graph for this function:

◆ LedOn()

void c7222::Platform::LedOn ( PicoWBoard::LedId  id)
inline

Turn a board LED on.

Here is the call graph for this function:

◆ SleepMs()

static void c7222::Platform::SleepMs ( uint32_t  ms)
static

Sleep for a number of milliseconds.

Parameters
msMilliseconds to sleep.

◆ SleepUntil()

static void c7222::Platform::SleepUntil ( std::chrono::steady_clock::time_point  target)
static

Sleep until a steady-clock time point.

Parameters
targetTarget time point.

◆ SleepUs()

static void c7222::Platform::SleepUs ( uint64_t  us)
static

Sleep for a number of microseconds.

Parameters
usMicroseconds to sleep.

◆ TightLoopContents()

static void c7222::Platform::TightLoopContents ( )
static

Body for tight polling loops (no-op hook).

◆ ToggleLed()

void c7222::Platform::ToggleLed ( PicoWBoard::LedId  id)
inline

Toggle a board LED.

Here is the call graph for this function:

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