emit.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. int i;
  37. char optstring[] = "xvb:d:f:r:";
  38. int option;
  39. unsigned char global=NO_GLOBAL;
  40. unsigned char number = NUMBER1;
  41. unsigned char section = SECTION_A;
  42. char *idString=0;
  43. unsigned long int frequency = DEFAULT_FREQ;
  44. unsigned char onOff = ON;
  45. unsigned int verbose = 0;
  46. unsigned repeat = 1;
  47. /* reading options */
  48. //opterr=0; /* Pas de message d'erreur automatique */
  49. while ((option = getopt(argc, argv, optstring)) != -1) {
  50. switch (option) {
  51. case 'b':
  52. // Decode the button ie : A3
  53. section = optarg[0] - (optarg[0]<'Z' ? 'A' : 'a');
  54. if (strlen(optarg)>1) {
  55. number = optarg[1] - '0';
  56. }
  57. if (section== 6) {
  58. // G was requested
  59. global = GLOBAL;
  60. }
  61. break;
  62. case 'r':
  63. // repeatition code
  64. sscanf(optarg, "%d", &repeat);
  65. break;
  66. case 'd':
  67. // command id
  68. idString = (char*)malloc(1+strlen(optarg)/2);
  69. for(i=0; i<strlen(optarg)/2 + strlen(optarg) % 2; i++) {
  70. idString[i] = (optarg[i*2] - (optarg[i*2]<='9' ? '0' : (optarg[i*2]<='Z' ? 'A' : 'a')-10)) << 4;
  71. if (optarg[1+i*2]) {
  72. idString[i] += optarg[1+i*2] - (optarg[1+i*2]<='9' ? '0' : (optarg[1+i*2]<='Z' ? 'A' : 'a')-10);
  73. }
  74. }
  75. break;
  76. case 'f':
  77. // SPI frequency
  78. sscanf(optarg, "%lu", &frequency);
  79. break;
  80. case 'x':
  81. // OFF ?
  82. onOff = OFF;
  83. break;
  84. case 'v':
  85. // Show the bits to send ?
  86. verbose = 1;
  87. break;
  88. default:
  89. usage(argv);
  90. return 0;
  91. break;
  92. }
  93. }
  94. // No ID specified, it will be "00 00 00 00 00 00 0"
  95. if (!idString) {
  96. idString = (char*)malloc(7);
  97. memset(idString, 0, 7);
  98. }
  99. // Show informations
  100. printf("Frequency config on SPI: %dHz\n", frequency);
  101. if (global) {
  102. printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
  103. } else {
  104. printf("Sending command %c%d: %s\n", section+'A', number, (onOff == OFF) ? "OFF" : "ON");
  105. }
  106. // Preparing the buffer
  107. buffer = createBitBuffer();
  108. // Building the data
  109. for (i=0; i<repeat; i++) {
  110. pushCode(&buffer, idString, section, number, onOff, global, frequency);
  111. }
  112. printf("\n");
  113. // Want to see the bits ?
  114. if (verbose) {
  115. printfBitBuffer(buffer);
  116. }
  117. #ifdef __arm__
  118. // preparing the output
  119. if (wiringPiSPISetup(0, frequency) < 0) {
  120. printf("SPI Setup Failed: %s\n", strerror(errno));
  121. return -1;
  122. }
  123. // Send data
  124. wiringPiSPIDataRW(0, (unsigned char*)buffer.data, buffer.byteSize);
  125. #endif
  126. // release memory
  127. destroyBitBuffer(buffer);
  128. return 0;
  129. }