Nicholas Meyer / Mbed 2 deprecated RTOSS

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE RTOS FATFileSystem

Revision:
12:0fe16c716237
Parent:
11:50e44c84411c
diff -r 50e44c84411c -r 0fe16c716237 main.cpp
--- a/main.cpp	Tue Feb 20 17:54:41 2018 +0000
+++ b/main.cpp	Tue Oct 12 03:45:20 2021 +0000
@@ -2,214 +2,183 @@
 #include "rtos.h"
 #include "SDFileSystem.h"
 #include "wave_player.h"
-
-
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+#include "uLCD_4DGL.h"
 
-//LED lighting effects with sound using the RTOS
-//Plays the wave file "sample.wav" on the USB flash drive
-//using waveplayer code library
-//Class D style audio output on P26 or
-//use the Audio Out jack for connection to a set of amplified PC speakers (at higher volume)
-//Needs a USB flash drive inserted with the *.wav files on it to run with audio effects
-//Pins are setup for mbed LPC1768 on mbed application board
-//Analog Audio Out Jack for PC Speakers- Sparkfun also has a breakout
-//For more info see:
-//http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/
+BusOut myled(LED1,LED2,LED3,LED4);
+Serial  pc(USBTX, USBRX);
+Serial  dev(p28,p27);
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
 AnalogOut DACout(p18);
-//On Board Speaker on Application board but very low volume using PWM
-PwmOut PWMout(p26);
-//LEDs used in demo
-PwmOut myled2(LED2);
-PwmOut myled4(LED4);
-//Use a PWM output to enable dimming
-//1.0 is full on, 0.0 off, 0.5 50% on
+PwmOut red(p21);
+PwmOut green(p22);
+PwmOut blue(p23);
+wave_player waver(&DACout);
+DigitalOut led1(LED1);
+DigitalOut led4(LED4);
+
+Mutex mutex;
 
 inline float random_number()
 {
     return (rand()/(float(RAND_MAX)));
 }
-void beacon(void const *args)
-{
+
+void rgb(void const *args) {
     while(1) {
-        //LED warm up effect using PWM
-        for(int i=0; i<50; i++) {
-            myled2 = i/50.0;
-            Thread::wait(1000.0*0.02);
+        for (int i = 0; i < 10; i++) {
+            red = i / 10.0;
+            green = ((i + 3) % 10) / 10.0;
+            blue = ((i + 6) % 10) / 10.0;
+            Thread::wait(100);  
+            
         }
-        //LED at full brightness level
-        myled2 = 1.0;
-        Thread::wait(1000.0*0.25);
-        //LED cool down effect using PWM
-        for(int i=49; i>0; i--) {
-            myled2 = i/50.0;
-            Thread::wait(1000.0*0.02);
+
+        for (int i = 9; i >= 0; i--) {
+            red = i / 10.0;
+            green = ((i + 3) % 10) / 10.0;
+            blue = ((i + 6) % 10) / 10.0;
+            Thread::wait(100);  
         }
-        //LED off
-        myled2 = 0.0;
-        Thread::wait(1000.0*1.5);
+
     }
 }
-void welding(void const *args)
-{
-    float x = 0.0;
+
+void lcdStart(void const *args) {
+    while(1) { 
+        for (int i = 59; i >= 0; i--) {
+            mutex.lock();   
+            uLCD.locate(2,2);
+            uLCD.printf("%2d", i);
+            mutex.unlock();
+            Thread::wait(100);     
+        }
+    }
+}
+
+void lcdEnd(void const *args) {
     while(1) {
-        //get a new random number for PWM
-        x = random_number();
-        //add some exponential brightness scaling
-        //for more of a fast flash effect
-        myled2 = x*x*x;
-        //fast update rate for welding flashes
-        Thread::wait(1000.0*0.02);
-        //add a random pause between welds
-        if (random_number()>0.995) {
-            myled2 = 0.0;
-            Thread::wait(1000.0*random_number());
+        for (int i = 0; i <= 59; i++) {
+            mutex.lock();
+            uLCD.locate(0,0);
+            uLCD.printf("%2d", i);
+            mutex.unlock();
+            Thread::wait(100);
         }
     }
 }
-void fire(void const *args)
+
+void dev_recv()
 {
-    while(1) {
-//get a new random number for PWM
-        myled2 = random_number();
-//a bit slower time delay can be used for fire
-        wait(0.04);
+    led1 = !led1;
+    while(dev.readable()) {
+        mutex.lock();
+        pc.putc(dev.getc());
+        mutex.unlock();
     }
 }
-void police(void const *args)
-{
+
+void receive(void const *args) {
+    char bnum=0;
+    char bhit=0;
     while(1) {
-        //flash three times on LED1
-        for(int i=0; i<3; i++) {
-            //ramp up brightness level
-            for(double x = 0.0; x <= 1.0; x = x+0.2) {
-                myled2 = x*x;
-                wait(.02);
+        Thread::wait(20);
+        mutex.lock();
+        if (dev.getc()=='!') {
+            if (dev.getc()=='B') { //button data packet
+                bnum = dev.getc(); //button number
+                bhit = dev.getc(); //1=hit, 0=release
+                if (dev.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
+                    myled = bnum - '0'; //current button number will appear on LEDs
+                    switch (bnum) {
+                        case '1': //number button 1
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '2': //number button 2
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '3': //number button 3
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '4': //number button 4
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '5': //button 5 up arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '6': //button 6 down arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '7': //button 7 left arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '8': //button 8 right arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                }
             }
         }
-        myled2=0.0; //LED1 off
-        //flash three times on LED2
-        for(int i=0; i<3; i++) {
-            //ramp up brightness level
-            for(double x = 0.0; x <= 1.0; x = x+0.2) {
-                myled4 = x*x;
-                wait(.02);
-            }
-        }
-        myled4=0.0; //LED2 off
+        mutex.unlock();
     }
 }
-void lighthouse(void const *args)
+
+void pc_recv()
 {
-    float y=0.0;
-
-    while(1) {
-        for(double x=0.0; x <= 3.14159; x = x + 0.0314159) {
-            y = sin(x); //nice periodic function 0..1..0
-            myled2 = y*y*y;//exponential effect - needs a sharp peak
-            Thread::wait(1000.0*.025);
-        }
-        myled2 = 0.0;
-        //most lighthouses have a 5 second delay - so add another 2.5
-        Thread::wait(1000.0*2.5);
+    led4 = !led4;
+    while(pc.readable()) {
+        mutex.lock();
+        dev.putc(pc.getc());
+        mutex.unlock();
     }
 }
 
 
-//function to simulate incandescent bulb warm up
-//and cool down cycles
-void incandescent_bulb(PwmOut led, float on_time = 0.1,
-                       float warm_time = 0.2)
-{
-    //LED warm up effect using PWM
-    for(int i=0; i<10; i++) {
-        led = i/10.0;
-        Thread::wait(1000.0*warm_time/10.0);
-    }
-    //LED at full brightness level
-    led = 1.0;
-    Thread::wait(1000.0*on_time);
-    //LED cool down effect using PWM
-    for(int i=9; i>0; i--) {
-        led = i/10.0;
-        Thread::wait(1000.0*warm_time/10.0);
-    }
-    //LED off
-    led = 0.0;
-}
-//LED Railroad Crossing Lighting Effect
-void rrcrossing(void const *args)
-{
+int main() {
+    
+    Thread thread1(rgb);
+    Thread thread2(lcdStart);
+    Thread thread3(lcdEnd);
+    Thread thread4(receive);
+    
     while(1) {
-        incandescent_bulb(myled2);
-        incandescent_bulb(myled4);
+        FILE *wave_file;
+        wave_file = fopen("/sd/Music.wav", "r");
+        waver.play(wave_file);
+        fclose(wave_file);
     }
 }
-
-//wave player plays a *.wav file to D/A
-wave_player waver(&DACout);
-
-int main()
-{
-
-    FILE *wave_file;
-    while(1) {
-        {
-            //Lighthouse demo with foghorn
-            Thread thread(lighthouse); //Start LED effect thread
-            for(int i=0; i<4; ++i) {
-                //open wav file and play it
-                wave_file=fopen("/sd/foghorn.wav","r");
-                waver.play(wave_file); //Plays (*.wav file
-                fclose(wave_file);
-                Thread::wait(1925);
-            }
-            //end of lighthouse demo;
-            thread.terminate(); //kills LED effect thread whan audio stops
-
-        } //code block forces thread out of scope reclaims mem-can recreate thread object
-        {
-            //Arc Welding with sound
-            Thread thread(welding);
-            //open wav file and play it
-            wave_file=fopen("/sd/welding.wav","r");
-            waver.play(wave_file);
-            fclose(wave_file);
-            //end of welding demo;
-            thread.terminate();
-        }
-        {
-            //Police Lights with siren
-            //code block forces thread out of scope
-            Thread thread(police);
-            //open wav file and play it
-            wave_file=fopen("/sd/emgsiren.wav","r");
-            waver.play(wave_file);
-            fclose(wave_file);
-            //end of police light demo;
-            thread.terminate();
-        }
-        {
-            //Fire with sound
-            Thread thread(fire);
-            //open wav file and play it
-            wave_file=fopen("/sd/fire.wav","r");
-            waver.play(wave_file);
-            fclose(wave_file);
-            //end of fire demo;
-            thread.terminate();
-        }
-
-        {
-            //RR Crossing Lights with Signal Bells
-            Thread thread(rrcrossing);
-            //open wav file and play it
-            wave_file=fopen("/sd/SignalBell.wav","r");
-            waver.play(wave_file);
-            fclose(wave_file);
-            //end of railroad crossing demo;
-            thread.terminate();
-        }
-    }
-}