Nespresso RGB Sensor / Mbed 2 deprecated EnvoyNespressoEndpointColorDetectorV2

Dependencies:   ColorDetectorV2 ExternalFlashClient SNICInterface mbed-rtos mbed nsdl

Fork of EnvoyNespressoEndpoint by Nespresso RGB Sensor

Revision:
22:23c2f8c016af
Parent:
20:4b6dcda0dcfd
Child:
23:eabec88a57c5
diff -r 324a75920f64 -r 23c2f8c016af main.cpp
--- a/main.cpp	Tue Jul 07 19:48:21 2015 +0000
+++ b/main.cpp	Wed Jul 08 14:22:04 2015 +0000
@@ -16,13 +16,15 @@
 // ****************************************************************************
 // Configuration section
 
+//#define COLOR_SENSOR
+
 // I2C Settings
 #define COLOR_SENSOR_SCL I2C_SCL
 #define COLOR_SENSOR_SDA I2C_SDA
 
 // Envoy Settings
 uint32_t SSID_ADDRESS   = 0x0100;
-uint32_t KEY_ADDRESS    = 0x0200;
+uint32_t KEY_ADDRESS    = 0x0180;
 
 // Pins and SPI module for the external flash class
 PinName nSCS = D10;
@@ -39,7 +41,17 @@
                   .maxLength = sizeof(readBuffer)
                 };
 
-volatile bool wifiProvisioned = false;
+typedef enum {
+    STATE_SSID_READ,
+    STATE_SSID_WAIT,
+    STATE_KEY_READ,
+    STATE_KEY_WAIT,
+    STATE_WIFI,
+    STATE_NSDL,
+    STATE_MAIN
+} state_t;
+
+volatile state_t state;
 
 // debug LEDs
 DigitalOut led1(LED1);
@@ -65,10 +77,10 @@
 /* Change this IP address to that of your mbed Device Server installation */
 static const char* NSP_ADDRESS = "23.99.29.171"; // aka connector.mbed.org
 static const int NSP_PORT = 5683;
-char endpoint_name[] = "nespresso-machine-wifi";
+char endpoint_name[] = "nespresso-machine-wifi-envoy";
 uint8_t ep_type[] = {"nespresso-endpoint"};
 char nsp_domain[] = "56645321f5e24c49908e42f4d71b9ccb";
-uint8_t lifetime_ptr[] = {"1200"};
+uint8_t lifetime_ptr[] = {"1"};
 
 // ****************************************************************************
 // Ethernet initialization
@@ -86,37 +98,43 @@
 
 static void ethernet_init()
 {
-// Initialize Wi-Fi interface
-    int s = eth.init();
-
-    NSDL_DEBUG("init();\r\n");
+    char* ip = NULL;
+    
+    do {
+        // Initialize Wi-Fi interface
+        int s = eth.init();
+    
+        NSDL_DEBUG("init();\r\n");
+    
+        if( s != 0 ) {
+            NSDL_DEBUG( "Could not initialise wifi\n\r" );
+            //return -1;
+        }
+        wait(0.5);
+        s = eth.disconnect();
+        NSDL_DEBUG("disconnect();\r\n");
+        if( s != 0 ) {
+            NSDL_DEBUG( "disconnect failed\r\n" );
+            //return -1;
+        }
+    
+        wait(0.3);
+        // Connect AP
+        eth.connect( SSID
+                     , strlen(SSID)
+                     , DEMO_AP_SECURITY_TYPE
+                     , KEY
+                     , strlen(KEY));
+        NSDL_DEBUG("connect();\r\n");
+        wait(0.5);
+    
+        NSDL_DEBUG("IP Config();\r\n");
+        eth.setIPConfig( true );
+        
+        ip = eth.getIPAddress();
+    } while (ip == NULL);
 
-    if( s != 0 ) {
-        NSDL_DEBUG( "Could not initialise wifi\n\r" );
-        //return -1;
-    }
-    wait(0.5);
-    s = eth.disconnect();
-    NSDL_DEBUG("disconnect();\r\n");
-    if( s != 0 ) {
-        NSDL_DEBUG( "disconnect failed\r\n" );
-        //return -1;
-    }
-
-    wait(0.3);
-    // Connect AP
-    eth.connect( SSID
-                 , strlen(SSID)
-                 , DEMO_AP_SECURITY_TYPE
-                 , KEY
-                 , strlen(KEY));
-    NSDL_DEBUG("connect();\r\n");
-    wait(0.5);
-
-    NSDL_DEBUG("IP Config();\r\n");
-    eth.setIPConfig( true );
-
-    NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
+    NSDL_DEBUG("IP Address:%s ", ip);
 }
 
 // ****************************************************************************
@@ -174,16 +192,20 @@
 {
     // copy string. first byte is the length
     uint8_t length = block.data[0];
-    memcpy(KEY, &(block.data[1]), length);
-    KEY[length] = '\0';
+    
+    if (length < sizeof(KEY))
+    {
+        memcpy(KEY, &(block.data[1]), length);
+        KEY[length] = '\0';
+    
+        // debug output the entire buffer
+        for (int idx = 0; idx < block.length; idx++) {
+            // zero out content to verify next read
+            block.data[idx] = 0;
+        }
 
-    // debug output the entire buffer
-    for (int idx = 0; idx < block.length; idx++) {
-        // zero out content to verify next read
-        block.data[idx] = 0;
+        state = STATE_WIFI;    
     }
-
-    wifiProvisioned = true;
 }
 
 // Read the SSID
@@ -191,16 +213,37 @@
 {
     // copy string. first byte is the length
     uint8_t length = block.data[0];
-    memcpy(SSID, &(block.data[1]), length);
-    SSID[length] = '\0';
+    
+    if (length < sizeof(SSID))
+    {
+        memcpy(SSID, &(block.data[1]), length);
+        SSID[length] = '\0';
+    
+        // debug output the entire buffer
+        for (int idx = 0; idx < block.length; idx++) {
+            // zero out content to verify next read
+            block.data[idx] = 0;
+        }
+
+        state = STATE_KEY_READ;
+    }
+}
 
-    // debug output the entire buffer
-    for (int idx = 0; idx < block.length; idx++) {
-        // zero out content to verify next read
-        block.data[idx] = 0;
+// interrupt from flash signaling new data is available
+// TODO: implement log on flash so clients can see whether
+// they should read out more values
+void flashUpdated()
+{
+    // update state. read flash is called from main, not interrupt context
+    if (state == STATE_SSID_WAIT)
+    {
+        state = STATE_SSID_READ;
     }
-
-    wifiProvisioned = true;
+    else if (state == STATE_MAIN)
+    {
+        // restart system when new credentials have been entered
+        NVIC_SystemReset();
+    }
 }
 
 // ****************************************************************************
@@ -216,6 +259,10 @@
     ticker.attach(tickerISR, 1.0);
     NSDL_DEBUG("mbed Nespresso Demo\n");
 
+    // register flash update callback
+    flash.onUpdates(flashUpdated);
+
+#ifdef COLOR_SENSOR
     // Begin Color Sensor init
 
     // First, ensure I2C bus is released by toggling clock (fixes reset errors)
@@ -243,49 +290,81 @@
 
     // Pass reference to color detector
     set_color_detector(&detector);
+#endif
 
-    printf("read SSID / KEY from flash\r\n");
-    flash.read(SSID_ADDRESS, &block, readDone);
+    state = STATE_SSID_READ;
 
-    // wait for SSID to be read from Nordic chip
-    while(!wifiProvisioned) {
-        //printf("wait\r\n");
-    }
-    printf("SSID Provisioned Succesfully: ssid= %s\n\r",SSID);
-    wifiProvisioned = false;
-    wait(1); // Dont know why this has to be here, but it does, if I dont do it it doesnt work.
+    for (;;)
+    {
+        switch(state)
+        {
+            case STATE_SSID_READ:
+                            printf("\r\n");
+                            printf("read SSID from flash\r\n");
+                            printf("waiting for SSID");
+                            fflush(stdout);
+                            flash.read(SSID_ADDRESS, &block, readDone);
+                            
+                            state = STATE_SSID_WAIT;
+                            break;
+                            
+            case STATE_SSID_WAIT:            
+                            wait(1); // TODO: look into timing issues on the NRF51
+                            printf(".");
+                            fflush(stdout);
+                            break;
 
-    // now read the KEY
-    flash.read(KEY_ADDRESS, &block, readDone2);
-    // wait for SSID to be read from Nordic chip
-    while(!wifiProvisioned) {
-        //printf("wait\r\n");
-    }
-
-    printf("ssid: %s, key: %s\r\n", SSID,KEY);
+            case STATE_KEY_READ:
+                            printf("read KEY from flash\r\n");
+                            flash.read(KEY_ADDRESS, &block, readDone2);
+                            
+                            state = STATE_KEY_WAIT;
+                            break;
+    
+            case STATE_KEY_WAIT:
+                            break;
+                            
+            case STATE_WIFI:
+                            printf("ssid: %s, key: %s\r\n", SSID,KEY);
 
-    // Ignore rest of program, just for development, it breaks before you get here
-//    while(true) {}
-
-    // Initialize Ethernet interface first
-    ethernet_init();
-
-    // Initialize NSP node
-    nsp_init();
-
-    // Initialize NSDL stack
-    nsdl_init();
+                            // Initialize Ethernet interface first
+                            ethernet_init();
+                            
+                            state = STATE_NSDL;
+                            break;
+                            
+            case STATE_NSDL:
+                            // Initialize NSP node
+                            nsp_init();
+                        
+                            // Initialize NSDL stack
+                            nsdl_init();
+                        
+                            // Create NSDL resources
+                            create_resources();
+                        
+                            nsdl_event_loop_init();
+                            
+                            state = STATE_MAIN;
+                            break;
 
-    // Create NSDL resources
-    create_resources();
-
-    nsdl_event_loop_init();
-
-    while(true) {
-        // Check if any nsdl events need to be handled
-        nsdl_event_loop_run_once();
-
-        // Sample the color detector and potentially send samples to mDS
-        run_color_detector();
+            case STATE_MAIN:
+                            // Check if any nsdl events need to be handled
+                            nsdl_event_loop_run_once();
+                    
+#ifdef COLOR_SENSOR
+                            // Sample the color detector and potentially send samples to mDS
+                            run_color_detector();
+#else
+                            wait(1);
+                            printf(".");
+                            fflush(stdout);
+#endif
+                            
+                            break;
+            default:
+                            printf("unknown state\r\n");
+                            break;
+        }
     }
 }