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

Committer:
jah128
Date:
Mon Feb 03 13:01:30 2014 +0000
Revision:
3:1aa1de26966a
Parent:
2:e806b595f9ce
Child:
4:823174be9a6b
Draft version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 2:e806b595f9ce 1 /*******************************************************************************************
jah128 2:e806b595f9ce 2 *
jah128 2:e806b595f9ce 3 * University of York Robot Lab Pi Swarm Robot Library
jah128 1:37502eb3b70f 4 *
jah128 1:37502eb3b70f 5 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
jah128 1:37502eb3b70f 6 *
jah128 2:e806b595f9ce 7 * Version 0.5 February 2014
jah128 1:37502eb3b70f 8 *
jah128 2:e806b595f9ce 9 * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2
jah128 2:e806b595f9ce 10 *
jah128 2:e806b595f9ce 11 ******************************************************************************************/
jah128 0:46cd1498a39a 12
jah128 3:1aa1de26966a 13 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h
jah128 0:46cd1498a39a 14
jah128 0:46cd1498a39a 15
jah128 1:37502eb3b70f 16 PiSwarm piswarm;
jah128 1:37502eb3b70f 17 Serial pc (USBTX,USBRX);
jah128 0:46cd1498a39a 18
jah128 1:37502eb3b70f 19 //This is where the program code goes. In the simple demo, the outer LEDs are blinked.
jah128 1:37502eb3b70f 20 int main() {
jah128 1:37502eb3b70f 21 init();
jah128 1:37502eb3b70f 22
jah128 1:37502eb3b70f 23 int step = 0;
jah128 0:46cd1498a39a 24
jah128 1:37502eb3b70f 25 while(1) {
jah128 1:37502eb3b70f 26 step ++;
jah128 1:37502eb3b70f 27 if(step==6)step=0;
jah128 1:37502eb3b70f 28 switch (step%3){
jah128 2:e806b595f9ce 29 case 0: piswarm.set_oled_colour(50,0,0); break;
jah128 2:e806b595f9ce 30 case 1: piswarm.set_oled_colour(0,50,0); break;
jah128 2:e806b595f9ce 31 case 2: piswarm.set_oled_colour(0,0,50); break;
jah128 1:37502eb3b70f 32 }
jah128 1:37502eb3b70f 33 switch (step%2){
jah128 1:37502eb3b70f 34 case 0: piswarm.set_oleds(1,0,1,0,1,0,1,0,1,0); break;
jah128 1:37502eb3b70f 35 case 1: piswarm.set_oleds(0,1,0,1,0,1,0,1,0,1); break;
jah128 1:37502eb3b70f 36 }
jah128 1:37502eb3b70f 37 wait(0.25);
jah128 0:46cd1498a39a 38 }
jah128 0:46cd1498a39a 39 }
jah128 0:46cd1498a39a 40
jah128 2:e806b595f9ce 41 /***************************************************************************************************************************************
jah128 2:e806b595f9ce 42 *
jah128 2:e806b595f9ce 43 * Beyond this point, the outline of several optional functions is given
jah128 2:e806b595f9ce 44 *
jah128 2:e806b595f9ce 45 * This may be left blank but should not be deleted
jah128 2:e806b595f9ce 46 *
jah128 2:e806b595f9ce 47 **************************************************************************************************************************************/
jah128 2:e806b595f9ce 48
jah128 2:e806b595f9ce 49
jah128 1:37502eb3b70f 50 // Communications
jah128 1:37502eb3b70f 51
jah128 1:37502eb3b70f 52 // If using the communication stack (USE_COMMUNICATION_STACK = 1), functionality for handling user RF responses should be added to the following functions
jah128 1:37502eb3b70f 53 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 54
jah128 1:37502eb3b70f 55 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 56 // A 'user' RF Command has been received: write the code here to process it
jah128 1:37502eb3b70f 57 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 58 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 59 // request_response = 1 if a response is expected, 0 otherwise
jah128 1:37502eb3b70f 60 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 61 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 62 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 63 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 64 // length = Length of extra data bytes held (range 0 to 57)
jah128 1:37502eb3b70f 65
jah128 1:37502eb3b70f 66 }
jah128 1:37502eb3b70f 67
jah128 1:37502eb3b70f 68 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 69 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 70 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 71 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 72 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 73 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 74 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 75 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 76 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 77 // length = Length of extra data bytes held (range 0 to 57)
jah128 1:37502eb3b70f 78
jah128 1:37502eb3b70f 79 }
jah128 1:37502eb3b70f 80
jah128 1:37502eb3b70f 81 void processRawRFData(char * rstring, char cCount){
jah128 1:37502eb3b70f 82 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 83 // rstring = The received packet
jah128 1:37502eb3b70f 84 // cCount = Packet length
jah128 0:46cd1498a39a 85 }
jah128 0:46cd1498a39a 86
jah128 1:37502eb3b70f 87 void switch_pressed() {
jah128 1:37502eb3b70f 88 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 89 char switches = piswarm.get_switches();
jah128 1:37502eb3b70f 90
jah128 1:37502eb3b70f 91 //Do something...
jah128 0:46cd1498a39a 92 }