listen.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <malloc.h>
  5. #include "home_easy.h"
  6. #include "buffer.h"
  7. /**
  8. * Usage of this program
  9. */
  10. void usage(char** argv)
  11. {
  12. fprintf(stderr, "Chacon analyzer V1.0\nC. Meichel\n2013, October\n");
  13. fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
  14. fprintf(stderr, "\t-o output:\n\t\toutput file\n");
  15. fprintf(stderr, "\t-n number:\n\t\tnumber of bit to analyze (default 100000)\n");
  16. fprintf(stderr, "\t-t number:\n\t\ttempo between samples in µs (default 10)\n");
  17. fprintf(stderr, "\t-v:\n\t\tverbose\n");
  18. fprintf(stderr, "\t-a:\n\t\tperform a complete analyse even if an error was encountered\n");
  19. }
  20. /**
  21. * Main program
  22. *
  23. * @param argc number of arguments passed to the program
  24. * @param argv array of arguments passed to the program
  25. *
  26. * @return status
  27. */
  28. int main (int argc, char** argv)
  29. {
  30. char optstring[] = "n:t:o:va";
  31. unsigned int i;
  32. unsigned char previousBit;
  33. struct timeval* start;
  34. int option;
  35. unsigned int duration = 80;
  36. unsigned long int samples = 50000;
  37. unsigned char verbose = 0;
  38. unsigned char all = 0;
  39. FILE* output=0;
  40. BYTE_BUFFER buffer;
  41. /* reading options */
  42. opterr=0; /* Pas de message d'erreur automatique */
  43. while ((option = getopt(argc, argv, optstring)) != -1) {
  44. switch (option) {
  45. case 't':
  46. sscanf(optarg, "%d", &duration);
  47. break;
  48. case 'n':
  49. sscanf(optarg, "%lu", &samples);
  50. break;
  51. case 'a':
  52. all = 1;
  53. break;
  54. case 'v':
  55. verbose = 1;
  56. break;
  57. case 'o':
  58. output = fopen(optarg, "w");
  59. if (output==0) {
  60. fprintf(stderr, "Could not open file %s\n", optarg);
  61. }
  62. break;
  63. default:
  64. usage(argv);
  65. return 0;
  66. break;
  67. }
  68. }
  69. /* Configure the GPIO */
  70. initIO();
  71. /* Read the data */
  72. fprintf(stderr, "Reading data\n");
  73. buffer = readData(samples, duration);
  74. /* Analyzing the data */
  75. fprintf(stderr, "Analyzing\n");
  76. analyse(buffer, output ? output : stdout, all);
  77. if (verbose) {
  78. fprintf(output ? output : stdout, "\n\nRawData\n");
  79. previousBit=0;
  80. for(i=0; i<buffer.size; i++) {
  81. if ((previousBit == 0) && (buffer.data[i] == 1))
  82. fprintf(output ? output : stdout, "\n");
  83. fprintf(output ? output : stdout, "%c", buffer.data[i]+'0');
  84. previousBit = buffer.data[i];
  85. }
  86. }
  87. destroyByteBuffer(buffer);
  88. return 0 ;
  89. }