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.
 
 
 
 

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