slave 2

Dependencies:   mbed

Fork of ESE350-Whack-a-Mole-Slave by ESE519

Committer:
shibulal
Date:
Wed Oct 21 01:31:05 2015 +0000
Revision:
2:bd90ff182f98
Parent:
1:59384b12aa98
slave 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mlab4 0:ddc820578cb0 1 #include "mbed.h"
mlab4 0:ddc820578cb0 2 #include "MRF24J40.h"
mlab4 0:ddc820578cb0 3
mlab4 0:ddc820578cb0 4 #include <string>
mlab4 0:ddc820578cb0 5
mlab4 0:ddc820578cb0 6 // RF tranceiver to link with handheld.
mlab4 0:ddc820578cb0 7 MRF24J40 mrf(p11, p12, p13, p14, p21);
shibulal 1:59384b12aa98 8 AnalogIn light_sensor(p20);
mlab4 0:ddc820578cb0 9 // LEDs you can treat these as variables (led2 = 1 will turn led2 on!)
mlab4 0:ddc820578cb0 10 DigitalOut led1(LED1);
mlab4 0:ddc820578cb0 11 DigitalOut led2(LED2);
mlab4 0:ddc820578cb0 12 DigitalOut led3(LED3);
mlab4 0:ddc820578cb0 13 DigitalOut led4(LED4);
mlab4 0:ddc820578cb0 14
mlab4 0:ddc820578cb0 15 // Timer
mlab4 0:ddc820578cb0 16 Timer timer;
mlab4 0:ddc820578cb0 17
mlab4 0:ddc820578cb0 18 // Serial port for showing RX data.
mlab4 0:ddc820578cb0 19 Serial pc(USBTX, USBRX);
mlab4 0:ddc820578cb0 20
mlab4 0:ddc820578cb0 21 // Used for sending and receiving
mlab4 0:ddc820578cb0 22 char txBuffer[128];
mlab4 0:ddc820578cb0 23 char rxBuffer[128];
mlab4 0:ddc820578cb0 24 int rxLen;
mlab4 0:ddc820578cb0 25
mlab4 0:ddc820578cb0 26 //***************** Do not change these methods (please) *****************//
mlab4 0:ddc820578cb0 27
mlab4 0:ddc820578cb0 28 /**
mlab4 0:ddc820578cb0 29 * Receive data from the MRF24J40.
mlab4 0:ddc820578cb0 30 *
mlab4 0:ddc820578cb0 31 * @param data A pointer to a char array to hold the data
mlab4 0:ddc820578cb0 32 * @param maxLength The max amount of data to read.
mlab4 0:ddc820578cb0 33 */
mlab4 0:ddc820578cb0 34 int rf_receive(char *data, uint8_t maxLength)
mlab4 0:ddc820578cb0 35 {
mlab4 0:ddc820578cb0 36 uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
mlab4 0:ddc820578cb0 37 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 38
mlab4 0:ddc820578cb0 39 if(len > 10) {
mlab4 0:ddc820578cb0 40 //Remove the header and footer of the message
mlab4 0:ddc820578cb0 41 for(uint8_t i = 0; i < len-2; i++) {
mlab4 0:ddc820578cb0 42 if(i<8) {
mlab4 0:ddc820578cb0 43 //Make sure our header is valid first
mlab4 0:ddc820578cb0 44 if(data[i] != header[i])
mlab4 0:ddc820578cb0 45 return 0;
mlab4 0:ddc820578cb0 46 } else {
mlab4 0:ddc820578cb0 47 data[i-8] = data[i];
mlab4 0:ddc820578cb0 48 }
mlab4 0:ddc820578cb0 49 }
mlab4 0:ddc820578cb0 50
mlab4 0:ddc820578cb0 51 //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
mlab4 0:ddc820578cb0 52 }
mlab4 0:ddc820578cb0 53 return ((int)len)-10;
mlab4 0:ddc820578cb0 54 }
mlab4 0:ddc820578cb0 55
mlab4 0:ddc820578cb0 56 /**
mlab4 0:ddc820578cb0 57 * Send data to another MRF24J40.
mlab4 0:ddc820578cb0 58 *
mlab4 0:ddc820578cb0 59 * @param data The string to send
mlab4 0:ddc820578cb0 60 * @param maxLength The length of the data to send.
mlab4 0:ddc820578cb0 61 * If you are sending a null-terminated string you can pass strlen(data)+1
mlab4 0:ddc820578cb0 62 */
mlab4 0:ddc820578cb0 63 void rf_send(char *data, uint8_t len)
mlab4 0:ddc820578cb0 64 {
mlab4 0:ddc820578cb0 65 //We need to prepend the message with a valid ZigBee header
mlab4 0:ddc820578cb0 66 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 67 uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
mlab4 0:ddc820578cb0 68
mlab4 0:ddc820578cb0 69 for(uint8_t i = 0; i < len+8; i++) {
mlab4 0:ddc820578cb0 70 //prepend the 8-byte header
mlab4 0:ddc820578cb0 71 send_buf[i] = (i<8) ? header[i] : data[i-8];
mlab4 0:ddc820578cb0 72 }
mlab4 0:ddc820578cb0 73 //pc.printf("Sent: %s\r\n", send_buf+8);
mlab4 0:ddc820578cb0 74
mlab4 0:ddc820578cb0 75 mrf.Send(send_buf, len+8);
mlab4 0:ddc820578cb0 76 free(send_buf);
mlab4 0:ddc820578cb0 77 }
mlab4 0:ddc820578cb0 78
mlab4 0:ddc820578cb0 79
mlab4 0:ddc820578cb0 80 //***************** You can start coding here *****************//
mlab4 0:ddc820578cb0 81 int main (void)
mlab4 0:ddc820578cb0 82 {
shibulal 2:bd90ff182f98 83 uint8_t channel = 5;
mlab4 0:ddc820578cb0 84
mlab4 0:ddc820578cb0 85 //Set the Channel. 0 is default, 15 is max
mlab4 0:ddc820578cb0 86 mrf.SetChannel(channel);
mlab4 0:ddc820578cb0 87
mlab4 0:ddc820578cb0 88 //Start the timer
mlab4 0:ddc820578cb0 89 timer.start();
mlab4 0:ddc820578cb0 90
mlab4 0:ddc820578cb0 91 while(true) {
mlab4 0:ddc820578cb0 92 //Try to receive some data
mlab4 0:ddc820578cb0 93
shibulal 1:59384b12aa98 94 while(true){
shibulal 1:59384b12aa98 95 rxLen = rf_receive(rxBuffer, 128);
shibulal 2:bd90ff182f98 96 if (rxLen>0){
shibulal 2:bd90ff182f98 97 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 2:bd90ff182f98 98 //strcpy(rxBuffer,"");
shibulal 2:bd90ff182f98 99 if(!strcmp("activate_2", rxBuffer)) {
shibulal 1:59384b12aa98 100 break;
shibulal 1:59384b12aa98 101 }
shibulal 2:bd90ff182f98 102 }
shibulal 2:bd90ff182f98 103
shibulal 1:59384b12aa98 104 }
shibulal 1:59384b12aa98 105 led1=1;
shibulal 2:bd90ff182f98 106 while (light_sensor*100>10.0);
shibulal 1:59384b12aa98 107 led1=0;
shibulal 2:bd90ff182f98 108 strcpy(txBuffer, "finished_2");
shibulal 2:bd90ff182f98 109 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 2:bd90ff182f98 110 pc.printf("Sent: %s\r\n", txBuffer);
mlab4 0:ddc820578cb0 111 }
mlab4 0:ddc820578cb0 112 }