Coursework

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Piezo.h Source File

Piezo.h

00001 #ifndef PIEZO
00002 #define PIEZO
00003 
00004 #include "mbed.h"
00005 
00006 /** Class for the PWM Piezo speaker.
00007  *  This class is using PWM to make sounds with piezo speaker.
00008  *  Currently tested with LPC1768.\n\n
00009  *
00010  *
00011  *                                      Writen by: Jan Crha (TeaPack_CZ), 2016.
00012  *
00013  *  Last modified: 2016-10-01
00014  *
00015  *  Example:
00016  *  @code
00017  *  #include "mbed.h"
00018  *  #include "Piezo.h"
00019  *
00020  *  Piezo PS(p21);
00021  *
00022  *  int main(){
00023  *      int playTime = 120;
00024  *
00025  *      for(int i=0; i<2; i++)
00026  *      {
00027  *          PS.play(466.16,playTime);
00028  *          wait_ms(playTime);
00029  *          PS.play(415.30,playTime);
00030  *          wait_ms(playTime);
00031  *    
00032  *          PS.play(466.16,playTime);
00033  *          wait_ms(playTime);
00034  *          PS.play(349.23,playTime);
00035  *          wait_ms(playTime);
00036  *    
00037  *          PS.play(277.18,playTime/2);
00038  *          wait_ms(playTime/2);
00039  *          PS.play(349.23,playTime/2);
00040  *          wait_ms(playTime/2);
00041  *          wait_ms(playTime);
00042  *          PS.play(233.08,playTime/2);
00043  *          wait_ms(playTime/2);
00044  *    
00045  *          wait_ms(playTime*4);
00046  *      }
00047  *  }
00048  * @endcode
00049  */
00050 
00051 class Piezo
00052 {
00053 public:
00054     
00055     /** Constructor, Pin must be PWM capable*/
00056     Piezo(PinName Pwmout);
00057     
00058     /** Blocking function for playing sound of given frequency and
00059      *       duration in ms
00060      */
00061     void play(float, int);
00062     
00063     /** Nonblocking function for playing sound of given frequency and 
00064      *      duration in ms
00065      */
00066     void playAsync(float, int);
00067     
00068     /** Function for stopping all sounds */
00069     void stop();
00070     
00071 private:
00072     PwmOut _pwm;
00073     Ticker _timer;
00074 
00075     float Period;
00076 
00077     float min_freq;
00078     float max_freq;
00079 };
00080 
00081 #endif