sounds

Dependents:   Lab4

Committer:
trmontgomery
Date:
Fri Apr 05 19:42:09 2019 +0000
Revision:
0:fee2c7336410
Finished Lab 4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trmontgomery 0:fee2c7336410 1 #include "mbed.h"
trmontgomery 0:fee2c7336410 2 #include "buzzy_siren.h"
trmontgomery 0:fee2c7336410 3 #include "buzzy_begin.h"
trmontgomery 0:fee2c7336410 4 #include "buzzy_chomp.h"
trmontgomery 0:fee2c7336410 5 #include "buzzy_eatghost.h"
trmontgomery 0:fee2c7336410 6 #include "buzzy_death.h"
trmontgomery 0:fee2c7336410 7 #include "buzzy_eatfruit.h"
trmontgomery 0:fee2c7336410 8
trmontgomery 0:fee2c7336410 9
trmontgomery 0:fee2c7336410 10 class Speaker : public PwmOut
trmontgomery 0:fee2c7336410 11 {
trmontgomery 0:fee2c7336410 12 public:
trmontgomery 0:fee2c7336410 13 enum BUZZY_Sounds {NO_SOUND, BEGIN, SIREN, CHOMP, EAT_GHOST, DEATH, EAT_FRUIT};
trmontgomery 0:fee2c7336410 14
trmontgomery 0:fee2c7336410 15 Speaker(PinName nPin):PwmOut(nPin)
trmontgomery 0:fee2c7336410 16 {
trmontgomery 0:fee2c7336410 17 m_ulAudioArrayIndex = 0;
trmontgomery 0:fee2c7336410 18 SwitchSound(NO_SOUND);
trmontgomery 0:fee2c7336410 19 }
trmontgomery 0:fee2c7336410 20
trmontgomery 0:fee2c7336410 21 void PlayNextValue()
trmontgomery 0:fee2c7336410 22 {
trmontgomery 0:fee2c7336410 23 if (m_pAudioArray != NULL)
trmontgomery 0:fee2c7336410 24 {
trmontgomery 0:fee2c7336410 25 write((float)m_pAudioArray[ GetNextValue() ]/255.0f);
trmontgomery 0:fee2c7336410 26 }
trmontgomery 0:fee2c7336410 27 }
trmontgomery 0:fee2c7336410 28
trmontgomery 0:fee2c7336410 29 void SwitchSound ( const BUZZY_Sounds &newSound)
trmontgomery 0:fee2c7336410 30 {
trmontgomery 0:fee2c7336410 31 if (newSound == m_enActiveAudioArray)
trmontgomery 0:fee2c7336410 32 {
trmontgomery 0:fee2c7336410 33 return;
trmontgomery 0:fee2c7336410 34 }
trmontgomery 0:fee2c7336410 35
trmontgomery 0:fee2c7336410 36 m_ulAudioArrayIndex = 0;
trmontgomery 0:fee2c7336410 37 m_enActiveAudioArray = newSound;
trmontgomery 0:fee2c7336410 38 switch (newSound)
trmontgomery 0:fee2c7336410 39 {
trmontgomery 0:fee2c7336410 40 case NO_SOUND:
trmontgomery 0:fee2c7336410 41 m_pAudioArray = NULL;
trmontgomery 0:fee2c7336410 42 break;
trmontgomery 0:fee2c7336410 43 case BEGIN:
trmontgomery 0:fee2c7336410 44 m_pAudioArray = &data_BEGIN[0];
trmontgomery 0:fee2c7336410 45 break;
trmontgomery 0:fee2c7336410 46 case CHOMP:
trmontgomery 0:fee2c7336410 47 m_pAudioArray = &data_CHOMP[0];
trmontgomery 0:fee2c7336410 48 break;
trmontgomery 0:fee2c7336410 49 case EAT_GHOST:
trmontgomery 0:fee2c7336410 50 m_pAudioArray = &data_GHOST[0];
trmontgomery 0:fee2c7336410 51 break;
trmontgomery 0:fee2c7336410 52 case DEATH:
trmontgomery 0:fee2c7336410 53 m_pAudioArray = &data_DEATH[0];
trmontgomery 0:fee2c7336410 54 break;
trmontgomery 0:fee2c7336410 55 case EAT_FRUIT:
trmontgomery 0:fee2c7336410 56 m_pAudioArray = &data_EAT_FRUIT[0];
trmontgomery 0:fee2c7336410 57 break;
trmontgomery 0:fee2c7336410 58 case SIREN:
trmontgomery 0:fee2c7336410 59 default:
trmontgomery 0:fee2c7336410 60 m_pAudioArray = &data_SIREN[0];
trmontgomery 0:fee2c7336410 61 break;
trmontgomery 0:fee2c7336410 62 }
trmontgomery 0:fee2c7336410 63
trmontgomery 0:fee2c7336410 64 }
trmontgomery 0:fee2c7336410 65
trmontgomery 0:fee2c7336410 66 unsigned int GetNextValue()
trmontgomery 0:fee2c7336410 67 {
trmontgomery 0:fee2c7336410 68 m_ulAudioArrayIndex++;
trmontgomery 0:fee2c7336410 69 switch (m_enActiveAudioArray)
trmontgomery 0:fee2c7336410 70 {
trmontgomery 0:fee2c7336410 71 case BEGIN:
trmontgomery 0:fee2c7336410 72 m_ulAudioArrayIndex %= NUM_BEGIN_ELEMENTS;
trmontgomery 0:fee2c7336410 73 break;
trmontgomery 0:fee2c7336410 74 case CHOMP:
trmontgomery 0:fee2c7336410 75 m_ulAudioArrayIndex %= NUM_CHOMP_ELEMENTS;
trmontgomery 0:fee2c7336410 76 break;
trmontgomery 0:fee2c7336410 77 case EAT_GHOST:
trmontgomery 0:fee2c7336410 78 m_ulAudioArrayIndex %= NUM_GHOST_ELEMENTS;
trmontgomery 0:fee2c7336410 79 break;
trmontgomery 0:fee2c7336410 80 case DEATH:
trmontgomery 0:fee2c7336410 81 m_ulAudioArrayIndex %= NUM_DEATH_ELEMENTS;
trmontgomery 0:fee2c7336410 82 if (m_ulAudioArrayIndex == 0)
trmontgomery 0:fee2c7336410 83 {
trmontgomery 0:fee2c7336410 84 SwitchSound(SIREN);
trmontgomery 0:fee2c7336410 85 }
trmontgomery 0:fee2c7336410 86 break;
trmontgomery 0:fee2c7336410 87 case EAT_FRUIT:
trmontgomery 0:fee2c7336410 88 m_ulAudioArrayIndex %= NUM_EAT_FRUIT_ELEMENTS;
trmontgomery 0:fee2c7336410 89 break;
trmontgomery 0:fee2c7336410 90 case SIREN:
trmontgomery 0:fee2c7336410 91 default:
trmontgomery 0:fee2c7336410 92 m_ulAudioArrayIndex %= NUM_SIREN_ELEMENTS;
trmontgomery 0:fee2c7336410 93 break;
trmontgomery 0:fee2c7336410 94 }
trmontgomery 0:fee2c7336410 95 return m_ulAudioArrayIndex;
trmontgomery 0:fee2c7336410 96 }
trmontgomery 0:fee2c7336410 97 private:
trmontgomery 0:fee2c7336410 98 unsigned long m_ulAudioArrayIndex;
trmontgomery 0:fee2c7336410 99 BUZZY_Sounds m_enActiveAudioArray;
trmontgomery 0:fee2c7336410 100 const unsigned char *m_pAudioArray;
trmontgomery 0:fee2c7336410 101
trmontgomery 0:fee2c7336410 102 };
trmontgomery 0:fee2c7336410 103
trmontgomery 0:fee2c7336410 104
trmontgomery 0:fee2c7336410 105