Elena Tosetti / Mbed 2 deprecated Nucleo_toggle_ios

Dependencies:   mbed

Fork of Nucleo_toggle_ios by manuele lupo

Files at this revision

API Documentation at this revision

Comitter:
manuelelupo
Date:
Wed Mar 09 19:11:11 2016 +0000
Parent:
0:8e04d1e48c99
Child:
2:a4655d837dd3
Commit message:
mapped relay 0

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Mar 08 19:08:22 2016 +0000
+++ b/main.cpp	Wed Mar 09 19:11:11 2016 +0000
@@ -1,45 +1,89 @@
 #include "mbed.h"
 #include <string.h>
-
-#define IOS (0x200) // PA_8 + PA_9
- 
  
 #define BAUD_RATE 115200
  
 #define RELAY_OPEN "open"
 #define RELAY_CLOSE "close"
-
+#define RESET_MASK  0xFFFF
+#define OPEN    1
 
-PortOut myIOs(PortA, IOS);
-DigitalOut myled(LED1);
-       
- 
+PortOut aIOs(PortA, RESET_MASK);
+PortOut bIOs(PortB, RESET_MASK);
+
+// Command string 
 char action[256] = {0};
-int id = 0;
-char c = '0';
+char command[12];
+    
+#define N_RELAY 1
+typedef struct relay{
+    int mask;
+    int port;
+} relay_t;
+
+// Map arrays
+relay_t relays_gpio[N_RELAY]; 
+ 
+ // Relays mapping
+ void map_relays(){
+     // Relay 0
+     // PA8 & PA9
+     relays_gpio[0].mask = 0x0000 | 1<<8 | 1<< 9;
+     relays_gpio[0].port = PortA;
+}
 
-#define N_RELAY 16
+// Handle action
+int handle_action(int port, int mask, int action){
+    if (port == PortA){
+        // Reset all Ports
+        aIOs = aIOs | RESET_MASK;
+        //bIOs = bIOs & RESET_MASK;
+        if (action)
+            aIOs = aIOs ^ mask;
+    } else if (port == PortB){
+        // Reset all Ports
+        aIOs = aIOs | RESET_MASK;
+        //bIOs = bIOs | RESET_MASK;
+        if (action)
+            bIOs = bIOs ^ mask;        
+    } else {
+            aIOs = 0xFFFF;
+            //bIOs = bIOs & RESET_MASK;
+            return -1;
+    }
+        
+    return 0;
+}
 
- 
+// Main routine
 int main() {
     
     // Initialize serial console
     //Configure boud rate 
     Serial s(USBTX, USBRX); //default for nrf51 is p0.09 p0.11
     s.baud(BAUD_RATE);  
-            
+          
+    map_relays();  
+    
     while(1) {   
         s.printf("Action:\r\n");       
-        s.scanf( "%s" , action );
-        s.printf(">> %s %d!\r\n",action, id);
-        
+        s.scanf( "%s" , action);
+
         if (strstr(action, "_r") != NULL){
-            if (strstr(action, RELAY_OPEN) != NULL){
-                myIOs = myIOs | IOS; // Toggle IOs level
-                s.printf("OK\r\n");
-            } else if (strstr(action, RELAY_CLOSE) != NULL){
-                myIOs = myIOs & 0; // Toggle IOs level
-                s.printf("OK\r\n");
+            char *o = strstr(action, RELAY_OPEN);
+            char *c = strstr(action, RELAY_CLOSE);
+            if ( o != NULL ){
+                int id = atoi(o+strlen(RELAY_OPEN)+1);
+                if (id <  N_RELAY){
+                    handle_action(relays_gpio[id].port, relays_gpio[id].mask, OPEN );
+                    s.printf("OK open %d\r\n", id);
+                }
+            } else if ( c != NULL ){
+                int id = atoi(c+strlen(RELAY_CLOSE)+1);
+                if (id <  N_RELAY){
+                    handle_action(relays_gpio[id].port, relays_gpio[id].mask, !OPEN );
+                    s.printf("OK close %d\r\n", id);
+                }
             } else {
                 s.printf("ERROR: %s unhandled action\r\n",action);
             }
@@ -47,7 +91,7 @@
            s.printf("ERROR: %s unhandled action\r\n",action);
 
         memset(action, 0x0, sizeof(action));
-        id = 255;
+
     }
 }
  
\ No newline at end of file