Leong Kum Loong (NYP) / Mbed 2 deprecated Tone_Example

Dependencies:   C12832 mbed tone_h

Files at this revision

API Documentation at this revision

Comitter:
reesey
Date:
Mon Nov 20 03:54:22 2017 +0000
Parent:
2:826e915c3452
Commit message:
Written by Leong Kum Loong.; 20 Nov 2017, REV. 2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
tone.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 826e915c3452 -r b6163cc5d7b1 main.cpp
--- a/main.cpp	Sun Nov 19 10:18:27 2017 +0000
+++ b/main.cpp	Mon Nov 20 03:54:22 2017 +0000
@@ -4,28 +4,92 @@
 
 /*
     Written by Leong Kum Loong.
-    19 Nov 2017, REV. 1
+    20 Nov 2017, REV. 2
     
     When tone.h reference is included, DO NOT declare pin p26 as it is used by my library.
 */
 
 C12832 lcd(p5, p7, p6, p8, p11);
-DigitalIn up(p15), down(p12);
+DigitalIn up(p15), down(p12), left(p13), right(p16), ctr(p14);
 
-int main() {
+int frequency = 500;    //Preset tone frequency.
+
+void writeMenu(){
     lcd.cls();
     lcd.locate(0,1);
-    lcd.printf("Push joystick to play");
+    lcd.printf("UP/DOWN for example.");
     lcd.locate(0,12);
-    lcd.printf("UP for Mario");
+    lcd.printf("LEFT/RIGHT set frequency.");
     lcd.locate(0,22);
-    lcd.printf("DOWN for Star Wars");
+    lcd.printf("BUTTON to PLAY/STOP.");
+}
+
+void writeFreq(){
+    lcd.cls();
+    lcd.locate(0,1);
+    lcd.printf("Frequency: %dHz", frequency);
+}
+
+int main() {
+    writeMenu();
+    int play;
     
     while(1) {
         if(up){
             mario_example();
         }else if(down){
             starwars_example();    
+        }else if(left){
+            int i=0;
+            do{
+                while(left){
+                    if(frequency > 0)frequency-=100;
+                    wait_ms(100);
+                    writeFreq();
+                }
+                wait_ms(300);
+                while(i<20){
+                    if(left){
+                        break;
+                    }else if(right){
+                        i=20;
+                        break;
+                    }
+                    wait_ms(100);
+                    i++;
+                }
+            } while(i<20);
+            writeMenu();
+        }else if(right){
+            int i=0;
+            do{
+                while(right){
+                    if(frequency < 20000)frequency+=100;
+                    wait_ms(100);
+                    writeFreq();
+                }
+                wait_ms(300);
+                while(i<20){
+                    if(right){
+                        break;
+                    }else if(left){
+                        i=20;
+                        break;
+                    }
+                    wait_ms(100);
+                    i++;
+                }
+            }while(i<20);
+            writeMenu();
+        }else if(ctr){
+            while(ctr);
+            wait_ms(300);
+            play=!play;
+            if(play){
+                tone(frequency);
+            }else{
+                noTone();
+            }
         }
     }
 }
\ No newline at end of file
diff -r 826e915c3452 -r b6163cc5d7b1 tone.lib
--- a/tone.lib	Sun Nov 19 10:18:27 2017 +0000
+++ b/tone.lib	Mon Nov 20 03:54:22 2017 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/Leong-Kum-Loong/code/tone_h/#7f2974b69b48
+https://os.mbed.com/teams/Leong-Kum-Loong/code/tone_h/#9ddabd24f48c