You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

105 lines
1.9 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifndef __USE_ISOC99
  4. #define __USE_ISOC99
  5. #endif
  6. #include <math.h>
  7. #include "zoom.h"
  8. #include "serial-util.h"
  9. #define zputs(s) do { if (safewrite(fd, s, strlen(s)) != strlen(s)) \
  10. return -1; } while(0)
  11. #define zputc(ch) do { const char ____c = ch; \
  12. if (safewrite(fd, &____c, 1) != 1) return -1; } while(0)
  13. static int last_dac, last_adc;
  14. static int verify_prompt(int fd)
  15. {
  16. char s[128];
  17. int dac1, dac2, adc1, adc2;
  18. if (fdgets(s, 128, fd, 1000) == NULL)
  19. return -1;
  20. chomp(s);
  21. if (sscanf(s, "%x %d %x %d", &dac1, &dac2, &adc1, &adc2) != 4)
  22. return -1;
  23. // if (dac1 != dac2 || adc1 != adc2)
  24. if (adc1 != adc2)
  25. return -1;
  26. last_dac = dac1;
  27. last_adc = adc1;
  28. return 0;
  29. }
  30. int zoom_init_real(int fd, int dozero)
  31. {
  32. zputs(dozero ? "00000000" : " ");
  33. drain(fd);
  34. zputc(dozero ? '0' : ' ');
  35. if (verify_prompt(fd) < 0)
  36. return -1;
  37. return 0;
  38. }
  39. int zoom_zero_start(int fd)
  40. {
  41. char s[128];
  42. zputc('z');
  43. if (fdgets(s, 128, fd, 1000) == NULL)
  44. return -1;
  45. chomp(s);
  46. if (strcmp(s, "zeroing input...") != 0)
  47. return -1;
  48. return 0;
  49. }
  50. int zoom_zero_stop(int fd)
  51. {
  52. zputc(' ');
  53. if (verify_prompt(fd) < 0)
  54. return -1;
  55. return last_dac;
  56. }
  57. int zoom_sweep(int fd, int dac[ZOOM_SWEEP_COUNT], int adc[ZOOM_SWEEP_COUNT])
  58. {
  59. char s[128];
  60. int i;
  61. char c;
  62. zputc('s');
  63. if (fdgets(s, 128, fd, 1000) == NULL)
  64. return -1;
  65. if (strncmp(s, "sweep around", 12) != 0)
  66. return -1;
  67. for (i = 0; i < ZOOM_SWEEP_COUNT; i++) {
  68. if (fdgets(s, 128, fd, 1000) == NULL)
  69. return -1;
  70. chomp(s);
  71. if (sscanf(s, "%d %d%c", &dac[i], &adc[i], &c) != 2)
  72. return -2;
  73. }
  74. if (verify_prompt(fd) < 0)
  75. return -3;
  76. return 0;
  77. }
  78. int zoom_write_dac(int fd, int dac)
  79. {
  80. char s[128];
  81. sprintf(s, "v%04x", dac);
  82. zputs(s);
  83. if (verify_prompt(fd) < 0)
  84. return -1;
  85. return last_dac;
  86. }
  87. /* Run mode: */
  88. void zoomrun_trigger_calibrate(int fd)
  89. {
  90. char c = 'c';
  91. write(fd, &c, 1);
  92. drain_timeout(fd, 0);
  93. }