Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

X10Interface.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           X10Interface.cpp  -  description
00003                              -------------------
00004     begin                : Tue Sep 4 2001
00005     copyright            : (C) 2001 by 
00006     email                : 
00007  ***************************************************************************/
00008 
00009 
00010 #include <stdio.h> /* Standard input/output definitions */
00011 #include <string.h> /* String function definitions */
00012 #include <unistd.h> /* UNIX standard function definitions */
00013 #include <errno.h>
00014 #include <time.h>
00015 #include <assert.h>
00016 
00017 #include "X10Interface.h"
00018 #include "BitOp.h"
00019 #include "SerialOp.h"
00020 
00021 
00022 
00023 namespace rwil {
00024 using namespace BitOp;
00025 using namespace SerialOp;
00026   namespace X10 {
00027 
00028     //Initialize the X10 controller for sending data
00029     void send_init()
00030     {
00031       int n=0;
00032       char buf[10];
00033       time_t temp;
00034 
00035       temp = time(0);
00036 
00037       if(fd==0)
00038         {
00039           open_port();
00040           configure_port();
00041         }
00042 
00043     writetwo:
00044       sleep(1);
00045 
00046       while(n<1)
00047         {       n=write(fd, "\002", 1);
00048         if(n < 1) printf("Write \\x02 failed"); }
00049       //        write(fd, "\x63\x46\x4c\x45\x41", 5);
00050 
00051       buf[0]=0x20;
00052       while(buf[0]!=0x06)
00053         {       
00054           if((time(0)-temp) > 5) return;
00055           n=read(fd, buf, 1);
00056           errno=0;
00057           if(buf[0]==0x15) goto writetwo;
00058           //            printf("%d&%d*%d=",fd, (int)buf[0], n);
00059         }
00060       while((buf[0]!=CARRIAGE) &&(buf[0]!=0xa))
00061         {
00062           n=read(fd, buf, 1);
00063           if(buf[0]!=CARRIAGE && n ==1 &&(buf[0]!=0xa)) printf("Expected CARRIAGE got 0x%x\n", (int) buf[0]);
00064           if((time(0)-temp) > 5) return;
00065         }
00066     }
00067 
00068     //Send a simple X10 command
00069     void send_X10(char realhouse, char realnumber, char command)
00070     {
00071       int n, x;
00072       char buf[11];
00073       time_t temp;
00074       temp = time(0);
00075       //        printf("before init --");
00076       send_init();
00077 
00078 
00079       //        printf(" and after\n");
00080 
00081       buf[0] = 0x63;
00082       buf[1] = realhouse;
00083       buf[2] = realnumber;
00084       buf[3] = command;
00085       buf[4] = 0x41;
00086       //        for(n=0;n<5;n++) printf("%X.", buf[n]);
00087       //        printf("here\n");
00088 
00089       n= write(fd, buf, 5);
00090       if(n!=5) printf("Write failed\n");
00091         
00092       buf[2] = 0;
00093       while(buf[2] != command)
00094         {
00095           n = 0;
00096           while(n<5)
00097             {
00098               x= read(fd, buf+n, 1);
00099               if((time(0)-temp) > 5) return;
00100               if(x ==1)
00101                 {
00102                   //                            printf("%x>", (unsigned)buf[n]);
00103                   n++;
00104                 }
00105             }
00106         }
00107     }
00108 
00109 
00110 
00111     /*inputLength is in bits */
00112     //Add the signature to the data and create the complemented form
00113     //of the data
00114     int createCompAndAddSig(char* input, char *output, int inputlength)
00115     {
00116       int inPos= 0;
00117       int outPos = 0;
00118       int bitLength = inputlength;
00119         
00120       SetBit(output, 0, 1);
00121       SetBit(output, 1, 1);
00122       SetBit(output, 2, 1);
00123       SetBit(output, 3, 0);
00124         
00125       outPos = 4;
00126         
00127       for(;inPos < bitLength; inPos++)
00128         {       
00129           SetBit(output, outPos, input[inPos/8] & (1 << (7-inPos%8)) ? 1 : 0);
00130           outPos++;
00131           SetBit(output, outPos, input[inPos/8] & (1 << (7-inPos%8)) ? 0 : 1);
00132           outPos++;
00133         }
00134       return outPos; /*Not sure if this is right*/
00135     }
00136 
00138     void send_Dim_X10(char realhouse, char realnumber, int percent)
00139     {
00140       char data[10], output[20], realout[20];
00141       int loc;
00142       int brightsend;
00143       time_t temp;
00144       //      printf("Send Dim %c %c %d\n", realhouse, realnumber, percent);
00145       temp = time(0);
00146 
00147       send_init();
00148         
00149       //House Code A
00150       SetBits(data, 0, 4, realhouse - 0x40);
00151       //Extended Code 1
00152       SetBits(data, 4, 5, 0x0F);
00153         
00154       //Unit Code
00155       SetBits(data, 9, 4, (realnumber - 0x40) >> 1);    
00156         
00157       //Full on is 0x3f ...  0x20 is about 50%
00158       if(percent < 0) brightsend = 0;
00159       else if(percent > 100) brightsend = 0x3f;
00160       else brightsend = percent * 62 /100;
00161         
00162         
00163       SetBits(data, 13, 8, brightsend);
00164       //PresetDim
00165       SetBits(data, 21, 8, 0x31);
00166         
00167       send_arbitrary(data, 29);
00168 
00169       loc = createCompAndAddSig(data, output, 29);
00170       SetBits(output, loc, 20, 0);
00171       //        PrintBits(output, 80);
00172 
00173       memcpy(realout, "\x80\x09", 2);
00174       memcpy(realout+2, output, 10);
00175       //        PrintBits(realout, 120);
00176 
00177       /*        write(fd, realout, 12);
00178         
00179                 while(1)
00180                 {
00181                 if((time(0)-temp) > 5) return;
00182                 loc = read(fd, data, 1);
00183                 if((loc==1) && ((data[0] == CARRIAGE)|| (data[0] == 0xa)))
00184                 break;
00185                 //      printf("=%x\n", 0xFF &(int)data[0]);
00186                 }*/
00187     }
00188 
00189     //Strips the signature from the incoming data
00190     int stripSigAndCompress(char * input, char* output, int inputlength)
00191     {
00192       int inPos = 12;
00193       int outPos = 0;
00194         
00195       for(;inPos < inputlength; inPos+=2)
00196         {
00197           SetBit(output, outPos, input[inPos/8] & (1 << (7-inPos%8)) ? 1 : 0);
00198           outPos++;
00199         }
00200       return outPos;
00201     }
00202 
00203     void send_arbitrary(char data[10], int sizebits)
00204     {
00205       char output[25];
00206       int loc;
00207       time_t temp;
00208 
00209       temp = time(0);
00210         
00211       send_init();
00212         
00213       loc = createCompAndAddSig(data, output + 2, sizebits);
00214       SetBits(output + 2, loc, 16, 0);
00215       output[0] = 0x80;
00216       output[1] = loc / 8 + 1;
00217       //    PrintBits(output, 120);
00218 
00219       write(fd, output, loc / 8 + 4);
00220         
00221       while(1)
00222         {
00223           if((time(0)-temp) > 5) return;
00224           loc = read(fd, data, 1);
00225           if((loc==1) && ((data[0] == CARRIAGE)|| (data[0] == 0xa)))
00226             break;
00227           //    printf("=%x\n", 0xFF &(int)data[0]);
00228         }
00229     }
00230 
00231     //Read data from the X10 interface until either a Carriage return is found or the buffer is full
00232     //The function will demangle and strip the incoming data
00233     //bufsize is measured in bytes
00234     //the max buffer size supported is 100 bytes
00235     //Return value is the number of bits gathered
00237     int readFromX10(char *buffer, int bufSize)
00238     {
00239       char data[10], output[250];
00240       int n;
00241       int loc;
00242       n=0;
00243       data[0] = 0;
00244       while(1)
00245         {
00246           loc = read(fd, data, 1);
00247           if(loc==1)
00248             {
00249               /*                        printf("=%x\n", 0xFF &(int)data[0]);*/
00250               if(data[0] == CARRIAGE)
00251                 break;
00252               output[n] = data[0];
00253               n++;
00254               if(n>=bufSize*2) break;
00255             }
00256         }
00257         
00258       return stripSigAndCompress(output, buffer, 8 * n);
00259     }
00260 
00261 
00262     //Get the status of the X10 device ... this has been proofed against
00263     //lockup with a 10 second timeout, if the timeout is reached a
00264     //-1 is returned otherwise a percentage representing status is returned
00265     int get_status_X10(char realhouse, char realnumber)
00266     {
00267       char data[10], output[20], realout[20];
00268       int loc;
00269       int n;
00270       time_t temp;
00271 
00272       temp = time(0);
00273 
00274       send_init();
00275         
00276       //House Code A
00277       SetBits(data, 0, 4, realhouse - 0x40);
00278       //Extended Code 1
00279       SetBits(data, 4, 5, 0x0F);
00280         
00281       //Unit Code
00282       SetBits(data, 9, 4, (realnumber - 0x40) >> 1);    
00283         
00284       //Status Check
00285       SetBits(data, 13, 8, 0x00);
00286       //
00287       SetBits(data, 21, 8, 0x37);
00288         
00289         
00290       loc = createCompAndAddSig(data, output, 29);
00291       SetBits(output, loc, 20, 0);
00292       //        PrintBits(output, 80);
00293 
00294       memcpy(realout, "\x80\x09", 2);
00295       memcpy(realout+2, output, 10);
00296 
00297       write(fd, realout, 12);
00298         
00299       while(1)
00300         {
00301           if((time(0)-temp) > 10) return -1;
00302           loc = read(fd, data, 1);
00303           /*if(loc==1)  printf("=%x\n", 0xFF &(int)data[0]);*/
00304           if((loc==1) && ((data[0] == CARRIAGE)|| (data[0] == 0xa)))
00305             break;
00306         }
00307         
00308       n=0;
00309       data[0] = 0;
00310       while(1)
00311         {
00312           if((time(0)-temp) > 10) return -1;
00313           loc = read(fd, data, 1);
00314           if(loc==1)
00315             {
00316               /*                        printf("=%x\n", 0xFF &(int)data[0]);*/
00317               if((data[0] == CARRIAGE)|| (data[0] == 0xa))
00318                 break;
00319               output[n] = data[0];
00320               n++;
00321             }
00322         }
00323       stripSigAndCompress(output, realout, 56);
00324       /*PrintBits(realout, 25);*/
00325       return GetBits(realout, 15,6)*100/62;
00326     }
00327 
00328   }//End namespace X10
00329 }

Generated on Tue Apr 9 13:53:08 2002 for RWIL - Real World Interface Library by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001