Simple tool for PWM piezo speaker
Piezo.cpp@3:df1e38c2c57b, 2016-10-01 (annotated)
- Committer:
- TeaPack_CZ
- Date:
- Sat Oct 01 21:24:26 2016 +0000
- Revision:
- 3:df1e38c2c57b
- Parent:
- 0:39de01daa2de
Add documentation for public functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TeaPack_CZ | 0:39de01daa2de | 1 | #include "Piezo.h" |
TeaPack_CZ | 0:39de01daa2de | 2 | |
TeaPack_CZ | 0:39de01daa2de | 3 | Piezo::Piezo(PinName Pwmout):_pwm(Pwmout) |
TeaPack_CZ | 0:39de01daa2de | 4 | { |
TeaPack_CZ | 0:39de01daa2de | 5 | min_freq = 1.0; |
TeaPack_CZ | 0:39de01daa2de | 6 | max_freq = 20000.0; |
TeaPack_CZ | 0:39de01daa2de | 7 | |
TeaPack_CZ | 0:39de01daa2de | 8 | _pwm.period(1); |
TeaPack_CZ | 0:39de01daa2de | 9 | _pwm.write(0.0); |
TeaPack_CZ | 0:39de01daa2de | 10 | } |
TeaPack_CZ | 0:39de01daa2de | 11 | |
TeaPack_CZ | 0:39de01daa2de | 12 | void Piezo::play(float frequency, int duration_ms) |
TeaPack_CZ | 0:39de01daa2de | 13 | { |
TeaPack_CZ | 0:39de01daa2de | 14 | float Frequency = (frequency < min_freq)? min_freq : frequency; |
TeaPack_CZ | 0:39de01daa2de | 15 | Frequency = (frequency > max_freq)? max_freq : frequency; |
TeaPack_CZ | 0:39de01daa2de | 16 | |
TeaPack_CZ | 0:39de01daa2de | 17 | Period = 1.0/Frequency; |
TeaPack_CZ | 0:39de01daa2de | 18 | |
TeaPack_CZ | 0:39de01daa2de | 19 | _pwm.period(Period); |
TeaPack_CZ | 0:39de01daa2de | 20 | _pwm.write(0.5); |
TeaPack_CZ | 0:39de01daa2de | 21 | wait_ms(duration_ms); |
TeaPack_CZ | 0:39de01daa2de | 22 | _pwm.write(0.0); |
TeaPack_CZ | 0:39de01daa2de | 23 | } |
TeaPack_CZ | 0:39de01daa2de | 24 | |
TeaPack_CZ | 0:39de01daa2de | 25 | void Piezo::playAsync(float frequency, int duration_ms) |
TeaPack_CZ | 0:39de01daa2de | 26 | { |
TeaPack_CZ | 0:39de01daa2de | 27 | float Frequency = (frequency < min_freq)? min_freq : frequency; |
TeaPack_CZ | 0:39de01daa2de | 28 | Frequency = (frequency > max_freq)? max_freq : frequency; |
TeaPack_CZ | 0:39de01daa2de | 29 | |
TeaPack_CZ | 0:39de01daa2de | 30 | Period = 1.0/Frequency; |
TeaPack_CZ | 0:39de01daa2de | 31 | |
TeaPack_CZ | 0:39de01daa2de | 32 | _pwm.period(Period); |
TeaPack_CZ | 0:39de01daa2de | 33 | _pwm.write(0.5); |
TeaPack_CZ | 0:39de01daa2de | 34 | _timer.detach(); |
TeaPack_CZ | 0:39de01daa2de | 35 | _timer.attach_us(this,&Piezo::stop, duration_ms*1000); |
TeaPack_CZ | 0:39de01daa2de | 36 | } |
TeaPack_CZ | 0:39de01daa2de | 37 | |
TeaPack_CZ | 0:39de01daa2de | 38 | void Piezo::stop(void) |
TeaPack_CZ | 0:39de01daa2de | 39 | { |
TeaPack_CZ | 0:39de01daa2de | 40 | _pwm.write(0.0); |
TeaPack_CZ | 0:39de01daa2de | 41 | } |