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.
 
 
 
 

89 lines
1.7 KiB

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