test publish
Dependencies: mbed GroveEarbudSensor
Revision 4:23aba111f73a, committed 2016-03-25
- Comitter:
- age2pierre
- Date:
- Fri Mar 25 09:34:21 2016 +0000
- Parent:
- 3:b4f04b8b8797
- Child:
- 5:ee265ab0752d
- Commit message:
- Added Notes enum class and Speaker class
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Notes.h Fri Mar 25 09:34:21 2016 +0000 @@ -0,0 +1,43 @@ +#ifndef _NOTES_H_ +#define _NOTES_H_ + +enum class Notes { + DO_4, + DO_d_4, + RE_4, + RE_d_4, + MI_4, + FA_4, + FA_d_4, + SOL_4, + SOL_d_4, + LA_4, + LA_d_4, + SI_4, + DO_5, + DO_d_5, + RE_5, + RE_d_5, + MI_5, + FA_5, + FA_d_5, + SOL_5, + SOL_d_5, + LA_5, + LA_d_5, + SI_5, + DO_6, + DO_d_6, + RE_6, + RE_d_6, + MI_6, + FA_6, + FA_d_6, + SOL_6, + SOL_d_6, + LA_6, + LA_d_6, + SI_6 +} + +#endif
--- a/Speaker.cpp Thu Mar 24 16:35:09 2016 +0000 +++ b/Speaker.cpp Fri Mar 25 09:34:21 2016 +0000 @@ -0,0 +1,140 @@ +#include "Speaker.h" + +Speaker::Speaker(PwmOut argPin) +{ + this->pin = argPin; + this->pulseWidth = 0.5; +} + +void Speaker::play(Notes note) +{ + switch(note) { + case Notes::DO_4 : + pin->period_us(1911); + break; + case Notes::DO_d_4 : + pin->period_us(1804); + break; + case Notes::RE_4 : + pin->period_us(1703); + break; + case Notes::RE_d_4 : + pin->period_us(1607); + break; + case Notes::MI_4 : + pin->period_us(1517); + break; + case Notes::FA_4 : + pin->period_us(1432); + break; + case Notes::FA_d_4 : + pin->period_us(1351); + break; + case Notes::SOL_4 : + pin->period_us(1276); + break; + case Notes::SOL_d_4 : + pin->period_us(1204); + break; + case Notes::LA_4 : + pin->period_us(1136); + break; + case Notes::LA_d_4 : + pin->period_us(1073); + break; + case Notes::SI_4 : + pin->period_us(1012); + break; + case Notes::DO_5 : + pin->period_us(956); + break; + case Notes::DO_d_5 : + pin->period_us(902); + break; + case Notes::RE_5 : + pin->period_us(851); + break; + case Notes::RE_d_5 : + pin->period_us(804); + break; + case Notes::MI_5 : + pin->period_us(758); + break; + case Notes::FA_5 : + pin->period_us(716); + break; + case Notes::FA_d_5 : + pin->period_us(676); + break; + case Notes::SOL_5 : + pin->period_us(638); + break; + case Notes::SOL_d_5 : + pin->period_us(602); + break; + case Notes::LA_5 : + pin->period_us(568); + break; + case Notes::LA_d_5 : + pin->period_us(536); + break; + case Notes::SI_5 : + pin->period_us(506); + break; + case Notes::DO_6 : + pin->period_us(478); + break; + case Notes::DO_d_6 : + pin->period_us(451); + break; + case Notes::RE_6 : + pin->period_us(426); + break; + case Notes::RE_d_6 : + pin->period_us(402); + break; + case Notes::MI_6 : + pin->period_us(379); + break; + case Notes::FA_6 : + pin->period_us(358); + break; + case Notes::FA_d_6 : + pin->period_us(338); + break; + case Notes::SOL_6 : + pin->period_us(318); + break; + case Notes::SOL_d_6 : + pin->period_us(301); + break; + case Notes::LA_6 : + pin->period_us(284); + break; + case Notes::LA_d_6 : + pin->period_us(268); + break; + case Notes::SI_6 : + pin->period_us(253); + break; + case default : + this->mute(); + break; + } + pin->write(this->pulse); +} + +bool Speaker::setPulseWidth(float argPulseWidth) +{ + if ((argPulseWidth >= 0.0) && (argPulseWidth <= 1.0)) { + this->pulseWidth = argPulseWidth; + return true; + } + else + return false; +} + +void Speaker::mute() +{ + +} \ No newline at end of file
--- a/Speaker.h Thu Mar 24 16:35:09 2016 +0000 +++ b/Speaker.h Fri Mar 25 09:34:21 2016 +0000 @@ -0,0 +1,22 @@ +#ifndef _SPEAKER_H_ +#define _SPEAKER_H_ + +#include "mbed.h" +#include "Notes.h" + +/** +This class is used to generate music note through a speaker by sending a pwm signal +*/ +class Speaker { + private : + PwmOut* pin; + float pulseWidth; + protected : + public : + Speaker(PwmOut argPin); + bool setPulseWidth(float argPulseWidth); + void play(Notes note); + void mute(); +} + +#endif \ No newline at end of file