Cameron Reid
/
SmartThings_Level_Control
Mbed Level control for Samsung SmartThings.
Main.cpp@0:671c7986b866, 2016-09-21 (annotated)
- Committer:
- Cameron
- Date:
- Wed Sep 21 09:38:07 2016 +0000
- Revision:
- 0:671c7986b866
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cameron | 0:671c7986b866 | 1 | #include "mbed.h" |
Cameron | 0:671c7986b866 | 2 | #include "C12832.h" |
Cameron | 0:671c7986b866 | 3 | |
Cameron | 0:671c7986b866 | 4 | #define AppV 0x01 |
Cameron | 0:671c7986b866 | 5 | #define HarV 0x01 |
Cameron | 0:671c7986b866 | 6 | |
Cameron | 0:671c7986b866 | 7 | C12832 lcd(p5, p7, p6, p8, p11); //configure LCD |
Cameron | 0:671c7986b866 | 8 | RawSerial xbee(p9,p10); //Set Serial to XBee |
Cameron | 0:671c7986b866 | 9 | Serial pc(USBTX, USBRX); //Set Serial to PC. |
Cameron | 0:671c7986b866 | 10 | DigitalOut led2 (LED2); //configure onboard LEDs |
Cameron | 0:671c7986b866 | 11 | DigitalOut led1 (LED1); |
Cameron | 0:671c7986b866 | 12 | PwmOut LED (LED4); |
Cameron | 0:671c7986b866 | 13 | volatile char x,y,z,len,sum; //some useful internal variables |
Cameron | 0:671c7986b866 | 14 | volatile int length, size, check; |
Cameron | 0:671c7986b866 | 15 | char packet[60]; |
Cameron | 0:671c7986b866 | 16 | char PacketAddr[10]; |
Cameron | 0:671c7986b866 | 17 | char Opacket[60]; |
Cameron | 0:671c7986b866 | 18 | char AI[8] = {0x7E,0x00,0x04,0x08,0x21,0x41,0x49,0x4C}; //create AT command to querey network joined? |
Cameron | 0:671c7986b866 | 19 | char MY[8] = {0x7E,0x00,0x04,0x08,0x21,0x4D,0x59,0x30}; //create AT command to get my network ID. |
Cameron | 0:671c7986b866 | 20 | char SL[8] = {0x7E,0x00,0x04,0x08,0x21,0x53,0x4C,0x37}; //create AT command to get 64bit LSB. |
Cameron | 0:671c7986b866 | 21 | char SH[8] = {0x7E,0x00,0x04,0x08,0x21,0x53,0x48,0x3B}; //create AT command to get 64bit MSB. |
Cameron | 0:671c7986b866 | 22 | char CB[8] = {0x7E,0x00,0x04,0x08,0x21,0x43,0x42,0x51}; //Create AT command to leave network. |
Cameron | 0:671c7986b866 | 23 | char LNw[2]; |
Cameron | 0:671c7986b866 | 24 | char Addr[8]; |
Cameron | 0:671c7986b866 | 25 | char pay[101]; |
Cameron | 0:671c7986b866 | 26 | char Buffer[101]; |
Cameron | 0:671c7986b866 | 27 | char EP, DEP, SEP, seqNum, cmdID, frmType; |
Cameron | 0:671c7986b866 | 28 | char atID[2]; |
Cameron | 0:671c7986b866 | 29 | char Clu[2]; |
Cameron | 0:671c7986b866 | 30 | char Pro[2]; |
Cameron | 0:671c7986b866 | 31 | char Man[3] = {'A','R','M'}; //Product manufacturer |
Cameron | 0:671c7986b866 | 32 | char Dev[10] = {'N','X','P','L','P','C','1','7','6','8'}; //Device Name |
Cameron | 0:671c7986b866 | 33 | |
Cameron | 0:671c7986b866 | 34 | void packet_interupt(); //Create subroutine to deal with incoming packet. |
Cameron | 0:671c7986b866 | 35 | void Psend(char API_packet[]); //Create subroutine to send API commands |
Cameron | 0:671c7986b866 | 36 | void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[], char PaySize); //function to create packets to send. |
Cameron | 0:671c7986b866 | 37 | void Builder(); //Get usefull infromation from packets. |
Cameron | 0:671c7986b866 | 38 | void checksum(); //Create the checksum for the packet. |
Cameron | 0:671c7986b866 | 39 | void DevAnnc(); //function to send the device announce command. |
Cameron | 0:671c7986b866 | 40 | void SimpleDesc(); //Function to create the Simple description packet |
Cameron | 0:671c7986b866 | 41 | void ActiveEPReq(); //Function to create the Active end point request. |
Cameron | 0:671c7986b866 | 42 | void ClusterBasic(); //Function to create the Cluster Basic. |
Cameron | 0:671c7986b866 | 43 | void OnOff(); //Function to process on off commands. |
Cameron | 0:671c7986b866 | 44 | void DefRes(); //Function to create the response for the on off command. |
Cameron | 0:671c7986b866 | 45 | void Level(); |
Cameron | 0:671c7986b866 | 46 | void Update(char val); |
Cameron | 0:671c7986b866 | 47 | void UpdateLevel(); |
Cameron | 0:671c7986b866 | 48 | |
Cameron | 0:671c7986b866 | 49 | int main(){ |
Cameron | 0:671c7986b866 | 50 | lcd.cls(); //clear lcd screen. |
Cameron | 0:671c7986b866 | 51 | lcd.locate(0,1); //locate the cursor. |
Cameron | 0:671c7986b866 | 52 | lcd.printf(" Joining network..."); //Print to lcd screen |
Cameron | 0:671c7986b866 | 53 | memset(packet, 0, sizeof(packet)); //Clear array |
Cameron | 0:671c7986b866 | 54 | |
Cameron | 0:671c7986b866 | 55 | xbee.attach(&packet_interupt, Serial::RxIrq); //attach interupt when recieving serial information. |
Cameron | 0:671c7986b866 | 56 | |
Cameron | 0:671c7986b866 | 57 | while(packet[5] != 0){ //While Xbee has not joined a network |
Cameron | 0:671c7986b866 | 58 | wait(1); //Wait 0.1 second. |
Cameron | 0:671c7986b866 | 59 | Psend(AI); //Send network joined status again. |
Cameron | 0:671c7986b866 | 60 | while(check == 0) |
Cameron | 0:671c7986b866 | 61 | {} //Read data. |
Cameron | 0:671c7986b866 | 62 | } |
Cameron | 0:671c7986b866 | 63 | lcd.cls(); //Clear lcd screen. |
Cameron | 0:671c7986b866 | 64 | lcd.locate(0,1); //Locate the cursor. |
Cameron | 0:671c7986b866 | 65 | lcd.printf(" Connected"); //Print to lcd screen. |
Cameron | 0:671c7986b866 | 66 | |
Cameron | 0:671c7986b866 | 67 | check = 0; |
Cameron | 0:671c7986b866 | 68 | Psend(MY); //Send AT command to get the 16bit network address. |
Cameron | 0:671c7986b866 | 69 | while(check == 0) //Wait for packet. |
Cameron | 0:671c7986b866 | 70 | {} |
Cameron | 0:671c7986b866 | 71 | LNw[0] = packet[5]; //Transfer 16 bit address payload. |
Cameron | 0:671c7986b866 | 72 | LNw[1] = packet[6]; //Transfer 16 bit address payload. |
Cameron | 0:671c7986b866 | 73 | |
Cameron | 0:671c7986b866 | 74 | check = 0; |
Cameron | 0:671c7986b866 | 75 | Psend(SH); //Send AT command to get the 64bit address MSB. |
Cameron | 0:671c7986b866 | 76 | while(check == 0) //Wait for packet. |
Cameron | 0:671c7986b866 | 77 | {} |
Cameron | 0:671c7986b866 | 78 | Addr[0] = packet[5]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 79 | Addr[1] = packet[6]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 80 | Addr[2] = packet[7]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 81 | Addr[3] = packet[8]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 82 | |
Cameron | 0:671c7986b866 | 83 | check = 0; |
Cameron | 0:671c7986b866 | 84 | Psend(SL); //Send AT command to get the 64bit address LSB. |
Cameron | 0:671c7986b866 | 85 | while (check == 0) //Wait for packet. |
Cameron | 0:671c7986b866 | 86 | {} |
Cameron | 0:671c7986b866 | 87 | Addr[4] = packet[5]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 88 | Addr[5] = packet[6]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 89 | Addr[6] = packet[7]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 90 | Addr[7] = packet[8]; //Transfer 64 bit address payload. |
Cameron | 0:671c7986b866 | 91 | DevAnnc(); //Send Device Announce. |
Cameron | 0:671c7986b866 | 92 | |
Cameron | 0:671c7986b866 | 93 | while(1) { |
Cameron | 0:671c7986b866 | 94 | lcd.locate(0,21); //Locate LCD cursor. |
Cameron | 0:671c7986b866 | 95 | lcd.printf("Packets recieved = %d", z); //Print to LCD display. |
Cameron | 0:671c7986b866 | 96 | check = 0; |
Cameron | 0:671c7986b866 | 97 | while(check == 0) //Wait for packet. |
Cameron | 0:671c7986b866 | 98 | {} |
Cameron | 0:671c7986b866 | 99 | if (packet[16] == 0x00){ //If packet profile = 16. |
Cameron | 0:671c7986b866 | 100 | if (packet[14] == 0x04){ //If packet cluster = 14. |
Cameron | 0:671c7986b866 | 101 | lcd.locate(0,11); //Locate LCD cursor. |
Cameron | 0:671c7986b866 | 102 | lcd.printf(" simple dec"); |
Cameron | 0:671c7986b866 | 103 | SimpleDesc(); //Process Simple Desciption request. |
Cameron | 0:671c7986b866 | 104 | } |
Cameron | 0:671c7986b866 | 105 | else if (packet[14] ==0x05){ //If packet cluster = 15. |
Cameron | 0:671c7986b866 | 106 | lcd.locate(0,11); //Locate LCD display |
Cameron | 0:671c7986b866 | 107 | lcd.printf(" Active EP req"); |
Cameron | 0:671c7986b866 | 108 | ActiveEPReq(); //Process active EP Request. |
Cameron | 0:671c7986b866 | 109 | } |
Cameron | 0:671c7986b866 | 110 | else{ |
Cameron | 0:671c7986b866 | 111 | lcd.locate(0,11); //Locate LCD cursor. |
Cameron | 0:671c7986b866 | 112 | lcd.printf(" Unknown Packet"); //Print to lcd screen. |
Cameron | 0:671c7986b866 | 113 | } |
Cameron | 0:671c7986b866 | 114 | } |
Cameron | 0:671c7986b866 | 115 | else{ //Else if packet profile is not = 00. |
Cameron | 0:671c7986b866 | 116 | if (packet[14] == 0x00){ //If packet Cluster = 00. |
Cameron | 0:671c7986b866 | 117 | lcd.locate(0,11); //Locate LCD cursor. |
Cameron | 0:671c7986b866 | 118 | lcd.printf(" Cluster Basic"); |
Cameron | 0:671c7986b866 | 119 | ClusterBasic(); //Process Cluster Basic request. |
Cameron | 0:671c7986b866 | 120 | } |
Cameron | 0:671c7986b866 | 121 | else if (packet[14] ==0x06){ //Else if packet profile is not = 06. |
Cameron | 0:671c7986b866 | 122 | lcd.locate(0,11); //Locate LCD cursor. |
Cameron | 0:671c7986b866 | 123 | lcd.printf(" on/off "); |
Cameron | 0:671c7986b866 | 124 | OnOff(); //Process On/Off. |
Cameron | 0:671c7986b866 | 125 | } |
Cameron | 0:671c7986b866 | 126 | else if (packet[14] == 0x08){ |
Cameron | 0:671c7986b866 | 127 | lcd.locate(0,11); |
Cameron | 0:671c7986b866 | 128 | lcd.printf(" Level control "); |
Cameron | 0:671c7986b866 | 129 | Level(); |
Cameron | 0:671c7986b866 | 130 | } |
Cameron | 0:671c7986b866 | 131 | else{ |
Cameron | 0:671c7986b866 | 132 | lcd.locate(0,11); //Locate the cursor. |
Cameron | 0:671c7986b866 | 133 | lcd.printf(" Unknown Packet"); //Print to lcd screen. |
Cameron | 0:671c7986b866 | 134 | lcd.printf(" %x", packet[14]); |
Cameron | 0:671c7986b866 | 135 | } |
Cameron | 0:671c7986b866 | 136 | } |
Cameron | 0:671c7986b866 | 137 | } //end of while(1) |
Cameron | 0:671c7986b866 | 138 | } |
Cameron | 0:671c7986b866 | 139 | |
Cameron | 0:671c7986b866 | 140 | /********************************* Packet interupt *************************************** |
Cameron | 0:671c7986b866 | 141 | When a new Xbee packet is revieved it is sent to this function to be processed. |
Cameron | 0:671c7986b866 | 142 | |
Cameron | 0:671c7986b866 | 143 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 144 | void packet_interupt(){ //packet interupt begins when incoming data sensed on the rx line. |
Cameron | 0:671c7986b866 | 145 | |
Cameron | 0:671c7986b866 | 146 | if (xbee.readable()) //If data is available |
Cameron | 0:671c7986b866 | 147 | x=xbee.getc(); //Get data |
Cameron | 0:671c7986b866 | 148 | if (x==0x7E){ //Test for start of Frame. |
Cameron | 0:671c7986b866 | 149 | led2 = 1; //Set indicator LED2 to on. |
Cameron | 0:671c7986b866 | 150 | z ++; //Increment packet counter. |
Cameron | 0:671c7986b866 | 151 | memset(packet, 0, sizeof(packet)); //Clear packet buffer. |
Cameron | 0:671c7986b866 | 152 | while(xbee.readable() == 0); //If data is available. |
Cameron | 0:671c7986b866 | 153 | x = xbee.getc(); //Get data MSB length. |
Cameron | 0:671c7986b866 | 154 | while(xbee.readable() == 0); //If data is available. |
Cameron | 0:671c7986b866 | 155 | len = xbee.getc(); //Get data LSB length. |
Cameron | 0:671c7986b866 | 156 | x = 0; //Counter to 0. |
Cameron | 0:671c7986b866 | 157 | sum = 0; //Set checksum calculator to 0. |
Cameron | 0:671c7986b866 | 158 | while(x < (len + 1)){ //Fill packet variable with entire API packet. |
Cameron | 0:671c7986b866 | 159 | while (xbee.readable() == 0); //If data is available. |
Cameron | 0:671c7986b866 | 160 | packet[x] = xbee.getc(); //Get data and place it in the data array. |
Cameron | 0:671c7986b866 | 161 | sum = (sum + packet[x]); //Add value to checksum calculation. |
Cameron | 0:671c7986b866 | 162 | x++; |
Cameron | 0:671c7986b866 | 163 | } |
Cameron | 0:671c7986b866 | 164 | sum = (0xFF - ( sum & 0xFF)); //Finish calculating the checksum. |
Cameron | 0:671c7986b866 | 165 | if (packet[x] == sum ){ //If the checksum and the calculated checksum are equal. |
Cameron | 0:671c7986b866 | 166 | if (packet[0] == 0x91){ //If packet ID = 0x91 |
Cameron | 0:671c7986b866 | 167 | Builder(); //Extract packet information |
Cameron | 0:671c7986b866 | 168 | check = 1; |
Cameron | 0:671c7986b866 | 169 | } |
Cameron | 0:671c7986b866 | 170 | else if(packet[0] == 0x88){ //Else if packet ID = 0x88. |
Cameron | 0:671c7986b866 | 171 | check = 1; |
Cameron | 0:671c7986b866 | 172 | } |
Cameron | 0:671c7986b866 | 173 | else |
Cameron | 0:671c7986b866 | 174 | memset(packet, 0, sizeof(packet)); //Clear array |
Cameron | 0:671c7986b866 | 175 | } |
Cameron | 0:671c7986b866 | 176 | else{ |
Cameron | 0:671c7986b866 | 177 | memset(packet, 0, sizeof(packet)); //Clear array |
Cameron | 0:671c7986b866 | 178 | } |
Cameron | 0:671c7986b866 | 179 | led2 = 0; //Set indicator LED2 to off. |
Cameron | 0:671c7986b866 | 180 | } |
Cameron | 0:671c7986b866 | 181 | } |
Cameron | 0:671c7986b866 | 182 | /************************************* Builder ****************************************** |
Cameron | 0:671c7986b866 | 183 | Extract all the important inforamtion from the packet and assoicate it to the relevant variable. |
Cameron | 0:671c7986b866 | 184 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 185 | void Builder() |
Cameron | 0:671c7986b866 | 186 | { |
Cameron | 0:671c7986b866 | 187 | x = 0; //Set the counter to 0. |
Cameron | 0:671c7986b866 | 188 | while (x < 10){ //send the command |
Cameron | 0:671c7986b866 | 189 | PacketAddr[x] = packet[(x+1)]; //Copy the packets 16 and 64bit address. |
Cameron | 0:671c7986b866 | 190 | x ++; |
Cameron | 0:671c7986b866 | 191 | } |
Cameron | 0:671c7986b866 | 192 | DEP = packet[11]; //Get the source endpoint. |
Cameron | 0:671c7986b866 | 193 | SEP = packet[12]; //Get the destination endpoint. |
Cameron | 0:671c7986b866 | 194 | Clu[0] = packet[13]; //Get Cluster ID MSB. |
Cameron | 0:671c7986b866 | 195 | Clu[1] = packet[14]; //Get Cluster ID LSB. |
Cameron | 0:671c7986b866 | 196 | Pro[0] = packet[15]; //Get profile ID MSB. |
Cameron | 0:671c7986b866 | 197 | Pro[1] = packet[16]; //Get profile ID LSB. |
Cameron | 0:671c7986b866 | 198 | x = 0; //Set counter to 0. |
Cameron | 0:671c7986b866 | 199 | while ((x+18) < len){ |
Cameron | 0:671c7986b866 | 200 | pay[x] = packet[(x+18)]; //Copy packet payload. |
Cameron | 0:671c7986b866 | 201 | x++; |
Cameron | 0:671c7986b866 | 202 | } |
Cameron | 0:671c7986b866 | 203 | } |
Cameron | 0:671c7986b866 | 204 | /************************************ PBuild ****************************************** |
Cameron | 0:671c7986b866 | 205 | Organise the information from other functions into the correct packet format. |
Cameron | 0:671c7986b866 | 206 | |
Cameron | 0:671c7986b866 | 207 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 208 | void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[],char PaySize){ |
Cameron | 0:671c7986b866 | 209 | |
Cameron | 0:671c7986b866 | 210 | size = 0; //Set size to 0. |
Cameron | 0:671c7986b866 | 211 | x = 0; //Set counter to 0. |
Cameron | 0:671c7986b866 | 212 | memset(Opacket, 0, sizeof(Opacket)); //Clear output buffer. |
Cameron | 0:671c7986b866 | 213 | |
Cameron | 0:671c7986b866 | 214 | size = 20 +(PaySize); //Find out the size of the out going packet. |
Cameron | 0:671c7986b866 | 215 | |
Cameron | 0:671c7986b866 | 216 | Opacket[0] = 0x7E; //Packet start bit. |
Cameron | 0:671c7986b866 | 217 | Opacket[1] = 0x00; //Packet length MSB. |
Cameron | 0:671c7986b866 | 218 | Opacket[2] = size; //Packet length LSB. |
Cameron | 0:671c7986b866 | 219 | Opacket[3] = 0x11; //Packet Type |
Cameron | 0:671c7986b866 | 220 | Opacket[4] = 0x00; //No response needed. |
Cameron | 0:671c7986b866 | 221 | Opacket[5] = FrameAddr[0]; //64 bit address High |
Cameron | 0:671c7986b866 | 222 | Opacket[6] = FrameAddr[1]; |
Cameron | 0:671c7986b866 | 223 | Opacket[7] = FrameAddr[2]; |
Cameron | 0:671c7986b866 | 224 | Opacket[8] = FrameAddr[3]; |
Cameron | 0:671c7986b866 | 225 | Opacket[9] = FrameAddr[4]; //64 bit address Low |
Cameron | 0:671c7986b866 | 226 | Opacket[10] = FrameAddr[5]; |
Cameron | 0:671c7986b866 | 227 | Opacket[11] = FrameAddr[6]; |
Cameron | 0:671c7986b866 | 228 | Opacket[12] = FrameAddr[7]; |
Cameron | 0:671c7986b866 | 229 | Opacket[13] = FrameAddr[8]; //16 bit PAN address |
Cameron | 0:671c7986b866 | 230 | Opacket[14] = FrameAddr[9]; |
Cameron | 0:671c7986b866 | 231 | Opacket[15] = sEP; //Source Endpoint |
Cameron | 0:671c7986b866 | 232 | Opacket[16] = dEP; //Destinaton Endpoint |
Cameron | 0:671c7986b866 | 233 | Opacket[17] = Cluster[0]; //Cluster ID |
Cameron | 0:671c7986b866 | 234 | Opacket[18] = Cluster[1]; |
Cameron | 0:671c7986b866 | 235 | Opacket[19] = Profile[0]; //Profile ID |
Cameron | 0:671c7986b866 | 236 | Opacket[20] = Profile[1]; |
Cameron | 0:671c7986b866 | 237 | Opacket[21] = 0x00; //Broadcast radius |
Cameron | 0:671c7986b866 | 238 | Opacket[22] = 0x00; //Trasmit option bitfield |
Cameron | 0:671c7986b866 | 239 | while (x < PaySize){ |
Cameron | 0:671c7986b866 | 240 | Opacket[(23 + x)] = Payload[x]; //Copy packet payload |
Cameron | 0:671c7986b866 | 241 | x ++; |
Cameron | 0:671c7986b866 | 242 | } |
Cameron | 0:671c7986b866 | 243 | |
Cameron | 0:671c7986b866 | 244 | checksum(); //Create the check sum. |
Cameron | 0:671c7986b866 | 245 | Psend(Opacket); //Send the packet. |
Cameron | 0:671c7986b866 | 246 | } |
Cameron | 0:671c7986b866 | 247 | /************************************ Checksum ****************************************** |
Cameron | 0:671c7986b866 | 248 | Create the checksum for outgoing packets. |
Cameron | 0:671c7986b866 | 249 | |
Cameron | 0:671c7986b866 | 250 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 251 | void checksum(){ |
Cameron | 0:671c7986b866 | 252 | |
Cameron | 0:671c7986b866 | 253 | y = 0; //Set Alternatie counter to 0 |
Cameron | 0:671c7986b866 | 254 | sum = 0; |
Cameron | 0:671c7986b866 | 255 | while (y < (Opacket[2])){ //Create the checksum. |
Cameron | 0:671c7986b866 | 256 | sum = (sum + Opacket[y + 3]); |
Cameron | 0:671c7986b866 | 257 | y++; |
Cameron | 0:671c7986b866 | 258 | } |
Cameron | 0:671c7986b866 | 259 | Opacket[23 + x] = (0xFF - ( sum & 0xFF)); |
Cameron | 0:671c7986b866 | 260 | } |
Cameron | 0:671c7986b866 | 261 | /************************************* Psend ******************************************** |
Cameron | 0:671c7986b866 | 262 | Send the packet out serially to the Xbee module. |
Cameron | 0:671c7986b866 | 263 | |
Cameron | 0:671c7986b866 | 264 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 265 | void Psend(char API_packet[]){ |
Cameron | 0:671c7986b866 | 266 | |
Cameron | 0:671c7986b866 | 267 | led1 = 1; //Set onboard LED1 on for diagnostics |
Cameron | 0:671c7986b866 | 268 | length = (API_packet[2] + 4); //Calaculate packet length using packet length identifier and add the start byte, 2 byte length and checksum. |
Cameron | 0:671c7986b866 | 269 | x = 0; |
Cameron | 0:671c7986b866 | 270 | while (x < length){ //Send the command |
Cameron | 0:671c7986b866 | 271 | xbee.putc(API_packet[x]); |
Cameron | 0:671c7986b866 | 272 | x ++; |
Cameron | 0:671c7986b866 | 273 | } |
Cameron | 0:671c7986b866 | 274 | led1 = 0; //Set diagnostics LED1 to off |
Cameron | 0:671c7986b866 | 275 | } |
Cameron | 0:671c7986b866 | 276 | /********************************* Device Announce ************************************** |
Cameron | 0:671c7986b866 | 277 | Send a Device announce packet when joining or re joining and network. |
Cameron | 0:671c7986b866 | 278 | For more informaion see ZigBee spec page 109 |
Cameron | 0:671c7986b866 | 279 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 280 | void DevAnnc(){ |
Cameron | 0:671c7986b866 | 281 | |
Cameron | 0:671c7986b866 | 282 | char AnncFrame[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE}; |
Cameron | 0:671c7986b866 | 283 | char AnncPayload[12]; //Create char array for payload. |
Cameron | 0:671c7986b866 | 284 | char DevClu[2] = {0x00, 0x13}; //Create char array for Cluster ID. |
Cameron | 0:671c7986b866 | 285 | char DevPro[2] = {0x00, 0x00}; //Create char array for Profile ID. |
Cameron | 0:671c7986b866 | 286 | AnncPayload[0] = 0x22; //Set seqence ID to 0x22. |
Cameron | 0:671c7986b866 | 287 | AnncPayload[1] = LNw[1]; //Copy 16bit PAN address little endian. |
Cameron | 0:671c7986b866 | 288 | AnncPayload[2] = LNw[0]; |
Cameron | 0:671c7986b866 | 289 | AnncPayload[3] = Addr[7]; //Copy 64bit address little endian. |
Cameron | 0:671c7986b866 | 290 | AnncPayload[4] = Addr[6]; |
Cameron | 0:671c7986b866 | 291 | AnncPayload[5] = Addr[5]; |
Cameron | 0:671c7986b866 | 292 | AnncPayload[6] = Addr[4]; |
Cameron | 0:671c7986b866 | 293 | AnncPayload[7] = Addr[3]; |
Cameron | 0:671c7986b866 | 294 | AnncPayload[8] = Addr[2]; |
Cameron | 0:671c7986b866 | 295 | AnncPayload[9] = Addr[1]; |
Cameron | 0:671c7986b866 | 296 | AnncPayload[10] = Addr[0]; |
Cameron | 0:671c7986b866 | 297 | AnncPayload[11] = 0x8C; //Set device parameters. |
Cameron | 0:671c7986b866 | 298 | |
Cameron | 0:671c7986b866 | 299 | PBuild(AnncFrame,0x00,0x00,DevPro,DevClu, AnncPayload, 12); //Send packet to builder. |
Cameron | 0:671c7986b866 | 300 | } |
Cameron | 0:671c7986b866 | 301 | /******************************** Simple Description ************************************ |
Cameron | 0:671c7986b866 | 302 | Send a response to a Simple Description Request. |
Cameron | 0:671c7986b866 | 303 | For more informaion see ZigBee spec page 159. |
Cameron | 0:671c7986b866 | 304 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 305 | void SimpleDesc(){ |
Cameron | 0:671c7986b866 | 306 | |
Cameron | 0:671c7986b866 | 307 | EP = pay[3]; //Get endpoint from payload. |
Cameron | 0:671c7986b866 | 308 | char DescClu[2] = {0x80,0x04}; //Create char array for cluster ID. |
Cameron | 0:671c7986b866 | 309 | char DescPro[2] = {0x00,0x00}; //Create char array for profile ID. |
Cameron | 0:671c7986b866 | 310 | |
Cameron | 0:671c7986b866 | 311 | if( EP == 0x76){ //If Endpoint = 38. |
Cameron | 0:671c7986b866 | 312 | memset(Buffer, 0, sizeof(Buffer)); //Clear array |
Cameron | 0:671c7986b866 | 313 | Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number |
Cameron | 0:671c7986b866 | 314 | Buffer[1] = 0x00; //Status $00 = success Table 2.93 on page 159 of ZBSpec |
Cameron | 0:671c7986b866 | 315 | Buffer[2] = pay[1]; //Set Network address little endian order |
Cameron | 0:671c7986b866 | 316 | Buffer[3] = pay[2]; |
Cameron | 0:671c7986b866 | 317 | Buffer[4] = 0x10; //Length in bytes of the Simple Descriptor to Follow |
Cameron | 0:671c7986b866 | 318 | Buffer[5] = 0x76; //Endpoint of the simple descriptor Table 2.38 on page 88 of ZBSpec |
Cameron | 0:671c7986b866 | 319 | Buffer[6] = 0x04; //Application Profile ID 2 Bytes Little endian. $0104 = Home Automation Profile |
Cameron | 0:671c7986b866 | 320 | Buffer[7] = 0x01; |
Cameron | 0:671c7986b866 | 321 | Buffer[8] = 0x02; //Device type 2 Bytes Little endian, $0002 = On/Off Output see page 42 of ZigBee Home Automation Profile |
Cameron | 0:671c7986b866 | 322 | Buffer[9] = 0x00; |
Cameron | 0:671c7986b866 | 323 | Buffer[10] = 0x00; //App Dev Version 4bits + reserved 4bits |
Cameron | 0:671c7986b866 | 324 | Buffer[11] = 0x04; //Input cluster count in this case we only have $02 input clusters |
Cameron | 0:671c7986b866 | 325 | Buffer[12] = 0x00; //Input cluster list 2 bytes each little endian. $0000 = Basic Cluster |
Cameron | 0:671c7986b866 | 326 | Buffer[13] = 0x00; |
Cameron | 0:671c7986b866 | 327 | Buffer[14] = 0x06; //Output cluster 2 bytes each little endian. $0006 = On / Off Cluster |
Cameron | 0:671c7986b866 | 328 | Buffer[15] = 0x00; |
Cameron | 0:671c7986b866 | 329 | Buffer[16] = 0x08; |
Cameron | 0:671c7986b866 | 330 | Buffer[17] = 0x00; |
Cameron | 0:671c7986b866 | 331 | Buffer[18] = 0x01; |
Cameron | 0:671c7986b866 | 332 | Buffer[19] = 0x04; |
Cameron | 0:671c7986b866 | 333 | Buffer[20] = 0x00; //Output cluster list. No output cluster. |
Cameron | 0:671c7986b866 | 334 | PBuild(PacketAddr,0x00,0x00,DescPro,DescClu, Buffer, 0x15); |
Cameron | 0:671c7986b866 | 335 | } |
Cameron | 0:671c7986b866 | 336 | |
Cameron | 0:671c7986b866 | 337 | else{ |
Cameron | 0:671c7986b866 | 338 | memset(Buffer, 0, sizeof(Buffer)); //Clear array. |
Cameron | 0:671c7986b866 | 339 | Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number. |
Cameron | 0:671c7986b866 | 340 | Buffer[1] = 0x82; //Status $82 = Invalid_EP page 212 of ZigBee Specification. |
Cameron | 0:671c7986b866 | 341 | Buffer[2] = pay[1]; //Set Network address little endian order. |
Cameron | 0:671c7986b866 | 342 | Buffer[3] = pay[2]; |
Cameron | 0:671c7986b866 | 343 | Buffer[4] = 0x00; //Length in bytes of simple descriptor to follow. |
Cameron | 0:671c7986b866 | 344 | PBuild(PacketAddr,0x00,0x00,DescPro,DescClu, Buffer, 0x05); |
Cameron | 0:671c7986b866 | 345 | } |
Cameron | 0:671c7986b866 | 346 | } |
Cameron | 0:671c7986b866 | 347 | /******************************** Active EP Request ************************************8 |
Cameron | 0:671c7986b866 | 348 | Send a response to and Active EP Request. |
Cameron | 0:671c7986b866 | 349 | For more informaion see ZigBee spec page 161. |
Cameron | 0:671c7986b866 | 350 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 351 | void ActiveEPReq(){ |
Cameron | 0:671c7986b866 | 352 | |
Cameron | 0:671c7986b866 | 353 | char EPReqClu[2] = {0x80, 0x05}; //Create char array for Cluster. |
Cameron | 0:671c7986b866 | 354 | char EPReqPro[2] = {0x00, 0x00}; //Create char array for Profile. |
Cameron | 0:671c7986b866 | 355 | memset(Buffer, 0, sizeof(Buffer)); //Clear array. |
Cameron | 0:671c7986b866 | 356 | Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number. |
Cameron | 0:671c7986b866 | 357 | Buffer[1] = 0x00; //Status $00 = success Table 2.93 on page 159 of ZBSpec. |
Cameron | 0:671c7986b866 | 358 | Buffer[2] = pay[1]; //Set Network address little endian order. |
Cameron | 0:671c7986b866 | 359 | Buffer[3] = pay[2]; |
Cameron | 0:671c7986b866 | 360 | Buffer[4] = 0x01; //Active end point count in this case 1. |
Cameron | 0:671c7986b866 | 361 | Buffer[5] = 0x76; //Endpoint 38. |
Cameron | 0:671c7986b866 | 362 | PBuild(PacketAddr,0x00,0x00,EPReqPro,EPReqClu, Buffer, 0x06); |
Cameron | 0:671c7986b866 | 363 | } |
Cameron | 0:671c7986b866 | 364 | /********************************** Cluster Basic *************************************** |
Cameron | 0:671c7986b866 | 365 | Process a Basic Cluster request. |
Cameron | 0:671c7986b866 | 366 | For more informaion see ZigBee cluster library page 78 |
Cameron | 0:671c7986b866 | 367 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 368 | void ClusterBasic(){ |
Cameron | 0:671c7986b866 | 369 | |
Cameron | 0:671c7986b866 | 370 | cmdID = 0; //Set command ID to 0. |
Cameron | 0:671c7986b866 | 371 | seqNum = 0; //Set seqence number to 0. |
Cameron | 0:671c7986b866 | 372 | memset(Buffer, 0, sizeof(Buffer)); //Clear array. |
Cameron | 0:671c7986b866 | 373 | memset(atID, 0, sizeof(atID)); //Clear array. |
Cameron | 0:671c7986b866 | 374 | |
Cameron | 0:671c7986b866 | 375 | seqNum = pay[1]; //Copy seqence number from payload. |
Cameron | 0:671c7986b866 | 376 | cmdID = pay[2]; //Copy command ID from payload. |
Cameron | 0:671c7986b866 | 377 | atID[0] = pay[3]; //Copy attribute ID LSB from payload. |
Cameron | 0:671c7986b866 | 378 | atID[1] = pay[4]; //Copy attribute ID MSB from payload. |
Cameron | 0:671c7986b866 | 379 | |
Cameron | 0:671c7986b866 | 380 | if ((cmdID == 0x00) && (atID[0] == 0x01) && (atID[1] == 0x00)){ //If Application version is requested. |
Cameron | 0:671c7986b866 | 381 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 382 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 383 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 384 | Buffer[3] = 0x01; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 385 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 386 | Buffer[5] = 0x00; //status 00 success. |
Cameron | 0:671c7986b866 | 387 | Buffer[6] = 0x20; //Attribute data type 0x20 = unsigned 8 bit integer. |
Cameron | 0:671c7986b866 | 388 | Buffer[7] = AppV; //Application version. |
Cameron | 0:671c7986b866 | 389 | PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08); //Send data to packet builder. |
Cameron | 0:671c7986b866 | 390 | } |
Cameron | 0:671c7986b866 | 391 | |
Cameron | 0:671c7986b866 | 392 | if ((cmdID == 0x00) && (atID[0] == 0x03) && (atID[1] == 0x00)){ //If Hardware version is requeted. |
Cameron | 0:671c7986b866 | 393 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 394 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 395 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 396 | Buffer[3] = 0x03; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 397 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 398 | Buffer[5] = 0x00; //status 00 success. |
Cameron | 0:671c7986b866 | 399 | Buffer[6] = 0x20; //Attribute data type 0x20 = unsigned 8 bit integer. |
Cameron | 0:671c7986b866 | 400 | Buffer[7] = HarV; //Hardware version. |
Cameron | 0:671c7986b866 | 401 | PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08); //Send data to packet builder. |
Cameron | 0:671c7986b866 | 402 | } |
Cameron | 0:671c7986b866 | 403 | |
Cameron | 0:671c7986b866 | 404 | if ((cmdID == 0x00) && (atID[0] == 0x04) && (atID[1] == 0x00)){ //If device Manufacturer is requested |
Cameron | 0:671c7986b866 | 405 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 406 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 407 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 408 | Buffer[3] = 0x04; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 409 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 410 | Buffer[5] = 0x00; //status 00 success. |
Cameron | 0:671c7986b866 | 411 | Buffer[6] = 0x42; //Attribute data type 0x42 = character string. |
Cameron | 0:671c7986b866 | 412 | Buffer[7] = sizeof(Man); //Size of string to follow. |
Cameron | 0:671c7986b866 | 413 | x = 0; |
Cameron | 0:671c7986b866 | 414 | while( x < (sizeof(Man))){ //Send sting byte by byte. |
Cameron | 0:671c7986b866 | 415 | Buffer[(x+8)] = Man[x]; |
Cameron | 0:671c7986b866 | 416 | x++; |
Cameron | 0:671c7986b866 | 417 | } |
Cameron | 0:671c7986b866 | 418 | PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, (0x08 + Buffer[7])); //Send data to packet builder. |
Cameron | 0:671c7986b866 | 419 | } |
Cameron | 0:671c7986b866 | 420 | |
Cameron | 0:671c7986b866 | 421 | if ((cmdID == 0x00) && (atID[0] == 0x05) && (atID[1] == 0x00)){ //If Device devoloper is requested. |
Cameron | 0:671c7986b866 | 422 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 423 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 424 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 425 | Buffer[3] = 0x05; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 426 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 427 | Buffer[5] = 0x00; //status 00 success. |
Cameron | 0:671c7986b866 | 428 | Buffer[6] = 0x42; //Attribute data type 0x42 = character string. |
Cameron | 0:671c7986b866 | 429 | Buffer[7] = sizeof(Dev) ; //Size of string to follow. |
Cameron | 0:671c7986b866 | 430 | x = 0; |
Cameron | 0:671c7986b866 | 431 | while( x < (sizeof(Dev))){ //Send sting byte by byte. |
Cameron | 0:671c7986b866 | 432 | Buffer[(x+8)] = Dev[x]; |
Cameron | 0:671c7986b866 | 433 | x++; |
Cameron | 0:671c7986b866 | 434 | } |
Cameron | 0:671c7986b866 | 435 | PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, (0x08 + Buffer[7])); //Send data to packet builder. |
Cameron | 0:671c7986b866 | 436 | } |
Cameron | 0:671c7986b866 | 437 | } |
Cameron | 0:671c7986b866 | 438 | /******************************** Switch Cluster ************************************ |
Cameron | 0:671c7986b866 | 439 | Process and respond to a Switch cluster command. |
Cameron | 0:671c7986b866 | 440 | For more informaion see ZigBee Cluster library page 125. |
Cameron | 0:671c7986b866 | 441 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 442 | void OnOff(){ |
Cameron | 0:671c7986b866 | 443 | |
Cameron | 0:671c7986b866 | 444 | cmdID = 0; //Set command ID to 0. |
Cameron | 0:671c7986b866 | 445 | seqNum = 0; //Set seqence number to 0. |
Cameron | 0:671c7986b866 | 446 | frmType = 0; //Set frame type to 0. |
Cameron | 0:671c7986b866 | 447 | memset(Buffer, 0, sizeof(Buffer)); //Clear array |
Cameron | 0:671c7986b866 | 448 | memset(atID, 0, sizeof(atID)); //Clear array |
Cameron | 0:671c7986b866 | 449 | |
Cameron | 0:671c7986b866 | 450 | frmType = pay[0]; //Get frame type from payload. |
Cameron | 0:671c7986b866 | 451 | frmType = frmType & 0x03; //Bitwise & with 0x03 to make sure you are looking at the first 2 bits. |
Cameron | 0:671c7986b866 | 452 | seqNum = pay[1]; //Get Seqence Number for payload. |
Cameron | 0:671c7986b866 | 453 | cmdID = pay[2]; //Get command ID from payload. |
Cameron | 0:671c7986b866 | 454 | atID[0] = pay[3]; //Get attribute ID MSB. |
Cameron | 0:671c7986b866 | 455 | atID[1] = pay[4]; //Get attribute ID LSB. |
Cameron | 0:671c7986b866 | 456 | |
Cameron | 0:671c7986b866 | 457 | if ((frmType == 0x00) && (cmdID == 0x00) && (atID[1] == 0x00)){ //If Command to check status is sent. |
Cameron | 0:671c7986b866 | 458 | if(LED > 0.1) |
Cameron | 0:671c7986b866 | 459 | Update(1); |
Cameron | 0:671c7986b866 | 460 | else |
Cameron | 0:671c7986b866 | 461 | Update(0); |
Cameron | 0:671c7986b866 | 462 | } |
Cameron | 0:671c7986b866 | 463 | |
Cameron | 0:671c7986b866 | 464 | if ((frmType == 0x01) && (cmdID == 0x00) && (atID[1] == 0x00)){ //command to turn off LED. |
Cameron | 0:671c7986b866 | 465 | LED = 0; //Turn LED off. |
Cameron | 0:671c7986b866 | 466 | DefRes(); |
Cameron | 0:671c7986b866 | 467 | UpdateLevel(); |
Cameron | 0:671c7986b866 | 468 | } |
Cameron | 0:671c7986b866 | 469 | |
Cameron | 0:671c7986b866 | 470 | if ((frmType == 0x01) && (cmdID == 0x01) && (atID[1] == 0x00)){ //Command to turn on LED. |
Cameron | 0:671c7986b866 | 471 | LED = 1; //Turn LED on. |
Cameron | 0:671c7986b866 | 472 | DefRes(); //Create response. |
Cameron | 0:671c7986b866 | 473 | UpdateLevel(); |
Cameron | 0:671c7986b866 | 474 | } |
Cameron | 0:671c7986b866 | 475 | |
Cameron | 0:671c7986b866 | 476 | if ((frmType == 0x01) && (cmdID == 0x02) && (atID[1] == 0x00)){ //Command to toggle LED. |
Cameron | 0:671c7986b866 | 477 | LED = !LED; //Toggle LED |
Cameron | 0:671c7986b866 | 478 | DefRes(); |
Cameron | 0:671c7986b866 | 479 | UpdateLevel(); |
Cameron | 0:671c7986b866 | 480 | } |
Cameron | 0:671c7986b866 | 481 | } |
Cameron | 0:671c7986b866 | 482 | /********************************* Default Response ************************************* |
Cameron | 0:671c7986b866 | 483 | Create a default response. |
Cameron | 0:671c7986b866 | 484 | |
Cameron | 0:671c7986b866 | 485 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 486 | void DefRes(){ |
Cameron | 0:671c7986b866 | 487 | |
Cameron | 0:671c7986b866 | 488 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 489 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 490 | Buffer[2] = 0x0B; //Command identifier 0x08 = default response. |
Cameron | 0:671c7986b866 | 491 | Buffer[3] = cmdID; //reply with the command ID sne to you. |
Cameron | 0:671c7986b866 | 492 | Buffer[4] = 0x00; //Send sucess 00 |
Cameron | 0:671c7986b866 | 493 | PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x05); //Send to packet builder. |
Cameron | 0:671c7986b866 | 494 | } |
Cameron | 0:671c7986b866 | 495 | /************************************* Level ******************************************** |
Cameron | 0:671c7986b866 | 496 | Process a level cluster request. |
Cameron | 0:671c7986b866 | 497 | |
Cameron | 0:671c7986b866 | 498 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 499 | void Level(){ |
Cameron | 0:671c7986b866 | 500 | |
Cameron | 0:671c7986b866 | 501 | cmdID = 0; //Set command ID to 0. |
Cameron | 0:671c7986b866 | 502 | seqNum = 0; //Set seqence number to 0. |
Cameron | 0:671c7986b866 | 503 | frmType = 0; //Set frame type to 0. |
Cameron | 0:671c7986b866 | 504 | memset(Buffer, 0, sizeof(Buffer)); //Clear array |
Cameron | 0:671c7986b866 | 505 | memset(atID, 0, sizeof(atID)); //Clear array |
Cameron | 0:671c7986b866 | 506 | |
Cameron | 0:671c7986b866 | 507 | frmType = pay[0]; //Get frame type from payload. |
Cameron | 0:671c7986b866 | 508 | frmType = frmType & 0x03; //Bitwise & with 0x03 to make sure you are looking at the first 2 bits. |
Cameron | 0:671c7986b866 | 509 | seqNum = pay[1]; //Get Seqence Number for payload. |
Cameron | 0:671c7986b866 | 510 | cmdID = pay[2]; //Get command ID from payload. |
Cameron | 0:671c7986b866 | 511 | atID[0] = pay[3]; //Get attribute ID MSB. |
Cameron | 0:671c7986b866 | 512 | atID[1] = pay[4]; |
Cameron | 0:671c7986b866 | 513 | |
Cameron | 0:671c7986b866 | 514 | if ((frmType == 0x00) && (cmdID == 0x00) && (atID[1] == 0x00)){ //If Command to check status is sent. |
Cameron | 0:671c7986b866 | 515 | UpdateLevel(); //Call Level update. |
Cameron | 0:671c7986b866 | 516 | } |
Cameron | 0:671c7986b866 | 517 | |
Cameron | 0:671c7986b866 | 518 | if ((frmType == 0x01) && (cmdID == 0x04)){ //command to set LED level. |
Cameron | 0:671c7986b866 | 519 | LED = float(pay[3])/100; //Set LED brightness |
Cameron | 0:671c7986b866 | 520 | UpdateLevel(); |
Cameron | 0:671c7986b866 | 521 | if((LED * 100) > 10) |
Cameron | 0:671c7986b866 | 522 | Update(1); |
Cameron | 0:671c7986b866 | 523 | else |
Cameron | 0:671c7986b866 | 524 | Update(0); |
Cameron | 0:671c7986b866 | 525 | } |
Cameron | 0:671c7986b866 | 526 | } |
Cameron | 0:671c7986b866 | 527 | /************************************** Update ****************************************** |
Cameron | 0:671c7986b866 | 528 | Update LEDs status. |
Cameron | 0:671c7986b866 | 529 | |
Cameron | 0:671c7986b866 | 530 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 531 | void Update(char val){ |
Cameron | 0:671c7986b866 | 532 | |
Cameron | 0:671c7986b866 | 533 | char Cluster[2] = {0x00, 0x06}; |
Cameron | 0:671c7986b866 | 534 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 535 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 536 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 537 | Buffer[3] = 0x00; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 538 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 539 | Buffer[5] = 0x00; //Status 00 = success. |
Cameron | 0:671c7986b866 | 540 | Buffer[6] = 0x10; //Attribute data type 10 = boolean. |
Cameron | 0:671c7986b866 | 541 | Buffer[7] = val; //Read LED state. |
Cameron | 0:671c7986b866 | 542 | PBuild(PacketAddr,SEP,DEP,Pro,Cluster, Buffer, 0x08); //Send to packet builder. |
Cameron | 0:671c7986b866 | 543 | } |
Cameron | 0:671c7986b866 | 544 | /*********************************** Update Level *************************************** |
Cameron | 0:671c7986b866 | 545 | Update LEDs brightness level. |
Cameron | 0:671c7986b866 | 546 | |
Cameron | 0:671c7986b866 | 547 | *****************************************************************************************/ |
Cameron | 0:671c7986b866 | 548 | void UpdateLevel(){ |
Cameron | 0:671c7986b866 | 549 | |
Cameron | 0:671c7986b866 | 550 | memset(Buffer, 0, sizeof(Buffer)); //Clear array |
Cameron | 0:671c7986b866 | 551 | char Cluster[2] = {0x00, 0x08}; |
Cameron | 0:671c7986b866 | 552 | Buffer[0] = 0x18; //Frame control direction is server to client. |
Cameron | 0:671c7986b866 | 553 | Buffer[1] = seqNum; //Reply with seqence number from request. |
Cameron | 0:671c7986b866 | 554 | Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response. |
Cameron | 0:671c7986b866 | 555 | Buffer[3] = 0x00; //Attribute Identfier (2 bytes) field being reported. |
Cameron | 0:671c7986b866 | 556 | Buffer[4] = 0x00; |
Cameron | 0:671c7986b866 | 557 | Buffer[5] = 0x00; //Status 00 = success. |
Cameron | 0:671c7986b866 | 558 | Buffer[6] = 0x20; |
Cameron | 0:671c7986b866 | 559 | Buffer[7] = (LED * 100); |
Cameron | 0:671c7986b866 | 560 | PBuild(PacketAddr,SEP,DEP,Pro,Cluster, Buffer, 0x08); //Send to packet builder. |
Cameron | 0:671c7986b866 | 561 | } |