Siegurd

Untitled

Aug 18th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. #define GET_BIT(X,N) ( ( (X) >> (N) ) & 1 )
  8.  
  9. double ref = 2.5;
  10. uint32_t raw_code = 0;
  11. double voltage = -1;
  12.  
  13. void ad7771_data_to_voltage(double ref, uint32_t *raw_code, double *voltage) {
  14. if((*raw_code <= 0x7FFFFF) && (*raw_code >= 0x00)) {
  15. *voltage = (double)(*raw_code * (ref/16777216.0));
  16. }
  17. else {
  18. *voltage = (double)((*raw_code * (ref/16777216.0))-ref);
  19. }
  20. }
  21.  
  22. timespec diff(timespec start, timespec end){
  23. timespec temp;
  24. if ((end.tv_nsec-start.tv_nsec)<0) {
  25. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  26. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  27. } else {
  28. temp.tv_sec = end.tv_sec-start.tv_sec;
  29. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  30. }
  31. return temp;
  32. }
  33.  
  34. int main() {
  35. int pipefd[2];
  36. if (pipe(pipefd) == -1) {
  37. perror("pipe");
  38. return 1;
  39. }
  40.  
  41. pid_t pid = fork();
  42. if (pid == -1) {
  43. perror("fork");
  44. return 1;
  45. }
  46.  
  47. if (pid == 0) { // Child process
  48. close(pipefd[0]); // Close read end
  49.  
  50. dup2(pipefd[1], STDOUT_FILENO);
  51.  
  52. const char* command = "arecord -c 8 -r 192 -f S32 -t raw -D hw";
  53. FILE* output_stream = popen(command, "r");
  54. if (output_stream == nullptr) {
  55. perror("popen");
  56. return 1;
  57. }
  58. char buf[32768];
  59. ssize_t bytesRead;
  60. bytesRead = read(pipefd[0], buf, sizeof(buf));
  61. timespec time1_, time2_;
  62.  
  63. clock_gettime(CLOCK_REALTIME, &time1_);
  64. for (int i1 = 0; i1 < 125; ++i1) { //125 readings should give 125*1/128000*1024 = 1 second period
  65. bytesRead = read(pipefd[0], buf, sizeof(buf));
  66. }
  67. clock_gettime(CLOCK_REALTIME, &time2_);
  68. std::cout<< "Data read took s ns: "<<diff(time1_,time2_).tv_sec <<" "<< diff(time1_,time2_).tv_nsec << std::endl;
  69.  
  70. uint32_t adcRawValues0 = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
  71. uint8_t channelId0 = GET_BIT(buf[3], 4) + GET_BIT(buf[3], 5) * 2 + GET_BIT(buf[3], 6) * 4;
  72. uint8_t channelError0 = GET_BIT(buf[3], 7);
  73.  
  74. uint32_t adcRawValues1 = (buf[18] << 16) | (buf[17] << 8) | (buf[16] << 0);
  75. uint8_t channelId1 = GET_BIT(buf[19], 4) + GET_BIT(buf[19], 5) * 2 + GET_BIT(buf[19], 6) * 4;
  76. uint8_t channelError1 = GET_BIT(buf[19], 7);
  77.  
  78. uint32_t adcRawValues2 = (buf[6] << 16) | (buf[5] << 8) | (buf[4] << 0);
  79. uint8_t channelId2 = GET_BIT(buf[7], 4) + GET_BIT(buf[7], 5) * 2 + GET_BIT(buf[7], 6) * 4;
  80. uint8_t channelError2 = GET_BIT(buf[7], 7);
  81.  
  82. uint32_t adcRawValues3 = (buf[22] << 16) | (buf[21] << 8) | (buf[20] << 0);
  83. uint8_t channelId3 = GET_BIT(buf[23], 4) + GET_BIT(buf[23], 5) * 2 + GET_BIT(buf[23], 6) * 4;
  84. uint8_t channelError3 = GET_BIT(buf[23], 7);
  85.  
  86. uint32_t adcRawValues4 = (buf[10] << 16) | (buf[9] << 8) | (buf[8] << 0);
  87. uint8_t channelId4 = GET_BIT(buf[11], 4) + GET_BIT(buf[11], 5) * 2 + GET_BIT(buf[11], 6) * 4;
  88. uint8_t channelError4 = GET_BIT(buf[11], 7);
  89.  
  90. uint32_t adcRawValues5 = (buf[26] << 16) | (buf[25] << 8) | (buf[24] << 0);
  91. uint8_t channelId5 = GET_BIT(buf[27], 4) + GET_BIT(buf[27], 5) * 2 + GET_BIT(buf[27], 6) * 4;
  92. uint8_t channelError5 = GET_BIT(buf[27], 7);
  93.  
  94. uint32_t adcRawValues6 = (buf[14] << 16) | (buf[13] << 8) | (buf[12] << 0);
  95. uint8_t channelId6 = GET_BIT(buf[15], 4) + GET_BIT(buf[15], 5) * 2 + GET_BIT(buf[15], 6) * 4;
  96. uint8_t channelError6 = GET_BIT(buf[15], 7);
  97.  
  98. uint32_t adcRawValues7 = (buf[30] << 16) | (buf[29] << 8) | (buf[28] << 0);
  99. uint8_t channelId7 = GET_BIT(buf[31], 4) + GET_BIT(buf[31], 5) * 2 + GET_BIT(buf[31], 6) * 4;
  100. uint8_t channelError7 = GET_BIT(buf[31], 7);
  101.  
  102.  
  103. ad7771_data_to_voltage(ref, &adcRawValues0, &voltage);
  104. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[3],buf[2],buf[1],buf[0], channelId0,voltage,channelError0, adcRawValues0);
  105. ad7771_data_to_voltage(ref, &adcRawValues1, &voltage);
  106. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[19],buf[18],buf[17],buf[16], channelId1,voltage,channelError1, adcRawValues1);
  107. ad7771_data_to_voltage(ref, &adcRawValues2, &voltage);
  108. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[7],buf[6],buf[5],buf[4], channelId2,voltage,channelError2, adcRawValues2);
  109. ad7771_data_to_voltage(ref, &adcRawValues3, &voltage);
  110. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[23],buf[22],buf[21],buf[20], channelId3,voltage,channelError3, adcRawValues3);
  111. ad7771_data_to_voltage(ref, &adcRawValues4, &voltage);
  112. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[11],buf[10],buf[9],buf[8], channelId4,voltage,channelError4, adcRawValues4);
  113. ad7771_data_to_voltage(ref, &adcRawValues5, &voltage);
  114. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[27],buf[26],buf[25],buf[24], channelId5,voltage,channelError5, adcRawValues5);
  115. ad7771_data_to_voltage(ref, &adcRawValues6, &voltage);
  116. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[15],buf[14],buf[13],buf[12], channelId6,voltage,channelError6, adcRawValues6);
  117. ad7771_data_to_voltage(ref, &adcRawValues7, &voltage);
  118. printf("HEADER: %hhX %hhX %hhX %hhX CH: %d Volts: %.6f Error: %d Value: %d\n", buf[21],buf[20],buf[19],buf[18], channelId7,voltage,channelError7, adcRawValues7);
  119.  
  120.  
  121.  
  122. // Close the write end of the pipe after the program is done
  123. pclose(output_stream);
  124. }
  125. else { // Parent process
  126. close(pipefd[1]); // Close write end
  127.  
  128. // Read data from the read end of the pipe
  129. char buffer[1024];
  130. ssize_t bytesRead;
  131. while ((bytesRead = read(pipefd[0], buffer, sizeof(buffer))) > 0) {
  132. // Process or use the data as needed
  133. // Here, we just print it to the console
  134. std::cout.write(buffer, bytesRead);
  135. }
  136.  
  137. close(pipefd[0]); // Close read end after reading is done
  138.  
  139. wait(NULL); // Wait for the child process to finish
  140. }
  141.  
  142. return 0;
  143. }
  144.  
  145.  
  146. root@BeagleBone:/home/debian# g++ -oread_stream read_stream.cpp && ./read_stream
  147. Recording raw data 'stdin' : Signed 32 bit Little Endian, Rate 192000 Hz, Channels 8
  148. Data read took s ns: 0 999621565
  149. HEADER: 89 7F F0 DB CH: 0 Volts: 1.249422 Error: 1 Value: 8384731
  150. HEADER: 94 FF FF F2 CH: 1 Volts: -0.000002 Error: 1 Value: 16777202
  151. HEADER: A2 0 0 5A CH: 2 Volts: 0.000013 Error: 1 Value: 90
  152. HEADER: B9 FF FF D4 CH: 3 Volts: -0.000007 Error: 1 Value: 16777172
  153. HEADER: C5 0 0 52 CH: 4 Volts: 0.000012 Error: 1 Value: 82
  154. HEADER: D0 FF FF FC CH: 5 Volts: -0.000001 Error: 1 Value: 16777212
  155. HEADER: EE 0 0 47 CH: 6 Volts: 0.000011 Error: 1 Value: 71
  156. HEADER: FF D4 94 FF CH: 7 Volts: -0.000008 Error: 1 Value: 16777159
  157.  
Advertisement
Add Comment
Please, Sign In to add comment