emitlib.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 int timings[4][2] = {
  12. {310, 310}, // bit 0
  13. {310, 1340}, // bit 1
  14. {275, 2675}, // start of frame
  15. {275, 9900} // end of data
  16. };
  17. unsigned char numbering[4] = {0x05, 0x06, 0x09, 0x0A};
  18. /**
  19. * Create a new buffer
  20. *
  21. * @return the created buffer
  22. */
  23. BUFFER createBuffer()
  24. {
  25. BUFFER buffer;
  26. buffer.byteSize = 1;
  27. buffer.bitSize = 0;
  28. buffer.data = (char*) malloc(1);
  29. buffer.data[0] = 0;
  30. return buffer;
  31. }
  32. /**
  33. * Release the memory
  34. *
  35. * @param buffer the buffer to destroy
  36. */
  37. void destroyBuffer(BUFFER buffer)
  38. {
  39. free(buffer.data);
  40. }
  41. /**
  42. * Print all the bits from a buffer
  43. *
  44. * @param buffer the buffer holding the data
  45. */
  46. void printfBinaryBuffer(BUFFER buffer)
  47. {
  48. printf("bytes: %d\nbits: %d\n", buffer.byteSize, buffer.bitSize);
  49. unsigned char byte;
  50. unsigned char bit;
  51. unsigned int x;
  52. int i;
  53. for(x=0; x<buffer.byteSize; x++) {
  54. byte = buffer.data[x];
  55. for(i=0x80; i>0; i>>=1) {
  56. if ((bit==0) && (byte & i)) {
  57. fprintf(stdout, "\n");
  58. }
  59. bit = ((byte & i) == i);
  60. fprintf(stdout, "%d", bit);
  61. }
  62. }
  63. }
  64. /**
  65. * Push a bit in a buffer
  66. *
  67. * @param buffer the buffer holding the data
  68. * @param bit the bit to push
  69. */
  70. void pushBit(BUFFER* buffer, unsigned char bit)
  71. {
  72. buffer->data[buffer->byteSize-1] |= bit << (7-buffer->bitSize);
  73. buffer->bitSize++;
  74. if (buffer->bitSize == 8) {
  75. buffer->bitSize = 0;
  76. buffer->byteSize++;
  77. buffer->data = (char*)realloc(buffer->data, buffer->byteSize);
  78. buffer->data[buffer->byteSize-1] = 0;
  79. }
  80. }
  81. /**
  82. * Append a bit that will be emitted for a specific time
  83. *
  84. * @param buffer the buffer holding the data
  85. * @param bit the bit to push
  86. * @param usec time in µs
  87. * @param clock frequency
  88. */
  89. void appendBit(BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
  90. {
  91. unsigned int i;
  92. for(i=0; i<(usec * freq) / 1e6; i++) {
  93. pushBit(buffer, bit);
  94. }
  95. }
  96. /**
  97. * Append data according to Chacon protocole
  98. *
  99. * @param buffer the buffer holding the data
  100. * @param type data type (BIT0 | BIT1 | START_OF_FRAME | END_OF_DATA)
  101. * @param clock frequency
  102. */
  103. void appendData(BUFFER* buffer, unsigned int type, unsigned int freq)
  104. {
  105. appendBit(buffer, 1, timings[type][0], freq);
  106. appendBit(buffer, 0, timings[type][1], freq);
  107. }
  108. /**
  109. * Append a byte according to Chacon protocole
  110. *
  111. * @param buffer the buffer holding the data
  112. * @param byte the byte to append
  113. * @param clock frequency
  114. */
  115. void appendByte(BUFFER* buffer, unsigned char byte, unsigned int freq)
  116. {
  117. int i;
  118. for(i=0x80; i>0; i>>=1) {
  119. appendData(buffer, ((byte & i) == i), freq);
  120. }
  121. printf("%02X ", byte);
  122. }
  123. /**
  124. * Append a complete command according to Chacon protocole
  125. *
  126. * @param buffer the buffer holding the data
  127. * @param id command id (refer to your remote)
  128. * @param section button section (0:A, 1:B, 2:C, 3:D)
  129. * @param nb button number(1, 2, 3, 4)
  130. * @param on boolean for on/off
  131. * @param global if true G button is selected (nb will be ignore)
  132. * @param clock frequency
  133. */
  134. void pushCode(BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
  135. {
  136. unsigned char byte6 = (id[6] & 0xf0) + numbering[on + (global ? 2 : 0)];
  137. unsigned char byte7 = (numbering[section] << 4) + numbering[nb];
  138. unsigned int i;
  139. if (global) {
  140. byte7 = 0x55;
  141. }
  142. appendData(buffer, START_OF_DATA, freq);
  143. appendData(buffer, START_OF_FRAME, freq);
  144. printf("SOF ");
  145. for(i=0; i<6; i++) {
  146. appendByte(buffer, id[i], freq);
  147. }
  148. appendByte(buffer, byte6, freq);
  149. appendByte(buffer, byte7, freq);
  150. appendData(buffer, START_OF_FRAME, freq);
  151. printf("EOD ");
  152. }