Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Revision:
29:6ff737b67e7d
Parent:
28:0e774865873d
Child:
30:15743b79c6cb
--- a/main.cpp	Mon Mar 04 22:11:02 2019 +0000
+++ b/main.cpp	Thu Mar 07 15:39:45 2019 +0000
@@ -48,7 +48,7 @@
 // Default block device
 BlockDevice* bd = BlockDevice::get_default_instance();
 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
-LittleFileSystem fs("fs", &sd);
+LittleFileSystem fs("fs");
 
 // Default User button for GET example
 InterruptIn button(BUTTON1);
@@ -95,7 +95,7 @@
  * @param newValue Updated value for the resource
  */
 void led_put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
-    printf("PUT received, new value: %s\n", newValue.c_str());
+    printf("*** PUT received, new value: %s                             \n", newValue.c_str());
     led = atoi(newValue.c_str());
 }
 
@@ -107,7 +107,7 @@
  * @param size Size of the body
  */
 void led_post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
-    printf("POST received. Payload: %s\n", res_led->get_value().c_str());
+    printf("*** POST received. Payload: %s                              \n", res_led->get_value().c_str());
     led = atoi(res_led->get_value().c_str());
 }
 
@@ -117,7 +117,7 @@
 void button_press() {
     int v = res_button->get_value_int() + 1;
     res_button->set_value(v);
-    printf("Button clicked %d times                                \n", v);
+    printf("*** Button clicked %d times                                 \n", v);
 }
 
 /**
@@ -126,7 +126,7 @@
  * @param status The delivery status of the notification
  */
 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
-    printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
+    printf("*** Button notification, status %s (%d)                     \n", MbedCloudClientResource::delivery_status_to_string(status), status);
 }
 
 /**
@@ -142,8 +142,9 @@
  * Initialize sensors
  */
 void sensors_init() {
-    uint8_t id;
+    uint8_t id1, id2, id3, id4;
 
+    printf ("\nSensors configuration:\n");
     // Initialize sensors
     sen_hum_temp.init(NULL);
     sen_press_temp.init(NULL);
@@ -154,21 +155,18 @@
     /// Call sensors enable routines
     sen_hum_temp.enable();
     sen_press_temp.enable();
-    //sen_mag.enable();
     sen_acc_gyro.enable_x();
     sen_acc_gyro.enable_g();
 
-    printf("\033[2J\033[20A");
-    printf ("\nSensors configuration:\n");
+    sen_hum_temp.read_id(&id1);
+    sen_press_temp.read_id(&id2);
+    sen_mag.read_id(&id3);
+    sen_acc_gyro.read_id(&id4);
 
-    sen_hum_temp.read_id(&id);
-    printf("HTS221  humidity & temperature    = 0x%X\n", id);
-    sen_press_temp.read_id(&id);
-    printf("LPS22HB pressure & temperature    = 0x%X\n", id);
-    sen_mag.read_id(&id);
-    printf("LIS3MDL magnetometer              = 0x%X\n", id);
-    sen_acc_gyro.read_id(&id);
-    printf("LSM6DSL accelerometer & gyroscope = 0x%X\n", id);
+    printf("HTS221  humidity & temperature    = 0x%X\n", id1);
+    printf("LPS22HB pressure & temperature    = 0x%X\n", id2);
+    printf("LIS3MDL magnetometer              = 0x%X\n", id3);
+    printf("LSM6DSL accelerometer & gyroscope = 0x%X\n", id4);
 
     printf("\n"); ;
 }
@@ -234,11 +232,18 @@
 int main(void) {
     printf("\nStarting Simple Pelion Device Management Client example\n");
 
-    // If the User button is pressed ons start, then format storage.
-    if (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE) {
-        printf("User button is pushed on start. Formatting the storage...\n");
-        bd->erase(0, bd->size());
-        int storage_status = fs.reformat(&sd);
+    int storage_status = fs.mount(&sd);
+    if (storage_status != 0) {
+        printf("Storage mounting failed.\n");
+    }
+    bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
+    if (btn_pressed) {
+        printf("User button is pushed on start...\n");
+    }
+    if (storage_status || btn_pressed) {
+        printf("Formatting the storage...\n");
+        sd.erase(0, sd.size());
+        storage_status = fs.reformat(&sd);
         if (storage_status != 0) {
             if (sd.erase(0, sd.size()) == 0) {
                 if (fs.format(&sd) == 0) {