ESE519 lab6 part 1 controller , blank implementation of part 1
Dependencies: Joystick_skeleton mbed
Fork of ESE519_Lab6_part1_skelleton by
main.cpp@1:98c414bbfe8a, 2016-10-20 (annotated)
- Committer:
- csharer
- Date:
- Thu Oct 20 23:44:40 2016 +0000
- Revision:
- 1:98c414bbfe8a
- Parent:
- 0:0ebe6f55caee
- Child:
- 3:1d10014a4646
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
csharer | 0:0ebe6f55caee | 1 | //ESE519 Lab6 Controller Part 1 (FULL IMPLIMENTATION) |
csharer | 0:0ebe6f55caee | 2 | //Author: Carter Sharer |
csharer | 0:0ebe6f55caee | 3 | //Date: 10/18/2016 |
csharer | 0:0ebe6f55caee | 4 | //Sources: ESE350 Whack-A-Mole: https://developer.mbed.org/users/mlab4/code/ESE350-Whack-a-Mole/ |
csharer | 0:0ebe6f55caee | 5 | //This is a simple working templete for sending and receving between two MRF24J40 Modules |
csharer | 0:0ebe6f55caee | 6 | //Contains a communication protocol for sending a receiving |
csharer | 0:0ebe6f55caee | 7 | |
csharer | 0:0ebe6f55caee | 8 | #include "mbed.h" |
csharer | 0:0ebe6f55caee | 9 | #include "MRF24J40.h" |
csharer | 0:0ebe6f55caee | 10 | #include <string> |
csharer | 0:0ebe6f55caee | 11 | #include "Joystick.h" |
csharer | 0:0ebe6f55caee | 12 | |
csharer | 0:0ebe6f55caee | 13 | #define SEND //Uncomment if you want to transmit data |
csharer | 1:98c414bbfe8a | 14 | #define RECEIVE //Uncomment if you want to receive data |
csharer | 0:0ebe6f55caee | 15 | |
csharer | 0:0ebe6f55caee | 16 | #define NONE 250 |
csharer | 0:0ebe6f55caee | 17 | |
csharer | 0:0ebe6f55caee | 18 | //============================ |
csharer | 0:0ebe6f55caee | 19 | //== Pin Assignments == |
csharer | 0:0ebe6f55caee | 20 | //============================ |
csharer | 0:0ebe6f55caee | 21 | //Knobs |
csharer | 0:0ebe6f55caee | 22 | #define POT1 p17 //Knob1 |
csharer | 0:0ebe6f55caee | 23 | #define POT2 p18 //Knob2 |
csharer | 0:0ebe6f55caee | 24 | #define POT3 p16 //Knob3 |
csharer | 0:0ebe6f55caee | 25 | #define POT4 p15 //Knob4 |
csharer | 0:0ebe6f55caee | 26 | //JoyStick |
csharer | 0:0ebe6f55caee | 27 | #define POTV p19 //Vertial |
csharer | 0:0ebe6f55caee | 28 | #define POTH p20 //Horizontal |
csharer | 0:0ebe6f55caee | 29 | //MRF24J |
csharer | 0:0ebe6f55caee | 30 | #define SDI p11 |
csharer | 0:0ebe6f55caee | 31 | #define SDO p12 |
csharer | 0:0ebe6f55caee | 32 | #define SCK p13 |
csharer | 0:0ebe6f55caee | 33 | #define CS p7 |
csharer | 0:0ebe6f55caee | 34 | #define RESET p8 |
csharer | 0:0ebe6f55caee | 35 | //Button |
csharer | 0:0ebe6f55caee | 36 | #define BUTTON1 p21 |
csharer | 0:0ebe6f55caee | 37 | #define COMMUNICATION_FORMAT "Jstick_h: %0.0f Jstick_v: %0.0f Knob1 %0.2f Knob2 %0.2f Knob3 %0.2f Knob4 %0.2f Button: %d" |
csharer | 0:0ebe6f55caee | 38 | |
csharer | 0:0ebe6f55caee | 39 | //============================ |
csharer | 0:0ebe6f55caee | 40 | //== Objects == |
csharer | 0:0ebe6f55caee | 41 | //============================ |
csharer | 0:0ebe6f55caee | 42 | //Knobs |
csharer | 0:0ebe6f55caee | 43 | AnalogIn pot1(POT1); |
csharer | 0:0ebe6f55caee | 44 | AnalogIn pot2(POT2); |
csharer | 0:0ebe6f55caee | 45 | AnalogIn pot3(POT3); |
csharer | 0:0ebe6f55caee | 46 | AnalogIn pot4(POT4); |
csharer | 0:0ebe6f55caee | 47 | float knob1, knob2, knob3, knob4; |
csharer | 0:0ebe6f55caee | 48 | |
csharer | 0:0ebe6f55caee | 49 | //Joystick |
csharer | 0:0ebe6f55caee | 50 | Joystick jstick(POTV, POTH); |
csharer | 0:0ebe6f55caee | 51 | float jstick_h, jstick_v; |
csharer | 0:0ebe6f55caee | 52 | |
csharer | 0:0ebe6f55caee | 53 | |
csharer | 0:0ebe6f55caee | 54 | //MRF24J40 |
csharer | 0:0ebe6f55caee | 55 | PinName mosi(SDI); |
csharer | 0:0ebe6f55caee | 56 | PinName miso(SDO); |
csharer | 0:0ebe6f55caee | 57 | PinName sck(SCK); |
csharer | 0:0ebe6f55caee | 58 | PinName cs(CS); |
csharer | 0:0ebe6f55caee | 59 | PinName reset(RESET); |
csharer | 0:0ebe6f55caee | 60 | // RF tranceiver |
csharer | 0:0ebe6f55caee | 61 | MRF24J40 mrf(mosi, miso, sck, cs, reset); |
csharer | 0:0ebe6f55caee | 62 | |
csharer | 0:0ebe6f55caee | 63 | //Button |
csharer | 0:0ebe6f55caee | 64 | DigitalIn Button(BUTTON1); |
csharer | 0:0ebe6f55caee | 65 | bool button; |
csharer | 0:0ebe6f55caee | 66 | |
csharer | 0:0ebe6f55caee | 67 | // LEDs |
csharer | 0:0ebe6f55caee | 68 | DigitalOut led1(LED1); |
csharer | 0:0ebe6f55caee | 69 | DigitalOut led2(LED2); |
csharer | 0:0ebe6f55caee | 70 | DigitalOut led3(LED3); |
csharer | 0:0ebe6f55caee | 71 | DigitalOut led4(LED4); |
csharer | 0:0ebe6f55caee | 72 | |
csharer | 0:0ebe6f55caee | 73 | // Timer |
csharer | 0:0ebe6f55caee | 74 | Timer timer; |
csharer | 0:0ebe6f55caee | 75 | |
csharer | 0:0ebe6f55caee | 76 | // Serial port for showing RX data. |
csharer | 0:0ebe6f55caee | 77 | Serial pc(USBTX, USBRX); |
csharer | 0:0ebe6f55caee | 78 | |
csharer | 0:0ebe6f55caee | 79 | |
csharer | 0:0ebe6f55caee | 80 | // Send / receive buffers. |
csharer | 0:0ebe6f55caee | 81 | // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends |
csharer | 0:0ebe6f55caee | 82 | // to reject data that doesn't have the right header. So the first |
csharer | 0:0ebe6f55caee | 83 | // 8 bytes in txBuffer look like a valid header. The remaining 120 |
csharer | 0:0ebe6f55caee | 84 | // bytes can be used for anything you like. |
csharer | 0:0ebe6f55caee | 85 | char txBuffer[128]; |
csharer | 0:0ebe6f55caee | 86 | char rxBuffer[128]; |
csharer | 0:0ebe6f55caee | 87 | int rxLen; |
csharer | 0:0ebe6f55caee | 88 | |
csharer | 0:0ebe6f55caee | 89 | //***************** Do not change these methods (please) *****************// |
csharer | 0:0ebe6f55caee | 90 | /** |
csharer | 0:0ebe6f55caee | 91 | * Receive data from the MRF24J40. |
csharer | 0:0ebe6f55caee | 92 | * |
csharer | 0:0ebe6f55caee | 93 | * @param data A pointer to a char array to hold the data |
csharer | 0:0ebe6f55caee | 94 | * @param maxLength The max amount of data to read. |
csharer | 0:0ebe6f55caee | 95 | */ |
csharer | 0:0ebe6f55caee | 96 | int rf_receive(char *data, uint8_t maxLength) |
csharer | 0:0ebe6f55caee | 97 | { |
csharer | 0:0ebe6f55caee | 98 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
csharer | 0:0ebe6f55caee | 99 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
csharer | 0:0ebe6f55caee | 100 | |
csharer | 0:0ebe6f55caee | 101 | if(len > 10) { |
csharer | 0:0ebe6f55caee | 102 | //Remove the header and footer of the message |
csharer | 0:0ebe6f55caee | 103 | for(uint8_t i = 0; i < len-2; i++) { |
csharer | 0:0ebe6f55caee | 104 | if(i<8) { |
csharer | 0:0ebe6f55caee | 105 | //Make sure our header is valid first |
csharer | 0:0ebe6f55caee | 106 | if(data[i] != header[i]) |
csharer | 0:0ebe6f55caee | 107 | return 0; |
csharer | 0:0ebe6f55caee | 108 | } else { |
csharer | 0:0ebe6f55caee | 109 | data[i-8] = data[i]; |
csharer | 0:0ebe6f55caee | 110 | } |
csharer | 0:0ebe6f55caee | 111 | } |
csharer | 0:0ebe6f55caee | 112 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
csharer | 0:0ebe6f55caee | 113 | } |
csharer | 0:0ebe6f55caee | 114 | return ((int)len)-10; |
csharer | 0:0ebe6f55caee | 115 | } |
csharer | 0:0ebe6f55caee | 116 | |
csharer | 0:0ebe6f55caee | 117 | /** |
csharer | 0:0ebe6f55caee | 118 | * Send data to another MRF24J40. |
csharer | 0:0ebe6f55caee | 119 | * |
csharer | 0:0ebe6f55caee | 120 | * @param data The string to send |
csharer | 0:0ebe6f55caee | 121 | * @param maxLength The length of the data to send. |
csharer | 0:0ebe6f55caee | 122 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
csharer | 0:0ebe6f55caee | 123 | */ |
csharer | 0:0ebe6f55caee | 124 | void rf_send(char *data, uint8_t len) |
csharer | 0:0ebe6f55caee | 125 | { |
csharer | 0:0ebe6f55caee | 126 | //We need to prepend the message with a valid ZigBee header |
csharer | 0:0ebe6f55caee | 127 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
csharer | 0:0ebe6f55caee | 128 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
csharer | 0:0ebe6f55caee | 129 | |
csharer | 0:0ebe6f55caee | 130 | for(uint8_t i = 0; i < len+8; i++) { |
csharer | 0:0ebe6f55caee | 131 | //prepend the 8-byte header |
csharer | 0:0ebe6f55caee | 132 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
csharer | 0:0ebe6f55caee | 133 | } |
csharer | 0:0ebe6f55caee | 134 | //pc.printf("Sent: %s\r\n", send_buf+8); |
csharer | 0:0ebe6f55caee | 135 | |
csharer | 0:0ebe6f55caee | 136 | mrf.Send(send_buf, len+8); |
csharer | 0:0ebe6f55caee | 137 | free(send_buf); |
csharer | 0:0ebe6f55caee | 138 | } |
csharer | 0:0ebe6f55caee | 139 | //***************** You can start coding here *****************// |
csharer | 0:0ebe6f55caee | 140 | |
csharer | 1:98c414bbfe8a | 141 | //Returns true if c is a letter (upper or lower case), false otherwise |
csharer | 0:0ebe6f55caee | 142 | bool isLetter(char c) { |
csharer | 1:98c414bbfe8a | 143 | |
csharer | 1:98c414bbfe8a | 144 | /* |
csharer | 1:98c414bbfe8a | 145 | * YOUR IMPLIMENTATION HERE |
csharer | 1:98c414bbfe8a | 146 | * |
csharer | 1:98c414bbfe8a | 147 | * |
csharer | 1:98c414bbfe8a | 148 | * |
csharer | 1:98c414bbfe8a | 149 | */ |
csharer | 1:98c414bbfe8a | 150 | |
csharer | 0:0ebe6f55caee | 151 | return false; |
csharer | 0:0ebe6f55caee | 152 | } |
csharer | 0:0ebe6f55caee | 153 | |
csharer | 1:98c414bbfe8a | 154 | //Returns true if c is a number character (0-9), false otherwise |
csharer | 0:0ebe6f55caee | 155 | bool isNumber(char c) { |
csharer | 1:98c414bbfe8a | 156 | |
csharer | 1:98c414bbfe8a | 157 | /* |
csharer | 1:98c414bbfe8a | 158 | * YOUR IMPLIMENTATION HERE |
csharer | 1:98c414bbfe8a | 159 | * |
csharer | 1:98c414bbfe8a | 160 | * |
csharer | 1:98c414bbfe8a | 161 | * |
csharer | 1:98c414bbfe8a | 162 | */ |
csharer | 1:98c414bbfe8a | 163 | |
csharer | 0:0ebe6f55caee | 164 | return false; |
csharer | 0:0ebe6f55caee | 165 | } |
csharer | 0:0ebe6f55caee | 166 | |
csharer | 0:0ebe6f55caee | 167 | //Pulls data out of rxBuffer and updates global variables accordingly |
csharer | 1:98c414bbfe8a | 168 | //Len is the length of the rxBuffer we are going to scan |
csharer | 0:0ebe6f55caee | 169 | void communication_protocal(int len) |
csharer | 0:0ebe6f55caee | 170 | { |
csharer | 0:0ebe6f55caee | 171 | bool found_name = false; |
csharer | 0:0ebe6f55caee | 172 | bool found_num = false; |
csharer | 0:0ebe6f55caee | 173 | bool complete_name = false; |
csharer | 0:0ebe6f55caee | 174 | bool complete_num = false; |
csharer | 0:0ebe6f55caee | 175 | uint8_t name_start = NONE; uint8_t name_end = NONE; |
csharer | 0:0ebe6f55caee | 176 | uint8_t num_start = NONE; uint8_t num_end = NONE; |
csharer | 0:0ebe6f55caee | 177 | |
csharer | 0:0ebe6f55caee | 178 | //Loop through all charaters in rxBuffer |
csharer | 0:0ebe6f55caee | 179 | for(uint8_t i = 0; i <= rxLen; i++) { |
csharer | 0:0ebe6f55caee | 180 | char c = rxBuffer[i]; |
csharer | 0:0ebe6f55caee | 181 | //pc.printf("Indexed char '%c'\r\n", c); |
csharer | 0:0ebe6f55caee | 182 | |
csharer | 1:98c414bbfe8a | 183 | //Is it the start of a name? (Check if its a letter) |
csharer | 0:0ebe6f55caee | 184 | if(isLetter(c) & name_start==NONE) { //if a num |
csharer | 0:0ebe6f55caee | 185 | //If We havent found a name yet, this is start of a name |
csharer | 0:0ebe6f55caee | 186 | if(found_name == false) { |
csharer | 0:0ebe6f55caee | 187 | //pc.printf("found name start at: '%c'\r\n", c); |
csharer | 0:0ebe6f55caee | 188 | name_start = i; |
csharer | 0:0ebe6f55caee | 189 | found_name = true; |
csharer | 0:0ebe6f55caee | 190 | } |
csharer | 0:0ebe6f55caee | 191 | } |
csharer | 0:0ebe6f55caee | 192 | //Is is a 'end of name' charater? Check for ' ', ':', '-' |
csharer | 0:0ebe6f55caee | 193 | else if(((c == ' ') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character |
csharer | 0:0ebe6f55caee | 194 | if(found_name) { |
csharer | 0:0ebe6f55caee | 195 | complete_name = true; |
csharer | 0:0ebe6f55caee | 196 | name_end = i; |
csharer | 0:0ebe6f55caee | 197 | //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]); |
csharer | 0:0ebe6f55caee | 198 | } |
csharer | 0:0ebe6f55caee | 199 | } |
csharer | 0:0ebe6f55caee | 200 | |
csharer | 0:0ebe6f55caee | 201 | //Is it a 'start of a number' charater? Check if its a number, or '-', or a '.' |
csharer | 0:0ebe6f55caee | 202 | else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) { |
csharer | 0:0ebe6f55caee | 203 | if(found_num == false) { |
csharer | 0:0ebe6f55caee | 204 | //pc.printf("found num start at: '%c'\r\n",c); |
csharer | 0:0ebe6f55caee | 205 | num_start = i; |
csharer | 0:0ebe6f55caee | 206 | found_num = true; |
csharer | 0:0ebe6f55caee | 207 | } |
csharer | 0:0ebe6f55caee | 208 | } |
csharer | 0:0ebe6f55caee | 209 | //Is it a 'end of number' character? Check if its a ' ', ':', '-', or a letter |
csharer | 0:0ebe6f55caee | 210 | else if( (((c==' ')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) { |
csharer | 0:0ebe6f55caee | 211 | if(found_num) { |
csharer | 0:0ebe6f55caee | 212 | complete_num = true; |
csharer | 0:0ebe6f55caee | 213 | num_end = i; |
csharer | 0:0ebe6f55caee | 214 | //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]); |
csharer | 0:0ebe6f55caee | 215 | } |
csharer | 0:0ebe6f55caee | 216 | } |
csharer | 0:0ebe6f55caee | 217 | |
csharer | 0:0ebe6f55caee | 218 | //If we have a complete name AND number value (ie. start and end of each != NONE) |
csharer | 0:0ebe6f55caee | 219 | if(found_name & found_num & complete_name & complete_num) { |
csharer | 0:0ebe6f55caee | 220 | //pc.printf("Found MATCH\r\n"); |
csharer | 0:0ebe6f55caee | 221 | //Reset flags |
csharer | 0:0ebe6f55caee | 222 | found_name = false; |
csharer | 0:0ebe6f55caee | 223 | found_num = false; |
csharer | 0:0ebe6f55caee | 224 | complete_name = false; |
csharer | 0:0ebe6f55caee | 225 | complete_num = false; |
csharer | 0:0ebe6f55caee | 226 | |
csharer | 0:0ebe6f55caee | 227 | //Set name |
csharer | 0:0ebe6f55caee | 228 | uint8_t nameLen = uint8_t((name_end-name_start) + 1); |
csharer | 0:0ebe6f55caee | 229 | char * name[nameLen]; |
csharer | 0:0ebe6f55caee | 230 | *name = &rxBuffer[name_start]; |
csharer | 0:0ebe6f55caee | 231 | rxBuffer[name_end] = '\0'; |
csharer | 0:0ebe6f55caee | 232 | |
csharer | 0:0ebe6f55caee | 233 | //Set num |
csharer | 0:0ebe6f55caee | 234 | uint8_t numLen = uint8_t((num_end-num_start) + 1); |
csharer | 0:0ebe6f55caee | 235 | char * num[numLen]; |
csharer | 0:0ebe6f55caee | 236 | *num = &rxBuffer[num_start]; |
csharer | 0:0ebe6f55caee | 237 | rxBuffer[num_end] = '\0'; |
csharer | 0:0ebe6f55caee | 238 | |
csharer | 1:98c414bbfe8a | 239 | //Now that we have isolated a name and its number value |
csharer | 1:98c414bbfe8a | 240 | //we want to set the corresponding value to this number. |
csharer | 1:98c414bbfe8a | 241 | //Ex: if name is 'Knob4' and num is '0.34', we want to the the |
csharer | 1:98c414bbfe8a | 242 | // variable name knob4 to the value 0.34. |
csharer | 1:98c414bbfe8a | 243 | //Do this for all variable names in COMMUNICATION_FORMAT |
csharer | 1:98c414bbfe8a | 244 | //HINT: look up strcmp, and atof |
csharer | 1:98c414bbfe8a | 245 | /* |
csharer | 1:98c414bbfe8a | 246 | * YOUR IMPLIMENTATION HERE |
csharer | 1:98c414bbfe8a | 247 | * |
csharer | 1:98c414bbfe8a | 248 | * |
csharer | 1:98c414bbfe8a | 249 | * |
csharer | 1:98c414bbfe8a | 250 | */ |
csharer | 1:98c414bbfe8a | 251 | |
csharer | 0:0ebe6f55caee | 252 | |
csharer | 0:0ebe6f55caee | 253 | //Reset flags |
csharer | 0:0ebe6f55caee | 254 | name_start = NONE; |
csharer | 0:0ebe6f55caee | 255 | name_end = NONE; |
csharer | 0:0ebe6f55caee | 256 | num_start = NONE; |
csharer | 0:0ebe6f55caee | 257 | num_end = NONE; |
csharer | 0:0ebe6f55caee | 258 | } |
csharer | 0:0ebe6f55caee | 259 | } |
csharer | 0:0ebe6f55caee | 260 | } |
csharer | 0:0ebe6f55caee | 261 | |
csharer | 0:0ebe6f55caee | 262 | int main (void) |
csharer | 0:0ebe6f55caee | 263 | { |
csharer | 0:0ebe6f55caee | 264 | //Set the Channel. 0 is default, 15 is max |
csharer | 0:0ebe6f55caee | 265 | uint8_t channel = 2; |
csharer | 0:0ebe6f55caee | 266 | mrf.SetChannel(channel); |
csharer | 0:0ebe6f55caee | 267 | |
csharer | 0:0ebe6f55caee | 268 | //Set Baud rate (9600-115200 is ideal) |
csharer | 0:0ebe6f55caee | 269 | pc.baud(115200); |
csharer | 0:0ebe6f55caee | 270 | pc.printf("\r\n Start! \r\n"); |
csharer | 0:0ebe6f55caee | 271 | |
csharer | 0:0ebe6f55caee | 272 | //Start Timer |
csharer | 0:0ebe6f55caee | 273 | timer.start(); |
csharer | 0:0ebe6f55caee | 274 | |
csharer | 1:98c414bbfe8a | 275 | //Scale Joystick Values, range[-100, 100] |
csharer | 1:98c414bbfe8a | 276 | jstick.setScale(-100, 100); |
csharer | 0:0ebe6f55caee | 277 | |
csharer | 0:0ebe6f55caee | 278 | while(1) { |
csharer | 0:0ebe6f55caee | 279 | //(1) Read Joystick Values, round to int8_t presision |
csharer | 0:0ebe6f55caee | 280 | jstick_h = (int8_t)jstick.horizontal(); |
csharer | 0:0ebe6f55caee | 281 | jstick_v = (int8_t)jstick.vertical(); |
csharer | 1:98c414bbfe8a | 282 | //pc.printf("H: %0.2f V:%0.2f \r\n", jstick.horizontal(), jstick.vertical()); |
csharer | 0:0ebe6f55caee | 283 | |
csharer | 0:0ebe6f55caee | 284 | //(2) Read Pot Values, Scale, and round to precision |
csharer | 0:0ebe6f55caee | 285 | knob1 = (uint8_t)(pot1.read() * 100); //rounded to uint8_t |
csharer | 0:0ebe6f55caee | 286 | knob2 = (pot2.read() * 100); |
csharer | 0:0ebe6f55caee | 287 | knob3 = (pot3.read()); |
csharer | 0:0ebe6f55caee | 288 | knob4 = (int)(pot4.read() * 100); //rounded to float |
csharer | 0:0ebe6f55caee | 289 | |
csharer | 0:0ebe6f55caee | 290 | //(3)Read Button Val, Add to buffer |
csharer | 0:0ebe6f55caee | 291 | button = !Button.read(); //button is active low |
csharer | 0:0ebe6f55caee | 292 | |
csharer | 0:0ebe6f55caee | 293 | #ifdef RECEIVE |
csharer | 0:0ebe6f55caee | 294 | //RECEIVE DATA: Try to receive some data |
csharer | 0:0ebe6f55caee | 295 | rxLen = rf_receive(rxBuffer, 128); |
csharer | 0:0ebe6f55caee | 296 | if(rxLen > 0) { |
csharer | 0:0ebe6f55caee | 297 | //Toggle the Led |
csharer | 0:0ebe6f55caee | 298 | led1 = led1^1; |
csharer | 0:0ebe6f55caee | 299 | |
csharer | 0:0ebe6f55caee | 300 | //(4) Process data with our protocal |
csharer | 0:0ebe6f55caee | 301 | communication_protocal(rxLen); |
csharer | 0:0ebe6f55caee | 302 | |
csharer | 0:0ebe6f55caee | 303 | //Print values once we recieve and process data |
csharer | 1:98c414bbfe8a | 304 | pc.printf("Received| "); |
csharer | 0:0ebe6f55caee | 305 | pc.printf(COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button); |
csharer | 0:0ebe6f55caee | 306 | pc.printf("\r\n"); |
csharer | 0:0ebe6f55caee | 307 | }//main if |
csharer | 0:0ebe6f55caee | 308 | #endif |
csharer | 0:0ebe6f55caee | 309 | |
csharer | 0:0ebe6f55caee | 310 | #ifdef SEND |
csharer | 0:0ebe6f55caee | 311 | //SEND DATA: Send some data every 1/2 second |
csharer | 0:0ebe6f55caee | 312 | if(timer.read_ms() >= 500) { |
csharer | 0:0ebe6f55caee | 313 | //Reset the timer to 0 |
csharer | 0:0ebe6f55caee | 314 | timer.reset(); |
csharer | 0:0ebe6f55caee | 315 | // Toggle LED 2. |
csharer | 0:0ebe6f55caee | 316 | led2 = led2^1; |
csharer | 0:0ebe6f55caee | 317 | |
csharer | 0:0ebe6f55caee | 318 | //(5) Add all values to buffer to be sent |
csharer | 0:0ebe6f55caee | 319 | sprintf(txBuffer, COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button); |
csharer | 0:0ebe6f55caee | 320 | |
csharer | 0:0ebe6f55caee | 321 | //(6) Send the buffer |
csharer | 0:0ebe6f55caee | 322 | rf_send(txBuffer, strlen(txBuffer) + 1); |
csharer | 1:98c414bbfe8a | 323 | pc.printf("Sent| %s\r\n", txBuffer); |
csharer | 0:0ebe6f55caee | 324 | } |
csharer | 0:0ebe6f55caee | 325 | #endif |
csharer | 0:0ebe6f55caee | 326 | |
csharer | 0:0ebe6f55caee | 327 | } //end loop |
csharer | 0:0ebe6f55caee | 328 | }//end main |