Example for sending and receiving simple broadcast commands using the RF interface on the Pi Swarm

Dependencies:   Pi_Swarm_Library_v06_alpha mbed

Fork of Pi_Swarm_Blank by piswarm

Revision:
5:ca18d81a3212
Parent:
4:823174be9a6b
--- a/main.cpp	Tue Feb 18 17:18:43 2014 +0000
+++ b/main.cpp	Tue Mar 10 16:33:54 2015 +0000
@@ -2,13 +2,14 @@
  *
  * University of York Robot Lab Pi Swarm Robot Library
  *
- * "Blank" Program
+ * User Command RF Test Program
  *
- * Use this file as the template to produce custom controllers
+ * Based on the blank program for 0.6 API, this adds handling for user RF messages from the
+ * table controller
  *
  * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
  * 
- * Version 0.6  February 2014
+ * Version 0.6  May 2014
  *
  * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2
  *
@@ -19,16 +20,28 @@
 
 PiSwarm piswarm;
 Serial pc (USBTX,USBRX);
+Timeout clear_screen_timeout;
 
 //This is where the program code goes.  In the simple demo, the outer LEDs are blinked.
 int main() {
     init();
-    
     int step = 0;
+    int function = 0;
+    //Each RF message will contain 1 byte: in this case the user ID.  (Note it is note strictly necessary as the
+    //user ID is already sent seperately and we are also sending the 4-bit function register, so for very simple
+    //comms the message part can be omitted.)
+    char message [2];
+    message[0] = piswarm.get_id() + 48;
     
     while(1) {
         step ++;
-        if(step==6)step=0;
+        //Every 6 steps (1.5 seconds) send an RF message, incrementing the function counter
+        if(step==6){
+            step=0;
+            function ++;
+            if(function == 16) function = 0;
+            broadcast_user_rf_command(function,message,1);
+        }
         switch (step%3){
             case 0:  piswarm.set_oled_colour(50,0,0); break;
             case 1:  piswarm.set_oled_colour(0,50,0); break;
@@ -42,6 +55,23 @@
     }
 }
 
+// This function is used to send the RF message:
+void broadcast_user_rf_command(int function, char * message, int length)
+{
+    //This function augments the communications stack
+    //It sends a 'user' RF command to all members (ie target_id = 0)
+    //It sends a 'request', not a 'command', meaning it will still be handled if commands are disabled (RF_ALLOW_COMMANDS set to 0, recommended)
+    //It takes three inputs:
+    // * function (an integer from 0 to 15)
+    // * message  (a char array)
+    // * length   (length of message in bytes)
+    send_rf_message(0,48+(function % 16),message,length);
+}
+
+void clear_screen(){
+    piswarm.cls();   
+}
+
 /***************************************************************************************************************************************
  *
  * Beyond this point, empty code blocks for optional functions is given
@@ -56,6 +86,14 @@
 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
 
 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
+    piswarm.cls();
+    piswarm.locate(0,0);
+    piswarm.printf("URF:%d",function);
+    if(length > 0) {
+        piswarm.locate(0,1);
+        piswarm.printf("%s",data);   
+    }
+    clear_screen_timeout.attach(&clear_screen,0.2);
     // A 'user' RF Command has been received:  write the code here to process it
     // sender = ID of the sender, range 0 to 31
     // broadcast_message = 1 is message sent to all robots, 0 otherwise
@@ -70,6 +108,7 @@
 }    
 
 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
+    //pc.printf("Response:%s\n",data);
     // A 'user' RF Response has been received:  write the code here to process it
     // sender = ID of the sender, range 0 to 31
     // broadcast_message = 1 is message sent to all robots, 0 otherwise