sandbox / Mbed OS simple-mbed-client-example

Dependencies:   simple-mbed-client

Fork of simple-mbed-client-example by Jan Jongboom

Revision:
14:ddc258abaaac
Parent:
13:d4da5e6aa952
Child:
16:71ec73ea9b6d
--- a/main.cpp	Sun May 15 20:28:52 2016 +0000
+++ b/main.cpp	Tue May 17 00:15:40 2016 +0000
@@ -29,14 +29,17 @@
 DigitalOut registeredLed(LED_BLUE, 1);
 InterruptIn btn(SW3);
 
+// We need a semaphore because we cannot update value in interrupt context
+Semaphore updates(0);
+
 // play function, invoked thru mbed Client
 void play(void* args) {
-	pc.printf("I'm gonna play a song!\n");
+	printf("I'm gonna play a song!\n");
 }
 
 // patternUpdated is called when the value of led/0/pattern changes
 void patternUpdated(string newPattern) {
-	pc.printf("Got a new pattern... %s\n", newPattern.c_str());
+	printf("Got a new pattern... %s\n", newPattern.c_str());
 }
 
 // Here we define some mbed Client resources
@@ -46,17 +49,18 @@
 
 // Status callbacks if you're interested in that
 void registered() {
-	pc.printf("registered\n");
+	printf("registered\n");
 	registeredLed = 0;
 }
 void unregistered() {
-	pc.printf("unregistered\n");
+	printf("unregistered\n");
 	registeredLed = 1;
 }
 
-// btn_count now lives in the cloud, so just manipulate it...
+// we cannot do blocking stuff here, or device will crash
+// release semaphore and update btn_count in the while() loop...
 void fall() {
-	btn_count = btn_count + 1;
+	updates.release();
 }
 
 // normal function
@@ -67,6 +71,8 @@
 int main() {
     pc.baud(115200);
     
+    printf("Hello world\n");
+    
     Ticker t;
     t.attach(&toggleLed, 1.0f);
     
@@ -77,21 +83,31 @@
     client.define_function("music/0/play", &play);
 
     if (lwip.connect() != 0) {		// connect to the internet
-    	pc.printf("Connect to eth failed...\n");
+    	printf("Connect to eth failed...\n");
     	return 1;
     }					
 
-    //lwipv4_socket_init();			// do I need this?
-    pc.printf("IP address %s\r\n", lwip.getIPAddress());
+    printf("IP address %s\r\n", lwip.getIPAddress());
     
     bool setup = client.setup(&lwip);		// start mbed client!
     if (!setup) {
     	printf("Setting up mbed_client failed...\n");
     	return 1;
     }
-    
+
     client.on_registered(&registered);
     client.on_unregistered(&unregistered);
     
-    while (1) {}
+    while (1) {
+    	int v = updates.wait(25000);
+    	
+    	if (v == 1) {
+    		btn_count = btn_count + 1;
+    		// printf is a bit inflexible, static_cast to get the right value
+    		printf("btn_count is now %d\n", static_cast<int>(btn_count));
+    	}
+    	
+    	// call keep_alive every now and then
+    	client.keep_alive();
+    }
 }