Britney Dorval
/
Yusheng-final_project
Final project with problem
Fork of Yusheng-final_project by
communication.h@10:5ef5fe8c7775, 2017-04-16 (annotated)
- Committer:
- britneyd
- Date:
- Sun Apr 16 19:52:52 2017 +0000
- Revision:
- 10:5ef5fe8c7775
- Parent:
- 6:ae3e6aefe908
Final project;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
csharer | 6:ae3e6aefe908 | 1 | //communication.h |
csharer | 6:ae3e6aefe908 | 2 | //Author: Carter Sharer |
csharer | 6:ae3e6aefe908 | 3 | //Date: 11/2/2016 |
csharer | 6:ae3e6aefe908 | 4 | |
csharer | 6:ae3e6aefe908 | 5 | //Taken from Part 2 (FULL IMPLEMENTATION) |
csharer | 6:ae3e6aefe908 | 6 | //If your implementation for part 2 was different then update thoes changes below |
csharer | 6:ae3e6aefe908 | 7 | |
csharer | 6:ae3e6aefe908 | 8 | #include <string> |
csharer | 6:ae3e6aefe908 | 9 | #define NONE 250 |
csharer | 6:ae3e6aefe908 | 10 | |
csharer | 6:ae3e6aefe908 | 11 | //============================ |
csharer | 6:ae3e6aefe908 | 12 | //== Pin Assignments == |
csharer | 6:ae3e6aefe908 | 13 | //============================ |
csharer | 6:ae3e6aefe908 | 14 | //Knobs |
csharer | 6:ae3e6aefe908 | 15 | #define POT1 p17 //Knob1 |
csharer | 6:ae3e6aefe908 | 16 | #define POT2 p18 //Knob2 |
csharer | 6:ae3e6aefe908 | 17 | #define POT3 p16 //Knob3 |
csharer | 6:ae3e6aefe908 | 18 | #define POT4 p15 //Knob4 |
csharer | 6:ae3e6aefe908 | 19 | //JoyStick |
csharer | 6:ae3e6aefe908 | 20 | #define POTV p19 //Vertial |
csharer | 6:ae3e6aefe908 | 21 | #define POTH p20 //Horizontal |
csharer | 6:ae3e6aefe908 | 22 | //MRF24J |
csharer | 6:ae3e6aefe908 | 23 | #define SDI p11 |
csharer | 6:ae3e6aefe908 | 24 | #define SDO p12 |
csharer | 6:ae3e6aefe908 | 25 | #define SCK p13 |
csharer | 6:ae3e6aefe908 | 26 | #define CS p7 |
csharer | 6:ae3e6aefe908 | 27 | #define RESET p8 |
csharer | 6:ae3e6aefe908 | 28 | //Button |
csharer | 6:ae3e6aefe908 | 29 | #define BUTTON1 p21 |
csharer | 6:ae3e6aefe908 | 30 | |
csharer | 6:ae3e6aefe908 | 31 | //MRF24J40 |
csharer | 6:ae3e6aefe908 | 32 | PinName mosi(SDI); |
csharer | 6:ae3e6aefe908 | 33 | PinName miso(SDO); |
csharer | 6:ae3e6aefe908 | 34 | PinName sck(SCK); |
csharer | 6:ae3e6aefe908 | 35 | PinName cs(CS); |
csharer | 6:ae3e6aefe908 | 36 | PinName reset(RESET); |
csharer | 6:ae3e6aefe908 | 37 | // RF tranceiver |
csharer | 6:ae3e6aefe908 | 38 | MRF24J40 mrf(mosi, miso, sck, cs, reset); |
csharer | 6:ae3e6aefe908 | 39 | |
csharer | 6:ae3e6aefe908 | 40 | // Send / receive buffers. |
csharer | 6:ae3e6aefe908 | 41 | // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends |
csharer | 6:ae3e6aefe908 | 42 | // to reject data that doesn't have the right header. So the first |
csharer | 6:ae3e6aefe908 | 43 | // 8 bytes in txBuffer look like a valid header. The remaining 120 |
csharer | 6:ae3e6aefe908 | 44 | // bytes can be used for anything you like. |
csharer | 6:ae3e6aefe908 | 45 | char txBuffer[128]; |
csharer | 6:ae3e6aefe908 | 46 | char rxBuffer[128]; |
csharer | 6:ae3e6aefe908 | 47 | int rxLen; |
csharer | 6:ae3e6aefe908 | 48 | |
csharer | 6:ae3e6aefe908 | 49 | |
csharer | 6:ae3e6aefe908 | 50 | //***************** Do not change these methods (please) *****************// |
csharer | 6:ae3e6aefe908 | 51 | /** |
csharer | 6:ae3e6aefe908 | 52 | * Receive data from the MRF24J40. |
csharer | 6:ae3e6aefe908 | 53 | * |
csharer | 6:ae3e6aefe908 | 54 | * @param data A pointer to a char array to hold the data |
csharer | 6:ae3e6aefe908 | 55 | * @param maxLength The max amount of data to read. |
csharer | 6:ae3e6aefe908 | 56 | */ |
csharer | 6:ae3e6aefe908 | 57 | int rf_receive(char *data, uint8_t maxLength) |
csharer | 6:ae3e6aefe908 | 58 | { |
csharer | 6:ae3e6aefe908 | 59 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
csharer | 6:ae3e6aefe908 | 60 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
csharer | 6:ae3e6aefe908 | 61 | |
csharer | 6:ae3e6aefe908 | 62 | if(len > 10) { |
csharer | 6:ae3e6aefe908 | 63 | //Remove the header and footer of the message |
csharer | 6:ae3e6aefe908 | 64 | for(uint8_t i = 0; i < len-2; i++) { |
csharer | 6:ae3e6aefe908 | 65 | if(i<8) { |
csharer | 6:ae3e6aefe908 | 66 | //Make sure our header is valid first |
csharer | 6:ae3e6aefe908 | 67 | if(data[i] != header[i]) { |
csharer | 6:ae3e6aefe908 | 68 | //mrf.Reset(); |
csharer | 6:ae3e6aefe908 | 69 | return 0; |
csharer | 6:ae3e6aefe908 | 70 | } |
csharer | 6:ae3e6aefe908 | 71 | } else { |
csharer | 6:ae3e6aefe908 | 72 | data[i-8] = data[i]; |
csharer | 6:ae3e6aefe908 | 73 | } |
csharer | 6:ae3e6aefe908 | 74 | } |
csharer | 6:ae3e6aefe908 | 75 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
csharer | 6:ae3e6aefe908 | 76 | } |
csharer | 6:ae3e6aefe908 | 77 | return ((int)len)-10; |
csharer | 6:ae3e6aefe908 | 78 | } |
csharer | 6:ae3e6aefe908 | 79 | |
csharer | 6:ae3e6aefe908 | 80 | /** |
csharer | 6:ae3e6aefe908 | 81 | * Send data to another MRF24J40. |
csharer | 6:ae3e6aefe908 | 82 | * |
csharer | 6:ae3e6aefe908 | 83 | * @param data The string to send |
csharer | 6:ae3e6aefe908 | 84 | * @param maxLength The length of the data to send. |
csharer | 6:ae3e6aefe908 | 85 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
csharer | 6:ae3e6aefe908 | 86 | */ |
csharer | 6:ae3e6aefe908 | 87 | void rf_send(char *data, uint8_t len) |
csharer | 6:ae3e6aefe908 | 88 | { |
csharer | 6:ae3e6aefe908 | 89 | //We need to prepend the message with a valid ZigBee header |
csharer | 6:ae3e6aefe908 | 90 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
csharer | 6:ae3e6aefe908 | 91 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
csharer | 6:ae3e6aefe908 | 92 | |
csharer | 6:ae3e6aefe908 | 93 | for(uint8_t i = 0; i < len+8; i++) { |
csharer | 6:ae3e6aefe908 | 94 | //prepend the 8-byte header |
csharer | 6:ae3e6aefe908 | 95 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
csharer | 6:ae3e6aefe908 | 96 | } |
csharer | 6:ae3e6aefe908 | 97 | //pc.printf("Sent: %s\r\n", send_buf+8); |
csharer | 6:ae3e6aefe908 | 98 | |
csharer | 6:ae3e6aefe908 | 99 | mrf.Send(send_buf, len+8); |
csharer | 6:ae3e6aefe908 | 100 | free(send_buf); |
csharer | 6:ae3e6aefe908 | 101 | } |
csharer | 6:ae3e6aefe908 | 102 | //***************** You can start coding here *****************// |
csharer | 6:ae3e6aefe908 | 103 | |
csharer | 6:ae3e6aefe908 | 104 | //Returns true if c is a letter, false otherwise |
csharer | 6:ae3e6aefe908 | 105 | bool isLetter(char c) { |
csharer | 6:ae3e6aefe908 | 106 | if(('a'<=c & c<='z') | ('A'<=c & c<='Z')) |
csharer | 6:ae3e6aefe908 | 107 | return true; |
csharer | 6:ae3e6aefe908 | 108 | return false; |
csharer | 6:ae3e6aefe908 | 109 | } |
csharer | 6:ae3e6aefe908 | 110 | |
csharer | 6:ae3e6aefe908 | 111 | //Returns true if c is a number, false otherwise |
csharer | 6:ae3e6aefe908 | 112 | bool isNumber(char c) { |
csharer | 6:ae3e6aefe908 | 113 | if('0'<=c & c<='9') |
csharer | 6:ae3e6aefe908 | 114 | return true; |
csharer | 6:ae3e6aefe908 | 115 | return false; |
csharer | 6:ae3e6aefe908 | 116 | } |
csharer | 6:ae3e6aefe908 | 117 | |
csharer | 6:ae3e6aefe908 | 118 | //Pulls data out fo rxBuffer and updates global variables accordingly |
csharer | 6:ae3e6aefe908 | 119 | void communication_protocal(int len) |
csharer | 6:ae3e6aefe908 | 120 | { |
csharer | 6:ae3e6aefe908 | 121 | bool found_name = false; |
csharer | 6:ae3e6aefe908 | 122 | bool found_num = false; |
csharer | 6:ae3e6aefe908 | 123 | bool complete_name = false; |
csharer | 6:ae3e6aefe908 | 124 | bool complete_num = false; |
csharer | 6:ae3e6aefe908 | 125 | uint8_t name_start = NONE; uint8_t name_end = NONE; |
csharer | 6:ae3e6aefe908 | 126 | uint8_t num_start = NONE; uint8_t num_end = NONE; |
csharer | 6:ae3e6aefe908 | 127 | |
csharer | 6:ae3e6aefe908 | 128 | //Loop through all charaters in rxBuffer |
csharer | 6:ae3e6aefe908 | 129 | for(uint8_t i = 0; i <= rxLen; i++) { |
csharer | 6:ae3e6aefe908 | 130 | char c = rxBuffer[i]; |
csharer | 6:ae3e6aefe908 | 131 | //pc.printf("Indexed char '%c'\r\n", c); |
csharer | 6:ae3e6aefe908 | 132 | |
csharer | 6:ae3e6aefe908 | 133 | //Find the start of a name (Check if its a letter) |
csharer | 6:ae3e6aefe908 | 134 | if(isLetter(c) & name_start==NONE) { //if a num |
csharer | 6:ae3e6aefe908 | 135 | //If We havent found a name yet, this is start of a name |
csharer | 6:ae3e6aefe908 | 136 | if(found_name == false) { |
csharer | 6:ae3e6aefe908 | 137 | //pc.printf("found name start at: '%c'\r\n", c); |
csharer | 6:ae3e6aefe908 | 138 | name_start = i; |
csharer | 6:ae3e6aefe908 | 139 | found_name = true; |
csharer | 6:ae3e6aefe908 | 140 | } |
csharer | 6:ae3e6aefe908 | 141 | } |
csharer | 6:ae3e6aefe908 | 142 | //Find 'end of name' charater: ' ', ':', '-' |
csharer | 6:ae3e6aefe908 | 143 | else if(((c == ' ') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character |
csharer | 6:ae3e6aefe908 | 144 | if(found_name) { |
csharer | 6:ae3e6aefe908 | 145 | complete_name = true; |
csharer | 6:ae3e6aefe908 | 146 | name_end = i; |
csharer | 6:ae3e6aefe908 | 147 | //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]); |
csharer | 6:ae3e6aefe908 | 148 | } |
csharer | 6:ae3e6aefe908 | 149 | } |
csharer | 6:ae3e6aefe908 | 150 | |
csharer | 6:ae3e6aefe908 | 151 | //Find 'start of a number' charater, Check if its a number, or '-', or a '.' |
csharer | 6:ae3e6aefe908 | 152 | else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) { |
csharer | 6:ae3e6aefe908 | 153 | if(found_num == false) { |
csharer | 6:ae3e6aefe908 | 154 | //pc.printf("found num start at: '%c'\r\n",c); |
csharer | 6:ae3e6aefe908 | 155 | num_start = i; |
csharer | 6:ae3e6aefe908 | 156 | found_num = true; |
csharer | 6:ae3e6aefe908 | 157 | } |
csharer | 6:ae3e6aefe908 | 158 | } |
csharer | 6:ae3e6aefe908 | 159 | //Found end of number character: ' ', ':', '-', or a letter |
csharer | 6:ae3e6aefe908 | 160 | else if( (((c==' ')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) { |
csharer | 6:ae3e6aefe908 | 161 | if(found_num) { |
csharer | 6:ae3e6aefe908 | 162 | complete_num = true; |
csharer | 6:ae3e6aefe908 | 163 | num_end = i; |
csharer | 6:ae3e6aefe908 | 164 | //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]); |
csharer | 6:ae3e6aefe908 | 165 | } |
csharer | 6:ae3e6aefe908 | 166 | } |
csharer | 6:ae3e6aefe908 | 167 | |
csharer | 6:ae3e6aefe908 | 168 | //If we have a complete name AND number value (ie. start and end of each != NONE) |
csharer | 6:ae3e6aefe908 | 169 | if(found_name & found_num & complete_name & complete_num) { |
csharer | 6:ae3e6aefe908 | 170 | //pc.printf("Found MATCH\r\n"); |
csharer | 6:ae3e6aefe908 | 171 | //Reset flags |
csharer | 6:ae3e6aefe908 | 172 | found_name = false; |
csharer | 6:ae3e6aefe908 | 173 | found_num = false; |
csharer | 6:ae3e6aefe908 | 174 | complete_name = false; |
csharer | 6:ae3e6aefe908 | 175 | complete_num = false; |
csharer | 6:ae3e6aefe908 | 176 | |
csharer | 6:ae3e6aefe908 | 177 | //Set name |
csharer | 6:ae3e6aefe908 | 178 | uint8_t nameLen = uint8_t((name_end-name_start) + 1); |
csharer | 6:ae3e6aefe908 | 179 | char * name[nameLen]; |
csharer | 6:ae3e6aefe908 | 180 | *name = &rxBuffer[name_start]; |
csharer | 6:ae3e6aefe908 | 181 | rxBuffer[name_end] = '\0'; |
csharer | 6:ae3e6aefe908 | 182 | |
csharer | 6:ae3e6aefe908 | 183 | //Set num |
csharer | 6:ae3e6aefe908 | 184 | uint8_t numLen = uint8_t((num_end-num_start) + 1); |
csharer | 6:ae3e6aefe908 | 185 | char * num[numLen]; |
csharer | 6:ae3e6aefe908 | 186 | *num = &rxBuffer[num_start]; |
csharer | 6:ae3e6aefe908 | 187 | rxBuffer[num_end] = '\0'; |
csharer | 6:ae3e6aefe908 | 188 | |
csharer | 6:ae3e6aefe908 | 189 | //Set variables |
csharer | 6:ae3e6aefe908 | 190 | if(strcmp(*name, "Jstick_h\0")==0) |
csharer | 6:ae3e6aefe908 | 191 | jstick_h = (int8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 192 | else if(strcmp(*name, "Jstick_v\0")==0) |
csharer | 6:ae3e6aefe908 | 193 | jstick_v = (int8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 194 | else if(strcmp(*name, "Knob1\0")==0) |
csharer | 6:ae3e6aefe908 | 195 | knob1 = (uint8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 196 | else if(strcmp(*name, "Knob2\0")==0) |
csharer | 6:ae3e6aefe908 | 197 | knob2 = (uint8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 198 | else if(strcmp(*name, "Knob3\0")==0) |
csharer | 6:ae3e6aefe908 | 199 | knob3 = (uint8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 200 | else if(strcmp(*name, "Knob4\0")==0) |
csharer | 6:ae3e6aefe908 | 201 | knob4 = (uint8_t)atoi(*num); |
csharer | 6:ae3e6aefe908 | 202 | else if(strcmp(*name, "Button\0")==0) |
csharer | 6:ae3e6aefe908 | 203 | button = (bool)atoi(*num); |
csharer | 6:ae3e6aefe908 | 204 | |
csharer | 6:ae3e6aefe908 | 205 | //Reset flags |
csharer | 6:ae3e6aefe908 | 206 | name_start = NONE; |
csharer | 6:ae3e6aefe908 | 207 | name_end = NONE; |
csharer | 6:ae3e6aefe908 | 208 | num_start = NONE; |
csharer | 6:ae3e6aefe908 | 209 | num_end = NONE; |
csharer | 6:ae3e6aefe908 | 210 | } |
csharer | 6:ae3e6aefe908 | 211 | } |
csharer | 6:ae3e6aefe908 | 212 | } |