comm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "plt.h"
  2. #define NONE "\e[0m"
  3. #define BLACK "\e[0;30m"
  4. #define L_BLACK "\e[1;30m"
  5. #define RED "\e[0;31m"
  6. #define L_RED "\e[1;31m"
  7. #define GREEN "\e[0;32m"
  8. #define L_GREEN "\e[1;32m"
  9. #define BROWN "\e[0;33m"
  10. #define YELLOW "\e[1;33m"
  11. #define BLUE "\e[0;34m"
  12. #define L_BLUE "\e[1;34m"
  13. #define PURPLE "\e[0;35m"
  14. #define L_PURPLE "\e[1;35m"
  15. #define CYAN "\e[0;36m"
  16. #define L_CYAN "\e[1;36m"
  17. #define GRAY "\e[0;37m"
  18. #define WHITE "\e[1;37m"
  19. #define BOLD "\e[1m"
  20. #define UNDERLINE "\e[4m"
  21. #define BLINK "\e[5m"
  22. #define REVERSE "\e[7m"
  23. #define HIDE "\e[8m"
  24. #define CLEAR "\e[2J"
  25. #define CLRLINE "\r\e[K"
  26. /* ms */
  27. static double comm_get_timeofday()
  28. {
  29. struct timeval tv;
  30. struct timezone tz;
  31. gettimeofday(&tv, &tz);
  32. return (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  33. }
  34. void comm_set_state(struct comm_t *comm, int state)
  35. {
  36. comm->state = state;
  37. if (state == COMMST_NORMAL)
  38. {
  39. strcpy(comm->szState, "norm");
  40. }
  41. else if (state == COMMST_ERR)
  42. {
  43. strcpy(comm->szState, "err");
  44. }
  45. else
  46. {
  47. strcpy(comm->szState, "unknown");
  48. }
  49. }
  50. int comm_get_state(struct comm_t *comm)
  51. {
  52. return comm->state;
  53. }
  54. void comm_set_dac_param_en(struct comm_t *comm, int val)
  55. {
  56. comm->dac.param_en = val;
  57. }
  58. int comm_get_dac_param_en(struct comm_t *comm)
  59. {
  60. return comm->dac.param_en;
  61. }
  62. void comm_start_cal_dac_timing(struct comm_t *comm)
  63. {
  64. comm->dac.timing_start = comm_get_timeofday();
  65. }
  66. void comm_stop_cal_dac_timing(struct comm_t *comm)
  67. {
  68. if (comm->dac.totalcnt < -0.1)
  69. { /* reset */
  70. comm->dac.timing_ave = -1.0;
  71. comm->dac.timing_max = -1.0;
  72. comm->dac.timing_cur = -1.0;
  73. comm->dac.totalcnt = 0.0;
  74. comm->dac.totaltime = 0.0;
  75. }
  76. else
  77. {
  78. comm->dac.timing_end = comm_get_timeofday();
  79. comm->dac.timing_cur = comm->dac.timing_end - comm->dac.timing_start;
  80. comm->dac.totalcnt += 1;
  81. comm->dac.totaltime += comm->dac.timing_cur;
  82. comm->dac.timing_ave = comm->dac.totaltime / comm->dac.totalcnt;
  83. if (comm->dac.timing_cur > comm->dac.timing_max)
  84. {
  85. comm->dac.timing_max = comm->dac.timing_cur;
  86. }
  87. if (comm->dac.totalcnt > 1640000)
  88. { /* auto reset */
  89. comm->dac.totalcnt = -1.0;
  90. }
  91. }
  92. }
  93. void comm_get_summary(struct comm_t *comm, char *buf, int len)
  94. {
  95. sprintf(buf, "chan:%d adr:%d state:%s dac_timing_ave(ms):%.0f max:%.0f cur:%.0f",
  96. comm->chanidx, comm->adr,
  97. comm->szState,
  98. comm->dac.timing_ave, comm->dac.timing_max, comm->dac.timing_cur);
  99. }
  100. void comm_reset(struct comm_t *comm)
  101. {
  102. comm_set_state(comm, COMMST_NORMAL);
  103. comm_set_dac_param_en(comm, 1);
  104. }
  105. int comm_get_adr(struct comm_t *comm)
  106. {
  107. return comm->adr;
  108. }
  109. int comm_get_chan_idx(struct comm_t *comm)
  110. {
  111. return comm->chanidx;
  112. }
  113. char *comm_get_state_str(struct comm_t *comm)
  114. {
  115. return comm->szState;
  116. }