Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

82 lines
2.4KB

  1. #include <stdio.h>
  2. #include "AnalogBinLogger.h"
  3. FILE *source;
  4. FILE *destination;
  5. int count = 0;
  6. int main(int argc, char** argv) {
  7. metadata_t meta;
  8. adcdata_t adc;
  9. // Make sure no padding/size problems.
  10. if (sizeof(meta) != 512 || sizeof(adc) != 512) {
  11. printf("block size error\n");
  12. return 0;
  13. }
  14. if (argc != 3) {
  15. printf("missing arguments:\n");
  16. printf("%s binFile csvFile\n", argv[0]);
  17. return 0;
  18. }
  19. source = fopen(argv[1], "rb");
  20. if (!source) {
  21. printf("open failed for %s\n", argv[1]);
  22. return 0;
  23. }
  24. if (fread(&meta, sizeof(meta), 1, source) != 1) {
  25. printf("read meta data failed\n");
  26. return 0;
  27. }
  28. if ( meta.pinCount == 0
  29. || meta.pinCount > (sizeof(meta.pinNumber)/sizeof(meta.pinNumber[0]))
  30. || meta.adcFrequency < 50000 || meta.adcFrequency > 4000000) {
  31. printf("Invalid meta data\n");
  32. return 0;
  33. }
  34. destination = fopen(argv[2], "w");
  35. if (!destination) {
  36. printf("open failed for %s\n", argv[2]);
  37. return 0;
  38. }
  39. int pinCount = meta.pinCount;
  40. printf("pinCount: %d\n", pinCount);
  41. printf("Sample pins:");
  42. for (unsigned i = 0; i < meta.pinCount; i++) {
  43. printf(" %d", meta.pinNumber[i]);
  44. }
  45. printf("\n");
  46. printf("ADC clock rate: %g kHz\n", 0.001*meta.adcFrequency);
  47. float sampleInterval = (float)meta.sampleInterval/(float)meta.cpuFrequency;
  48. printf("Sample rate: %g per sec\n", 1.0/sampleInterval);
  49. printf("Sample interval: %.4f usec\n", 1.0e6*sampleInterval);
  50. fprintf(destination, "Interval,%.4f,usec\n", 1.0e6*sampleInterval);
  51. // Write header with pin numbers
  52. for (int i = 0; i < ((int)meta.pinCount - 1); i++) {
  53. fprintf(destination, "pin%d,", meta.pinNumber[i]);
  54. }
  55. fprintf(destination, "pin%d\n", meta.pinNumber[meta.pinCount - 1]);
  56. unsigned maxCount = meta.recordEightBits ? DATA_DIM8 : DATA_DIM16;
  57. while (!feof(source)) {
  58. if (fread(&adc, sizeof(adc), 1, source) != 1) break;
  59. if (adc.count > maxCount) {
  60. printf("****Invalid data block****\n");
  61. return 0;
  62. }
  63. if (adc.overrun) {
  64. fprintf(destination, "Overruns,%d\n", adc.overrun);
  65. }
  66. for (int i = 0; i < adc.count; i++) {
  67. unsigned value = meta.recordEightBits ? adc.data.u8[i] : adc.data.u16[i];
  68. if ((i + 1)%pinCount) {
  69. fprintf(destination, "%d,", value);
  70. } else {
  71. fprintf(destination, "%d\n", value);
  72. }
  73. }
  74. count += adc.count;
  75. }
  76. printf("%d ADC values read\n", count);
  77. fclose(source);
  78. fclose(destination);
  79. return 0;
  80. }