master

Dependencies:   mbed

Fork of ESE350-Whack-a-Mole by Eric Berdinis

Committer:
shibulal
Date:
Tue Oct 20 18:08:37 2015 +0000
Revision:
1:d905afad53c0
Parent:
0:ddc820578cb0
Child:
2:4c79ab84c285
master

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"
shibulal 1:d905afad53c0 3 #include <string>
mlab4 0:ddc820578cb0 4
shibulal 1:d905afad53c0 5
mlab4 0:ddc820578cb0 6
mlab4 0:ddc820578cb0 7 // RF tranceiver to link with handheld.
mlab4 0:ddc820578cb0 8 MRF24J40 mrf(p11, p12, p13, p14, p21);
mlab4 0:ddc820578cb0 9
mlab4 0:ddc820578cb0 10 // LEDs you can treat these as variables (led2 = 1 will turn led2 on!)
mlab4 0:ddc820578cb0 11 DigitalOut led1(LED1);
mlab4 0:ddc820578cb0 12 DigitalOut led2(LED2);
mlab4 0:ddc820578cb0 13 DigitalOut led3(LED3);
mlab4 0:ddc820578cb0 14 DigitalOut led4(LED4);
mlab4 0:ddc820578cb0 15
mlab4 0:ddc820578cb0 16 // Timer
mlab4 0:ddc820578cb0 17 Timer timer;
mlab4 0:ddc820578cb0 18
mlab4 0:ddc820578cb0 19 // Serial port for showing RX data.
mlab4 0:ddc820578cb0 20 Serial pc(USBTX, USBRX);
mlab4 0:ddc820578cb0 21
mlab4 0:ddc820578cb0 22 // Used for sending and receiving
mlab4 0:ddc820578cb0 23 char txBuffer[128];
mlab4 0:ddc820578cb0 24 char rxBuffer[128];
mlab4 0:ddc820578cb0 25 int rxLen;
mlab4 0:ddc820578cb0 26
mlab4 0:ddc820578cb0 27 //***************** Do not change these methods (please) *****************//
mlab4 0:ddc820578cb0 28
mlab4 0:ddc820578cb0 29 /**
mlab4 0:ddc820578cb0 30 * Receive data from the MRF24J40.
mlab4 0:ddc820578cb0 31 *
mlab4 0:ddc820578cb0 32 * @param data A pointer to a char array to hold the data
mlab4 0:ddc820578cb0 33 * @param maxLength The max amount of data to read.
mlab4 0:ddc820578cb0 34 */
mlab4 0:ddc820578cb0 35 int rf_receive(char *data, uint8_t maxLength)
mlab4 0:ddc820578cb0 36 {
mlab4 0:ddc820578cb0 37 uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
mlab4 0:ddc820578cb0 38 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 39
mlab4 0:ddc820578cb0 40 if(len > 10) {
mlab4 0:ddc820578cb0 41 //Remove the header and footer of the message
mlab4 0:ddc820578cb0 42 for(uint8_t i = 0; i < len-2; i++) {
mlab4 0:ddc820578cb0 43 if(i<8) {
mlab4 0:ddc820578cb0 44 //Make sure our header is valid first
mlab4 0:ddc820578cb0 45 if(data[i] != header[i])
mlab4 0:ddc820578cb0 46 return 0;
mlab4 0:ddc820578cb0 47 } else {
mlab4 0:ddc820578cb0 48 data[i-8] = data[i];
mlab4 0:ddc820578cb0 49 }
mlab4 0:ddc820578cb0 50 }
mlab4 0:ddc820578cb0 51
mlab4 0:ddc820578cb0 52 //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
mlab4 0:ddc820578cb0 53 }
mlab4 0:ddc820578cb0 54 return ((int)len)-10;
mlab4 0:ddc820578cb0 55 }
mlab4 0:ddc820578cb0 56
mlab4 0:ddc820578cb0 57 /**
mlab4 0:ddc820578cb0 58 * Send data to another MRF24J40.
mlab4 0:ddc820578cb0 59 *
mlab4 0:ddc820578cb0 60 * @param data The string to send
mlab4 0:ddc820578cb0 61 * @param maxLength The length of the data to send.
mlab4 0:ddc820578cb0 62 * If you are sending a null-terminated string you can pass strlen(data)+1
mlab4 0:ddc820578cb0 63 */
mlab4 0:ddc820578cb0 64 void rf_send(char *data, uint8_t len)
mlab4 0:ddc820578cb0 65 {
mlab4 0:ddc820578cb0 66 //We need to prepend the message with a valid ZigBee header
mlab4 0:ddc820578cb0 67 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 68 uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
mlab4 0:ddc820578cb0 69
mlab4 0:ddc820578cb0 70 for(uint8_t i = 0; i < len+8; i++) {
mlab4 0:ddc820578cb0 71 //prepend the 8-byte header
mlab4 0:ddc820578cb0 72 send_buf[i] = (i<8) ? header[i] : data[i-8];
mlab4 0:ddc820578cb0 73 }
mlab4 0:ddc820578cb0 74 //pc.printf("Sent: %s\r\n", send_buf+8);
mlab4 0:ddc820578cb0 75
mlab4 0:ddc820578cb0 76 mrf.Send(send_buf, len+8);
mlab4 0:ddc820578cb0 77 free(send_buf);
mlab4 0:ddc820578cb0 78 }
mlab4 0:ddc820578cb0 79
mlab4 0:ddc820578cb0 80
mlab4 0:ddc820578cb0 81 //***************** You can start coding here *****************//
mlab4 0:ddc820578cb0 82 int main (void)
mlab4 0:ddc820578cb0 83 {
mlab4 0:ddc820578cb0 84 uint8_t channel = 2;
shibulal 1:d905afad53c0 85
mlab4 0:ddc820578cb0 86 //Set the Channel. 0 is default, 15 is max
mlab4 0:ddc820578cb0 87 mrf.SetChannel(channel);
shibulal 1:d905afad53c0 88 int action;
shibulal 1:d905afad53c0 89 int round_counter;
shibulal 1:d905afad53c0 90 int score_counter;
shibulal 1:d905afad53c0 91 char input;
mlab4 0:ddc820578cb0 92 //Start the timer
mlab4 0:ddc820578cb0 93 timer.start();
mlab4 0:ddc820578cb0 94
mlab4 0:ddc820578cb0 95 while(true) {
shibulal 1:d905afad53c0 96 while (true){
shibulal 1:d905afad53c0 97 input=pc.getc();
shibulal 1:d905afad53c0 98 if (input=='s'){
shibulal 1:d905afad53c0 99 break;
shibulal 1:d905afad53c0 100 }
shibulal 1:d905afad53c0 101 }
shibulal 1:d905afad53c0 102 pc.printf("start!");
shibulal 1:d905afad53c0 103 round_counter=0;
shibulal 1:d905afad53c0 104 score_counter=0;
shibulal 1:d905afad53c0 105 while(round_counter<10){
shibulal 1:d905afad53c0 106 action = rand()%2;
shibulal 1:d905afad53c0 107 if(action==0){
shibulal 1:d905afad53c0 108 wait(1);
shibulal 1:d905afad53c0 109 }
shibulal 1:d905afad53c0 110 else if (action==1){
shibulal 1:d905afad53c0 111 timer.reset();
shibulal 1:d905afad53c0 112 strcpy(txBuffer, "activate_1");
shibulal 1:d905afad53c0 113 rf_send(txBuffer, strlen(txBuffer));
shibulal 1:d905afad53c0 114 while(true){
shibulal 1:d905afad53c0 115 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:d905afad53c0 116 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:d905afad53c0 117 if(strcmp("finished_1", txBuffer)) {
shibulal 1:d905afad53c0 118 break;
shibulal 1:d905afad53c0 119 }
shibulal 1:d905afad53c0 120 }
shibulal 1:d905afad53c0 121 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 122 round_counter++;
shibulal 1:d905afad53c0 123 }
shibulal 1:d905afad53c0 124 else if (action==2){
shibulal 1:d905afad53c0 125 timer.reset();
shibulal 1:d905afad53c0 126 strcpy(txBuffer, "activate_2");
shibulal 1:d905afad53c0 127 rf_send(txBuffer, strlen(txBuffer));
shibulal 1:d905afad53c0 128 while(true){
shibulal 1:d905afad53c0 129 rxLen = rf_receive(rxBuffer, 10);
shibulal 1:d905afad53c0 130 if(strcmp("finished_2", txBuffer)) {
shibulal 1:d905afad53c0 131 break;
shibulal 1:d905afad53c0 132 }
shibulal 1:d905afad53c0 133 }
shibulal 1:d905afad53c0 134 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 135 round_counter++;
shibulal 1:d905afad53c0 136 }
shibulal 1:d905afad53c0 137 else if (action ==3){
shibulal 1:d905afad53c0 138 timer.reset();
shibulal 1:d905afad53c0 139 strcpy(txBuffer, "activate_3");
shibulal 1:d905afad53c0 140 rf_send(txBuffer, strlen(txBuffer));
shibulal 1:d905afad53c0 141 while(true){
shibulal 1:d905afad53c0 142 rxLen = rf_receive(rxBuffer, 10);
shibulal 1:d905afad53c0 143 if(strcmp("finished_3", txBuffer)) {
shibulal 1:d905afad53c0 144 break;
shibulal 1:d905afad53c0 145 }
shibulal 1:d905afad53c0 146 }
shibulal 1:d905afad53c0 147 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 148 round_counter++;
shibulal 1:d905afad53c0 149 }
mlab4 0:ddc820578cb0 150 }
shibulal 1:d905afad53c0 151 pc.printf("Score: %d", score_counter);
mlab4 0:ddc820578cb0 152 }
mlab4 0:ddc820578cb0 153 }