Playing around with making some modifications to the default RETRO firmware to add some more sounds, and I’m seeing some odd behavior.
If I set the period of the PWM object using either period_ms or period_us, it seems to work fine. If I try to set the period using just period (which takes a float argument), I get an initial click sound, then nothing. I’ve tried using the same frequency across all three methods, and only period(float) isn’t working.
For example, I modified the checkLives function to play a sound when the game is over:
void Game::checkLives() {
if (this->lives == 0) {
this->disp.clear();
this->drawString(Game::LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT);
this->drawString(Game::LOSE_2, DisplayN18::HEIGHT / 2);
this->pwm.period_us(4545);
this->pwm.write(0.5);
wait(1);
this->pwm.write(0.0);
while (this->circle.read())
wait_ms(1);
this->initialize();
}
else {
this->disp.drawCharacter(0, 0, static_cast<char>(this->lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK);
}
}
The code above works fine. But if I change this->pwm.period_us(4545) to this->pwm.period(1/220), no sound. The two should be equivalent, however.
Stranger still, if I use the following program (from one of the mbed PDF docs on PWM) as a standalone program, period(float) works fine:
// Oranges and Lemons program
#include "mbed.h"
PwmOut buzzer(P0_18);
float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440};
//frequency array
float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2};
//beat array
int main() {
while (1) {
for (int i=0; i<=11; i++) {
buzzer.period(1/(frequency[i]));
// set PWM period
buzzer=0.5;
// set duty cycle
wait(0.5*beat[i]);
// hold for beat period
}
}
}
My assumption is that I must be doing something wrong, but I’m not sure what. Am I missing something obvious here?