RTOS example running four threads

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Files at this revision

API Documentation at this revision

Comitter:
kzar
Date:
Tue Oct 16 20:19:23 2018 +0000
Commit message:
RTOS example running four threads;

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 9fc1245cb50f 4DGL-uLCD-SE.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
diff -r 000000000000 -r 9fc1245cb50f SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/kzar/code/SDFileSystem/#02afa4960b3d
diff -r 000000000000 -r 9fc1245cb50f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,97 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "uLCD_4DGL.h"
+#include "wave_player.h"
+#include "SDFileSystem.h"
+
+uLCD_4DGL lcd(p28,p27,p29);        // LCD (serial tx, serial rx, reset pin;)
+//Init leds using PwmOut
+PwmOut red(p26);
+PwmOut green(p25);
+PwmOut blue(p24);
+//Analog out for speaker, and waveplayer linked to this pin
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+//SDFileSystem sd(p5, p6, p7, p8, "sd");
+// mutex to make the lcd lib thread safe
+Mutex lcd_mutex;
+
+Semaphore lcd_sem(1);
+ 
+
+// Thread 1, led fire effect
+void thread1(void const *args)
+{
+    while(1) {
+        //ramp up brightness level
+        for(double x = 0.0; x <= 1.0; x = x+0.2) {
+            red = x*x;
+            Thread::wait(300);
+        }
+        red=0;
+        //ramp up brightness level
+        for(double x = 0.0; x <= 1.0; x = x+0.2) {
+            blue = x*x;
+            Thread::wait(300);
+        }
+        blue=0;
+    }
+}
+
+// Thread 2 update the lcd with the current rgb values
+void thread2(void const *args)
+{
+    while(true) {       // thread loop
+        //lcd_mutex.lock();
+        lcd_sem.wait();
+        lcd.color(WHITE);
+        lcd.locate(1,1);
+        lcd.printf("R: %f", (float)red);
+        lcd.locate(1,3);
+        lcd.printf("B: %f", (float)blue);
+        //lcd_mutex.unlock();
+        lcd_sem.release();
+        Thread::wait(150);
+    }
+}
+
+// Thread 3
+// print a sin function in a small window
+// the value of DACout pin  changes the speed of the sine wave
+void thread3(void const *args)
+{
+    int t = 0;
+    
+    while(1) {
+        //lcd_mutex.lock();
+        lcd_sem.wait();
+        lcd.locate(1,9);
+        lcd.color(RED);
+        lcd.printf("Time: %d seconds", t);
+        //lcd_mutex.unlock();
+        lcd_sem.release();
+        
+        t++;
+        Thread::wait(1000);
+    }
+}
+
+int main()
+{
+    //Get a file pointer for playing audio
+    FILE *wave_file;
+    //Jack up the lcds baud rate
+    lcd.baudrate(3000000);
+    green = 0;
+    
+    Thread t1(thread1); //start thread1
+    Thread t2(thread2); //start thread2
+    Thread t3(thread3); //start thread3
+
+    while(1) {
+        wave_file=fopen("/sd/mydir/siren.wav","r");
+        if(wave_file==NULL) printf("file open error!\n\n\r");
+        waver.play(wave_file);
+        fclose(wave_file);
+    }
+}
diff -r 000000000000 -r 9fc1245cb50f mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
diff -r 000000000000 -r 9fc1245cb50f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187
\ No newline at end of file
diff -r 000000000000 -r 9fc1245cb50f wave_player.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Tue Oct 16 20:19:23 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/sravet/code/wave_player/#acc3e18e77ad