Chumbaka

Dependencies:   Servo mbed Chumbaka_backup

Dependents:   Chumbaka_backup

Fork of Servo_HelloWorld by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
limcheecheng
Date:
Thu Apr 17 06:20:22 2014 +0000
Parent:
4:4e2ad2aa99b7
Child:
6:1141e5682610
Commit message:
updated to PWM speaker

Changed in this revision

Speaker/Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Speaker/Speaker.h	Wed Apr 16 10:39:31 2014 +0000
+++ b/Speaker/Speaker.h	Thu Apr 17 06:20:22 2014 +0000
@@ -1,52 +1,19 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
 class Speaker
 {
 public:
     Speaker(PinName pin) : _pin(pin) {
 // _pin(pin) means pass pin to the Speaker Constructor
-// precompute 32 sample points on one sine wave cycle
-// used for continuous sine wave output later
-        for(int k=0; k<32; k++) {
-            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
-            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
-        }
-
     }
-// class method to play a note based on AnalogOut class
+// class method to play a note based on PwmOut class
     void PlayNote(float frequency, float duration, float volume) {
-        // scale samples using current volume level arg
-        for(int k=0; k<32; k++) {
-            Analog_scaled_data[k] = Analog_out_data[k] * volume;
-        }
-        // reset to start of sample array
-        i=0;
-        // turn on timer interrupts to start sine wave output
-        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
-        // play note for specified time
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
         wait(duration);
-        // turns off timer interrupts
-        Sample_Period.detach();
-        // sets output to mid range - analog zero
-        this->_pin.write_u16(32768);
-
+        _pin = 0.0;
     }
-private:
-// sets up specified pin for analog using AnalogOut class
-    AnalogOut _pin;
-    // set up a timer to be used for sample rate interrupts
-    Ticker Sample_Period;
 
-    //variables used by interrupt routine and PlayNote
-    volatile int i;
-    short unsigned Analog_out_data[32];
-    short unsigned Analog_scaled_data[32];
-
-// Interrupt routine
-// used to output next analog sample whenever a timer interrupt occurs
-    void Sample_timer_interrupt(void) {
-        // send next analog sample out to D to A
-        this->_pin.write_u16(Analog_scaled_data[i]);
-        // increment pointer and wrap around back to 0 at 32
-        i = (i+1) & 0x01F;
-    }
-};
-
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- a/main.cpp	Wed Apr 16 10:39:31 2014 +0000
+++ b/main.cpp	Thu Apr 17 06:20:22 2014 +0000
@@ -1,20 +1,12 @@
-// Hello World to sweep a servo through its full range
+#include "mbed.h"
 
-#include "mbed.h"
-#include "Servo.h"
-
-Servo myservo(PTD4);
-
-int main() {    
+DigitalOut myled(LED2);
 
-//        myservo.calibrate(0.00095, 90.0);
-
-while (1){
-        myservo = 0;
-        wait(1);
-        myservo = 0.5;
-        wait(2);
-        myservo = 1.0;
-        wait(3);
-            }
-    }
\ No newline at end of file
+int main() {
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}