emitlib.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include "emitlib.h"
  6. /*unsigned int timings[4][2] = {
  7. {250, 250}, // bit 0
  8. {250, 1200}, // bit 1
  9. {250, 2600}, // start of frame
  10. {250, 9900} // end of data
  11. };*/
  12. unsigned int timings[4][2] = {
  13. {310, 310}, // bit 0
  14. {310, 1340}, // bit 1
  15. {275, 2675}, // start of frame
  16. {275, 9900} // end of data
  17. };
  18. unsigned char numbering[4] = {0x05, 0x06, 0x09, 0x0A};
  19. /**
  20. * Create a new buffer
  21. *
  22. * @return the created buffer
  23. */
  24. BIT_BUFFER createBitBuffer()
  25. {
  26. BIT_BUFFER buffer;
  27. buffer.byteSize = 1;
  28. buffer.bitSize = 0;
  29. buffer.data = (char*) malloc(1);
  30. buffer.data[0] = 0;
  31. return buffer;
  32. }
  33. /**
  34. * Release the memory
  35. *
  36. * @param buffer the buffer to destroy
  37. */
  38. void destroyBitBuffer(BIT_BUFFER buffer)
  39. {
  40. free(buffer.data);
  41. }
  42. /**
  43. * Create a new byte buffer
  44. *
  45. * @return the created buffer
  46. */
  47. BYTE_BUFFER createByteBuffer()
  48. {
  49. BYTE_BUFFER buffer;
  50. buffer.size = 0;
  51. buffer.data = (char*) malloc(1);
  52. return buffer;
  53. }
  54. /**
  55. * Release the memory
  56. *
  57. * @param buffer the buffer to destroy
  58. */
  59. void destroyByteBuffer(BYTE_BUFFER buffer)
  60. {
  61. free(buffer.data);
  62. }
  63. /**
  64. * Print all the bits from a buffer
  65. *
  66. * @param buffer the buffer holding the data
  67. */
  68. /*void printfBitBuffer(BIT_BUFFER buffer)
  69. {
  70. printf("bytes: %lu\nbits: %d\n", buffer.byteSize, buffer.bitSize);
  71. unsigned char byte;
  72. unsigned char bit;
  73. unsigned int x;
  74. int i;
  75. for(x=0; x<(buffer.bitSize ? buffer.byteSize : buffer.byteSize-1) ; x++) {
  76. byte = buffer.data[x];
  77. for(i=0x80; i>0; i>>=1) {
  78. if ((bit==0) && (byte & i)) {
  79. fprintf(stdout, "\n");
  80. }
  81. bit = ((byte & i) == i);
  82. fprintf(stdout, "%d", bit);
  83. }
  84. }
  85. }*/
  86. void printfBitBuffer(BYTE_BUFFER buffer)
  87. {
  88. printf("bytes: %lu\nbits: %lu\n", buffer.size, buffer.size*8);
  89. unsigned int x;
  90. for(x=0; x<buffer.size ; x++) {
  91. printfBit(buffer.data[x]);
  92. printf("\n");
  93. }
  94. }
  95. /**
  96. * Print a byte in binary
  97. *
  98. * @param byte the byte to print
  99. */
  100. void printfBit(unsigned char byte)
  101. {
  102. int i;
  103. for(i=0x80; i>0; i>>=1) {
  104. fprintf(stdout, "%d", ((byte & i) == i));
  105. }
  106. }
  107. /**
  108. * Print all the bytes from a buffer
  109. *
  110. * @param buffer the buffer holding the data
  111. */
  112. void printfByteBuffer(BYTE_BUFFER buffer)
  113. {
  114. unsigned int i;
  115. for(i=0; i<buffer.size; i++) {
  116. fprintf(stdout, "%02X ", (unsigned char)buffer.data[i]);
  117. }
  118. fprintf(stdout, "\n");
  119. }
  120. /**
  121. * Push a bit in a buffer
  122. *
  123. * @param buffer the buffer holding the data
  124. * @param bit the bit to push
  125. */
  126. /*void bitPushBit(BIT_BUFFER* buffer, unsigned char bit)
  127. {
  128. buffer->data[buffer->byteSize-1] |= bit << (7-buffer->bitSize);
  129. buffer->bitSize++;
  130. if (buffer->bitSize == 8) {
  131. buffer->bitSize = 0;
  132. buffer->byteSize++;
  133. buffer->data = (char*)realloc(buffer->data, buffer->byteSize);
  134. buffer->data[buffer->byteSize-1] = 0;
  135. }
  136. }*/
  137. /**
  138. * Push a byte in a buffer
  139. *
  140. * @param buffer the buffer holding the data
  141. * @param byte the byte to push
  142. */
  143. void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
  144. {
  145. buffer->size++;
  146. buffer->data = (char*)realloc(buffer->data, buffer->size);
  147. buffer->data[buffer->size-1] = byte;
  148. }
  149. /**
  150. * Push a word in a buffer
  151. *
  152. * @param buffer the buffer holding the data
  153. * @param word the word to push
  154. */
  155. void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
  156. {
  157. unsigned char* data = (unsigned char*)&word;
  158. pushByte(buffer, data[1]);
  159. pushByte(buffer, data[0]);
  160. }
  161. /**
  162. * Push some bytes in a buffer
  163. *
  164. * @param buffer the buffer holding the data
  165. * @param bytes the bytes to push
  166. * @param len the number of bytes to push
  167. */
  168. void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
  169. {
  170. buffer->data = (char*)realloc(buffer->data, buffer->size+len);
  171. memcpy(&buffer->data[buffer->size], byte, len);
  172. buffer->size += len;
  173. }
  174. /**
  175. * Encode bits with HomeEasy encoding (1 => 10, 0 => 01)
  176. *
  177. * @param buffer the buffuer to encode
  178. *
  179. * @return new buffer
  180. * */
  181. BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
  182. {
  183. BYTE_BUFFER result = createByteBuffer();
  184. unsigned int i;
  185. for(i=0; i<buffer->size; i++) {
  186. pushWord(&result, encodeByte(buffer->data[i]));
  187. }
  188. return result;
  189. }
  190. /**
  191. * Decode bits with HomeEasy encoding (1 => 10, 0 => 01)
  192. *
  193. * @param buffer the buffuer to decode
  194. *
  195. * @return new buffer
  196. * */
  197. BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
  198. {
  199. BYTE_BUFFER result = createByteBuffer();
  200. unsigned short int word;
  201. unsigned char *byte = (unsigned char*)&word;
  202. unsigned int i;
  203. for(i=0; i<(buffer->size+1)/2; i++) {
  204. byte[1] = buffer->data[2*i];
  205. byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
  206. pushByte(&result, decodeByte(word));
  207. }
  208. return result;
  209. }
  210. /**
  211. * Decode a byte according to HomeEasy
  212. *
  213. * @param byte the byte to decode
  214. *
  215. * @return the decoded byte
  216. */
  217. unsigned char decodeByte(unsigned short int word)
  218. {
  219. unsigned char decodedChar=0;
  220. int j;
  221. int shift = 7;
  222. for(j=0x8000;j>0; j>>=2) {
  223. decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift;
  224. shift -=1;
  225. }
  226. return decodedChar;
  227. }
  228. /**
  229. * Encode a byte according to HomeEasy
  230. *
  231. * @param byte the byte to encode
  232. *
  233. * @return the encoded byte
  234. */
  235. unsigned short int encodeByte(unsigned char byte)
  236. {
  237. unsigned short int encodedChar=0;
  238. int j;
  239. int shift = 14;
  240. for(j=0x80;j>0; j>>=1) {
  241. encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
  242. shift -=2;
  243. }
  244. return encodedChar;
  245. }
  246. /**
  247. * Gives the bit at a specific position
  248. *
  249. * @param buffer the buffer to evaluate
  250. * @param n the bit position
  251. *
  252. * @return the bit value
  253. */
  254. unsigned int bitAt(BYTE_BUFFER buffer, unsigned long int n)
  255. {
  256. unsigned char byte = buffer.data[n / 8];
  257. return (byte & (0x80 >> (n % 8)) != 0);
  258. }
  259. /**
  260. * Append a complete command according to Chacon protocole
  261. *
  262. * @param id command id (refer to your remote)
  263. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  264. * @param nb button number(1, 2, 3, 4)
  265. * @param on boolean for on/off
  266. */
  267. BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on)
  268. {
  269. unsigned char last = id[3] & 0xc0;
  270. BYTE_BUFFER command;
  271. char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
  272. // adding global
  273. last |= (formatedSection == 'G' ? 0x20 : 0);
  274. // adding on/off
  275. last |= (on ? 0x10 : 0);
  276. // adding section
  277. last |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
  278. // adding num
  279. last |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
  280. //Preparing output buffer
  281. command = createByteBuffer();
  282. pushBytes(&command, id, 3);
  283. pushByte(&command, last);
  284. return command;
  285. }
  286. /**
  287. * Append a bit that will be emitted for a specific time
  288. *
  289. * @param buffer the buffer holding the data
  290. * @param bit the bit to push
  291. * @param usec time in µs
  292. * @param clock frequency
  293. */
  294. /*void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
  295. {
  296. unsigned int i;
  297. for(i=0; i<(usec * freq) / 1e6; i++) {
  298. bitPushBit(buffer, bit);
  299. }
  300. }*/
  301. /**
  302. * Append data according to Chacon protocole
  303. *
  304. * @param buffer the buffer holding the data
  305. * @param type data type (BIT0 | BIT1 | START_OF_FRAME | END_OF_DATA)
  306. * @param clock frequency
  307. */
  308. /*void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq)
  309. {
  310. appendBit(buffer, 1, timings[type][0], freq);
  311. appendBit(buffer, 0, timings[type][1], freq);
  312. }*/
  313. /**
  314. * Append a byte according to Chacon protocole
  315. *
  316. * @param buffer the buffer holding the data
  317. * @param byte the byte to append
  318. * @param clock frequency
  319. */
  320. /*void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq)
  321. {
  322. int i;
  323. for(i=0x80; i>0; i>>=1) {
  324. appendData(buffer, ((byte & i) == i), freq);
  325. }
  326. printf("%02X ", byte);
  327. }*/
  328. /**
  329. * Append a complete command according to Chacon protocole
  330. *
  331. * @param buffer the buffer holding the data
  332. * @param id command id (refer to your remote)
  333. * @param section button section (0:A, 1:B, 2:C, 3:D)
  334. * @param nb button number(1, 2, 3, 4)
  335. * @param on boolean for on/off
  336. * @param global if true G button is selected (nb will be ignore)
  337. * @param clock frequency
  338. */
  339. /*void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
  340. {
  341. unsigned char byte6 = (id[6] & 0xf0) + numbering[on + (global ? 2 : 0)];
  342. unsigned char byte7 = (numbering[section] << 4) + numbering[nb];
  343. unsigned int i;
  344. if (global) {
  345. byte7 = 0x55;
  346. }
  347. appendData(buffer, START_OF_DATA, freq);
  348. appendData(buffer, START_OF_FRAME, freq);
  349. printf("SOF ");
  350. for(i=0; i<6; i++) {
  351. appendByte(buffer, id[i], freq);
  352. }
  353. appendByte(buffer, byte6, freq);
  354. appendByte(buffer, byte7, freq);
  355. appendData(buffer, START_OF_FRAME, freq);
  356. printf("EOD ");
  357. }*/