FluxSand 1.0
FluxSand - Interactive Digital Hourglass
|
PWM driver with Beep and PlayNote functionality. More...
#include <bsp_pwm.hpp>
Public Types | |
enum class | NoteName { C = 0 , Cs , D , Ds , E , F , Fs , G , Gs , A , As , B } |
Musical note names used for MIDI note calculation. More... | |
Public Member Functions | |
PWM (int channel, int frequency_hz=1000, float duty_percent=0.0f, int chip=2) | |
Constructor: initializes PWM channel, sets initial frequency and duty cycle. | |
~PWM () | |
Destructor: disables PWM output. | |
void | SetFrequency (uint32_t hz) |
Set the output frequency of PWM. | |
void | SetDutyCycle (float percent) |
Set the duty cycle of PWM output. | |
void | Enable () |
Enable the PWM output signal. | |
void | Disable () |
Disable the PWM output signal. | |
void | Beep (uint32_t freq, uint32_t duration_ms) |
Output a beep with specific frequency and duration. | |
void | PlayNote (NoteName note, uint32_t octave, uint32_t duration_ms) |
Play a musical note based on note name and octave. | |
void | SetAlarmConfig (uint32_t freq, uint32_t duration_ms, uint32_t delay_ms) |
Set configuration parameters for alarm beep. | |
void | TriggerAlarm () |
Trigger an alarm sound followed by a delay. | |
Private Member Functions | |
int | WriteSysfs (const std::string &path, int value) const |
Write an integer value to a sysfs file. | |
Private Attributes | |
std::string | chip_path_ |
Sysfs path to the PWM chip. | |
std::string | pwm_path_ |
Sysfs path to the PWM channel. | |
int | period_ns_ |
PWM period in nanoseconds. | |
int | channel_ |
PWM channel number. | |
uint32_t | alarm_freq_ = 1500 |
Default alarm frequency in Hz. | |
uint32_t | alarm_duration_ = 300 |
Default alarm duration in ms. | |
uint32_t | alarm_delay_ = 300 |
Default alarm delay in ms. | |
std::thread | note_thread_ |
Background thread for playing notes. | |
NoteName | note_ |
Current note name. | |
uint32_t | octave_ |
Current octave. | |
uint32_t | duration_ms_ |
Duration of note in ms. | |
std::binary_semaphore | note_sem_ {0} |
Semaphore to trigger note play. | |
PWM driver with Beep and PlayNote functionality.
This class controls PWM output on Raspberry Pi 5 using GPIO12 (channel 0) and GPIO13 (channel 1).
⚠️ Requires enabling the following overlay in /boot/firmware/config.txt
: dtoverlay=pwm-2chan
Definition at line 21 of file bsp_pwm.hpp.
|
strong |
Musical note names used for MIDI note calculation.
Definition at line 27 of file bsp_pwm.hpp.
|
inline |
Constructor: initializes PWM channel, sets initial frequency and duty cycle.
channel | PWM channel number (0 or 1). |
frequency_hz | Initial frequency in Hz. Default is 1000 Hz. |
duty_percent | Initial duty cycle (range: 0.0 ~ 1.0). |
chip | PWM chip number. Default is 2 for Raspberry Pi 5. |
Definition at line 37 of file bsp_pwm.hpp.
|
inline |
|
inline |
Output a beep with specific frequency and duration.
freq | Frequency in Hz. |
duration_ms | Duration in milliseconds. |
Definition at line 107 of file bsp_pwm.hpp.
|
inline |
|
inline |
|
inline |
Play a musical note based on note name and octave.
note | Note (C, D, E, etc.). |
octave | Octave number (e.g., 4 for A4 = 440 Hz). |
duration_ms | Duration of the note in milliseconds. |
Definition at line 121 of file bsp_pwm.hpp.
|
inline |
Set configuration parameters for alarm beep.
freq | Alarm frequency in Hz. |
duration_ms | Duration of tone in milliseconds. |
delay_ms | Delay after the tone in milliseconds. |
Definition at line 134 of file bsp_pwm.hpp.
|
inline |
Set the duty cycle of PWM output.
percent | Duty cycle as a float between 0.0 and 1.0. |
Definition at line 87 of file bsp_pwm.hpp.
|
inline |
Set the output frequency of PWM.
hz | Desired frequency in Hz. |
Definition at line 78 of file bsp_pwm.hpp.
|
inline |
Trigger an alarm sound followed by a delay.
Definition at line 143 of file bsp_pwm.hpp.
|
inlineprivate |
Write an integer value to a sysfs file.
path | Full path to the sysfs file. |
value | Integer value to write. |
Definition at line 170 of file bsp_pwm.hpp.
|
private |
Default alarm delay in ms.
Definition at line 156 of file bsp_pwm.hpp.
|
private |
Default alarm duration in ms.
Definition at line 155 of file bsp_pwm.hpp.
|
private |
Default alarm frequency in Hz.
Definition at line 154 of file bsp_pwm.hpp.
|
private |
PWM channel number.
Definition at line 152 of file bsp_pwm.hpp.
|
private |
Sysfs path to the PWM chip.
Definition at line 149 of file bsp_pwm.hpp.
|
private |
Duration of note in ms.
Definition at line 161 of file bsp_pwm.hpp.
|
private |
Current note name.
Definition at line 159 of file bsp_pwm.hpp.
|
private |
|
private |
Background thread for playing notes.
Definition at line 158 of file bsp_pwm.hpp.
|
private |
Current octave.
Definition at line 160 of file bsp_pwm.hpp.
|
private |
PWM period in nanoseconds.
Definition at line 151 of file bsp_pwm.hpp.
|
private |
Sysfs path to the PWM channel.
Definition at line 150 of file bsp_pwm.hpp.