home_easy.c 8.4 KB

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