Britney Dorval / Mbed 2 deprecated Yusheng-final_project

Dependencies:   mbed

Fork of Yusheng-final_project by Carter Sharer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers communication.h Source File

communication.h

00001 //communication.h
00002 //Author: Carter Sharer 
00003 //Date: 11/2/2016
00004 
00005 //Taken from Part 2 (FULL IMPLEMENTATION)
00006 //If your implementation for part 2 was different then update thoes changes below
00007 
00008 #include <string>
00009 #define NONE 250
00010 
00011 //============================
00012 //==    Pin Assignments     ==
00013 //============================
00014 //Knobs
00015 #define POT1 p17  //Knob1
00016 #define POT2 p18  //Knob2
00017 #define POT3 p16  //Knob3
00018 #define POT4 p15  //Knob4
00019 //JoyStick
00020 #define POTV p19 //Vertial
00021 #define POTH p20 //Horizontal
00022 //MRF24J
00023 #define SDI p11
00024 #define SDO p12
00025 #define SCK p13
00026 #define CS p7
00027 #define RESET p8
00028 //Button
00029 #define BUTTON1 p21
00030 
00031 //MRF24J40
00032 PinName mosi(SDI);
00033 PinName miso(SDO);
00034 PinName sck(SCK);
00035 PinName cs(CS);
00036 PinName reset(RESET);
00037 // RF tranceiver
00038 MRF24J40 mrf(mosi, miso, sck, cs, reset);
00039 
00040 // Send / receive buffers.
00041 // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends
00042 // to reject data that doesn't have the right header. So the first
00043 // 8 bytes in txBuffer look like a valid header. The remaining 120
00044 // bytes can be used for anything you like.
00045 char txBuffer[128];
00046 char rxBuffer[128];
00047 int rxLen;
00048 
00049 
00050 //***************** Do not change these methods (please) *****************//
00051 /**
00052 * Receive data from the MRF24J40.
00053 *
00054 * @param data A pointer to a char array to hold the data
00055 * @param maxLength The max amount of data to read.
00056 */
00057 int rf_receive(char *data, uint8_t maxLength)
00058 {
00059     uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
00060     uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
00061 
00062     if(len > 10) {
00063         //Remove the header and footer of the message
00064         for(uint8_t i = 0; i < len-2; i++) {
00065             if(i<8) {
00066                 //Make sure our header is valid first
00067                 if(data[i] != header[i]) {
00068                     //mrf.Reset();
00069                     return 0;
00070                 }
00071             } else {
00072                 data[i-8] = data[i];
00073             }
00074         }
00075         //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
00076     }
00077     return ((int)len)-10;
00078 }
00079 
00080 /**
00081 * Send data to another MRF24J40.
00082 *
00083 * @param data The string to send
00084 * @param maxLength The length of the data to send.
00085 *                  If you are sending a null-terminated string you can pass strlen(data)+1
00086 */
00087 void rf_send(char *data, uint8_t len)
00088 {
00089     //We need to prepend the message with a valid ZigBee header
00090     uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
00091     uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
00092 
00093     for(uint8_t i = 0; i < len+8; i++) {
00094         //prepend the 8-byte header
00095         send_buf[i] = (i<8) ? header[i] : data[i-8];
00096     }
00097     //pc.printf("Sent: %s\r\n", send_buf+8);
00098 
00099     mrf.Send(send_buf, len+8);
00100     free(send_buf);
00101 }
00102 //***************** You can start coding here *****************//
00103 
00104 //Returns true if c is a letter, false otherwise
00105 bool isLetter(char c) {
00106     if(('a'<=c & c<='z') | ('A'<=c & c<='Z'))
00107         return true;
00108     return false;
00109 }
00110 
00111 //Returns true if c is a number, false otherwise
00112 bool isNumber(char c) {
00113     if('0'<=c & c<='9')
00114         return true;
00115     return false;
00116 }
00117 
00118 //Pulls data out fo rxBuffer and updates global variables accordingly 
00119 void communication_protocal(int len)
00120 {
00121     bool found_name = false;
00122     bool found_num = false;
00123     bool complete_name = false;
00124     bool complete_num = false;
00125     uint8_t name_start = NONE; uint8_t name_end = NONE;
00126     uint8_t num_start = NONE; uint8_t num_end = NONE;
00127     
00128     //Loop through all charaters in rxBuffer
00129     for(uint8_t i = 0; i <= rxLen; i++) {
00130         char c = rxBuffer[i];
00131         //pc.printf("Indexed char '%c'\r\n", c);
00132 
00133         //Find the start of a name (Check if its a letter)
00134         if(isLetter(c) & name_start==NONE) { //if a num
00135             //If We havent found a name yet, this is start of a name
00136             if(found_name == false) {
00137                 //pc.printf("found name start at: '%c'\r\n", c);
00138                 name_start = i;
00139                 found_name = true;
00140             }
00141         }
00142         //Find 'end of name' charater: ' ', ':', '-'
00143         else if(((c == ' ') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character
00144             if(found_name) {
00145                 complete_name = true;
00146                 name_end = i;
00147                 //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]);
00148             }
00149         }
00150 
00151         //Find 'start of a number' charater, Check if its a number, or '-', or a '.'
00152         else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) {
00153             if(found_num == false) {
00154                 //pc.printf("found num start at: '%c'\r\n",c);
00155                 num_start = i;
00156                 found_num = true;
00157             }
00158         }
00159         //Found end of number character: ' ', ':', '-', or a letter
00160         else if( (((c==' ')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) {
00161             if(found_num) {
00162                 complete_num = true;
00163                 num_end = i;
00164                 //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]);
00165             }
00166         }
00167         
00168         //If we have a complete name AND number value (ie. start and end of each != NONE)
00169         if(found_name & found_num & complete_name & complete_num) {
00170             //pc.printf("Found MATCH\r\n");
00171             //Reset flags
00172             found_name = false;
00173             found_num = false;
00174             complete_name = false;
00175             complete_num = false;
00176             
00177             //Set name
00178             uint8_t nameLen = uint8_t((name_end-name_start) + 1);
00179             char * name[nameLen];
00180             *name = &rxBuffer[name_start];
00181             rxBuffer[name_end] = '\0';
00182             
00183             //Set num
00184             uint8_t numLen = uint8_t((num_end-num_start) + 1);
00185             char * num[numLen];
00186             *num = &rxBuffer[num_start];
00187             rxBuffer[num_end] = '\0';
00188             
00189             //Set variables
00190             if(strcmp(*name, "Jstick_h\0")==0)
00191                 jstick_h = (int8_t)atoi(*num);
00192             else if(strcmp(*name, "Jstick_v\0")==0) 
00193                 jstick_v = (int8_t)atoi(*num);
00194             else if(strcmp(*name, "Knob1\0")==0) 
00195                 knob1 = (uint8_t)atoi(*num);
00196             else if(strcmp(*name, "Knob2\0")==0) 
00197                 knob2 = (uint8_t)atoi(*num);
00198             else if(strcmp(*name, "Knob3\0")==0)
00199                 knob3 = (uint8_t)atoi(*num);
00200             else if(strcmp(*name, "Knob4\0")==0) 
00201                 knob4 = (uint8_t)atoi(*num);
00202             else if(strcmp(*name, "Button\0")==0) 
00203                 button = (bool)atoi(*num);
00204             
00205             //Reset flags
00206             name_start = NONE;
00207             name_end = NONE;
00208             num_start = NONE;
00209             num_end = NONE;
00210         }
00211     }
00212 }