listenlib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * Calculate a duration
  9. *
  10. * @param start start of the computing or null if
  11. * this is the first time this function is inoked
  12. *
  13. * @return current time
  14. */
  15. struct timeval* showTime(struct timeval* start)
  16. {
  17. struct timeval* stop;
  18. long tv_sec;
  19. long tv_usec;
  20. stop = (struct timeval*)malloc(sizeof(struct timeval));
  21. gettimeofday(stop, 0);
  22. if (!start) return stop;
  23. tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
  24. tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
  25. fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
  26. return stop;
  27. }
  28. /**
  29. * Reading data from GPIO
  30. *
  31. * @param data buffer data
  32. * @param samples number of samples to read
  33. * @param duration waiting time between samples
  34. */
  35. void readData(unsigned char* data, unsigned long int samples, unsigned int duration)
  36. {
  37. struct timeval* start;
  38. unsigned long int i;
  39. start = showTime(0);
  40. for(i=0; i<samples; i++) {
  41. data[i] = digitalRead(LED);
  42. usleep(duration);
  43. }
  44. data[samples] = 123;
  45. showTime(start);
  46. }
  47. /**
  48. * Calculate the length of the frame
  49. *
  50. * @param data data to scan
  51. * @param high length of high level
  52. * @param low length of low level
  53. *
  54. * @return total length of the frame
  55. */
  56. unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low)
  57. {
  58. unsigned int i=0;
  59. if (high) *high = 0;
  60. if (data[i] == 1) {
  61. for(i=0; data[i]; i++) ;
  62. if (high) *high = i;
  63. }
  64. for(; !data[i]; i++) ;
  65. if (low) *low = i - *high;
  66. return i;
  67. }
  68. /**
  69. * Analyse the data
  70. *
  71. * @param data buffer data
  72. * @param samples number of samples to read
  73. * @param output output to render the result
  74. * @param all if false the analyze stops at the first error encoutered
  75. */
  76. void analyse(unsigned char* data, unsigned long int samples, FILE* output, unsigned char all)
  77. {
  78. unsigned long int cursor=0;
  79. int detectMarker = 1;
  80. int currentMarker = 0;
  81. unsigned int lowLengthFrame;
  82. unsigned int highLengthFrame;
  83. unsigned int totalLengthFrame;
  84. unsigned char currentByte = 0;
  85. unsigned char bitNumber = 0;
  86. unsigned char startData = 0;
  87. while (cursor<samples) {
  88. totalLengthFrame = frameSize(&data[cursor], &highLengthFrame, &lowLengthFrame);
  89. if (currentMarker>0) {
  90. /* Try to detect a start frame marker */
  91. if ((lowLengthFrame > 20) && (lowLengthFrame<50)) {
  92. detectMarker++;
  93. bitNumber = 0;
  94. currentByte = 0;
  95. fprintf(output, "\n%02d: ", detectMarker);
  96. } else {
  97. currentMarker = detectMarker;
  98. }
  99. /* we are in the data range */
  100. if (currentMarker == detectMarker) {
  101. /* Push bit */
  102. currentByte = currentByte << 1;
  103. if ((lowLengthFrame<2) || (highLengthFrame>5)) {
  104. fprintf(output, "There might be an error from here\n");
  105. if (!all) return;
  106. }
  107. currentByte += (lowLengthFrame < 5 ? 0 : 1);
  108. /* check if the byte is completed */
  109. if (bitNumber == 7) {
  110. bitNumber = 0;
  111. fprintf(output, "%02X ", currentByte);
  112. currentByte = 0;
  113. } else {
  114. bitNumber++;
  115. }
  116. }
  117. /* We are in the data range */
  118. } else {
  119. if ((lowLengthFrame > 20) && (lowLengthFrame<50) && (startData)) {
  120. currentMarker = 1;
  121. currentByte = 0;
  122. bitNumber = 0;
  123. fprintf(output, "DataStart at %lu\n", cursor);
  124. fprintf(output, "\n%02d: ", detectMarker);
  125. }
  126. if (lowLengthFrame > 100) {
  127. startData = 1;
  128. }
  129. }
  130. cursor += totalLengthFrame;
  131. }
  132. }