home_easy.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, 2675}, // 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 a complete command according to Chacon protocole
  121. *
  122. * @param id command id (refer to your remote)
  123. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  124. * @param nb button number(1, 2, 3, 4)
  125. * @param on boolean for on/off
  126. * @param repeat number of repeatition
  127. */
  128. void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
  129. {
  130. BYTE_BUFFER command;
  131. unsigned int i;
  132. // build the command
  133. command = createHomeEasyCommand(id, section, nb, on);
  134. // switch to real time
  135. scheduler_realtime();
  136. // send header
  137. sendHomeEasyBit(START_OF_DATA);
  138. sendHomeEasyBit(START_OF_FRAME);
  139. // repeat the command
  140. for(i=0; i<repeat; i++) {
  141. sendHomeEasyBytes(command);
  142. sendHomeEasyBit(END_OF_FRAME);
  143. }
  144. // Exit real time mode
  145. scheduler_standard();
  146. }
  147. /**
  148. * Configure the GPIO output pin
  149. *
  150. * @param pinNumber wiringPi pin number
  151. */
  152. void setHomeEasyTransmittorPin(unsigned char pinNumber)
  153. {
  154. homeEasyPinOut = pinNumber;
  155. }
  156. /**
  157. * read the GPIO output pin
  158. *
  159. * @return wiringPi pin number
  160. */
  161. unsigned char getHomeEasyTransmittorPin()
  162. {
  163. return homeEasyPinOut;
  164. }
  165. /**
  166. * Configure the GPIO input pin
  167. *
  168. * @param pinNumber wiringPi pin number
  169. */
  170. void setHomeEasyReceptorPin(unsigned char pinNumber)
  171. {
  172. homeEasyPinIn = pinNumber;
  173. }
  174. /**
  175. * read the GPIO input pin
  176. *
  177. * @return wiringPi pin number
  178. */
  179. unsigned char getHomeEasyReceptorPin()
  180. {
  181. return homeEasyPinIn;
  182. }
  183. /**
  184. * Init input/output
  185. *
  186. * @return status
  187. */
  188. int initIO()
  189. {
  190. int status;
  191. status = wiringPiSetup();
  192. if (status != -1) {
  193. printf("Setting up GPIO\n");
  194. pinMode(homeEasyPinIn, INPUT);
  195. pinMode(homeEasyPinOut, OUTPUT);
  196. } else {
  197. printf("GPIO setup failed %d\n", status);
  198. }
  199. return status;
  200. }
  201. /**
  202. * Send a bit to the RF transmitter
  203. *
  204. * @param bit the bit to transmit (0 | 1 | TRIGGER0 | TRIGGER1 | END_OF_FRAME)
  205. */
  206. void sendHomeEasyBit(unsigned char bit)
  207. {
  208. digitalWrite(homeEasyPinOut, HIGH);
  209. delayMicroseconds(timings[bit][0]);
  210. digitalWrite(homeEasyPinOut, LOW);
  211. delayMicroseconds(timings[bit][1]);
  212. digitalWrite(homeEasyPinOut, HIGH);
  213. }
  214. /**
  215. * Send a byte to the RF transmitter
  216. *
  217. * @param byte the byte to transmit
  218. */
  219. void sendHomeEasyByte(unsigned char byte)
  220. {
  221. int i;
  222. for(i=0x80;i>0; i>>=1) {
  223. sendHomeEasyBit((byte & i) == i);
  224. }
  225. }
  226. /**
  227. * Send the content of a buffer to the RF transmitter
  228. *
  229. * @param buffer the buffer to transmit
  230. */
  231. void sendHomeEasyBytes(BYTE_BUFFER buffer)
  232. {
  233. unsigned int i;
  234. for(i=0; i<buffer.size; i++) {
  235. sendHomeEasyByte(buffer.data[i]);
  236. }
  237. }
  238. /**
  239. * Calculate the length of the frame
  240. *
  241. * @param data data to scan (each byte represent a bit)
  242. * @param high length of high level
  243. * @param low length of low level
  244. *
  245. * @return total length of the frame
  246. */
  247. unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low)
  248. {
  249. unsigned int i=0;
  250. if (high) *high = 0;
  251. if (data[i] == 1) {
  252. for(i=0; data[i]; i++) ;
  253. if (high) *high = i;
  254. }
  255. for(; !data[i]; i++) ;
  256. if (low) *low = i - *high;
  257. return i;
  258. }
  259. /**
  260. * Reading data from GPIO
  261. *
  262. * @param samples number of samples to read
  263. * @param duration waiting time between samples
  264. *
  265. * @return buffer
  266. */
  267. BYTE_BUFFER readData(unsigned long int samples, unsigned int duration)
  268. {
  269. struct timeval* start;
  270. BYTE_BUFFER result;
  271. unsigned long int i;
  272. result = createByteBuffer();
  273. result.size = samples;
  274. result.data = (char*) realloc(result.data, samples);
  275. start = showTime(0);
  276. for(i=0; i<samples; i++) {
  277. result.data[i] = digitalRead(homeEasyPinIn);
  278. delayMicroseconds(duration);
  279. }
  280. showTime(start);
  281. return result;
  282. }