#include #include #include #include #include #include "home_easy.h" #include "buffer.h" #include "utils.h" unsigned int timings[5][2] = { {275, 275}, // bit 0 {275, 1300}, // bit 1 {275, 9900}, // end of data {275, 2675}, // start of frame {275, 2675}, // end of data }; unsigned char homeEasyPinOut = 0; unsigned char homeEasyPinIn = 2; /** * Encode bits with HomeEasy encoding (1 => 10, 0 => 01) * * @param buffer the buffuer to encode * * @return new buffer * */ BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer) { BYTE_BUFFER result = createByteBuffer(); unsigned int i; for(i=0; isize; i++) { pushWord(&result, encodeByte(buffer->data[i])); } return result; } /** * Decode bits with HomeEasy encoding (1 => 10, 0 => 01) * * @param buffer the buffuer to decode * * @return new buffer * */ BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer) { BYTE_BUFFER result = createByteBuffer(); unsigned short int word; unsigned char *byte = (unsigned char*)&word; unsigned int i; for(i=0; i<(buffer->size+1)/2; i++) { byte[1] = buffer->data[2*i]; byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0); pushByte(&result, decodeByte(word)); } return result; } /** * Decode a byte according to HomeEasy * * @param byte the byte to decode * * @return the decoded byte */ unsigned char decodeByte(unsigned short int word) { unsigned char decodedChar=0; int j; int shift = 7; for(j=0x8000;j>0; j>>=2) { decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift; shift -=1; } return decodedChar; } /** * Encode a byte according to HomeEasy * * @param byte the byte to encode * * @return the encoded byte */ unsigned short int encodeByte(unsigned char byte) { unsigned short int encodedChar=0; int j; int shift = 14; for(j=0x80;j>0; j>>=1) { encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift; shift -=2; } return encodedChar; } /** * Create a complete command according to Chacon protocole * * @param id command id (refer to your remote) * @param section button section ('A' | 'B' | 'C' | 'D' | 'G') * @param nb button number(1, 2, 3, 4) * @param on boolean for on/off */ BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on) { unsigned char last = id[3] & 0xc0; BYTE_BUFFER command; char formatedSection = (section<='Z' ? section : section + 'A' - 'a'); // adding global last |= (formatedSection == 'G' ? 0x20 : 0); // adding on/off last |= (on ? 0x10 : 0); // adding section last |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c; // adding num last |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;; //Preparing output buffer command = createByteBuffer(); pushBytes(&command, id, 3); pushByte(&command, last); return command; } /** * Send a complete command according to Chacon protocole * * @param id command id (refer to your remote) * @param section button section ('A' | 'B' | 'C' | 'D' | 'G') * @param nb button number(1, 2, 3, 4) * @param on boolean for on/off * @param repeat number of repeatition */ void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on, unsigned char repeat) { BYTE_BUFFER command; unsigned int i; command = createHomeEasyCommand(id, section, nb, on); sendHomeEasyBit(TRIGGER0); sendHomeEasyBit(TRIGGER1); for(i=0; i0; i>>=1) { sendHomeEasyBit((byte & i) == i); } } /** * Send the content of a buffer to the RF transmitter * * @param buffer the buffer to transmit */ void sendHomeEasyBytes(BYTE_BUFFER buffer) { unsigned int i; for(i=0; i