emit.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <stdio.h>
  2. #ifdef __arm__
  3. #include <wiringPiSPI.h>
  4. #endif
  5. #include "emitlib.h"
  6. #include <malloc.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #define DEFAULT_FREQ 20000
  11. /**
  12. * Usage of this program
  13. */
  14. void usage(char** argv)
  15. {
  16. fprintf(stderr, "Chacon command V1.0\nC. Meichel\n2013, October\n");
  17. fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
  18. fprintf(stderr, "\t-b button\n\t\tbutton (A-D1-4|G). For instance A1, G, D3\n");
  19. fprintf(stderr, "\t-x:\n\t\tOff (default On)\n");
  20. fprintf(stderr, "\t-r number:\n\t\tnumber of repeatitions\n");
  21. fprintf(stderr, "\t-d id:\n\t\thexadecimal id from your command (13 letters <=> 52 bits)\n");
  22. fprintf(stderr, "\t-f frequency: configuration frequency for the SPI (default %dHz)\n", DEFAULT_FREQ);
  23. fprintf(stderr, "\t-v: verbose bits\n");
  24. }
  25. /**
  26. * Main program
  27. *
  28. * @param argc number of arguments passed to the program
  29. * @param argv array of arguments passed to the program
  30. *
  31. * @return status
  32. */
  33. int main(int argc, char** argv)
  34. {
  35. //BIT_BUFFER buffer;
  36. BYTE_BUFFER command;
  37. BYTE_BUFFER encoded;
  38. int i;
  39. char optstring[] = "xvb:d:f:r:";
  40. int option;
  41. unsigned char number = 1;
  42. unsigned char section = 'A';
  43. char *idString=0;
  44. unsigned long int frequency = DEFAULT_FREQ;
  45. unsigned char onOff = ON;
  46. unsigned int verbose = 0;
  47. unsigned repeat = 1;
  48. /* reading options */
  49. //opterr=0; /* Pas de message d'erreur automatique */
  50. while ((option = getopt(argc, argv, optstring)) != -1) {
  51. switch (option) {
  52. case 'b':
  53. // Decode the button ie : A3
  54. section = optarg[0] - (optarg[0]<'Z' ? 0 : 'a' - 'A');
  55. if (strlen(optarg)>1) {
  56. number = optarg[1] - '0';
  57. }
  58. break;
  59. case 'r':
  60. // repeatition code
  61. sscanf(optarg, "%d", &repeat);
  62. break;
  63. case 'd':
  64. // command id
  65. idString = (char*)malloc(1+strlen(optarg)/2);
  66. for(i=0; i<strlen(optarg)/2 + strlen(optarg) % 2; i++) {
  67. idString[i] = (optarg[i*2] - (optarg[i*2]<='9' ? '0' : (optarg[i*2]<='Z' ? 'A' : 'a')-10)) << 4;
  68. if (optarg[1+i*2]) {
  69. idString[i] += optarg[1+i*2] - (optarg[1+i*2]<='9' ? '0' : (optarg[1+i*2]<='Z' ? 'A' : 'a')-10);
  70. }
  71. }
  72. break;
  73. case 'f':
  74. // SPI frequency
  75. sscanf(optarg, "%lu", &frequency);
  76. break;
  77. case 'x':
  78. // OFF ?
  79. onOff = OFF;
  80. break;
  81. case 'v':
  82. // Show the bits to send ?
  83. verbose = 1;
  84. break;
  85. default:
  86. usage(argv);
  87. return 0;
  88. break;
  89. }
  90. }
  91. // No ID specified, it will be "00 00 00 00 00 00 0"
  92. if (!idString) {
  93. idString = (char*)malloc(7);
  94. memset(idString, 0, 7);
  95. }
  96. // Show informations
  97. printf("Frequency config on SPI: %luHz\n", frequency);
  98. if (section == 'G') {
  99. printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
  100. } else {
  101. printf("Sending command %c%d: %s\n", section, number, (onOff == OFF) ? "OFF" : "ON");
  102. }
  103. command = createHomeEasyCommand(idString, section, number, onOff);
  104. printfByteBuffer(command);
  105. printf("Code to emit:\n");
  106. encoded = homeEasyEncode(&command);
  107. printfByteBuffer(encoded);
  108. destroyByteBuffer(command);
  109. destroyByteBuffer(encoded);
  110. // Preparing the buffer
  111. /*buffer = createBitBuffer();
  112. // Building the data
  113. for (i=0; i<repeat; i++) {
  114. pushCode(&buffer, idString, section, number, onOff, global, frequency);
  115. }
  116. printf("\n");
  117. // Want to see the bits ?
  118. if (verbose) {
  119. printfBitBuffer(buffer);
  120. }
  121. #ifdef __arm__
  122. // preparing the output
  123. if (wiringPiSPISetup(0, frequency) < 0) {
  124. printf("SPI Setup Failed: %s\n", strerror(errno));
  125. return -1;
  126. }
  127. // Send data
  128. wiringPiSPIDataRW(0, (unsigned char*)buffer.data, buffer.byteSize);
  129. #endif
  130. // release memory
  131. destroyBitBuffer(buffer);*/
  132. return 0;
  133. }