emit.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /**
  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-v: verbose bits\n");
  23. }
  24. /**
  25. * Main program
  26. *
  27. * @param argc number of arguments passed to the program
  28. * @param argv array of arguments passed to the program
  29. *
  30. * @return status
  31. */
  32. int main(int argc, char** argv)
  33. {
  34. //BIT_BUFFER buffer;
  35. BYTE_BUFFER command;
  36. BYTE_BUFFER encoded;
  37. int i;
  38. char optstring[] = "xvb:d:r:";
  39. int option;
  40. unsigned char number = 1;
  41. unsigned char section = 'A';
  42. char *idString=0;
  43. unsigned long int id=0;
  44. unsigned char onOff = ON;
  45. unsigned int verbose = 0;
  46. unsigned repeat = 5;
  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' ? 0 : 'a' - 'A');
  54. if (strlen(optarg)>1) {
  55. number = optarg[1] - '0';
  56. }
  57. break;
  58. case 'r':
  59. // repeatition code
  60. sscanf(optarg, "%d", &repeat);
  61. break;
  62. case 'd':
  63. // command id
  64. sscanf(optarg, "%08X", &id);
  65. break;
  66. case 'x':
  67. // OFF ?
  68. onOff = OFF;
  69. break;
  70. case 'v':
  71. // Show the bits to send ?
  72. verbose = 1;
  73. break;
  74. default:
  75. usage(argv);
  76. return 0;
  77. break;
  78. }
  79. }
  80. // Show informations
  81. printf("device id: %08X\n", id);
  82. if (section == 'G') {
  83. printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
  84. } else {
  85. printf("Sending command %c%d: %s\n", section, number, (onOff == OFF) ? "OFF" : "ON");
  86. }
  87. // Display mor information
  88. if (verbose) {
  89. command = createHomeEasyCommand(id, section, number, onOff);
  90. printf("Frame to send: %08X\n", command);
  91. printf("Code to emit:\n");
  92. encoded = homeEasyEncode(&command);
  93. printfByteBuffer(encoded);
  94. }
  95. // Send the data
  96. initIO();
  97. sendHomeEasyCommand(id, section, number, onOff, repeat);
  98. // release the memory
  99. destroyByteBuffer(command);
  100. destroyByteBuffer(encoded);
  101. return 0;
  102. }