FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
PWM Class Reference

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.
 

Detailed Description

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.

Member Enumeration Documentation

◆ NoteName

enum class PWM::NoteName
strong

Musical note names used for MIDI note calculation.

Definition at line 27 of file bsp_pwm.hpp.

27{ C = 0, Cs, D, Ds, E, F, Fs, G, Gs, A, As, B };

Constructor & Destructor Documentation

◆ PWM()

PWM::PWM ( int  channel,
int  frequency_hz = 1000,
float  duty_percent = 0.0f,
int  chip = 2 
)
inline

Constructor: initializes PWM channel, sets initial frequency and duty cycle.

Parameters
channelPWM channel number (0 or 1).
frequency_hzInitial frequency in Hz. Default is 1000 Hz.
duty_percentInitial duty cycle (range: 0.0 ~ 1.0).
chipPWM chip number. Default is 2 for Raspberry Pi 5.

Definition at line 37 of file bsp_pwm.hpp.

39 : channel_(channel), period_ns_(static_cast<int>(1e9 / frequency_hz)) {
40 chip_path_ = "/sys/class/pwm/pwmchip" + std::to_string(chip);
41 pwm_path_ = chip_path_ + "/pwm" + std::to_string(channel);
42
43 // Export the PWM channel if it has not been exported yet.
44 if (access(pwm_path_.c_str(), F_OK) != 0) {
45 FILE* fp = fopen((chip_path_ + "/export").c_str(), "w");
46 if (fp) {
47 (void)(fprintf(fp, "%d", channel));
48 (void)(fclose(fp));
49 usleep(100000); // Allow sysfs to initialize.
50 }
51 }
52
53 WriteSysfs(pwm_path_ + "/period", period_ns_);
54 SetDutyCycle(duty_percent);
55 Enable();
56
57 // Launch background thread to play notes asynchronously.
58 note_thread_ = std::thread([this]() {
59 while (true) {
60 note_sem_.acquire();
61 float midi = static_cast<float>(note_) +
62 (static_cast<float>(octave_) + 1.0f) * 12.0f;
63 float freq = 440.0f * std::pow(2.0f, (midi - 69.0f) / 12.0f);
64 Beep(static_cast<uint32_t>(freq), duration_ms_);
65 }
66 });
67 }
int channel_
PWM channel number.
Definition bsp_pwm.hpp:152
void SetDutyCycle(float percent)
Set the duty cycle of PWM output.
Definition bsp_pwm.hpp:87
void Enable()
Enable the PWM output signal.
Definition bsp_pwm.hpp:95
NoteName note_
Current note name.
Definition bsp_pwm.hpp:159
uint32_t octave_
Current octave.
Definition bsp_pwm.hpp:160
std::binary_semaphore note_sem_
Semaphore to trigger note play.
Definition bsp_pwm.hpp:162
std::thread note_thread_
Background thread for playing notes.
Definition bsp_pwm.hpp:158
void Beep(uint32_t freq, uint32_t duration_ms)
Output a beep with specific frequency and duration.
Definition bsp_pwm.hpp:107
int WriteSysfs(const std::string &path, int value) const
Write an integer value to a sysfs file.
Definition bsp_pwm.hpp:170
std::string chip_path_
Sysfs path to the PWM chip.
Definition bsp_pwm.hpp:149
std::string pwm_path_
Sysfs path to the PWM channel.
Definition bsp_pwm.hpp:150
uint32_t duration_ms_
Duration of note in ms.
Definition bsp_pwm.hpp:161
int period_ns_
PWM period in nanoseconds.
Definition bsp_pwm.hpp:151

◆ ~PWM()

PWM::~PWM ( )
inline

Destructor: disables PWM output.

Definition at line 72 of file bsp_pwm.hpp.

72{ Disable(); }
void Disable()
Disable the PWM output signal.
Definition bsp_pwm.hpp:100

Member Function Documentation

◆ Beep()

void PWM::Beep ( uint32_t  freq,
uint32_t  duration_ms 
)
inline

Output a beep with specific frequency and duration.

Parameters
freqFrequency in Hz.
duration_msDuration in milliseconds.

Definition at line 107 of file bsp_pwm.hpp.

107 {
108 SetFrequency(freq);
109 SetDutyCycle(0.5f);
110 Enable();
111 usleep(duration_ms * 1000);
112 Disable();
113 }
void SetFrequency(uint32_t hz)
Set the output frequency of PWM.
Definition bsp_pwm.hpp:78

◆ Disable()

void PWM::Disable ( )
inline

Disable the PWM output signal.

Definition at line 100 of file bsp_pwm.hpp.

100{ WriteSysfs(pwm_path_ + "/enable", 0); }

◆ Enable()

void PWM::Enable ( )
inline

Enable the PWM output signal.

Definition at line 95 of file bsp_pwm.hpp.

95{ WriteSysfs(pwm_path_ + "/enable", 1); }

◆ PlayNote()

void PWM::PlayNote ( NoteName  note,
uint32_t  octave,
uint32_t  duration_ms 
)
inline

Play a musical note based on note name and octave.

Parameters
noteNote (C, D, E, etc.).
octaveOctave number (e.g., 4 for A4 = 440 Hz).
duration_msDuration of the note in milliseconds.

Definition at line 121 of file bsp_pwm.hpp.

121 {
122 note_ = note;
123 octave_ = octave;
124 duration_ms_ = duration_ms;
125 note_sem_.release();
126 }

◆ SetAlarmConfig()

void PWM::SetAlarmConfig ( uint32_t  freq,
uint32_t  duration_ms,
uint32_t  delay_ms 
)
inline

Set configuration parameters for alarm beep.

Parameters
freqAlarm frequency in Hz.
duration_msDuration of tone in milliseconds.
delay_msDelay after the tone in milliseconds.

Definition at line 134 of file bsp_pwm.hpp.

134 {
135 alarm_freq_ = freq;
136 alarm_duration_ = duration_ms;
137 alarm_delay_ = delay_ms;
138 }
uint32_t alarm_freq_
Default alarm frequency in Hz.
Definition bsp_pwm.hpp:154
uint32_t alarm_delay_
Default alarm delay in ms.
Definition bsp_pwm.hpp:156
uint32_t alarm_duration_
Default alarm duration in ms.
Definition bsp_pwm.hpp:155

◆ SetDutyCycle()

void PWM::SetDutyCycle ( float  percent)
inline

Set the duty cycle of PWM output.

Parameters
percentDuty cycle as a float between 0.0 and 1.0.

Definition at line 87 of file bsp_pwm.hpp.

87 {
88 int duty_ns = static_cast<int>(percent * static_cast<float>(period_ns_));
89 WriteSysfs(pwm_path_ + "/duty_cycle", duty_ns);
90 }

◆ SetFrequency()

void PWM::SetFrequency ( uint32_t  hz)
inline

Set the output frequency of PWM.

Parameters
hzDesired frequency in Hz.

Definition at line 78 of file bsp_pwm.hpp.

78 {
79 period_ns_ = static_cast<int>(1e9 / hz);
80 WriteSysfs(pwm_path_ + "/period", period_ns_);
81 }

◆ TriggerAlarm()

void PWM::TriggerAlarm ( )
inline

Trigger an alarm sound followed by a delay.

Definition at line 143 of file bsp_pwm.hpp.

143 {
145 usleep(alarm_delay_ * 1000);
146 }

◆ WriteSysfs()

int PWM::WriteSysfs ( const std::string &  path,
int  value 
) const
inlineprivate

Write an integer value to a sysfs file.

Parameters
pathFull path to the sysfs file.
valueInteger value to write.
Returns
Write result (number of characters written) or -1 on error.

Definition at line 170 of file bsp_pwm.hpp.

170 {
171 FILE* fp = fopen(path.c_str(), "w");
172 if (!fp) {
173 return -1;
174 }
175 int result = fprintf(fp, "%d", value);
176 (void)(fclose(fp));
177 return result;
178 }

Field Documentation

◆ alarm_delay_

uint32_t PWM::alarm_delay_ = 300
private

Default alarm delay in ms.

Definition at line 156 of file bsp_pwm.hpp.

◆ alarm_duration_

uint32_t PWM::alarm_duration_ = 300
private

Default alarm duration in ms.

Definition at line 155 of file bsp_pwm.hpp.

◆ alarm_freq_

uint32_t PWM::alarm_freq_ = 1500
private

Default alarm frequency in Hz.

Definition at line 154 of file bsp_pwm.hpp.

◆ channel_

int PWM::channel_
private

PWM channel number.

Definition at line 152 of file bsp_pwm.hpp.

◆ chip_path_

std::string PWM::chip_path_
private

Sysfs path to the PWM chip.

Definition at line 149 of file bsp_pwm.hpp.

◆ duration_ms_

uint32_t PWM::duration_ms_
private

Duration of note in ms.

Definition at line 161 of file bsp_pwm.hpp.

◆ note_

NoteName PWM::note_
private

Current note name.

Definition at line 159 of file bsp_pwm.hpp.

◆ note_sem_

std::binary_semaphore PWM::note_sem_ {0}
private

Semaphore to trigger note play.

Definition at line 162 of file bsp_pwm.hpp.

162{0};

◆ note_thread_

std::thread PWM::note_thread_
private

Background thread for playing notes.

Definition at line 158 of file bsp_pwm.hpp.

◆ octave_

uint32_t PWM::octave_
private

Current octave.

Definition at line 160 of file bsp_pwm.hpp.

◆ period_ns_

int PWM::period_ns_
private

PWM period in nanoseconds.

Definition at line 151 of file bsp_pwm.hpp.

◆ pwm_path_

std::string PWM::pwm_path_
private

Sysfs path to the PWM channel.

Definition at line 150 of file bsp_pwm.hpp.


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