The firmware of the Grove Node

Dependencies:   BLE_API color_pixels mbed-src-nrf51822 nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
9:84cb66d0375d
Child:
10:f34ff4e47741
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/node.cpp	Thu Nov 06 02:22:01 2014 +0000
@@ -0,0 +1,229 @@
+
+
+#include "unified_driver.h"
+#include "mbed.h"
+#include "UARTService.h"
+
+#define LOG(...)      //  { printf(__VA_ARGS__); }
+
+extern UARTService *uartServicePtr;
+
+
+extern "C" driver_t analog_sensor_driver;
+extern "C" driver_t analog_thermometer_driver;
+extern "C" driver_t time_sensor_driver;
+
+driver_t *sensor_list[] =
+{
+    &analog_sensor_driver,
+    &analog_thermometer_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,
+    &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;
+void *sensor_object;
+void *actuator_object;
+float sensor_data[16] = {0,};
+float actuator_data[8] = {0,};
+
+float action_list[8][8] = 
+{
+    {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, };
+
+
+void node_init()
+{
+    sensor_id = 0;
+    sensor = sensor_list[0];
+    sensor_init_data[0] = p3;
+    sensor_init_data[1] = p4;
+    sensor->init(&sensor_object, sensor_init_data);
+    
+    actuator = actuator_list[0];
+    actuator_init_data[0] = p1;
+    actuator_init_data[1] = p2;
+    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 (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);
+                }
+            }
+
+        }
+    }
+}
+
+void node_parse(int argc, char *argv[])
+{
+    if (2 == argc) {
+        int param1 = atoi(argv[1]);
+        if (0 == strcmp(argv[0], "s")) {
+            if (param1 != sensor_id) {
+                sensor_id = param1;
+                sensor->fini(&sensor_object);
+                sensor = sensor_list[param1];
+                sensor->init(&sensor_object, sensor_init_data);
+                
+                equation_number = 0; 
+            }
+            
+            LOG("select sensor [%d]\n", sensor_id); 
+        } else if (0 == strcmp(argv[0], "a")) {
+            if (param1 != actuator_id) {
+                actuator_id = param1;
+                actuator->fini(&actuator_object);
+                actuator = actuator_list[param1];
+                actuator->init(&actuator_object, actuator_init_data);  
+                
+                action_number = 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++;
+        }
+    } 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) {
+                float value = atof(argv[i + 1]);
+                actuator_data[i] = value;
+                LOG(" %f", value);
+            } else {
+                actuator_data[i] = 0;
+            }
+        }
+        
+        LOG("\n");
+        
+        actuator->write(&actuator_object, actuator_data);
+    }
+     
+}