Brian Daniels / Mbed 2 deprecated nespresso_demo

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Revision:
4:ab3c8d25260e
Parent:
3:8e1117ec91ba
Child:
6:8729a0db0e25
--- a/resources/nespresso.cpp	Mon Jun 30 09:44:00 2014 +0000
+++ b/resources/nespresso.cpp	Mon Jul 14 10:45:33 2014 +0000
@@ -4,8 +4,9 @@
 #include "rtos.h"
 #include "nsdl_support.h"
 #include "nespresso.h"
+#include "detection.h"
 
-#define NESPRESSO_RES_ID    "sen/0"
+#define NESPRESSO_RES_ID    "sensor/capsule"
 
 extern Serial pc;
 /* stored data for observable resource */
@@ -13,19 +14,38 @@
 static uint8_t *obs_token_ptr = NULL;
 static uint8_t obs_token_len = 0;
 
-static uint8_t res_nespresso_val[] = {"Decaffeinto Lungo (Orange)"};
+static const char* g_name = "no_capsule";
+
+static RGB_Detection g_detector(p23,p24,p25,p20);
+
 
 /*
  *
  *
  */
+/*
 const char* do_detection()
 {
     static int counter = 0;
     
     const char *names[] = {
-        {"VOLLUTO"},
-        {"INDRIYA"},
+      "KAZAAR",
+      "DHARKAN",    
+      "RISTRETTO",
+      "ARPEGGIO",
+      "ROMA",
+      "LIVANTO",
+      "CAPRICCIO",  
+      "VOLLUTO",
+      "COSI",
+      "INDRIYA FROM INDIA",
+      "ROSABAYA DE COLUMBIA",
+      "DULSAO DO BRASIL",
+      "BUKEELA KA ETHIOPIA",
+      "FORTISSIMO LUNGO RENOVATION",
+      "DECAFFEINATO LUNGO",
+      "DECAFFEINATO",
+      "DECAFFEINATO INTENSO"
     };
     const int num_names = sizeof(names) / sizeof(names[0]);
     
@@ -34,11 +54,53 @@
     counter++;
     return names[counter % num_names];
 }
+*/
+/*
+    console.printf("Started\r\n");
+    RGB_Detection detector(p23,p24,p25,p20);
+    
+    console.baud(115200);
+    
+    while (1) 
+    {
+        const char* name = detector.run(); 
+        console.printf("%s\r\n", name);
+    }
+*/
 
 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
 /* Node updates nespresso value every 10 seconds. Notification sending is done here. */
 static void exec_call_thread(void const *args)
 {
+    pc.printf("Entering thread\r\n");
+    int32_t time = 0;
+    while (true)
+    {
+        time++;
+        sn_nsdl_exec(time);
+        pc.printf("obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
+        // if (obs_number != 0 && obs_token_ptr != NULL)
+        {
+            obs_number++;        
+            pc.printf("Waiting for detector...\r\n");    
+            g_name = g_detector.run(); //do_detection();            
+            int len = strlen(g_name);
+            pc.printf("Detected %s\r\n", g_name);
+            if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)g_name, len, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 
+            {
+                pc.printf("Failed to send observation\r\n");
+            }
+            else 
+            {
+                pc.printf("Sent observation successfully\r\n");
+            }
+        }
+    }
+}
+
+#if 0
+static void exec_call_thread(void const *args)
+{
     int32_t time = 0;
     while (true)
     {
@@ -47,7 +109,7 @@
         if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
         {
             obs_number++;
-            if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)res_nespresso_val, (sizeof(res_nespresso_val)-1), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 
+            if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)g_res_nespresso_val, (strlen(res_nespresso_val)-1), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 
             {
                 pc.printf("Observation sending failed\r\n");
             }
@@ -58,6 +120,9 @@
         }
     }
 }
+#endif
+
+
 
 /* Only GET method allowed */
 static uint8_t nespresso_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
@@ -68,8 +133,8 @@
 
     coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
 
-    coap_res_ptr->payload_len = sizeof(res_nespresso_val)-1;
-    coap_res_ptr->payload_ptr = (uint8_t*)res_nespresso_val;
+    coap_res_ptr->payload_len = strlen(g_name)-1;
+    coap_res_ptr->payload_ptr = (uint8_t*)g_name;
 
     if(received_coap_ptr->token_ptr)
     {
@@ -107,7 +172,8 @@
 int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr)
 {
     static Thread exec_thread(exec_call_thread);
-    
+    pc.printf("Created thread\r\n");
     nsdl_create_dynamic_resource(resource_ptr, sizeof(NESPRESSO_RES_ID)-1, (uint8_t*)NESPRESSO_RES_ID, 0, 0, 1, &nespresso_resource_cb, SN_GRS_GET_ALLOWED);
+    pc.printf("Created resource\r\n");
     return 0;
 }
\ No newline at end of file