Carter Montgomery / Sounds

Dependents:   Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Speaker.h Source File

Speaker.h

00001 #include "mbed.h"
00002 #include "buzzy_siren.h"
00003 #include "buzzy_begin.h"
00004 #include "buzzy_chomp.h"
00005 #include "buzzy_eatghost.h"
00006 #include "buzzy_death.h"
00007 #include "buzzy_eatfruit.h"
00008 
00009 
00010 class Speaker : public PwmOut
00011 {
00012 public:
00013     enum BUZZY_Sounds {NO_SOUND, BEGIN, SIREN, CHOMP, EAT_GHOST, DEATH, EAT_FRUIT};
00014     
00015     Speaker(PinName nPin):PwmOut(nPin)
00016     {
00017        m_ulAudioArrayIndex = 0;
00018        SwitchSound(NO_SOUND);
00019     }
00020     
00021     void PlayNextValue()
00022     {
00023         if (m_pAudioArray != NULL)
00024         {
00025             write((float)m_pAudioArray[ GetNextValue() ]/255.0f);
00026         }       
00027     }
00028     
00029     void SwitchSound ( const BUZZY_Sounds &newSound)
00030     {
00031         if (newSound == m_enActiveAudioArray)
00032         {
00033             return;
00034         }
00035             
00036         m_ulAudioArrayIndex = 0;
00037         m_enActiveAudioArray = newSound;
00038         switch (newSound)
00039         {           
00040             case NO_SOUND:
00041                 m_pAudioArray = NULL;
00042                 break;   
00043             case BEGIN:
00044                 m_pAudioArray = &data_BEGIN[0];
00045                 break;   
00046             case CHOMP:
00047                  m_pAudioArray = &data_CHOMP[0];
00048                 break;
00049             case EAT_GHOST:
00050                 m_pAudioArray = &data_GHOST[0];
00051                 break;
00052             case DEATH:
00053                 m_pAudioArray = &data_DEATH[0];
00054                 break;
00055             case EAT_FRUIT:
00056                 m_pAudioArray = &data_EAT_FRUIT[0];
00057                 break;              
00058             case SIREN:
00059             default:
00060                 m_pAudioArray = &data_SIREN[0];
00061                 break; 
00062         }
00063                              
00064     }
00065     
00066     unsigned int GetNextValue()
00067     {
00068         m_ulAudioArrayIndex++;
00069         switch (m_enActiveAudioArray)
00070         {           
00071             case BEGIN:
00072                 m_ulAudioArrayIndex %= NUM_BEGIN_ELEMENTS;
00073                 break;   
00074             case CHOMP:
00075                  m_ulAudioArrayIndex %= NUM_CHOMP_ELEMENTS;
00076                 break;
00077             case EAT_GHOST:
00078                  m_ulAudioArrayIndex %= NUM_GHOST_ELEMENTS;
00079                 break;
00080             case DEATH:
00081                  m_ulAudioArrayIndex %= NUM_DEATH_ELEMENTS;
00082                  if (m_ulAudioArrayIndex == 0)
00083                  {
00084                      SwitchSound(SIREN);
00085                  }
00086                 break;
00087             case EAT_FRUIT:
00088                  m_ulAudioArrayIndex %= NUM_EAT_FRUIT_ELEMENTS;
00089                 break;              
00090             case SIREN:
00091             default:
00092                  m_ulAudioArrayIndex %= NUM_SIREN_ELEMENTS;
00093                 break;          
00094         }
00095         return m_ulAudioArrayIndex;
00096     }
00097 private:
00098     unsigned long m_ulAudioArrayIndex;
00099     BUZZY_Sounds m_enActiveAudioArray;
00100     const unsigned char *m_pAudioArray;
00101     
00102 };
00103 
00104             
00105