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