mbed application board demo includes lcd, pot, joystick, rgb led, websocket, mbed rtos. websocket demo url is => http://sockets.mbed.org/gordonlu/viewer

Dependencies:   C12832 EthernetInterface WebSocketClient mbed-rtos mbed

Fork of rtos_basic by mbed official

Files at this revision

API Documentation at this revision

Comitter:
gordonlu
Date:
Fri Feb 06 08:49:53 2015 +0000
Parent:
7:71124ec77f83
Child:
9:999997e8e4cc
Commit message:
demo v1

Changed in this revision

EthernetInterface.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Fri Feb 06 08:49:53 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#d1ccbed7687a
--- a/main.cpp	Fri Feb 06 04:11:33 2015 +0000
+++ b/main.cpp	Fri Feb 06 08:49:53 2015 +0000
@@ -1,34 +1,10 @@
-/*#include "mbed.h"
-#include "rtos.h"
-#include "C12832.h"
- 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-C12832 lcd(p5, p7, p6, p8, p11);
- 
-void led2_thread(void const *args) {
-    while (true) {
-        led2 = !led2;
-        Thread::wait(1000);
-    }
-}
- 
-int main() {
-    Thread thread(led2_thread);
-      lcd.cls();
-    lcd.locate(0,3);
-    lcd.printf("mbed application board!");
-    while (true) {
-        led1 = !led1;
-        Thread::wait(500);
-    }
-}
-*/
 #include "mbed.h"
 #include "rtos.h"
 #include "C12832.h"
+#include "EthernetInterface.h"
+#include "math.h"
 
-      
+#define POT_SENSITIVITY (0.01f)      
 C12832 lcd(p5, p7, p6, p8, p11);
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
@@ -40,10 +16,85 @@
 AnalogIn pot1(p19);
 AnalogIn pot2(p20);
 BusIn joystick(p12, p13, p14, p15, p16);
+
+Mutex lcd_mutex;
  
 
 const int LEDNUM = 4;
 
+void potJoystickThread(void const *args)
+{
+    static float pt1 = 0;
+    static float pt2 = 0;
+    static int js = 0;
+    
+    printf("PotJoysticThread Start\r\n");
+    
+    while(1)
+    {
+    
+        float p1 = pot1.read();
+        float p2 = pot2.read();
+        int j = joystick.read();
+            
+        if(fabs(p1 - pt1) > POT_SENSITIVITY || fabs(p2 - pt2) > POT_SENSITIVITY || j != js)
+        {      
+            pt1 = p1;
+            pt2 = p2;
+            js = j;   
+            
+            r = 1 - pt1;
+            g = 1 - pt2;
+            
+            if(0x10 == js)b = 0; //right, turn on blue led 
+            else if (0x02 == js)  b = 1;   //left, turn of blue led
+            else if(0x01 == js)   b.write( b + 0.05); //down
+            else if(0x08 == js)   b.write( b - 0.05); //up
+            
+            printf("Pot1 : %.2f  Pot2 : %.2f  Joystick:%02X\r\n", pt1, pt2, js);
+                
+            //lcd display
+            lcd_mutex.lock();
+            lcd.locate(0,11);
+            lcd.printf("Pot1 : %.2f Pot2 : %.2f JS:%02X", pt1, pt2, js);
+            lcd_mutex.unlock();
+        }
+       
+        Thread::wait(100);
+    }
+    
+}
+
+void netThread(void const *args)
+{
+   printf("EthernetInterface Thread Start\r\n");
+   EthernetInterface eth;
+   eth.init(); //Use DHCP
+   printf("MAC:%s\r\n",eth.getMACAddress()); 
+   lcd.locate(0,21);
+   lcd.printf("MAC:%s\r\n",eth.getMACAddress());
+   
+   printf("Connecting...\r\n");
+   
+   while(1)
+   {
+        if(0 != eth.connect())
+        {
+            printf("Connecting fail! Try again. \r\nConnecting....\r\n");   
+            Thread::wait(100);   
+        }
+        else
+        {
+            printf("Connect OK! IP Address is %s\r\n", eth.getIPAddress());
+            lcd_mutex.lock();
+            lcd.locate(0,21);
+            lcd.printf("IP ADDRESS : %s\r\n",eth.getIPAddress());
+            lcd_mutex.unlock();
+            break;
+        }
+    }
+}
+
 void rollingLed()
 {
     static int onled = 0;
@@ -60,7 +111,9 @@
 }
 int main()
 {
-    int j=1;
+   
+    int i = 0;
+
     lcd.cls();
 
     r.period(0.001);
@@ -70,32 +123,18 @@
     r = 1;
     g = 1;
     b = 1;
-              
+    
+    Thread t1(netThread);   
+    Thread t2(potJoystickThread);   
+           
     while(true)
     {  
-        float pt1 = pot1.read();
-        float pt2 = pot2.read();
-        int js = joystick.read();
-        
-        
+        lcd_mutex.lock();
         lcd.locate(0,1);
-        lcd.printf("Counting : %d  JoyStick:%02X",j++ , js);
-        lcd.locate(0,11);
-        lcd.printf("Pot1 : %.3f  Pot2 : %.3f", pt1,pt2);
-        lcd.locate(0,21);
-        lcd.printf("-----------Pot END-----------");
+        lcd.printf("Second : %06d ",i++);
+        lcd_mutex.unlock();
         
-        r = 1 - pt1;
-        g = 1 - pt2;
-        
-        if(0x10 == js)b = 0; //right, turn on blue led 
-        else if (0x02 == js)  b = 1;   //left, turn of blue led
-        else if(0x01 == js)   b.write( b + 0.1); //down
-        else if(0x08 == js)   b.write( b - 0.1); //up
-        
-        printf("Pot1 : %.3f  Pot2 : %.3f  Joystick:%02X\r\n", pt1, pt2, js);
         rollingLed();
-        Thread::wait(100);
- 
+        Thread::wait(1000); 
     }
 }
\ No newline at end of file