emitlib.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include "emitlib.h"
  5. unsigned int timings[4][2] = {
  6. {250, 250}, // bit 0
  7. {250, 1200}, // bit 1
  8. {250, 2600}, // start of frame
  9. {250, 9900} // end of data
  10. };
  11. unsigned char numbering[4] = {0x05, 0x06, 0x09, 0x0A};
  12. /**
  13. * Create a new buffer
  14. *
  15. * @return the created buffer
  16. */
  17. BIT_BUFFER createBitBuffer()
  18. {
  19. BIT_BUFFER buffer;
  20. buffer.byteSize = 1;
  21. buffer.bitSize = 0;
  22. buffer.data = (char*) malloc(1);
  23. buffer.data[0] = 0;
  24. return buffer;
  25. }
  26. /**
  27. * Release the memory
  28. *
  29. * @param buffer the buffer to destroy
  30. */
  31. void destroyBitBuffer(BIT_BUFFER buffer)
  32. {
  33. free(buffer.data);
  34. }
  35. /**
  36. * Create a new byte buffer
  37. *
  38. * @return the created buffer
  39. */
  40. BIT_BUFFER createByteBuffer()
  41. {
  42. BYTE_BUFFER buffer;
  43. buffer.size = 0;
  44. buffer.data = (char*) malloc(1);
  45. return buffer;
  46. }
  47. /**
  48. * Release the memory
  49. *
  50. * @param buffer the buffer to destroy
  51. */
  52. void destroyByteBuffer(BIT_BUFFER buffer)
  53. {
  54. free(buffer.data);
  55. }
  56. /**
  57. * Print all the bits from a buffer
  58. *
  59. * @param buffer the buffer holding the data
  60. */
  61. void printfBitBuffer(BIT_BUFFER buffer)
  62. {
  63. printf("bytes: %d\nbits: %d\n", buffer.byteSize, buffer.bitSize);
  64. unsigned char byte;
  65. unsigned char bit;
  66. unsigned int x;
  67. int i;
  68. for(x=0; x<buffer.byteSize; x++) {
  69. byte = buffer.data[x];
  70. for(i=0x80; i>0; i>>=1) {
  71. if ((bit==0) && (byte & i)) {
  72. fprintf(stdout, "\n");
  73. }
  74. bit = ((byte & i) == i);
  75. fprintf(stdout, "%d", bit);
  76. }
  77. }
  78. }
  79. /**
  80. * Print all the bytes from a buffer
  81. *
  82. * @param buffer the buffer holding the data
  83. */
  84. void printfByteBuffer(BYTE_BUFFER buffer)
  85. {
  86. unsigned int i;
  87. for(i=0; i<buffer.size; i++) {
  88. fprintf(stdout, "%02X ", buffer.data[i];
  89. }
  90. }
  91. /**
  92. * Push a bit in a buffer
  93. *
  94. * @param buffer the buffer holding the data
  95. * @param bit the bit to push
  96. */
  97. void pushBit(BIT_BUFFER* buffer, unsigned char bit)
  98. {
  99. buffer->data[buffer->byteSize-1] |= bit << (7-buffer->bitSize);
  100. buffer->bitSize++;
  101. if (buffer->bitSize == 8) {
  102. buffer->bitSize = 0;
  103. buffer->byteSize++;
  104. buffer->data = (char*)realloc(buffer->data, buffer->byteSize);
  105. buffer->data[buffer->byteSize-1] = 0;
  106. }
  107. }
  108. /**
  109. * Push a byte in a buffer
  110. *
  111. * @param buffer the buffer holding the data
  112. * @param byt the byte to push
  113. */
  114. void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
  115. {
  116. buffer->size++
  117. buffer->data = (char*)realloc(buffer->size);
  118. buffer->data[buffer->size-1] = byte;
  119. }
  120. /**
  121. * Push some bytes in a buffer
  122. *
  123. * @param buffer the buffer holding the data
  124. * @param bytes the bytes to push
  125. * @param len the number of bytes to push
  126. */
  127. void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
  128. {
  129. buffer->data = (char*)realloc(buffer->size+len);
  130. memcpy(&buffer->data[buffer->size], byte, len);
  131. buffer->size += len;
  132. }
  133. /**
  134. * Encode bits with HomeEasy encoding (1 => 10, 0 => 01)
  135. *
  136. * @param buffer the buffuer to encode
  137. *
  138. * @return new buffer
  139. * */
  140. BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
  141. {
  142. BYTE_BUFFER result = createByteBuffer();
  143. unsigned char byte;
  144. unsigned char encodedByte;
  145. unsigned int i;
  146. for(i=0; i<buffer->size; i++) {
  147. byte = buffer->data[i];
  148. pushByte(buffer, encodeByte(byte & 0x0f);
  149. pushByte(buffer, encodeByte((byte & 0xf0) >> 4);
  150. }
  151. return result;
  152. }
  153. /**
  154. * Encode a byte according to HomeEasy
  155. *
  156. * @param byte the byte to encode
  157. *
  158. * @return the encoded byte
  159. */
  160. unsigned char encodeByte(unsigned char byte)
  161. {
  162. unsigned char encodedChar=0;
  163. int j;
  164. int shift = 6;
  165. for(j=0x08;j>0; j>>=1) {
  166. encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
  167. shift -=2;
  168. }
  169. return encodedChar;
  170. }
  171. /**
  172. * Decode bits with HomeEasy encoding (1 => 10, 0 => 01)
  173. *
  174. * @param buffer the buffuer to decode
  175. *
  176. * @return new buffer
  177. * */
  178. BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
  179. {
  180. BYTE_BUFFER result = createByteBuffer();
  181. unsigned char byte;
  182. unsigned int i;
  183. /*for(i=0; i<buffer->size; i++) {
  184. byte = buffer->data[i];
  185. decodeByte(byte);
  186. }*/
  187. return result;
  188. }
  189. /**
  190. * Decode a byte according to HomeEasy
  191. *
  192. * @param byte the byte to decode
  193. *
  194. * @return the decoded byte
  195. */
  196. unsigned char decodeByte(unsigned char byte)
  197. {
  198. unsigned char decodedChar=0;
  199. int j;
  200. int shift = 6;
  201. for(j=0x80;j>0; j>>=2) {
  202. decodedChar |= (((byte & j) == j) ? 0x01 : 0x00) << shift;
  203. shift -=1;
  204. }
  205. return decodedChar;
  206. }
  207. /**
  208. * Append a bit that will be emitted for a specific time
  209. *
  210. * @param buffer the buffer holding the data
  211. * @param bit the bit to push
  212. * @param usec time in µs
  213. * @param clock frequency
  214. */
  215. void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
  216. {
  217. unsigned int i;
  218. for(i=0; i<(usec * freq) / 1e6; i++) {
  219. pushBit(buffer, bit);
  220. }
  221. }
  222. /**
  223. * Append data according to Chacon protocole
  224. *
  225. * @param buffer the buffer holding the data
  226. * @param type data type (BIT0 | BIT1 | START_OF_FRAME | END_OF_DATA)
  227. * @param clock frequency
  228. */
  229. void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq)
  230. {
  231. appendBit(buffer, 1, timings[type][0], freq);
  232. appendBit(buffer, 0, timings[type][1], freq);
  233. }
  234. /**
  235. * Append a byte according to Chacon protocole
  236. *
  237. * @param buffer the buffer holding the data
  238. * @param byte the byte to append
  239. * @param clock frequency
  240. */
  241. void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq)
  242. {
  243. int i;
  244. for(i=0x80; i>0; i>>=1) {
  245. appendData(buffer, ((byte & i) == i), freq);
  246. }
  247. printf("%02X ", byte);
  248. }
  249. /**
  250. * Append a complete command according to Chacon protocole
  251. *
  252. * @param buffer the buffer holding the data
  253. * @param id command id (refer to your remote)
  254. * @param section button section (0:A, 1:B, 2:C, 3:D)
  255. * @param nb button number(1, 2, 3, 4)
  256. * @param on boolean for on/off
  257. * @param global if true G button is selected (nb will be ignore)
  258. * @param clock frequency
  259. */
  260. void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
  261. {
  262. unsigned char byte6 = (id[6] & 0xf0) + numbering[on + (global ? 2 : 0)];
  263. unsigned char byte7 = (numbering[section] << 4) + numbering[nb];
  264. unsigned int i;
  265. if (global) {
  266. byte7 = 0x55;
  267. }
  268. appendData(buffer, START_OF_FRAME, freq);
  269. printf("SOF ");
  270. for(i=0; i<6; i++) {
  271. appendByte(buffer, id[i], freq);
  272. }
  273. appendByte(buffer, byte6, freq);
  274. appendByte(buffer, byte7, freq);
  275. appendData(buffer, END_OF_DATA, freq);
  276. printf("EOD ");
  277. }