home_easy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <wiringPi.h>
  6. #include "home_easy.h"
  7. #include "buffer.h"
  8. #include "utils.h"
  9. unsigned int timings[5][2] = {
  10. //{310, 310}, // bit 0
  11. //{310, 1340}, // bit 1
  12. {275, 275}, // bit 0
  13. {275, 1300}, // bit 1
  14. {275, 9900}, // start of data
  15. {275, 2675}, // start of frame
  16. {275, 10000}, // end of data
  17. };
  18. unsigned char homeEasyPinOut = 0;
  19. unsigned char homeEasyPinIn = 2;
  20. /**
  21. * Encode bits with HomeEasy encoding (1 => 10, 0 => 01)
  22. *
  23. * @param buffer the buffuer to encode
  24. *
  25. * @return new buffer
  26. * */
  27. BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
  28. {
  29. BYTE_BUFFER result = createByteBuffer();
  30. unsigned int i;
  31. for(i=0; i<buffer->size; i++) {
  32. pushWord(&result, encodeByte(buffer->data[i]));
  33. }
  34. return result;
  35. }
  36. /**
  37. * Decode bits with HomeEasy encoding (1 => 10, 0 => 01)
  38. *
  39. * @param buffer the buffuer to decode
  40. *
  41. * @return new buffer
  42. * */
  43. BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
  44. {
  45. BYTE_BUFFER result = createByteBuffer();
  46. unsigned short int word;
  47. unsigned char *byte = (unsigned char*)&word;
  48. unsigned int i;
  49. for(i=0; i<(buffer->size+1)/2; i++) {
  50. byte[1] = buffer->data[2*i];
  51. byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
  52. pushByte(&result, decodeByte(word));
  53. }
  54. return result;
  55. }
  56. /**
  57. * Decode a byte according to HomeEasy
  58. *
  59. * @param byte the byte to decode
  60. *
  61. * @return the decoded byte
  62. */
  63. unsigned char decodeByte(unsigned short int word)
  64. {
  65. unsigned char decodedChar=0;
  66. int j;
  67. int shift = 7;
  68. for(j=0x8000;j>0; j>>=2) {
  69. decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift;
  70. shift -=1;
  71. }
  72. return decodedChar;
  73. }
  74. /**
  75. * Encode a byte according to HomeEasy
  76. *
  77. * @param byte the byte to encode
  78. *
  79. * @return the encoded byte
  80. */
  81. unsigned short int encodeByte(unsigned char byte)
  82. {
  83. unsigned short int encodedChar=0;
  84. int j;
  85. int shift = 14;
  86. for(j=0x80;j>0; j>>=1) {
  87. encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
  88. shift -=2;
  89. }
  90. return encodedChar;
  91. }
  92. /**
  93. * Create a complete command according to Chacon protocole
  94. *
  95. * @param id command id (refer to your remote)
  96. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  97. * @param nb button number(1, 2, 3, 4)
  98. * @param on boolean for on/off
  99. */
  100. BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on)
  101. {
  102. unsigned char last = id[3] & 0xc0;
  103. BYTE_BUFFER command;
  104. char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
  105. // adding global
  106. last |= (formatedSection == 'G' ? 0x20 : 0);
  107. // adding on/off
  108. last |= (on ? 0x10 : 0);
  109. // adding section
  110. last |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
  111. // adding num
  112. last |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
  113. //Preparing output buffer
  114. command = createByteBuffer();
  115. pushBytes(&command, id, 3);
  116. pushByte(&command, last);
  117. return command;
  118. }
  119. /**
  120. * Send n times a data frame
  121. *
  122. * @param frame the data to send
  123. * @param repeat number of repeatition
  124. */
  125. void sendFrame(BYTE_BUFFER frame, unsigned int repeat)
  126. {
  127. unsigned int i;
  128. // switch to real time
  129. scheduler_realtime();
  130. // send header
  131. sendHomeEasyBit(START_OF_DATA);
  132. sendHomeEasyBit(START_OF_FRAME);
  133. // repeat the command
  134. for(i=0; i<repeat; i++) {
  135. sendHomeEasyBytes(frame);
  136. sendHomeEasyBit(END_OF_FRAME);
  137. }
  138. // Exit real time mode
  139. scheduler_standard();
  140. }
  141. /**
  142. * Send a complete command according to Chacon protocole
  143. *
  144. * @param id command id (refer to your remote)
  145. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  146. * @param nb button number(1, 2, 3, 4)
  147. * @param on boolean for on/off
  148. * @param repeat number of repeatition
  149. */
  150. void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
  151. {
  152. BYTE_BUFFER command;
  153. BYTE_BUFFER encoded;
  154. // build the command
  155. command = createHomeEasyCommand(id, section, nb, on);
  156. // encode the command
  157. encoded = homeEasyEncode(&command);
  158. // send data
  159. sendFrame(encoded, repeat);
  160. // release the memory
  161. destroyByteBuffer(command);
  162. destroyByteBuffer(encoded);
  163. }
  164. /**
  165. * Configure the GPIO output pin
  166. *
  167. * @param pinNumber wiringPi pin number
  168. */
  169. void setHomeEasyTransmittorPin(unsigned char pinNumber)
  170. {
  171. homeEasyPinOut = pinNumber;
  172. }
  173. /**
  174. * read the GPIO output pin
  175. *
  176. * @return wiringPi pin number
  177. */
  178. unsigned char getHomeEasyTransmittorPin()
  179. {
  180. return homeEasyPinOut;
  181. }
  182. /**
  183. * Configure the GPIO input pin
  184. *
  185. * @param pinNumber wiringPi pin number
  186. */
  187. void setHomeEasyReceptorPin(unsigned char pinNumber)
  188. {
  189. homeEasyPinIn = pinNumber;
  190. }
  191. /**
  192. * read the GPIO input pin
  193. *
  194. * @return wiringPi pin number
  195. */
  196. unsigned char getHomeEasyReceptorPin()
  197. {
  198. return homeEasyPinIn;
  199. }
  200. /**
  201. * Init input/output
  202. *
  203. * @return status
  204. */
  205. int initIO()
  206. {
  207. int status;
  208. status = wiringPiSetup();
  209. if (status != -1) {
  210. pinMode(homeEasyPinIn, INPUT);
  211. pinMode(homeEasyPinOut, OUTPUT);
  212. } else {
  213. printf("GPIO setup failed %d\n", status);
  214. }
  215. return status;
  216. }
  217. /**
  218. * Send a bit to the RF transmitter
  219. *
  220. * @param bit the bit to transmit (0 | 1 | TRIGGER0 | TRIGGER1 | END_OF_FRAME)
  221. */
  222. void sendHomeEasyBit(unsigned char bit)
  223. {
  224. digitalWrite(homeEasyPinOut, HIGH);
  225. delayMicroseconds(timings[bit][0]);
  226. digitalWrite(homeEasyPinOut, LOW);
  227. delayMicroseconds(timings[bit][1]);
  228. digitalWrite(homeEasyPinOut, HIGH);
  229. }
  230. /**
  231. * Send a byte to the RF transmitter
  232. *
  233. * @param byte the byte to transmit
  234. */
  235. void sendHomeEasyByte(unsigned char byte)
  236. {
  237. int i;
  238. for(i=0x80;i>0; i>>=1) {
  239. sendHomeEasyBit((byte & i) == i);
  240. }
  241. }
  242. /**
  243. * Send the content of a buffer to the RF transmitter
  244. *
  245. * @param buffer the buffer to transmit
  246. */
  247. void sendHomeEasyBytes(BYTE_BUFFER buffer)
  248. {
  249. unsigned int i;
  250. for(i=0; i<buffer.size; i++) {
  251. sendHomeEasyByte(buffer.data[i]);
  252. }
  253. }
  254. /**
  255. * Calculate the length of the frame
  256. *
  257. * @param data data to scan (each byte represent a bit)
  258. * @param high length of high level
  259. * @param low length of low level
  260. *
  261. * @return total length of the frame
  262. */
  263. unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low)
  264. {
  265. unsigned int i=0;
  266. if (high) *high = 0;
  267. if (data[i] == 1) {
  268. for(i=0; data[i]; i++) ;
  269. if (high) *high = i;
  270. }
  271. for(; !data[i]; i++) ;
  272. if (low) *low = i - *high;
  273. return i;
  274. }
  275. /**
  276. * Reading data from GPIO
  277. *
  278. * @param samples number of samples to read
  279. * @param duration waiting time between samples
  280. *
  281. * @return buffer
  282. */
  283. BYTE_BUFFER readData(unsigned long int samples, unsigned int duration)
  284. {
  285. struct timeval* start;
  286. BYTE_BUFFER result;
  287. unsigned long int i;
  288. result = createByteBuffer();
  289. result.size = samples;
  290. result.data = (char*) realloc(result.data, samples);
  291. start = showTime(0);
  292. for(i=0; i<samples; i++) {
  293. result.data[i] = digitalRead(homeEasyPinIn);
  294. delayMicroseconds(duration);
  295. }
  296. showTime(start);
  297. return result;
  298. }