emit.c 4.4 KB

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