diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-03-06 10:41:44 -0800 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-03-06 10:45:03 -0800 |
commit | cdf4df771d223c27ea9b8b08a449c23d020dde6b (patch) | |
tree | 566045291d6b266a952ab24410972b041d7232ed | |
parent | 1e37dad1fc37f7626fa0c83441940bab673ef4c5 (diff) |
Cleanout un-used time and GPIO helpers
-rw-r--r-- | README.md | 33 | ||||
-rw-r--r-- | util.c | 90 | ||||
-rw-r--r-- | util.h | 19 |
3 files changed, 8 insertions, 134 deletions
@@ -16,35 +16,16 @@ int scanDoubleCallback (FILE *f, void *value); le_result_t ioutil_readIntFromFile (const char *path, int *value); le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); - ``` -### GPIO - -Functions to manipulate sysfs GPIO. - +### Time ```C -void getGpioPath (char *outputStr, int pin, char *subDir); -le_result_t gpio_exportPin (int pin); -le_result_t gpio_unexportPin (int pin); -le_result_t gpio_setDirection (int pin, char *direction); -le_result_t gpio_setInput (int pin); -le_result_t gpio_setOutput (int pin); -le_result_t gpio_setActiveState (int pin, bool isActiveLow); -le_result_t gpio_isActive (int pin, bool *isActive); -le_result_t gpio_setValue (int pin, bool state); -le_result_t gpio_setLow (int pin); -le_result_t gpio_setHigh (int pin); - +int util_getUnixDatetime (); +time_t util_getMTime (char *path); +uint64_t GetCurrentTimestamp (void); ``` -### Timing - -Functions to help with sample timing. Could potentially be replaced with system calls - +### Other ```C -// TODO give these a quasi "namespace" -void delayMicro (unsigned long microsecs); -void delayMilli (unsigned long millisecs); -long getTimeMicrosecs (); -``` +le_result_t util_flattenRes (le_result_t *res, int nRes); +```
\ No newline at end of file @@ -45,72 +45,6 @@ le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size return LE_OK; } -// TODO verify this is working -le_result_t gpio_exportPin (int pin) { - return ioutil_writeToFile("/sys/class/gpio/export", &pin, sizeof(int), 1); -} - -le_result_t gpio_unexportPin (int pin) { - return ioutil_writeToFile("/sys/class/gpio/unexport", &pin, sizeof(int), 1); -} - -void getGpioPath (char *outputStr, int pin, char *subDir) { - sprintf(outputStr, "/sys/class/gpio/gpio%d/%s", pin, subDir); -} - -le_result_t gpio_setDirection (int pin, char *direction) { - char path[50]; - getGpioPath(path, pin, "direction"); - return ioutil_writeToFile(path, direction, sizeof(char), strlen(direction)); -} - -le_result_t gpio_setInput (int pin) { - return gpio_setDirection(pin, "in"); -} - -le_result_t gpio_setOutput (int pin) { - return gpio_setDirection(pin, "out"); -} - -le_result_t gpio_setActiveState (int pin, bool isActiveLow) { - // Any non zero value toggles the existing value - // so we must read the existing value first - char path[50]; - int isActiveLowSet; - getGpioPath(path, pin, "active_low"); - le_result_t readRes = ioutil_readIntFromFile(path, &isActiveLowSet); - le_result_t writeRes = LE_OK; - if (isActiveLow != isActiveLowSet) { - writeRes = ioutil_writeToFile(path, "1", sizeof(char), 1); - } - return readRes == LE_OK && writeRes == LE_OK ? - LE_OK : LE_FAULT; -} - -le_result_t gpio_isActive (int pin, bool *isActive) { - char path[50]; - getGpioPath(path, pin, "value"); - int val; - le_result_t readRes = ioutil_readIntFromFile(path, &val); - LE_INFO("Raw value (from %s): %d", path, val); - *isActive = val; - return readRes; -} - -le_result_t gpio_setValue (int pin, bool state) { - char path[50]; - getGpioPath(path, pin, "value"); - return ioutil_writeToFile(path, &state, sizeof(bool), 1); -} - -le_result_t gpio_setLow (int pin) { - return gpio_setValue(pin, LOW); -} - -le_result_t gpio_setHigh (int pin) { - return gpio_setValue(pin, HIGH); -} - le_result_t util_flattenRes (le_result_t *res, int nRes) { for (int i = 0; i < nRes; i++) { le_result_t *resPtr = res + i; @@ -135,31 +69,9 @@ time_t util_getMTime (char *path) { else return st.st_mtime; } -/** - * Convenience function to get current time as uint64_t. - * - * @return - * Current time as a uint64_t - */ uint64_t GetCurrentTimestamp(void) { struct timeval tv; gettimeofday(&tv, NULL); uint64_t utcMilliSec = (uint64_t)(tv.tv_sec) * 1000 + (uint64_t)(tv.tv_usec) / 1000; return utcMilliSec; -} - -// Functions below are deprecated -void delayMicro (unsigned long microsecs) { - unsigned long retTime = getTimeMicrosecs() + microsecs; - while (getTimeMicrosecs() < retTime); -} - -void delayMilli (unsigned long millisecs) { - delayMicro(millisecs * 1000); -} - -long getTimeMicrosecs () { - struct timeval currentTime; - gettimeofday(¤tTime, NULL); - return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec; -} +}
\ No newline at end of file @@ -15,26 +15,7 @@ le_result_t ioutil_readIntFromFile (const char *path, int *value); le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); -// gpio -void getGpioPath (char *outputStr, int pin, char *subDir); -le_result_t gpio_exportPin (int pin); -le_result_t gpio_unexportPin (int pin); -le_result_t gpio_setDirection (int pin, char *direction); -le_result_t gpio_setInput (int pin); -le_result_t gpio_setOutput (int pin); -le_result_t gpio_setActiveState (int pin, bool isActiveLow); -le_result_t gpio_isActive (int pin, bool *isActive); -le_result_t gpio_setValue (int pin, bool state); -le_result_t gpio_setLow (int pin); -le_result_t gpio_setHigh (int pin); - -// time util -// TODO give these a quasi "namespace" -void delayMicro (unsigned long microsecs); -void delayMilli (unsigned long millisecs); -long getTimeMicrosecs (); uint64_t GetCurrentTimestamp (void); - time_t util_getMTime (char *path); int util_getUnixDatetime (); le_result_t util_flattenRes (le_result_t *res, int nRes); |