Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Files at this revision

API Documentation at this revision

Comitter:
elmbed
Date:
Mon Jan 18 08:51:31 2016 +0000
Parent:
61:dd7002ceea96
Child:
63:efba30dea1f0
Commit message:
Pattern mode seems to work & more work on master/slave integration

Changed in this revision

DataStore.cpp Show annotated file Show diff for this revision Revisions of this file
DataStore.hh Show annotated file Show diff for this revision Revisions of this file
Radio.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
proto_code.cpp Show annotated file Show diff for this revision Revisions of this file
types.h Show annotated file Show diff for this revision Revisions of this file
--- a/DataStore.cpp	Sun Jan 17 07:03:51 2016 +0000
+++ b/DataStore.cpp	Mon Jan 18 08:51:31 2016 +0000
@@ -30,6 +30,21 @@
 static const int MASK_BLOCK   = 0x01;
 static const int TIME_BLOCK_0 = 0x02;
 static const int TIME_BLOCK_1 = 0x03;
+static const int PARAM_BLOCK  = 0x04;
+
+static const uint32_t MAGIC = 0xA020;
+
+extern bool is_master;
+
+typedef struct __attribute__((packed))
+{
+    uint32_t magic;    
+    uint32_t node_id;
+    uint32_t is_master;  
+}persistent_params;
+
+static persistent_params p_params;
+
 
 /* Writes the NRF error message to the phone.
  *
@@ -150,13 +165,14 @@
  * block 1 = mask table
  * block 2 = time_table (lower)
  * block 3 = time_table (higher)
+ * block 4 = parameters
  */
 static uint32_t pstorage_setup()
 {
     pstorage_module_param_t pstorage_param;    
 
     pstorage_param.block_size  = data_size;         
-    pstorage_param.block_count = 4;  
+    pstorage_param.block_count = 5;  
     pstorage_param.cb          = pstorage_cb_handler;
 
     return pstorage_register(&pstorage_param, &pstorage_id);
@@ -325,17 +341,75 @@
     }
 }
 
+bool datastore_is_master()
+{
+    return (p_params.is_master == 1);    
+}
+
+uint32_t datastore_node_id()
+{
+    return p_params.node_id;    
+}
+
+static void write_params()
+{
+    uint32_t err_code = 0;
+    pstorage_handle_t p_block_id;
+    
+    err_code = pstorage_block_identifier_get(&pstorage_id, PARAM_BLOCK, &p_block_id);
+    writeError(err_code);
+    err_code = pstorage_store(&p_block_id, (uint8_t*)&p_params, sizeof(persistent_params), 0x00);
+    writeError(err_code); 
+}
+
+bool read_params()
+{
+    uint32_t err_code = 0;
+    pstorage_handle_t p_block_id;
+    
+    err_code = pstorage_block_identifier_get(&pstorage_id, PARAM_BLOCK, &p_block_id);
+    writeError(err_code);
+    err_code = pstorage_load((uint8_t*)&p_params, &p_block_id, sizeof(persistent_params), 0x00);
+    writeError(err_code);
+    
+    if (p_params.magic != MAGIC)
+    {
+        // Default the parameters.
+        p_params.magic     = MAGIC;
+        p_params.node_id   = is_master ? 1 : 99;
+        p_params.is_master = is_master;  
+        
+        write_params();  
+        
+        return false;
+    }   
+    
+    return true;
+}
+
+void datastore_set_master()
+{
+    p_params.is_master = 1;
+    p_params.node_id   = 1;
+    
+    write_params(); 
+}
+
+void datastore_set_node_id(uint32_t node_id)
+{
+    p_params.node_id = node_id;
+    
+    write_params();
+}
+    
+
 void init_datastore()
 {
     uint32_t pstorageErr = pstorage_init();  // This needs to be called, even though apparently called in bootloader--Check other stuff.
     pstorage_access_status_get( &p_count );
     pstorage_setup();   
     
+    read_params();
     datastore_read_patterns(); 
-    
-    if(Dbg)
-    {
-        write_test_pattern();
-    }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DataStore.hh	Mon Jan 18 08:51:31 2016 +0000
@@ -0,0 +1,20 @@
+#ifndef DATASTORE_HH
+#define DATASTORE_HH
+
+void init_datastore();
+uint32_t datastore_node_id();
+bool datastore_is_master();
+void datastore_set_master();
+void datastore_set_node_id(uint32_t node_id);
+void write_test_pattern();
+uint16_t* datastore_get_times();
+uint8_t* datastore_get_masks();
+uint8_t* datastore_get_cones();
+void datastore_write_pattern(uint8_t pattern);
+void datastore_write_patterns();
+void datastore_read_patterns();
+
+bool read_params();
+
+
+#endif
--- a/Radio.cpp	Sun Jan 17 07:03:51 2016 +0000
+++ b/Radio.cpp	Mon Jan 18 08:51:31 2016 +0000
@@ -2,6 +2,7 @@
 #include <SPI.h>
 #include "types.h"
 #include "TA.h"
+#include "DataStore.hh"
 
 #define RADIO_STARTUP_ID 99
 #define NETWORKID     101   //the same on all nodes that talk to each other
@@ -25,13 +26,7 @@
 extern "C" void writeToPhone(char *format, ...);
 extern unsigned long millis();
 
-#ifdef MASTER
-static bool is_master = true;
-#else
-static bool is_master = false;
-#endif
-
-static int my_node_id = NODE_ID;
+extern bool is_master;
 
 struct node_id_mapping
 {
@@ -51,7 +46,7 @@
 
 void radio_init()
 {
-  radio.initialize(FREQUENCY, my_node_id, NETWORKID);
+  radio.initialize(FREQUENCY, datastore_node_id(), NETWORKID);
   radio.encrypt(0);
   radio.promiscuous(promiscuousMode);
 }
@@ -74,9 +69,9 @@
     if (em->m.command == 'R')
     {
         // We have been given a node id
-        my_node_id = em->m.value;
+        datastore_set_node_id(em->m.value);
         
-        if( Dbg )  writeToPhone("NID: %d\r\n", my_node_id);
+        if( Dbg )  writeToPhone("NID: %d\r\n", datastore_node_id());
         
         // Now reset the radio :)
         radio_init();
@@ -219,7 +214,7 @@
             slave_process(&em);    
             
         }
-        else if (radio.TARGETID == my_node_id)
+        else if (radio.TARGETID == datastore_node_id())
         {
             memcpy(m, &lm, sizeof(Message));
             
@@ -253,14 +248,14 @@
         return; // Only needed for slave cones    
     }
     
-    if (my_node_id != RADIO_STARTUP_ID && current - last_send > 1000L)
+    if (datastore_node_id() != RADIO_STARTUP_ID && current - last_send > 1000L)
     {
-        if( Dbg )  writeToPhone("NID: %d\r\n", my_node_id);
+        if( Dbg )  writeToPhone("NID: %d\r\n", datastore_node_id());
         last_send = current;
     }
     
     // Send out an i'm here message
-    if (my_node_id != RADIO_STARTUP_ID || current - last_send < 1000L)
+    if (datastore_node_id() != RADIO_STARTUP_ID || current - last_send < 1000L)
     {
         return;
     }
--- a/main.cpp	Sun Jan 17 07:03:51 2016 +0000
+++ b/main.cpp	Mon Jan 18 08:51:31 2016 +0000
@@ -59,6 +59,7 @@
 
 #include "types.h"
 #include "TA.h"
+#include "DataStore.hh"
 
 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
@@ -80,7 +81,6 @@
 void loop();
 void setup();
 void getRadioInput(char *ibuffer, int size);
-void init_datastore();
 
 #define BLENANO  0  // BLE Nano vs. TA New Baseboard (rev Aug-Nov 2015.)
 
@@ -470,6 +470,11 @@
 
 int main( void )
 {
+    init_datastore();
+    
+    wait_us(400);
+    
+    //is_master = //datastore_is_master();
     Ticker ticker;
 
     ble.init();
@@ -511,7 +516,6 @@
     setup();
     radio_init();
     tmr.start();
-    init_datastore();
     
     int mac_addr = ((int)macAddr[0]);
     mac_addr    |= ((int)macAddr[1] << 8);
@@ -535,12 +539,9 @@
 
         toPhoneChk();  
 
-        if (is_master)
-        {
-            int bytes = MIN( MAX_LEN, phoneP->readable() );
-            getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
-        }
-
+        int bytes = MIN( MAX_LEN, phoneP->readable() );
+        getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
+        
         loop();
         radio_loop(mac_addr);
     }
--- a/proto_code.cpp	Sun Jan 17 07:03:51 2016 +0000
+++ b/proto_code.cpp	Mon Jan 18 08:51:31 2016 +0000
@@ -1,5 +1,6 @@
 #include <TA.h>
 #include <types.h>
+#include "DataStore.hh"
 
 #define RED p3
 #define GREEN p5
@@ -116,8 +117,8 @@
 uint8_t checkButtons(void);
 Event getInputEvent(void); 
 void getRadioInput(char *ibuffer, int size);
-void interpret(char parameter, int value);
-
+void interpret_master(char parameter, int value);
+void interpret_slave(char parameter, int value);
 
 extern "C" void writeToPhone(char *format, ...);
 
@@ -132,10 +133,6 @@
 
 char local_input[50] = {0};
 
-void datastore_write_patterns();
-void datastore_read_patterns();
-void write_test_pattern();
-
 static void master_setup()
 {
     ta.initialize(NODE_ID);
@@ -702,7 +699,15 @@
             value = atoi(buffer);
       }
 
-      interpret(parameter, value);
+      if (is_master)
+      {
+        interpret_master(parameter, value);
+      }
+      else
+      {
+          interpret_slave(parameter, value);
+      }
+      
       DEBUG("After interp: '%c'\r\n", parameter);
       parameter = '_';
       buffer[0] = 0;
@@ -726,7 +731,22 @@
   }
 }
 
-static void interpret(char parameter, int value)
+static void interpret_slave(char parameter, int value)
+{
+    switch(parameter)
+    {
+        case 'M':
+            datastore_set_master();
+            writeToPhone("Power Cycle\r\n");
+            break;
+        case 's':
+            writeToPhone("MS: %d\r\n", datastore_is_master());
+            writeToPhone("RP: %d\r\n", read_params());
+            break;
+    }   
+}
+
+static void interpret_master(char parameter, int value)
 {
   int remainder;
   uint16_t split;
@@ -937,6 +957,7 @@
     {
       length = STATIONS;
       offset = (value - 1) * STATIONS;
+      datastore_write_pattern(value-1);
       writeToPhone("Saved sequence %d\r\n", value);
     }
     else
@@ -949,15 +970,13 @@
      resetSensors();
      ta.resetTouch();
      break;
+    case '5': // DEBUG CODE!
+       write_test_pattern();
+       datastore_write_patterns();
+       break;
      
    case 'z':
     find_cones();
-    /*m->value = value;
-    m->command = 'z';
-    m->cone = 2;
-    Serial.println("sending...");
-    ta.send(m);
-    Serial.println("sent");*/
     break;
   }
 }
@@ -1261,9 +1280,9 @@
       state_i = RUNNING_I;
       //send 'p' or 'f' command as appropriate
       if(mode == FREEFORM_M)
-        interpret('f', 0);
+        interpret_master('f', 0);
       else 
-        interpret('p', 0);
+        interpret_master('p', 0);
     }
     
     break;
@@ -1273,7 +1292,7 @@
     break;
   case MENU_I:
     if(first_i)
-        interpret('q', 0);
+        interpret_master('q', 0);
     // every 3 seconds we cycle through the menu
     // one option per second
     // light changes color and stays on for 500ms
@@ -1342,7 +1361,7 @@
       
       if(event.value == 3)
       {
-        interpret('p', sequence + 1);
+        interpret_master('p', sequence + 1);
         state_i = IDLE_I;
       }
       
--- a/types.h	Sun Jan 17 07:03:51 2016 +0000
+++ b/types.h	Mon Jan 18 08:51:31 2016 +0000
@@ -1,6 +1,8 @@
 #ifndef TYPES_H
 #define TYPES_H
 
+#define D
+
 #ifdef D
 const static bool Dbg = true;
 #define SL_DEBUG(...)  writeToPhone(__VA_ARGS__)