main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/statvfs.h>
  8. #include <sys/statfs.h>
  9. #include <sys/types.h>
  10. #include <signal.h>
  11. #include <poll.h>
  12. #include <fcntl.h>
  13. #include <errno.h>
  14. #include "../defines.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. #define _USE_BIN_ADC 1
  17. /////////////////////////////////////////////////////////////////////////////
  18. #ifdef _DEBUG
  19. #define TRACE(...) fprintf(stdout, __VA_ARGS__), fflush(stdout)
  20. #else // _DEBUG
  21. #define TRACE(...)
  22. #endif // _DEBUG
  23. #define UNUSED(v) (void)v
  24. #define _countof(a) (sizeof(a) / sizeof(*a))
  25. #define _CYCLE_INTV 500
  26. static bool g_bRun = true;
  27. /////////////////////////////////////////////////////////////////////////////
  28. typedef struct _ATTRIBS
  29. {
  30. const char *pszName;
  31. double (*fmt)(const char*);
  32. }ATTRIBS, *LPATTRIBS;
  33. typedef const ATTRIBS *LPCATTRIBS;
  34. /////////////////////////////////////////////////////////////////////////////
  35. static void _SigHandler(int sig)
  36. {
  37. UNUSED(sig);
  38. g_bRun = false;
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. #if !_USE_BIN_ADC
  42. static double _FmtUVers(const char *pszVal)
  43. {
  44. int nVal = atoi(pszVal);
  45. return (double)nVal / 100.0 + 0.4;
  46. }
  47. static double _FmtUBatV3(const char *pszVal)
  48. {
  49. int nVal = atoi(pszVal);
  50. return (double)nVal / 100.0;
  51. }
  52. static double _FmtTemp(const char *pszVal)
  53. {
  54. int nVal = atoi(pszVal);
  55. return (double)nVal / 10.0;
  56. }
  57. static double _FmtUV5Vsys(const char *pszVal)
  58. {
  59. int nVal = atoi(pszVal);
  60. return (double)nVal / 100.0;
  61. }
  62. static double _FmtUV3V6Bat(const char *pszVal)
  63. {
  64. int nVal = atoi(pszVal);
  65. return (double)nVal / 100.0;
  66. }
  67. static double _FmtTempTIVA(const char *pszVal)
  68. {
  69. int nVal = atoi(pszVal);
  70. return 147.5 - 187.5 * (double)nVal / 4096.0;
  71. }
  72. #endif // _USE_BIN_ADC
  73. /////////////////////////////////////////////////////////////////////////////
  74. int main(int argc, char *argv[])
  75. {
  76. static const ATTRIBS attribs[] =
  77. {
  78. #if _USE_BIN_ADC
  79. {
  80. .pszName = "AdcBin"
  81. }
  82. #else
  83. {
  84. // _USE_BIN_ADC
  85. }
  86. {
  87. .pszName = "UVers",
  88. .fmt = _FmtUVers
  89. },
  90. {
  91. .pszName = "UBatV3",
  92. .fmt = _FmtUBatV3
  93. },
  94. {
  95. .pszName = "TempBoard",
  96. .fmt = _FmtTemp
  97. },
  98. {
  99. .pszName = "UV5Vsys",
  100. .fmt = _FmtUV5Vsys
  101. },
  102. {
  103. .pszName = "UV3V6Bat",
  104. .fmt = _FmtUV3V6Bat
  105. },
  106. {
  107. .pszName = "TempTIVA",
  108. .fmt = _FmtTempTIVA
  109. }
  110. #endif // _USE_BIN_ADC
  111. };
  112. const ATTRIBS *attribsCur[_countof(attribs)];
  113. /////////////////////////////////////////////////////////////////////////
  114. #if _USE_BIN_ADC
  115. TIVA_ADC tadc;
  116. #else // _USE_BIN_ADC
  117. char szBuf[256];
  118. #endif // _USE_BIN_ADC
  119. int nRet, nCntAtts, i;
  120. struct pollfd pfd[6];
  121. struct sigaction sa;
  122. memset(pfd, 0, sizeof(pfd));
  123. memset(&sa, 0, sizeof(sa));
  124. UNUSED(argc);
  125. UNUSED(argv);
  126. /////////////////////////////////////////////////////////////////////////
  127. // handle signals
  128. sa.sa_handler = _SigHandler;
  129. sigaction(SIGHUP, &sa, NULL); // handles user's terminal disconnect
  130. sigaction(SIGQUIT, &sa, NULL); // handles Ctrl + '\'
  131. sigaction(SIGTERM, &sa, NULL); // handles normal termination
  132. sigaction(SIGABRT, &sa, NULL); // handles abnormal termination (i.e. abort())
  133. sigaction(SIGINT, &sa, NULL); // handles Ctrl + 'C'
  134. // ignore signals
  135. sa.sa_handler = SIG_IGN;
  136. sigaction(SIGTSTP, &sa, NULL); // ignores Ctrl + 'Z'
  137. sigaction(SIGCHLD, &sa, NULL); // ignores child process termination
  138. sigaction(0, &sa, NULL); // ignores shell termination
  139. /////////////////////////////////////////////////////////////////////////
  140. for(i = 0, nCntAtts = 0; i < (int)_countof(attribs); ++i)
  141. {
  142. int fd;
  143. char szDevNode[128];
  144. sprintf(szDevNode, "/sys/gfa/tiva/%s", attribs[i].pszName);
  145. if((fd = open(szDevNode, O_RDONLY, 0)) >= 0)
  146. {
  147. pfd[nCntAtts].fd = fd;
  148. pfd[nCntAtts].events = POLLPRI;
  149. attribsCur[nCntAtts] = &attribs[i];
  150. ++nCntAtts;
  151. }
  152. }
  153. /////////////////////////////////////////////////////////////////////////
  154. if(nCntAtts > 0)
  155. {
  156. while(g_bRun && ((nRet = poll(pfd, nCntAtts, _CYCLE_INTV)) >= 0))
  157. {
  158. if(nRet > 0)
  159. {
  160. #if !_USE_BIN_ADC
  161. for(i = 0; i < nCntAtts; ++i)
  162. {
  163. if(pfd[i].revents & POLLPRI)
  164. {
  165. szBuf[0] = '\0';
  166. if((nRet = read(pfd[i].fd, szBuf, sizeof(szBuf))) >= 0)
  167. {
  168. szBuf[nRet] = '\0';
  169. lseek(pfd[i].fd, 0, SEEK_SET);
  170. TRACE("%-8s: %s\n", attribsCur[i]->pszName, szBuf);
  171. }
  172. else
  173. {
  174. g_bRun = false;
  175. break;
  176. }
  177. }
  178. }
  179. #else // _USE_BIN_ADC
  180. if(pfd[0].revents & POLLPRI)
  181. {
  182. if((nRet = read(pfd[0].fd, &tadc, sizeof(tadc))) >= 0)
  183. {
  184. lseek(pfd[0].fd, 0, SEEK_SET);
  185. TRACE("UVers : %.2f V\n", (double)tadc.UVers / 100.0 + 0.4);
  186. TRACE("UBatV3 : %.2f V\n", (double)tadc.UBatV3 / 100.0);
  187. TRACE("Temp : %.2f °C\n", (double)tadc.Temp / 10.0);
  188. TRACE("UV5Vsys : %.2f V\n", (double)tadc.UV5Vsys / 100.0);
  189. TRACE("UV3V6Bat: %.2f V\n", (double)tadc.UV3V6Bat / 100.0);
  190. TRACE("TempTIVA: %.2f °C\n\n", 147.5 - 187.5 * (double)tadc.TempTIVA / 4096.0);
  191. }
  192. else
  193. {
  194. g_bRun = false;
  195. break;
  196. }
  197. }
  198. #endif // _USE_BIN_ADC
  199. }
  200. }
  201. if(nRet < 0)
  202. {
  203. TRACE("%s\n", strerror(errno));
  204. }
  205. }
  206. for(i = 0; i < nCntAtts; ++i)
  207. {
  208. if(pfd[i].fd >= 0)
  209. close(pfd[i].fd);
  210. }
  211. return 0;
  212. }