![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
aa
Dependencies: mbed
Fork of ESE350-Whack-a-Mole_MOLENODE1 by
WhackAMole_MOLENODE.cpp@1:dcc3ba9ffe63, 2015-10-20 (annotated)
- Committer:
- jfields
- Date:
- Tue Oct 20 00:17:40 2015 +0000
- Revision:
- 1:dcc3ba9ffe63
mole node
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jfields | 1:dcc3ba9ffe63 | 1 | #include "mbed.h" |
jfields | 1:dcc3ba9ffe63 | 2 | #include "MRF24J40.h" |
jfields | 1:dcc3ba9ffe63 | 3 | |
jfields | 1:dcc3ba9ffe63 | 4 | #include <string> |
jfields | 1:dcc3ba9ffe63 | 5 | |
jfields | 1:dcc3ba9ffe63 | 6 | // RF tranceiver to link with handheld. |
jfields | 1:dcc3ba9ffe63 | 7 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
jfields | 1:dcc3ba9ffe63 | 8 | |
jfields | 1:dcc3ba9ffe63 | 9 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
jfields | 1:dcc3ba9ffe63 | 10 | DigitalOut led1(LED1); |
jfields | 1:dcc3ba9ffe63 | 11 | DigitalOut led2(LED2); |
jfields | 1:dcc3ba9ffe63 | 12 | DigitalOut led3(LED3); |
jfields | 1:dcc3ba9ffe63 | 13 | DigitalOut led4(LED4); |
jfields | 1:dcc3ba9ffe63 | 14 | |
jfields | 1:dcc3ba9ffe63 | 15 | AnalogIn sensor(p18); |
jfields | 1:dcc3ba9ffe63 | 16 | |
jfields | 1:dcc3ba9ffe63 | 17 | // Timer |
jfields | 1:dcc3ba9ffe63 | 18 | Timer timer; |
jfields | 1:dcc3ba9ffe63 | 19 | |
jfields | 1:dcc3ba9ffe63 | 20 | // Serial port for showing RX data. |
jfields | 1:dcc3ba9ffe63 | 21 | Serial pc(USBTX, USBRX); |
jfields | 1:dcc3ba9ffe63 | 22 | |
jfields | 1:dcc3ba9ffe63 | 23 | // Used for sending and receiving |
jfields | 1:dcc3ba9ffe63 | 24 | char txBuffer[128]; |
jfields | 1:dcc3ba9ffe63 | 25 | char rxBuffer[128]; |
jfields | 1:dcc3ba9ffe63 | 26 | int rxLen; |
jfields | 1:dcc3ba9ffe63 | 27 | |
jfields | 1:dcc3ba9ffe63 | 28 | int num_readings = 0; |
jfields | 1:dcc3ba9ffe63 | 29 | |
jfields | 1:dcc3ba9ffe63 | 30 | //***************** Do not change these methods (please) *****************// |
jfields | 1:dcc3ba9ffe63 | 31 | |
jfields | 1:dcc3ba9ffe63 | 32 | /** |
jfields | 1:dcc3ba9ffe63 | 33 | * Receive data from the MRF24J40. |
jfields | 1:dcc3ba9ffe63 | 34 | * |
jfields | 1:dcc3ba9ffe63 | 35 | * @param data A pointer to a char array to hold the data |
jfields | 1:dcc3ba9ffe63 | 36 | * @param maxLength The max amount of data to read. |
jfields | 1:dcc3ba9ffe63 | 37 | */ |
jfields | 1:dcc3ba9ffe63 | 38 | int rf_receive(char *data, uint8_t maxLength) |
jfields | 1:dcc3ba9ffe63 | 39 | { |
jfields | 1:dcc3ba9ffe63 | 40 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
jfields | 1:dcc3ba9ffe63 | 41 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
jfields | 1:dcc3ba9ffe63 | 42 | |
jfields | 1:dcc3ba9ffe63 | 43 | if(len > 10) { |
jfields | 1:dcc3ba9ffe63 | 44 | //Remove the header and footer of the message |
jfields | 1:dcc3ba9ffe63 | 45 | for(uint8_t i = 0; i < len-2; i++) { |
jfields | 1:dcc3ba9ffe63 | 46 | if(i<8) { |
jfields | 1:dcc3ba9ffe63 | 47 | //Make sure our header is valid first |
jfields | 1:dcc3ba9ffe63 | 48 | if(data[i] != header[i]) |
jfields | 1:dcc3ba9ffe63 | 49 | return 0; |
jfields | 1:dcc3ba9ffe63 | 50 | } else { |
jfields | 1:dcc3ba9ffe63 | 51 | data[i-8] = data[i]; |
jfields | 1:dcc3ba9ffe63 | 52 | } |
jfields | 1:dcc3ba9ffe63 | 53 | } |
jfields | 1:dcc3ba9ffe63 | 54 | |
jfields | 1:dcc3ba9ffe63 | 55 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
jfields | 1:dcc3ba9ffe63 | 56 | } |
jfields | 1:dcc3ba9ffe63 | 57 | return ((int)len)-10; |
jfields | 1:dcc3ba9ffe63 | 58 | } |
jfields | 1:dcc3ba9ffe63 | 59 | |
jfields | 1:dcc3ba9ffe63 | 60 | /** |
jfields | 1:dcc3ba9ffe63 | 61 | * Send data to another MRF24J40. |
jfields | 1:dcc3ba9ffe63 | 62 | * |
jfields | 1:dcc3ba9ffe63 | 63 | * @param data The string to send |
jfields | 1:dcc3ba9ffe63 | 64 | * @param maxLength The length of the data to send. |
jfields | 1:dcc3ba9ffe63 | 65 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
jfields | 1:dcc3ba9ffe63 | 66 | */ |
jfields | 1:dcc3ba9ffe63 | 67 | void rf_send(char *data, uint8_t len) |
jfields | 1:dcc3ba9ffe63 | 68 | { |
jfields | 1:dcc3ba9ffe63 | 69 | //We need to prepend the message with a valid ZigBee header |
jfields | 1:dcc3ba9ffe63 | 70 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
jfields | 1:dcc3ba9ffe63 | 71 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
jfields | 1:dcc3ba9ffe63 | 72 | |
jfields | 1:dcc3ba9ffe63 | 73 | for(uint8_t i = 0; i < len+8; i++) { |
jfields | 1:dcc3ba9ffe63 | 74 | //prepend the 8-byte header |
jfields | 1:dcc3ba9ffe63 | 75 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
jfields | 1:dcc3ba9ffe63 | 76 | } |
jfields | 1:dcc3ba9ffe63 | 77 | //pc.printf("Sent: %s\r\n", send_buf+8); |
jfields | 1:dcc3ba9ffe63 | 78 | |
jfields | 1:dcc3ba9ffe63 | 79 | mrf.Send(send_buf, len+8); |
jfields | 1:dcc3ba9ffe63 | 80 | free(send_buf); |
jfields | 1:dcc3ba9ffe63 | 81 | } |
jfields | 1:dcc3ba9ffe63 | 82 | |
jfields | 1:dcc3ba9ffe63 | 83 | |
jfields | 1:dcc3ba9ffe63 | 84 | //***************** You can start coding here *****************// |
jfields | 1:dcc3ba9ffe63 | 85 | int main (void) |
jfields | 1:dcc3ba9ffe63 | 86 | { |
jfields | 1:dcc3ba9ffe63 | 87 | uint8_t channel = 2; |
jfields | 1:dcc3ba9ffe63 | 88 | |
jfields | 1:dcc3ba9ffe63 | 89 | //Set the Channel. 0 is default, 15 is max |
jfields | 1:dcc3ba9ffe63 | 90 | mrf.SetChannel(channel); |
jfields | 1:dcc3ba9ffe63 | 91 | |
jfields | 1:dcc3ba9ffe63 | 92 | |
jfields | 1:dcc3ba9ffe63 | 93 | while(true) { |
jfields | 1:dcc3ba9ffe63 | 94 | //Try to receive some data |
jfields | 1:dcc3ba9ffe63 | 95 | rxLen = rf_receive(rxBuffer, 128); |
jfields | 1:dcc3ba9ffe63 | 96 | if(rxLen > 0) { |
jfields | 1:dcc3ba9ffe63 | 97 | //Toggle the Led |
jfields | 1:dcc3ba9ffe63 | 98 | led1 = led1^1; |
jfields | 1:dcc3ba9ffe63 | 99 | pc.printf("Received: %s\r\n", rxBuffer); |
jfields | 1:dcc3ba9ffe63 | 100 | |
jfields | 1:dcc3ba9ffe63 | 101 | if (strcmp(rxBuffer, "MOLE1")) { |
jfields | 1:dcc3ba9ffe63 | 102 | led1 = 1; |
jfields | 1:dcc3ba9ffe63 | 103 | } |
jfields | 1:dcc3ba9ffe63 | 104 | if (strcmp(rxBuffer, "MOLE2")) { |
jfields | 1:dcc3ba9ffe63 | 105 | led2 = 1; |
jfields | 1:dcc3ba9ffe63 | 106 | } |
jfields | 1:dcc3ba9ffe63 | 107 | if (strcmp(rxBuffer, "MOLE3")) { |
jfields | 1:dcc3ba9ffe63 | 108 | led3 = 1; |
jfields | 1:dcc3ba9ffe63 | 109 | } |
jfields | 1:dcc3ba9ffe63 | 110 | |
jfields | 1:dcc3ba9ffe63 | 111 | // read sensor |
jfields | 1:dcc3ba9ffe63 | 112 | num_readings = 0; |
jfields | 1:dcc3ba9ffe63 | 113 | while (num_readings < 10) { |
jfields | 1:dcc3ba9ffe63 | 114 | float val = sensor.read(); |
jfields | 1:dcc3ba9ffe63 | 115 | //printf("sensor = %f\n", sensor.read()); |
jfields | 1:dcc3ba9ffe63 | 116 | if (val < 0.6) num_readings++; |
jfields | 1:dcc3ba9ffe63 | 117 | } |
jfields | 1:dcc3ba9ffe63 | 118 | |
jfields | 1:dcc3ba9ffe63 | 119 | // SEND |
jfields | 1:dcc3ba9ffe63 | 120 | strcpy(txBuffer, "HIT"); |
jfields | 1:dcc3ba9ffe63 | 121 | //Send the buffer |
jfields | 1:dcc3ba9ffe63 | 122 | rf_send(txBuffer, strlen(txBuffer) + 1); |
jfields | 1:dcc3ba9ffe63 | 123 | pc.printf("Sent: %s\r\n", txBuffer); |
jfields | 1:dcc3ba9ffe63 | 124 | led1 = 0; |
jfields | 1:dcc3ba9ffe63 | 125 | led2 = 0; |
jfields | 1:dcc3ba9ffe63 | 126 | led3 = 0; |
jfields | 1:dcc3ba9ffe63 | 127 | |
jfields | 1:dcc3ba9ffe63 | 128 | } |
jfields | 1:dcc3ba9ffe63 | 129 | |
jfields | 1:dcc3ba9ffe63 | 130 | } |
jfields | 1:dcc3ba9ffe63 | 131 | } |
jfields | 1:dcc3ba9ffe63 | 132 |