![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
For master node of whack a mole program
Fork of ESE350-Whack-a-Mole by
whack_master.cpp@1:691460a19f54, 2015-10-17 (annotated)
- Committer:
- prithviganeshk
- Date:
- Sat Oct 17 23:06:04 2015 +0000
- Revision:
- 1:691460a19f54
- Child:
- 2:73c0d4f0ff5c
slave_master
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prithviganeshk | 1:691460a19f54 | 1 | #include "mbed.h" |
prithviganeshk | 1:691460a19f54 | 2 | #include "MRF24J40.h" |
prithviganeshk | 1:691460a19f54 | 3 | #include <string> |
prithviganeshk | 1:691460a19f54 | 4 | |
prithviganeshk | 1:691460a19f54 | 5 | |
prithviganeshk | 1:691460a19f54 | 6 | // RF tranceiver to link with handheld. |
prithviganeshk | 1:691460a19f54 | 7 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
prithviganeshk | 1:691460a19f54 | 8 | |
prithviganeshk | 1:691460a19f54 | 9 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
prithviganeshk | 1:691460a19f54 | 10 | DigitalOut led1(LED1); |
prithviganeshk | 1:691460a19f54 | 11 | DigitalOut led2(LED2); |
prithviganeshk | 1:691460a19f54 | 12 | // Timer |
prithviganeshk | 1:691460a19f54 | 13 | Timer timer; |
prithviganeshk | 1:691460a19f54 | 14 | |
prithviganeshk | 1:691460a19f54 | 15 | // Serial port for showing RX data. |
prithviganeshk | 1:691460a19f54 | 16 | Serial pc(USBTX, USBRX); |
prithviganeshk | 1:691460a19f54 | 17 | |
prithviganeshk | 1:691460a19f54 | 18 | // Used for sending and receiving |
prithviganeshk | 1:691460a19f54 | 19 | char txBuffer[128]; |
prithviganeshk | 1:691460a19f54 | 20 | char rxBuffer[128]; |
prithviganeshk | 1:691460a19f54 | 21 | int rxLen; |
prithviganeshk | 1:691460a19f54 | 22 | |
prithviganeshk | 1:691460a19f54 | 23 | //***************** Do not change these methods (please) *****************// |
prithviganeshk | 1:691460a19f54 | 24 | |
prithviganeshk | 1:691460a19f54 | 25 | /** |
prithviganeshk | 1:691460a19f54 | 26 | * Receive data from the MRF24J40. |
prithviganeshk | 1:691460a19f54 | 27 | * |
prithviganeshk | 1:691460a19f54 | 28 | * @param data A pointer to a char array to hold the data |
prithviganeshk | 1:691460a19f54 | 29 | * @param maxLength The max amount of data to read. |
prithviganeshk | 1:691460a19f54 | 30 | */ |
prithviganeshk | 1:691460a19f54 | 31 | int rf_receive(char *data, uint8_t maxLength) |
prithviganeshk | 1:691460a19f54 | 32 | { |
prithviganeshk | 1:691460a19f54 | 33 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
prithviganeshk | 1:691460a19f54 | 34 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
prithviganeshk | 1:691460a19f54 | 35 | |
prithviganeshk | 1:691460a19f54 | 36 | if(len > 10) { |
prithviganeshk | 1:691460a19f54 | 37 | //Remove the header and footer of the message |
prithviganeshk | 1:691460a19f54 | 38 | for(uint8_t i = 0; i < len-2; i++) { |
prithviganeshk | 1:691460a19f54 | 39 | if(i<8) { |
prithviganeshk | 1:691460a19f54 | 40 | //Make sure our header is valid first |
prithviganeshk | 1:691460a19f54 | 41 | if(data[i] != header[i]) |
prithviganeshk | 1:691460a19f54 | 42 | return 0; |
prithviganeshk | 1:691460a19f54 | 43 | } else { |
prithviganeshk | 1:691460a19f54 | 44 | data[i-8] = data[i]; |
prithviganeshk | 1:691460a19f54 | 45 | } |
prithviganeshk | 1:691460a19f54 | 46 | } |
prithviganeshk | 1:691460a19f54 | 47 | |
prithviganeshk | 1:691460a19f54 | 48 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
prithviganeshk | 1:691460a19f54 | 49 | } |
prithviganeshk | 1:691460a19f54 | 50 | return ((int)len)-10; |
prithviganeshk | 1:691460a19f54 | 51 | } |
prithviganeshk | 1:691460a19f54 | 52 | |
prithviganeshk | 1:691460a19f54 | 53 | /** |
prithviganeshk | 1:691460a19f54 | 54 | * Send data to another MRF24J40. |
prithviganeshk | 1:691460a19f54 | 55 | * |
prithviganeshk | 1:691460a19f54 | 56 | * @param data The string to send |
prithviganeshk | 1:691460a19f54 | 57 | * @param maxLength The length of the data to send. |
prithviganeshk | 1:691460a19f54 | 58 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
prithviganeshk | 1:691460a19f54 | 59 | */ |
prithviganeshk | 1:691460a19f54 | 60 | void rf_send(char *data, uint8_t len) |
prithviganeshk | 1:691460a19f54 | 61 | { |
prithviganeshk | 1:691460a19f54 | 62 | //We need to prepend the message with a valid ZigBee header |
prithviganeshk | 1:691460a19f54 | 63 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
prithviganeshk | 1:691460a19f54 | 64 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
prithviganeshk | 1:691460a19f54 | 65 | |
prithviganeshk | 1:691460a19f54 | 66 | for(uint8_t i = 0; i < len+8; i++) { |
prithviganeshk | 1:691460a19f54 | 67 | //prepend the 8-byte header |
prithviganeshk | 1:691460a19f54 | 68 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
prithviganeshk | 1:691460a19f54 | 69 | } |
prithviganeshk | 1:691460a19f54 | 70 | //pc.printf("Sent: %s\r\n", send_buf+8); |
prithviganeshk | 1:691460a19f54 | 71 | |
prithviganeshk | 1:691460a19f54 | 72 | mrf.Send(send_buf, len+8); |
prithviganeshk | 1:691460a19f54 | 73 | free(send_buf); |
prithviganeshk | 1:691460a19f54 | 74 | } |
prithviganeshk | 1:691460a19f54 | 75 | |
prithviganeshk | 1:691460a19f54 | 76 | |
prithviganeshk | 1:691460a19f54 | 77 | //***************** You can start coding here *****************// |
prithviganeshk | 1:691460a19f54 | 78 | int main (void) |
prithviganeshk | 1:691460a19f54 | 79 | { |
prithviganeshk | 1:691460a19f54 | 80 | |
prithviganeshk | 1:691460a19f54 | 81 | //Set the Number of slaves |
prithviganeshk | 1:691460a19f54 | 82 | int slaves = 2; |
prithviganeshk | 1:691460a19f54 | 83 | |
prithviganeshk | 1:691460a19f54 | 84 | //Channels of the nodes |
prithviganeshk | 1:691460a19f54 | 85 | //channel1 = 15; //channel of the first node |
prithviganeshk | 1:691460a19f54 | 86 | //channel2 = 14; //channel of the first node |
prithviganeshk | 1:691460a19f54 | 87 | uint8_t channel; |
prithviganeshk | 1:691460a19f54 | 88 | |
prithviganeshk | 1:691460a19f54 | 89 | //Set the Count to 10 |
prithviganeshk | 1:691460a19f54 | 90 | int count = 0; |
prithviganeshk | 1:691460a19f54 | 91 | int begin = 0; |
prithviganeshk | 1:691460a19f54 | 92 | int end = 0; |
prithviganeshk | 1:691460a19f54 | 93 | int diff = 0; |
prithviganeshk | 1:691460a19f54 | 94 | int tdiff = 0; |
prithviganeshk | 1:691460a19f54 | 95 | |
prithviganeshk | 1:691460a19f54 | 96 | //Start the timer |
prithviganeshk | 1:691460a19f54 | 97 | timer.start(); |
prithviganeshk | 1:691460a19f54 | 98 | |
prithviganeshk | 1:691460a19f54 | 99 | //Set the yes and no arrays |
prithviganeshk | 1:691460a19f54 | 100 | char yes[] = "y"; |
prithviganeshk | 1:691460a19f54 | 101 | char no[] = "n"; |
prithviganeshk | 1:691460a19f54 | 102 | |
prithviganeshk | 1:691460a19f54 | 103 | while(count < 10) { |
prithviganeshk | 1:691460a19f54 | 104 | |
prithviganeshk | 1:691460a19f54 | 105 | //Send message to random node |
prithviganeshk | 1:691460a19f54 | 106 | |
prithviganeshk | 1:691460a19f54 | 107 | //Calculate random number |
prithviganeshk | 1:691460a19f54 | 108 | //srand(time(0)); |
prithviganeshk | 1:691460a19f54 | 109 | //channel = (rand()%(slaves)) + 14; //set to minimum channel |
prithviganeshk | 1:691460a19f54 | 110 | channel = 15; |
prithviganeshk | 1:691460a19f54 | 111 | |
prithviganeshk | 1:691460a19f54 | 112 | //Set the Channel to the random node selected |
prithviganeshk | 1:691460a19f54 | 113 | mrf.SetChannel(channel); |
prithviganeshk | 1:691460a19f54 | 114 | |
prithviganeshk | 1:691460a19f54 | 115 | //Send message to the random slave and start timer |
prithviganeshk | 1:691460a19f54 | 116 | strcpy(txBuffer, "set"); |
prithviganeshk | 1:691460a19f54 | 117 | rf_send(txBuffer, strlen(txBuffer) + 1); |
prithviganeshk | 1:691460a19f54 | 118 | //Switch on the Led when sending data |
prithviganeshk | 1:691460a19f54 | 119 | led2 = 1; |
prithviganeshk | 1:691460a19f54 | 120 | printf("Sending signal to channel %u \n", channel); |
prithviganeshk | 1:691460a19f54 | 121 | begin = timer.read_us(); |
prithviganeshk | 1:691460a19f54 | 122 | |
prithviganeshk | 1:691460a19f54 | 123 | //Wait for reply from the slave |
prithviganeshk | 1:691460a19f54 | 124 | |
prithviganeshk | 1:691460a19f54 | 125 | while(1){ |
prithviganeshk | 1:691460a19f54 | 126 | |
prithviganeshk | 1:691460a19f54 | 127 | rxLen = rf_receive(rxBuffer, 128); |
prithviganeshk | 1:691460a19f54 | 128 | |
prithviganeshk | 1:691460a19f54 | 129 | if(rxLen > 0) { |
prithviganeshk | 1:691460a19f54 | 130 | |
prithviganeshk | 1:691460a19f54 | 131 | //Calculate time difference |
prithviganeshk | 1:691460a19f54 | 132 | end = timer.read_us(); |
prithviganeshk | 1:691460a19f54 | 133 | diff = end - begin; |
prithviganeshk | 1:691460a19f54 | 134 | tdiff += diff; |
prithviganeshk | 1:691460a19f54 | 135 | |
prithviganeshk | 1:691460a19f54 | 136 | //Switch off Led to indicate received data |
prithviganeshk | 1:691460a19f54 | 137 | led2 = 0; |
prithviganeshk | 1:691460a19f54 | 138 | |
prithviganeshk | 1:691460a19f54 | 139 | //Check if mole was pressed or not |
prithviganeshk | 1:691460a19f54 | 140 | pc.printf("Received: %s\r\n", rxBuffer); |
prithviganeshk | 1:691460a19f54 | 141 | |
prithviganeshk | 1:691460a19f54 | 142 | //If it was pressed - increment the count |
prithviganeshk | 1:691460a19f54 | 143 | if(strcmp (yes,rxBuffer) == 0){ |
prithviganeshk | 1:691460a19f54 | 144 | pc.printf("pressed\n"); |
prithviganeshk | 1:691460a19f54 | 145 | count++; |
prithviganeshk | 1:691460a19f54 | 146 | } |
prithviganeshk | 1:691460a19f54 | 147 | //If it was not pressed - dont alter the count |
prithviganeshk | 1:691460a19f54 | 148 | else if(strcmp (no,rxBuffer) == 0){ |
prithviganeshk | 1:691460a19f54 | 149 | pc.printf("Not pressed\n"); |
prithviganeshk | 1:691460a19f54 | 150 | } |
prithviganeshk | 1:691460a19f54 | 151 | |
prithviganeshk | 1:691460a19f54 | 152 | break; |
prithviganeshk | 1:691460a19f54 | 153 | |
prithviganeshk | 1:691460a19f54 | 154 | }//end of "if" receive |
prithviganeshk | 1:691460a19f54 | 155 | |
prithviganeshk | 1:691460a19f54 | 156 | }//end of inner while |
prithviganeshk | 1:691460a19f54 | 157 | |
prithviganeshk | 1:691460a19f54 | 158 | }//end of while count |
prithviganeshk | 1:691460a19f54 | 159 | |
prithviganeshk | 1:691460a19f54 | 160 | printf("Your did it in %d milliseconds\n",tdiff); |
prithviganeshk | 1:691460a19f54 | 161 | |
prithviganeshk | 1:691460a19f54 | 162 | }// end of main |