emit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdio.h>
  2. #include <wiringPiSPI.h>
  3. #include "emitlib.h"
  4. #include <malloc.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #define DEFAULT_FREQ 20000
  9. /**
  10. * Usage of this program
  11. */
  12. void usage(char** argv)
  13. {
  14. fprintf(stderr, "Chacon command V1.0\nC. Meichel\n2013, October\n");
  15. fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
  16. fprintf(stderr, "\t-b button\n\t\tbutton (A-D1-4|G). For instance A1, G, D3\n");
  17. fprintf(stderr, "\t-x:\n\t\tOff (default On)\n");
  18. fprintf(stderr, "\t-r number:\n\t\tnumber of repeatitions\n");
  19. fprintf(stderr, "\t-d id:\n\t\thexadecimal id from your command (13 letters <=> 52 bits)\n");
  20. fprintf(stderr, "\t-f frequency: configuration frequency for the SPI (default %dHz)\n", DEFAULT_FREQ);
  21. fprintf(stderr, "\t-v: verbose bits\n");
  22. }
  23. /**
  24. * Main program
  25. *
  26. * @param argc number of arguments passed to the program
  27. * @param argv array of arguments passed to the program
  28. *
  29. * @return status
  30. */
  31. int main(int argc, char** argv)
  32. {
  33. BUFFER buffer;
  34. int i;
  35. char optstring[] = "xvb:d:f:r:";
  36. int option;
  37. unsigned char global=NO_GLOBAL;
  38. unsigned char number = NUMBER1;
  39. unsigned char section = SECTION_A;
  40. char *idString=0;
  41. unsigned long int frequency = DEFAULT_FREQ;
  42. unsigned char onOff = ON;
  43. unsigned int verbose = 0;
  44. unsigned repeat = 1;
  45. /* reading options */
  46. //opterr=0; /* Pas de message d'erreur automatique */
  47. while ((option = getopt(argc, argv, optstring)) != -1) {
  48. switch (option) {
  49. case 'b':
  50. // Decode the button ie : A3
  51. section = optarg[0] - (optarg[0]<'Z' ? 'A' : 'a');
  52. if (strlen(optarg)>1) {
  53. number = optarg[1] - '0';
  54. }
  55. if (section== 6) {
  56. // G was requested
  57. global = GLOBAL;
  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: %dHz\n", frequency);
  99. if (global) {
  100. printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
  101. } else {
  102. printf("Sending command %c%d: %s\n", section+'A', number, (onOff == OFF) ? "OFF" : "ON");
  103. }
  104. // Preparing the buffer
  105. buffer = createBuffer();
  106. // Building the data
  107. for (i=0; i<repeat; i++) {
  108. pushCode(&buffer, idString, section, number, onOff, global, frequency);
  109. }
  110. printf("\n");
  111. // Want to see the bits ?
  112. if (verbose) {
  113. printfBinaryBuffer(buffer);
  114. }
  115. // preparing the output
  116. if (wiringPiSPISetup(0, frequency) < 0) {
  117. printf("SPI Setup Failed: %s\n", strerror(errno));
  118. return -1;
  119. }
  120. // Send data
  121. wiringPiSPIDataRW(0, (unsigned char*)buffer.data, buffer.byteSize);
  122. // release memory
  123. destroyBuffer(buffer);
  124. return 0;
  125. }