emitlib.c 3.8 KB

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