home_easy.c 6.6 KB

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