master

Dependencies:   mbed

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

Committer:
shibulal
Date:
Wed Oct 21 01:30:31 2015 +0000
Revision:
2:4c79ab84c285
Parent:
1:d905afad53c0
Child:
3:3c0c008bacb3
update

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 {
shibulal 2:4c79ab84c285 84 uint8_t channel = 5;
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 2:4c79ab84c285 106 action = rand()%3;
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 2:4c79ab84c285 113 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 2:4c79ab84c285 114 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:d905afad53c0 115 while(true){
shibulal 1:d905afad53c0 116 rxLen = rf_receive(rxBuffer, 128);
shibulal 2:4c79ab84c285 117 if (rxLen>0){
shibulal 2:4c79ab84c285 118 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 2:4c79ab84c285 119 if(!strcmp("finished_1", rxBuffer)) {
shibulal 2:4c79ab84c285 120 break;
shibulal 2:4c79ab84c285 121 }
shibulal 1:d905afad53c0 122 }
shibulal 1:d905afad53c0 123 }
shibulal 1:d905afad53c0 124 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 125 round_counter++;
shibulal 1:d905afad53c0 126 }
shibulal 1:d905afad53c0 127 else if (action==2){
shibulal 1:d905afad53c0 128 timer.reset();
shibulal 1:d905afad53c0 129 strcpy(txBuffer, "activate_2");
shibulal 2:4c79ab84c285 130 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 2:4c79ab84c285 131 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:d905afad53c0 132 while(true){
shibulal 2:4c79ab84c285 133 rxLen = rf_receive(rxBuffer, 128);
shibulal 2:4c79ab84c285 134 if (rxLen>0){
shibulal 2:4c79ab84c285 135 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 2:4c79ab84c285 136 if(!strcmp("finished_2", rxBuffer)) {
shibulal 2:4c79ab84c285 137 break;
shibulal 2:4c79ab84c285 138 }
shibulal 1:d905afad53c0 139 }
shibulal 1:d905afad53c0 140 }
shibulal 1:d905afad53c0 141 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 142 round_counter++;
shibulal 1:d905afad53c0 143 }
shibulal 1:d905afad53c0 144 else if (action ==3){
shibulal 1:d905afad53c0 145 timer.reset();
shibulal 1:d905afad53c0 146 strcpy(txBuffer, "activate_3");
shibulal 2:4c79ab84c285 147 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:d905afad53c0 148 while(true){
shibulal 2:4c79ab84c285 149 rxLen = rf_receive(rxBuffer, 128);
shibulal 2:4c79ab84c285 150 if (rxLen>0){
shibulal 2:4c79ab84c285 151 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 2:4c79ab84c285 152 if(!strcmp("finished_3", rxBuffer)) {
shibulal 2:4c79ab84c285 153 break;
shibulal 2:4c79ab84c285 154 }
shibulal 1:d905afad53c0 155 }
shibulal 1:d905afad53c0 156 }
shibulal 1:d905afad53c0 157 score_counter+=timer.read_ms();
shibulal 1:d905afad53c0 158 round_counter++;
shibulal 1:d905afad53c0 159 }
mlab4 0:ddc820578cb0 160 }
shibulal 1:d905afad53c0 161 pc.printf("Score: %d", score_counter);
mlab4 0:ddc820578cb0 162 }
mlab4 0:ddc820578cb0 163 }