Kyle Carter / Mbed 2 deprecated multi-thread

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

Revision:
8:c6e79c7d287a
Parent:
7:bd0aa7f21f53
diff -r bd0aa7f21f53 -r c6e79c7d287a main.cpp
--- a/main.cpp	Wed Feb 15 13:58:42 2017 -0600
+++ b/main.cpp	Wed Apr 29 22:32:05 2020 +0000
@@ -1,27 +1,89 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "uLCD_4DGL.h"
 
-Mutex stdio_mutex;
+Mutex lcd_mutex;
+uLCD_4DGL lcd(p28, p27, p30);
+PwmOut red(p22);
+PwmOut green(p23);
+PwmOut blue(p24);
+DigitalOut led4(LED4);
 
-void notify(const char* name, int state) {
-    stdio_mutex.lock();
-    printf("%s: %d\n\r", name, state);
-    stdio_mutex.unlock();
-}
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+ 
+private:
+// sets up specified pin for PWM using PwmOut class 
+    PwmOut _pin;
+};
 
-void test_thread(void const *args) {
-    while (true) {
-        notify((const char*)args, 0); Thread::wait(1000);
-        notify((const char*)args, 1); Thread::wait(1000);
+// Thread 1
+// control RGB LED
+void thread1(void const *args)
+{
+    while(true) {       
+        red = 1;
+        wait(0.5);
+        red = 0;
+        blue = 1;
+        wait(0.5);
+        blue = 0;
+        Thread::wait(50);
     }
 }
 
-int main() {
-    Thread t2;
-    Thread t3;
+// Thread 2 control uLCD image
+void thread2(void const *args)
+{
+    lcd_mutex.lock();
+    lcd.cls();
+    lcd.media_init();
+    lcd.set_sector_address(0x003B, 0x9000);
+    lcd.display_image(0,0);
+    lcd_mutex.unlock();
+    Thread::wait(5000);
+}
 
-    t2.start(callback(test_thread, (void *)"Th 2"));
-    t3.start(callback(test_thread, (void *)"Th 3"));
+// Thread 3 control speaker
+void thread3(void const *args)
+{
+    Speaker mySpeaker(p21);
+    while(1){
+        mySpeaker.PlayNote(783.991,1.0,0.05);
+        mySpeaker.PlayNote(587.330,1.0,0.05);
+        Thread::wait(50);
+    }
+}
 
-    test_thread((void *)"Th 1");
+// Thread 4 control uLCD text
+void thread4(void const *args)
+{
+    lcd_mutex.lock();
+    lcd.locate(7,13);
+    lcd.printf("HALT!");
+    lcd_mutex.unlock();
+    Thread::wait(1000);
 }
+
+int main()
+{
+    Thread t1(thread1); //start thread1
+    Thread t2(thread2); //start thread2
+    Thread t3(thread3); //start thread3
+    Thread t4(thread4); //start thread4
+    while(1) {
+        led4 = !led4;
+        Thread::wait(1000);
+    }
+}