aa
Dependencies: mbed
Fork of ESE350-Whack-a-Mole_MOLENODE1 by
Revision 2:8f0ddb8d4de6, committed 2015-10-22
- Comitter:
- srijani987
- Date:
- Thu Oct 22 21:27:25 2015 +0000
- Parent:
- 1:dcc3ba9ffe63
- Commit message:
- mole3
Changed in this revision
WhackAMole_MOLE3.cpp | Show annotated file Show diff for this revision Revisions of this file |
WhackAMole_MOLENODE.cpp | Show diff for this revision Revisions of this file |
diff -r dcc3ba9ffe63 -r 8f0ddb8d4de6 WhackAMole_MOLE3.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WhackAMole_MOLE3.cpp Thu Oct 22 21:27:25 2015 +0000 @@ -0,0 +1,130 @@ +#include "mbed.h" +#include "MRF24J40.h" + +#include <string> + +// RF tranceiver to link with handheld. +MRF24J40 mrf(p11, p12, p13, p14, p21); + +// LEDs you can treat these as variables (led2 = 1 will turn led2 on!) +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +AnalogIn sensor(p18); + +// Timer +Timer timer; + +// Serial port for showing RX data. +Serial pc(USBTX, USBRX); + +// Used for sending and receiving +char txBuffer[128]; +char rxBuffer[128]; +int rxLen; + +int num_readings = 0; + +//***************** Do not change these methods (please) *****************// + +/** +* Receive data from the MRF24J40. +* +* @param data A pointer to a char array to hold the data +* @param maxLength The max amount of data to read. +*/ +int rf_receive(char *data, uint8_t maxLength) +{ + uint8_t len = mrf.Receive((uint8_t *)data, maxLength); + uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; + + if(len > 10) { + //Remove the header and footer of the message + for(uint8_t i = 0; i < len-2; i++) { + if(i<8) { + //Make sure our header is valid first + if(data[i] != header[i]) + return 0; + } else { + data[i-8] = data[i]; + } + } + + //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); + } + return ((int)len)-10; +} + +/** +* Send data to another MRF24J40. +* +* @param data The string to send +* @param maxLength The length of the data to send. +* If you are sending a null-terminated string you can pass strlen(data)+1 +*/ +void rf_send(char *data, uint8_t len) +{ + //We need to prepend the message with a valid ZigBee header + uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; + uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); + + for(uint8_t i = 0; i < len+8; i++) { + //prepend the 8-byte header + send_buf[i] = (i<8) ? header[i] : data[i-8]; + } + //pc.printf("Sent: %s\r\n", send_buf+8); + + mrf.Send(send_buf, len+8); + free(send_buf); +} + + +//***************** You can start coding here *****************// +int main (void) +{ + uint8_t channel = 15; + + //Set the Channel. 0 is default, 15 is max + mrf.SetChannel(channel); + + + while(true) { + //Try to receive some data + rxLen = rf_receive(rxBuffer, 128); + if(rxLen > 0) { + //Toggle the Led + led1 = led1^1; + pc.printf("Received: %s\r\n", rxBuffer); + + if (strcmp(rxBuffer, "MOLE3")) { + led1 = 1; + led2 = 1; + led3 = 1; + led4 = 1; + + // read sensor + num_readings = 0; + while (num_readings < 10) { + float val = sensor.read(); + //printf("sensor = %f\n", sensor.read()); + if (val < 0.6) num_readings++; + } + + // SEND + strcpy(txBuffer, "HIT"); + //Send the buffer + rf_send(txBuffer, strlen(txBuffer) + 1); + pc.printf("Sent: %s\r\n", txBuffer); + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + + } + + } + + } +}
diff -r dcc3ba9ffe63 -r 8f0ddb8d4de6 WhackAMole_MOLENODE.cpp --- a/WhackAMole_MOLENODE.cpp Tue Oct 20 00:17:40 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,132 +0,0 @@ -#include "mbed.h" -#include "MRF24J40.h" - -#include <string> - -// RF tranceiver to link with handheld. -MRF24J40 mrf(p11, p12, p13, p14, p21); - -// LEDs you can treat these as variables (led2 = 1 will turn led2 on!) -DigitalOut led1(LED1); -DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); - -AnalogIn sensor(p18); - -// Timer -Timer timer; - -// Serial port for showing RX data. -Serial pc(USBTX, USBRX); - -// Used for sending and receiving -char txBuffer[128]; -char rxBuffer[128]; -int rxLen; - -int num_readings = 0; - -//***************** Do not change these methods (please) *****************// - -/** -* Receive data from the MRF24J40. -* -* @param data A pointer to a char array to hold the data -* @param maxLength The max amount of data to read. -*/ -int rf_receive(char *data, uint8_t maxLength) -{ - uint8_t len = mrf.Receive((uint8_t *)data, maxLength); - uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; - - if(len > 10) { - //Remove the header and footer of the message - for(uint8_t i = 0; i < len-2; i++) { - if(i<8) { - //Make sure our header is valid first - if(data[i] != header[i]) - return 0; - } else { - data[i-8] = data[i]; - } - } - - //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); - } - return ((int)len)-10; -} - -/** -* Send data to another MRF24J40. -* -* @param data The string to send -* @param maxLength The length of the data to send. -* If you are sending a null-terminated string you can pass strlen(data)+1 -*/ -void rf_send(char *data, uint8_t len) -{ - //We need to prepend the message with a valid ZigBee header - uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; - uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); - - for(uint8_t i = 0; i < len+8; i++) { - //prepend the 8-byte header - send_buf[i] = (i<8) ? header[i] : data[i-8]; - } - //pc.printf("Sent: %s\r\n", send_buf+8); - - mrf.Send(send_buf, len+8); - free(send_buf); -} - - -//***************** You can start coding here *****************// -int main (void) -{ - uint8_t channel = 2; - - //Set the Channel. 0 is default, 15 is max - mrf.SetChannel(channel); - - - while(true) { - //Try to receive some data - rxLen = rf_receive(rxBuffer, 128); - if(rxLen > 0) { - //Toggle the Led - led1 = led1^1; - pc.printf("Received: %s\r\n", rxBuffer); - - if (strcmp(rxBuffer, "MOLE1")) { - led1 = 1; - } - if (strcmp(rxBuffer, "MOLE2")) { - led2 = 1; - } - if (strcmp(rxBuffer, "MOLE3")) { - led3 = 1; - } - - // read sensor - num_readings = 0; - while (num_readings < 10) { - float val = sensor.read(); - //printf("sensor = %f\n", sensor.read()); - if (val < 0.6) num_readings++; - } - - // SEND - strcpy(txBuffer, "HIT"); - //Send the buffer - rf_send(txBuffer, strlen(txBuffer) + 1); - pc.printf("Sent: %s\r\n", txBuffer); - led1 = 0; - led2 = 0; - led3 = 0; - - } - - } -} -