AUP_Lab7_RTOS

Dependencies:   C12832 MMA7660 mbed-rtos mbed

Fork of AUP_Lab6_Music by Lei Lei

Files at this revision

API Documentation at this revision

Comitter:
BrentLei
Date:
Wed Jul 08 05:34:29 2015 +0000
Parent:
3:640558c1c0d3
Commit message:
update

Changed in this revision

SongPlayer.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
--- a/SongPlayer.h	Wed Jul 08 00:09:57 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-#include "mbed.h"
-// new class to play a note on Speaker based on PwmOut class
-class SongPlayer
-{
-public:
-    SongPlayer(PinName pin) : _pin(pin) {
-// _pin(pin) means pass pin to the constructor
-    }
-// class method to play a note based on PwmOut class
-    void PlaySong(float frequency[], float duration[], float volume=1.0) {
-        vol = volume;
-        notecount = 0;
-        _pin.period(1.0/frequency[notecount]);
-        _pin = volume/2.0;
-        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
-        // setup timer to interrupt for next note to play
-        frequencyptr = frequency;
-        durationptr = duration;
-        //returns after first note starts to play
-    }
-    void nextnote();
-private:
-    Timeout noteduration;
-    PwmOut _pin;
-    int notecount;
-    float vol;
-    float * frequencyptr;
-    float * durationptr;
-};
-//Interrupt Routine to play next note
-void SongPlayer::nextnote()
-{
-    _pin = 0.0;
-    notecount++; //setup next note in song
-    if (durationptr[notecount]!=0.0) {
-        _pin.period(1.0/frequencyptr[notecount]);
-        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
-        _pin = vol/2.0;
-    } else
-        _pin = 0.0; //turn off on last note
-}
\ No newline at end of file
--- a/main.cpp	Wed Jul 08 00:09:57 2015 +0000
+++ b/main.cpp	Wed Jul 08 05:34:29 2015 +0000
@@ -1,59 +1,87 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "C12832.h"
 #include "MMA7660.h"
 
-// 添加旋律播放相关文件
-#include "SongPlayer.h"
+// Define the mutex
+Mutex lcd_mutex;
+Mutex acc_mutex;
+
+PwmOut      led(D9);
+AnalogIn    pot1(A0);
+C12832      lcd(D11, D13, D12, D7, D10);
+MMA7660     MMA(D14, D15);
 
-// 旋律数据
-float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
-                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
-                };
-float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
-                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
-                    };
+// Adjust the brightness of the LED
+void adjust_brightness(void const *args){
+    while(1){
+        led = 1 - pot1.read();
+        Thread::wait(50);
+    }
+}
+
+//Display LED brightness on the LCD
+void disp_led_thread(void const *args){
+    while(1){
+        lcd_mutex.lock();
+        lcd.locate(0, 0);
+        lcd.printf("Brightness: %.2f", 1 - led);
+        lcd_mutex.unlock();
+        Thread::wait(100);
+    }
+}
 
-PwmOut led(D5);
-DigitalIn button_up(A2);
-DigitalIn button_center(D4);
-DigitalIn button_down(A3);
-C12832 lcd(D11, D13, D12, D7, D10);
-MMA7660 MMA(D14, D15);
+// Display acceleration x on the LCD
+void disp_x_thread(void const *args){
+    while(1){
+        lcd_mutex.lock();
+        acc_mutex.lock();
+        lcd.locate(0, 8);
+        lcd.printf("x=%.2f", MMA.x());
+        acc_mutex.unlock();
+        lcd_mutex.unlock();
+        Thread::wait(100);
+    }
+}
 
-// 初始化D6引脚作为PWM用于驱动扬声器
-SongPlayer mySpeaker(D6);
+// Display acceleration y on the LCD
+void disp_y_thread(void const *args){
+    while(1){
+        lcd_mutex.lock();
+        acc_mutex.lock();
+        lcd.locate(40, 8);
+        lcd.printf("y=%.2f", MMA.y());
+        acc_mutex.unlock();
+        lcd_mutex.unlock();
+        Thread::wait(100);
+    }
+}
 
-double brightness = 1.0;
-double brightness_inc = 0.1;
+// Display acceleration z on the LCD
+void disp_z_thread(void const *args){
+    while(1){
+        lcd_mutex.lock();
+        acc_mutex.lock();
+        lcd.locate(80, 8);
+        lcd.printf("z=%.2f", MMA.z());
+        acc_mutex.unlock();
+        lcd_mutex.unlock();
+        Thread::wait(100);
+    }
+}
 
 int main()
 {
-    int bt_flag = 0;
     lcd.cls();
-    led.write(brightness);
-    
-    // 播放旋律
-    mySpeaker.PlaySong(note,duration,0.05);
-    
+
+    Thread thread1(adjust_brightness);
+    Thread thread2(disp_led_thread);
+    Thread thread3(disp_x_thread);
+    Thread thread4(disp_y_thread);
+    Thread thread5(disp_z_thread);
+
+    // Sleep and wait for interrupt
     while (1) {
-        bt_flag = 1;
-        if(button_up==1)
-            brightness -= brightness_inc;
-        else if(button_down==1)
-            brightness += brightness_inc;
-        else if(button_center==1)
-            brightness = (brightness>0.5)?1.0:0.0;
-        else
-            bt_flag = 0;
-        if(bt_flag==1)
-        {
-            brightness = (brightness>1.0)?0.0:brightness;
-            brightness = (brightness<0.0)?1.0:brightness;
-            led.write(brightness);
-        }
-        lcd.locate(0, 0);
-        lcd.printf("Brightness = %.1f\r\n", 1.0 - brightness);
-        lcd.printf("x=%.2f y=%.2f z=%.2f", MMA.x(), MMA.y(), MMA.z());
-        wait(0.2);
+        __wfi();
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Jul 08 05:34:29 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#e695cd34556b