Code with mutex and threads

Dependencies:   C12832 Servo mbed-rtos-edited mbed

Fork of NervousPuppySprintOne by Sprint One

Files at this revision

API Documentation at this revision

Comitter:
davidqpinho
Date:
Thu Feb 26 17:21:09 2015 +0000
Parent:
7:8b2cf5e6e888
Commit message:
Version 26.02

Changed in this revision

nervousPuppy.cpp Show annotated file Show diff for this revision Revisions of this file
nervousPuppy.h Show annotated file Show diff for this revision Revisions of this file
diff -r 8b2cf5e6e888 -r 921bd8c81549 nervousPuppy.cpp
--- a/nervousPuppy.cpp	Wed Jan 28 16:49:16 2015 +0000
+++ b/nervousPuppy.cpp	Thu Feb 26 17:21:09 2015 +0000
@@ -11,23 +11,57 @@
 
 C12832 lcd(p5, p7, p6, p8, p11);
 
+void sensor1_thread(void const *args) {
+    while (true) {
+    s1_thread.lock();
+    x = 100*ainRight.read(); 
+    s1_thread.unlock();
+    }
+} 
+void sensor2_thread(void const *args) {
+    while (true) {
+    s2_thread.lock();
+    y = 100*ainLeft.read();
+    s2_thread.unlock();
+    }
+} 
+
+/*void lcd_thread(void const *args) {
+    while (true) {
+        //lcdmutex_thread.lock();
+        lcd.cls();
+        lcd.locate(0,3);
+        lcd.printf("x = %f",x);
+        lcd.printf(" y = %f",y);
+        lcd.printf("rotating by ,%f",pos);
+        //lcdmutex_thread.unlock();
+    }
+}*/
+
+
 
 nervousPuppy::nervousPuppy(){
-    rotation = 0;
+    //rotation = 0;
     int pos = 1000;
+    
     rotate.Enable(1000,20000);
+    
+    //activating threads
+    Thread thr1(sensor1_thread); 
+    Thread thr2(sensor2_thread);
+    //Thread thr3(lcd_thread);
+  
+    
     while(1){
         
-
-        float x = 100*ainRight.read();
-        float y = 100*ainLeft.read();
-        
+    
+      
 
         lcd.cls();
         lcd.locate(0,3);
         lcd.printf("x = %f",x);
         lcd.printf(" y = %f",y);
-        
+      
         
         if(y > 3.5 && x < 3.5){
             lcd.printf("rotating by ,%f",pos);
@@ -38,7 +72,7 @@
             pos -= 50;
             rotate.SetPosition(pos);
         }
-        
+     
     }
 }
 
diff -r 8b2cf5e6e888 -r 921bd8c81549 nervousPuppy.h
--- a/nervousPuppy.h	Wed Jan 28 16:49:16 2015 +0000
+++ b/nervousPuppy.h	Thu Feb 26 17:21:09 2015 +0000
@@ -9,11 +9,18 @@
 #define Sin(a) sin(PI/180*(a))
 #define PI 3.1415926
 
+
+Mutex s1_thread;
+Mutex s2_thread;
+Mutex lcdmutex_thread;
+
+float x,y;
+
 class nervousPuppy{
     public:
 
         nervousPuppy();
-        
+                        
         bool isLonely();
         bool isScared();
         bool shutdown();