Coursework
Embed:
(wiki syntax)
Show/hide line numbers
Piezo.cpp
00001 #include "Piezo.h" 00002 00003 Piezo::Piezo(PinName Pwmout):_pwm(Pwmout) 00004 { 00005 min_freq = 1.0; 00006 max_freq = 20000.0; 00007 00008 _pwm.period(1); 00009 _pwm.write(0.0); 00010 } 00011 00012 void Piezo::play(float frequency, int duration_ms) 00013 { 00014 float Frequency = (frequency < min_freq)? min_freq : frequency; 00015 Frequency = (frequency > max_freq)? max_freq : frequency; 00016 00017 Period = 1.0/Frequency; 00018 00019 _pwm.period(Period); 00020 _pwm.write(0.5); 00021 wait_ms(duration_ms); 00022 _pwm.write(0.0); 00023 } 00024 00025 void Piezo::playAsync(float frequency, int duration_ms) 00026 { 00027 float Frequency = (frequency < min_freq)? min_freq : frequency; 00028 Frequency = (frequency > max_freq)? max_freq : frequency; 00029 00030 Period = 1.0/Frequency; 00031 00032 _pwm.period(Period); 00033 _pwm.write(0.5); 00034 _timer.detach(); 00035 _timer.attach_us(this,&Piezo::stop, duration_ms*1000); 00036 } 00037 00038 void Piezo::stop(void) 00039 { 00040 _pwm.write(0.0); 00041 }
Generated on Tue Aug 23 2022 22:47:18 by
1.7.2