Threads and programming

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Files at this revision

API Documentation at this revision

Comitter:
t00214916
Date:
Wed Aug 18 18:08:13 2021 +0000
Parent:
3:3ec443c0842a
Commit message:
Threads and programming

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 3ec443c0842a -r a4cf91f0c8e1 main.cpp
--- a/main.cpp	Wed Dec 05 08:06:20 2012 +0000
+++ b/main.cpp	Wed Aug 18 18:08:13 2021 +0000
@@ -1,7 +1,7 @@
 // example to test the mbed Lab Board lcd lib with the mbed rtos
 // Pot1 change the contrast
 // Pot2 change the speed of the sin wave
-
+ 
 #include "mbed.h"
 #include "rtos.h"
 #include "Small_6.h"
@@ -9,17 +9,22 @@
 #include "Arial_9.h"
 #include "stdio.h"
 #include "C12832_lcd.h"
-
+ 
+BusIn joy(p15,p12,p13,p16); // Joystick
+DigitalIn fire(p14);
+ 
+BusOut leds(LED1,LED2,LED3,LED4);   // Leds
+ 
 // LCD object
 C12832_LCD LCD;
-
+ 
 AnalogIn Pot1(p19);
 AnalogIn Pot2(p20);
-
-
+ 
+ 
 // mutex to make the lcd lib thread save
 Mutex lcd_mutex;
-
+ 
 // Thread 1
 // print counter into first line and wait for 1 s
 void thread1(void const *args)
@@ -35,7 +40,7 @@
         Thread::wait(1000);
     }
 }
-
+ 
 // Thread 2
 // print counter into third line and wait for 0,5s
 void thread2(void const *args)
@@ -51,7 +56,7 @@
         Thread::wait(500); // wait 0.5s
     }
 }
-
+ 
 // Thread 3
 // print a sin function in a small window
 // the value of pot 1 change the speed of the sinwave
@@ -66,46 +71,59 @@
     while(true) {       // thread loop
         v = Pot1.read_u16();  // get value of pot 1
         lcd_mutex.lock();
-        for (i=90; i<127; i++) {           
-            s = 8 * sin((long double)(i+k) /5);   // pixel to print  
+        for (i=90; i<127; i++) {
+            s = 8 * sin((long double)(i+k) /5);   // pixel to print
             a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
-            LCD.pixel(i,9 + (int)a ,0);           // erase pixel  
-            LCD.pixel(i,9 + (int)s ,1);           // print pixel  
+            LCD.pixel(i,9 + (int)a,0);            // erase pixel
+            LCD.pixel(i,9 + (int)s,1);            // print pixel
         }
-        LCD.copy_to_lcd();  // LCD.pixel do not update the lcd 
+        LCD.copy_to_lcd();  // LCD.pixel do not update the lcd
         lcd_mutex.unlock();
         k++;
         Thread::wait(v/100);   // value of pot1 / 100
     }
 }
-
+ 
 // Thread 4
 // input pot 2 and change the contrast of lcd
 void thread4(void const *args)
 {
     int k;
     while(true) {         // thread loop
-    k = Pot2.read_u16();  // get the value of poti 2
-    k = k >> 10;          // we need only 6 bit for contrast
-     lcd_mutex.lock();
-    LCD.set_contrast(k);
-     lcd_mutex.unlock();
-    Thread::wait(500);    // wait 0.5s
+        k = Pot2.read_u16();  // get the value of poti 2
+        k = k >> 10;          // we need only 6 bit for contrast
+        lcd_mutex.lock();
+        LCD.set_contrast(k);
+        lcd_mutex.unlock();
+        Thread::wait(500);    // wait 0.5s
+    }
 }
+// Thread 5
+// use the joystick to turn leds on and off
+void thread5(void const *args)
+{
+ 
+    while(1) {
+        if (fire) {
+            leds=0xf;
+        } else {
+            leds=joy;
+        }
+        wait(0.1);
+    }
 }
-
-
-// print the actual contrast 
+// print the actual contrast
 int main()
 {
     int j;
     LCD.cls();
-  
+ 
     Thread t1(thread1); //start thread1
     Thread t2(thread2); //start thread2
     Thread t3(thread3); //start thread3
     Thread t4(thread4); //start thread4
-
+    Thread t5(thread5); //start thread5
+ 
     while(true) {       // main is the next thread
         lcd_mutex.lock();
         LCD.locate(0,9);
@@ -115,5 +133,5 @@
         lcd_mutex.unlock();
         Thread::wait(500);   // wait 0.5s
     }
-
-}
+ 
+}
\ No newline at end of file