listen.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. #include <wiringPi.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #include <malloc.h>
  6. #include "listenlib.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. unsigned char* data;
  34. struct timeval* start;
  35. int option;
  36. unsigned int duration = 10;
  37. unsigned long int samples = 100000;
  38. unsigned char verbose = 0;
  39. unsigned char all = 0;
  40. FILE* output=0;
  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. wiringPiSetup () ;
  71. pinMode (LED, INPUT);
  72. /* Read the data */
  73. fprintf(stderr, "Reading data\n");
  74. data = (unsigned char*)malloc(samples);
  75. readData(data, samples, duration);
  76. /* Analyzing the data */
  77. fprintf(stderr, "Analyzing\n");
  78. analyse(data, samples, output ? output : stdout, all);
  79. if (verbose) {
  80. fprintf(output ? output : stdout, "\n\nRawData\n");
  81. previousBit=0;
  82. for(i=0; i<samples; i++) {
  83. if ((previousBit == 0) && (data[i] == 1))
  84. fprintf(output ? output : stdout, "\n");
  85. fprintf(output ? output : stdout, "%c", data[i]+'0');
  86. previousBit = data[i];
  87. }
  88. }
  89. free(data);
  90. return 0 ;
  91. }