API Application Example
API Header File
#ifndef __LED_H__ #define __LED_H__ #include <stdint.h> /* uint??_t */ #include <stdbool.h> /* Bool */ /***************************************************************************/ /* For LED */ /***************************************************************************/ // #define DEVICE_LED_DEBUG_MESSAGE #define DEVICE_LED_B14 "/sys/class/leds/b14/brightness" #define DEVICE_LED_A25 "/sys/class/leds/a25/brightness" #define DEVICE_LED_C21 "/sys/class/leds/c21/brightness" #define DEVICE_LED_B15 "/sys/class/leds/b15/brightness" #define DEVICE_LED_OIL_STATION_HANDY_READY DEVICE_LED_B14 #define DEVICE_LED_OIL_STATION_HANDY_PROCESS DEVICE_LED_A25 #define DEVICE_LED_OIL_STATION_HANDY_COLPLETE DEVICE_LED_C21 #define DEVICE_LED_OIL_STATION_HANDY_ERROR DEVICE_LED_B15 #define DEVICE_LED_OIL_STATION_AP_STATUS DEVICE_LED_B14 #define DEVICE_LED_OIL_STATION_AP_ERROR DEVICE_LED_B15 #define DEVICE_LED_OIL_STATION_PRINTER_BUSY DEVICE_LED_B14 #define DEVICE_LED_OIL_STATION_PRINTER_READY DEVICE_LED_A25 #define DEVICE_LED_OIL_STATION_PRINTER_NO_PAPER DEVICE_LED_B15 /* LED On/Off Input true = LED On false= LED Off Return 0 = Opened -1 = Input Parameter Error -2 = Fail to Access Device -3 = Fail to Operation */ extern int led_switch(uint8_t *pDeviceName, bool Switch/*true=on, false=Off*/); /* get LED state Return 0 = Opened -1 = Input Parameter Error -2 = Fail to Access Device -3 = Fail to Operation *pIsOn : true = LED is On, false = LED is Off */ extern int led_get(uint8_t *pDeviceName, bool *pIsOn); #endif /* __LED_H__ */
API Source File
#include <stdio.h> #include <stdint.h> /* uint??_t */ #include <stdbool.h> /* bool */ #include <stdlib.h> /* strtoul */ #include <string.h> /* for memset */ #include <unistd.h> /* for open/close .. */ #include <fcntl.h> /* for O_RDWR */ #include <sys/ioctl.h> /* for ioctl */ #include <sys/fcntl.h> /* for O_RDWR */ #include "LED.h" /* LED On/Off Input true = LED On false= LED Off Return 0 = Opened -1 = Input Parameter Error -2 = Fail to Access Device -3 = Fail to Operation */ int led_switch(uint8_t *pDeviceName, bool Switch/*true=on, false=Off*/) { FILE * pDeviceFile; char pString[2] = { 0, 0 }; int StringLength; if ( pDeviceName == (uint8_t *)0 ) { #ifdef DEVICE_LED_DEBUG_MESSAGE printf("led_switch : Input Parameter error\n"); #endif /* DEVICE_LED_DEBUG_MESSAGE */ return -1; } pDeviceFile = fopen ( pDeviceName, "w+"); if ( pDeviceFile == (FILE *)0 ) { #ifdef DEVICE_LED_DEBUG_MESSAGE printf ( "led_switch : failed to open LED device : %s\n", pDeviceName ); #endif /* DEVICE_LED_DEBUG_MESSAGE */ return -2; } if ( Switch == true ) { pString[0] = '1'; } else { pString[0] = '0'; } StringLength = (int)strlen(pString); if ( fwrite ( pString, StringLength, 1, pDeviceFile ) < 0 ) { #ifdef DEVICE_LED_DEBUG_MESSAGE printf ( "led_switch : failed to operation LED device : %s\n", pDeviceName ); #endif /* DEVICE_LED_DEBUG_MESSAGE */ return -3; } fclose ( pDeviceFile ); return 0; } /* get LED state Return 0 = Opened -1 = Input Parameter Error -2 = Fail to Access Device -3 = Fail to Operation *pIsOn : true = LED is On, false = LED is Off */ int led_get(uint8_t *pDeviceName, bool *pIsOn) { FILE * pDeviceFile; char pCommand[128] = {0}; char pString[10] = {0}; int Value; if ( ( pDeviceName == (uint8_t *)0 ) || ( pIsOn == (bool *)0 ) ) { #ifdef DEVICE_LED_DEBUG_MESSAGE printf("led_get : Input Parameter error\n"); #endif /* DEVICE_LED_DEBUG_MESSAGE */ return -1; } sprintf(pCommand, "cat %s", pDeviceName); pDeviceFile = popen(pCommand, "r"); if ( pDeviceFile == (FILE *)0 ) { #ifdef DEVICE_LED_DEBUG_MESSAGE printf("led_get : device open fail\n"); #endif /* DEVICE_LED_DEBUG_MESSAGE */ return -2; } while( fgets (pString, 10, pDeviceFile) ); Value = (uint32_t)strtoul ( pString, NULL, 10 ); fclose (pDeviceFile); *pIsOn = ((Value == 0) ? false:true); return 0; }