123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #include <stdio.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/statvfs.h>
- #include <sys/statfs.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <poll.h>
- #include <fcntl.h>
- #include <errno.h>
- #include "../gfaspi.h"
- /////////////////////////////////////////////////////////////////////////////
- #define _USE_BIN_ADC 1
- /////////////////////////////////////////////////////////////////////////////
- #ifdef _DEBUG
- #define TRACE(...) fprintf(stdout, __VA_ARGS__), fflush(stdout)
- #else // _DEBUG
- #define TRACE(...)
- #endif // _DEBUG
- #define UNUSED(v) (void)v
- #define _countof(a) (sizeof(a) / sizeof(*a))
- #define _CYCLE_INTV 500
- static bool g_bRun = true;
- /////////////////////////////////////////////////////////////////////////////
- typedef struct _ATTRIBS
- {
- const char *pszName;
- double (*fmt)(const char*);
- }ATTRIBS, *LPATTRIBS;
- typedef const ATTRIBS *LPCATTRIBS;
- /////////////////////////////////////////////////////////////////////////////
- static void _SigHandler(int sig)
- {
- UNUSED(sig);
- g_bRun = false;
- }
- /////////////////////////////////////////////////////////////////////////////
- #if !_USE_BIN_ADC
- static double _FmtUVers(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return (double)nVal / 100.0 + 0.4;
- }
- static double _FmtUBatV3(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return (double)nVal / 100.0;
- }
- static double _FmtTemp(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return (double)nVal / 10.0;
- }
- static double _FmtUV5Vsys(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return (double)nVal / 100.0;
- }
- static double _FmtUV3V6Bat(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return (double)nVal / 100.0;
- }
- static double _FmtTempTIVA(const char *pszVal)
- {
- int nVal = atoi(pszVal);
- return 147.5 - 187.5 * (double)nVal / 4096.0;
- }
- #endif // _USE_BIN_ADC
- /////////////////////////////////////////////////////////////////////////////
- int main(int argc, char *argv[])
- {
- static const ATTRIBS attribs[] =
- {
- #if _USE_BIN_ADC
- {
- .pszName = "AdcBin"
- }
- #else
- {
- // _USE_BIN_ADC
- }
- {
- .pszName = "UVers",
- .fmt = _FmtUVers
- },
- {
- .pszName = "UBatV3",
- .fmt = _FmtUBatV3
- },
- {
- .pszName = "TempBoard",
- .fmt = _FmtTemp
- },
- {
- .pszName = "UV5Vsys",
- .fmt = _FmtUV5Vsys
- },
- {
- .pszName = "UV3V6Bat",
- .fmt = _FmtUV3V6Bat
- },
- {
- .pszName = "TempTIVA",
- .fmt = _FmtTempTIVA
- }
- #endif // _USE_BIN_ADC
- };
- const ATTRIBS *attribsCur[_countof(attribs)];
- /////////////////////////////////////////////////////////////////////////
- #if _USE_BIN_ADC
- TIVA_ADC tadc;
- #else // _USE_BIN_ADC
- char szBuf[256];
- #endif // _USE_BIN_ADC
- int nRet, nCntAtts, i;
- struct pollfd pfd[6];
- struct sigaction sa;
- memset(pfd, 0, sizeof(pfd));
- memset(&sa, 0, sizeof(sa));
- UNUSED(argc);
- UNUSED(argv);
- /////////////////////////////////////////////////////////////////////////
- // handle signals
- sa.sa_handler = _SigHandler;
- sigaction(SIGHUP, &sa, NULL); // handles user's terminal disconnect
- sigaction(SIGQUIT, &sa, NULL); // handles Ctrl + '\'
- sigaction(SIGTERM, &sa, NULL); // handles normal termination
- sigaction(SIGABRT, &sa, NULL); // handles abnormal termination (i.e. abort())
- sigaction(SIGINT, &sa, NULL); // handles Ctrl + 'C'
- // ignore signals
- sa.sa_handler = SIG_IGN;
- sigaction(SIGTSTP, &sa, NULL); // ignores Ctrl + 'Z'
- sigaction(SIGCHLD, &sa, NULL); // ignores child process termination
- sigaction(0, &sa, NULL); // ignores shell termination
- /////////////////////////////////////////////////////////////////////////
- for(i = 0, nCntAtts = 0; i < (int)_countof(attribs); ++i)
- {
- int fd;
- char szDevNode[128];
- sprintf(szDevNode, "/sys/gfa/tiva/adc/%s", attribs[i].pszName);
- if((fd = open(szDevNode, O_RDONLY, 0)) >= 0)
- {
- pfd[nCntAtts].fd = fd;
- pfd[nCntAtts].events = POLLPRI;
- attribsCur[nCntAtts] = &attribs[i];
- ++nCntAtts;
- }
- }
- /////////////////////////////////////////////////////////////////////////
-
- if(nCntAtts > 0)
- {
- while(g_bRun && ((nRet = poll(pfd, nCntAtts, _CYCLE_INTV)) >= 0))
- {
- if(nRet > 0)
- {
- #if !_USE_BIN_ADC
- for(i = 0; i < nCntAtts; ++i)
- {
- if(pfd[i].revents & POLLPRI)
- {
- szBuf[0] = '\0';
- if((nRet = read(pfd[i].fd, szBuf, sizeof(szBuf))) >= 0)
- {
- szBuf[nRet] = '\0';
- lseek(pfd[i].fd, 0, SEEK_SET);
- TRACE("%-8s: %s\n", attribsCur[i]->pszName, szBuf);
- }
- else
- {
- g_bRun = false;
- break;
- }
- }
- }
- #else // _USE_BIN_ADC
- if(pfd[0].revents & POLLPRI)
- {
- if((nRet = read(pfd[0].fd, &tadc, sizeof(tadc))) >= 0)
- {
- lseek(pfd[0].fd, 0, SEEK_SET);
- TRACE("UVers : %.2f V\n", (double)tadc.UVers / 100.0 + 0.4);
- TRACE("UBatV3 : %.2f V\n", (double)tadc.UBatV3 / 100.0);
- TRACE("Temp : %.2f °C\n", (double)tadc.Temp / 10.0);
- TRACE("UV5Vsys : %.2f V\n", (double)tadc.UV5Vsys / 100.0);
- TRACE("UV3V6Bat: %.2f V\n", (double)tadc.UV3V6Bat / 100.0);
- TRACE("TempTIVA: %.2f °C\n\n", 147.5 - 187.5 * (double)tadc.TempTIVA / 4096.0);
- }
- else
- {
- g_bRun = false;
- break;
- }
- }
- #endif // _USE_BIN_ADC
- }
- }
- if(nRet < 0)
- {
- TRACE("%s\n", strerror(errno));
- }
- }
- for(i = 0; i < nCntAtts; ++i)
- {
- if(pfd[i].fd >= 0)
- close(pfd[i].fd);
- }
- return 0;
- }
|