test program for mbed Lab Board

Dependencies:   C12832_lcd LCD_fonts mbed-rtos mbed

Dependents:   Experiment_5

Test program for the mbed application board. It use the mbed rtos to create 5 threads.

/media/uploads/dreschpe/test_rtos.jpg

Two threads are counting, one setup the contrast with pot 1, one is printing a rotating sinwave and the last prints the actual contrast.

Revision:
2:a69c8c5f5b03
Parent:
1:1c6a9eaf55b5
Child:
3:3ec443c0842a
--- a/main.cpp	Tue Oct 16 17:51:10 2012 +0000
+++ b/main.cpp	Fri Oct 26 18:18:33 2012 +0000
@@ -1,8 +1,7 @@
-// example to test the mbed Lab Board lcd
+// 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"
@@ -11,114 +10,110 @@
 #include "stdio.h"
 #include "C12832_lcd.h"
 
-Serial pc(USBTX, USBRX);
-
-
 // LCD object
 C12832_LCD LCD("LCD");
 
 AnalogIn Pot1(p19);
 AnalogIn Pot2(p20);
 
-// defines to make lib thread save
-#define _lock lcd_mutex.lock();
-#define _unlock lcd_mutex.unlock();
+
+// mutex to make the lcd lib thread save
 Mutex lcd_mutex;
 
-// print data into first line and wait for 1s
+// Thread 1
+// print counter into first line and wait for 1 s
 void thread1(void const *args)
 {
     int i;
     while(true) {       // thread loop
-        _lock
+        lcd_mutex.lock();
         LCD.locate(0,0);
         LCD.set_font((unsigned char*) Small_6);
         printf("Thread1 count: %d",i);
-        LCD.copy_to_lcd();
-        _unlock
+        lcd_mutex.unlock();
         i++;
         Thread::wait(1000);
     }
 }
 
-// print data into third line and wait for 0,5s
+// Thread 2
+// print counter into third line and wait for 0,5s
 void thread2(void const *args)
 {
     int k;
     while(true) {       // thread loop
-        _lock
+        lcd_mutex.lock();
         LCD.locate(0,20);
         LCD.set_font((unsigned char*) Arial_9);
         printf("Thread 2 count : %d",k);
-        LCD.copy_to_lcd();
-        _unlock
+        lcd_mutex.unlock();
         k++;
-        Thread::wait(500);
+        Thread::wait(500); // wait 0.5s
     }
 }
 
-
-void thread4(void const *args)
+// Thread 3
+// print a sin function in a small window
+// the value of pot 1 change the speed of the sinwave
+void thread3(void const *args)
 {
     int i,k,v;
     double s,a;
     k = 1;
-    _lock
+    lcd_mutex.lock();
     LCD.rect(89,0,127,17,1);
-    _unlock
+    lcd_mutex.unlock();
     while(true) {       // thread loop
-        _lock
         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);
-            a = 8 * sin((long double)(i+k-1) /5);
-            LCD.pixel(i,9 + (int)a ,0);
-            LCD.pixel(i,9 + (int)s ,1);
+            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.copy_to_lcd();
-        _unlock
+        LCD.copy_to_lcd();  // LCD.pixel do not update the lcd 
+        lcd_mutex.unlock();
         k++;
-        Thread::wait(v/100);
+        Thread::wait(v/100);   // value of pot1 / 100
     }
 }
 
-
-void thread5(void const *args)
+// 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
-    _lock
+    k = k >> 10;          // we need only 6 bit for contrast
+     lcd_mutex.lock();
     LCD.set_contrast(k);
-    _unlock
-    Thread::wait(500);  
+     lcd_mutex.unlock();
+    Thread::wait(500);    // wait 0.5s
 }
 }
 
+
+// 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
 
-    LCD.claim(stdout);      // send stdout to the LCD display
-    LCD.cls();
-    //start thread1
-    Thread t1(thread1);
-    //start thread2
-    Thread t2(thread2);
-    //start thread4
-    Thread t4(thread4);
-    Thread t5(thread5);
-
-    while(true) {   // this is the third thread
-        _lock
+    while(true) {       // main is the next thread
+        lcd_mutex.lock();
         LCD.locate(0,9);
         LCD.set_font((unsigned char*) Small_7);
-        j = LCD.get_contrast();
+        j = LCD.get_contrast();    // read the actual contrast
         printf("contrast : %d",j);
-        LCD.copy_to_lcd();
-        _unlock
-        Thread::wait(500);
+        lcd_mutex.unlock();
+        Thread::wait(500);   // wait 0.5s
     }
 
 }