The firmware of the Grove Node

Dependencies:   BLE_API color_pixels mbed-src-nrf51822 nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
10:f34ff4e47741
Parent:
9:84cb66d0375d
Child:
11:c0885b74a63a
--- a/node.cpp	Thu Nov 06 02:22:01 2014 +0000
+++ b/node.cpp	Thu Jun 04 09:34:13 2015 +0000
@@ -1,119 +1,143 @@
 
 
-#include "unified_driver.h"
+#include "udriver.h"
 #include "mbed.h"
+#include "BLEDevice.h"
 #include "UARTService.h"
+#include "color_pixels.h"
 
-#define LOG(...)      //  { printf(__VA_ARGS__); }
+#define LOG(...)       { printf(__VA_ARGS__); }
+
+#define MAX_INIT_DATA           4
+#define MAX_SENOSR_DIMENSION    9
+#define MAX_ACTUATOR_DATA_BUF   16
+#define MAX_EQUATION_NUMBER     2
+#define MAX_TASK_NUMBER         8
+
+
+typedef struct {
+    uint8_t     d;      // dimension
+    uint8_t     op;     // operator
+    uint8_t     extra;
+    float       value;
+} equation_t;
+    
+typedef struct {
+    equation_t  equation_list[MAX_EQUATION_NUMBER];
+    uint8_t     output[MAX_ACTUATOR_DATA_BUF];
+} task_t;
+    
+
 
 extern UARTService *uartServicePtr;
+extern volatile bool ble_is_connected;
 
+extern driver_t analog_sensor_driver;
+extern driver_t temperature_sensor_driver;
+extern driver_t time_sensor_driver;
 
-extern "C" driver_t analog_sensor_driver;
-extern "C" driver_t analog_thermometer_driver;
-extern "C" driver_t time_sensor_driver;
+extern driver_t relay_driver;
+extern driver_t led_driver;
+extern driver_t color_led_strip_driver;
 
-driver_t *sensor_list[] =
-{
+driver_t *sensor_list[] = {
     &analog_sensor_driver,
-    &analog_thermometer_driver,
+    &temperature_sensor_driver,
     &time_sensor_driver,
 };
 
-
-extern "C" driver_t switch_driver;
-extern "C" driver_t led_driver;
-extern "C" driver_t color_led_strip_driver;
-
-driver_t *actuator_list[] = 
-{
-    &switch_driver,
+driver_t *actuator_list[] = {
+    &relay_driver,
     &led_driver,
     &color_led_strip_driver
 };
 
 
-equation_t equation_list[8] = 
-{
-    {0, '>', 0.50},
-    {0, '<', 0.49},
-};
-
-int equation_number = 2;
-
 driver_t *sensor;
 driver_t *actuator;
-int sensor_id = 0;
-int actuator_id = 0;
+int sensor_index = 0;
+int actuator_index = 0;
 void *sensor_object;
 void *actuator_object;
-float sensor_data[16] = {0,};
-float actuator_data[8] = {0,};
+float sensor_data[MAX_SENOSR_DIMENSION] = {0,};
+float actuator_data[MAX_ACTUATOR_DATA_BUF] = {0,};
 
-float action_list[8][8] = 
+int sensor_init_data[MAX_INIT_DATA] = {0, };
+int actuator_init_data[MAX_INIT_DATA] = {0, };
+
+task_t task_list[MAX_TASK_NUMBER];
+uint32_t task_mask = 0;
+
+bool equation_test(equation_t *equation, float *data)
 {
-    {1, },
-    {0.0, },
-    {0.5, 1.0},
-};
-
-uint8_t action_number = 2;
-
-uint32_t sensor_init_data[8] = {0, };
-uint32_t actuator_init_data[8] = {0, };
-
-int8_t event_to_action_map[8] = {0, 2, };
-
+    bool result = false;
+    if (0x01 == equation->op) {         // >
+        result = data[equation->d] > equation->value;
+    } else if (0x02 == equation->op) {  // <
+        result = data[equation->d] < equation->value;
+    } else if (0x03 == equation->op) {  // =
+        result = data[equation->d] == equation->value;
+    }
+    
+    return true;
+}
 
 void node_init()
 {
-    sensor_id = 0;
-    sensor = sensor_list[0];
+    sensor_index = 0;
+    sensor = sensor_list[sensor_index];
     sensor_init_data[0] = p3;
     sensor_init_data[1] = p4;
     sensor->init(&sensor_object, sensor_init_data);
-    
-    actuator = actuator_list[0];
+
+    actuator_index = 2;
+    actuator = actuator_list[actuator_index];
     actuator_init_data[0] = p1;
     actuator_init_data[1] = p2;
+    actuator_init_data[2] = 30;
     actuator->init(&actuator_object, actuator_init_data);
 }
 
 void node_tick()
 {
     sensor->read(&sensor_object, sensor_data);
-    
+
+
     for (int d = 0; d < sensor->d; d++) {
         float value = sensor_data[d];
-        
-        uartServicePtr->printf("i %d %f\n", d, value);
-        
-        for (int e = 0; e < equation_number; e++) {
-            equation_t *equation = equation_list + e;
-            
-            bool result;
-            if (equation->d == d) {
-                if ('>' == equation->op) {
-                    result = value > equation->value;
-                } else if ('<' == equation->op) {
-                    result = value < equation->value;
-                } else if ('=' == equation->op) {
-                    result = value == equation->value;
+
+        if (ble_is_connected) {
+            uartServicePtr->printf("i %d %f\n", d, value);
+        }
+    }
+    
+    uint32_t mask = task_mask;
+    int task_index = 0;
+    while (mask) {
+        if (mask & 1) {
+            task_t *task = task_list + task_index;
+            bool result = false;
+            for (int i = 0; i < MAX_EQUATION_NUMBER; i++) {
+                equation_t *equation = task->equation_list + i;
+                if (0 == equation->extra) {
+                    break;
+                }
+                
+                bool equation_result = equation_test(equation, sensor_data);
+                if ('&' == equation->extra) {
+                    result = result && equation_result;
+                } else {
+                    result = result || equation;
                 }
             }
             
             if (result) {
-                LOG("event [%d] occured\n", e);
-                
-                int a = event_to_action_map[e];
-                if (a != -1) {
-                    actuator->write(&actuator_object, action_list[a]);
-                    
-                    LOG("execute action [%d]\n", a);
-                }
+                actuator->write(&actuator_object, task->output);
             }
-
         }
+        
+        task_index++;
+        mask >>= 1;
     }
 }
 
@@ -122,94 +146,37 @@
     if (2 == argc) {
         int param1 = atoi(argv[1]);
         if (0 == strcmp(argv[0], "s")) {
-            if (param1 != sensor_id) {
-                sensor_id = param1;
+            if (param1 != sensor_index) {
+                sensor_index = param1;
                 sensor->fini(&sensor_object);
                 sensor = sensor_list[param1];
                 sensor->init(&sensor_object, sensor_init_data);
-                
-                equation_number = 0; 
+
+                task_mask = 0;
             }
-            
-            LOG("select sensor [%d]\n", sensor_id); 
+
+            LOG("select sensor [%d]\n", sensor_index);
         } else if (0 == strcmp(argv[0], "a")) {
-            if (param1 != actuator_id) {
-                actuator_id = param1;
+            if (param1 != actuator_index) {
+                actuator_index = param1;
                 actuator->fini(&actuator_object);
                 actuator = actuator_list[param1];
-                actuator->init(&actuator_object, actuator_init_data);  
+                actuator->init(&actuator_object, actuator_init_data);
                 
-                action_number = 0;
+                task_mask = 0;
             }
-                
-            LOG("select sensor [%d]\n", sensor_id);
-        } 
 
-        memset(event_to_action_map, -1, sizeof(event_to_action_map));
-    } else if (5 == argc && 0 == strcmp(argv[0], "e")) {
-        uint8_t event_id = atoi(argv[1]);
-        if (event_id > equation_number) {
-            return;
-        }
-        uint8_t dimention = atoi(argv[2]);
-        uint8_t symbol = argv[3][0];
-        float   value = atof(argv[4]);
-        
-        equation_t *equation = equation_list + event_id;
-        equation->d = dimention;
-        equation->op = symbol;
-        equation->value     = value;
-        
-        LOG("set event [%d]: %d %c %f\n", event_id, dimention, symbol, value);
-        
-        
-        if (event_id == equation_number) {
-            equation_number++;
+            LOG("select actuator [%d]\n", actuator_index);
         }
-    } else if (3 == argc && 0 == strcmp(argv[0], "m")) {
-        uint8_t event_id = atoi(argv[1]);
-        uint8_t action_id = atoi(argv[2]);
-        
-        event_to_action_map[event_id] = action_id;
-        
-        LOG("map event [%d] -> action [%d]\n", event_id, action_id);
-    }
-    
-    if (0 == strcmp(argv[0], "f")) {
-        uint8_t action_id = atoi(argv[1]);
-        if (action_id > action_number) {
-            return;
-        } else if (action_id == action_number) {
-            action_number++;
-        }
-        
-        int n = argc - 2;
-        if (n > 8) {
-            n = 8;
-        }
-        
-        LOG("set action [%d]:", action_id);
-        for (int i = 0; i < 8; i++) {
-            if (i < n) {
-                float value = atof(argv[i + 2]);
-                action_list[action_id][i] = value;
-                LOG(" %f", value);
-            } else {
-                action_list[action_id][i] = 0;
-            }
-        }
-        
-        LOG("\n");
-        
- //       actuator->write(&actuator_object, action_list[action_id]);
+
     } else if (0 == strcmp(argv[0], "o")) {
 
         int n = argc - 1;
         if (n > 8) {
             n = 8;
         }
-        
-        
+
+
         LOG("set output:");
         for (int i = 0; i < 8; i++) {
             if (i < n) {
@@ -220,10 +187,10 @@
                 actuator_data[i] = 0;
             }
         }
-        
+
         LOG("\n");
-        
+
         actuator->write(&actuator_object, actuator_data);
     }
-     
+
 }