Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD mbed MMA8451Q TSI
Diff: ButtonMusical.cpp
- Branch:
- mono
- Revision:
- 10:6e18b220e10c
- Parent:
- 8:60231840370f
--- a/ButtonMusical.cpp Wed Sep 23 15:38:04 2015 +0000
+++ b/ButtonMusical.cpp Fri Oct 16 16:38:29 2015 +0000
@@ -2,28 +2,33 @@
#include "Note.cpp"
#include "Button.cpp"
-class ButtonMusical: public Button{
+class ButtonMusical: public Button
+{
public:
- ButtonMusical(PinName pin, Note note, Speaker* speaker) : Button(pin), _note(note), sp(speaker) {
- pressTimes=0;
+
+ ButtonMusical(PinName pin, Note note, Speaker* speaker) : Button(pin), _note(note), sp(speaker) {
+ alreadyPressed=false;
}
-
void press() {
- if(pressTimes==0)
- {
+ if(!alreadyPressed) {
sp->changeFrequency(_note.getFrequency());
- pressTimes++;
+ buttonsPressed++;
+ alreadyPressed=true;
}
}
-
- void release() {
- sp->changeFrequency(-_note.getFrequency()*pressTimes);
- pressTimes=0;
+
+ void release() {
+ if(alreadyPressed) {
+ if(--buttonsPressed==0)
+ sp->stop();
+ alreadyPressed=false;
+ }
}
-
private:
Note _note;
- int pressTimes;
+ bool alreadyPressed;
Speaker* sp;
+
+ int static buttonsPressed;
};
\ No newline at end of file
