diff --git a/source/common.c b/source/common.c index e000a66..d940042 100644 --- a/source/common.c +++ b/source/common.c @@ -601,23 +601,30 @@ int pocketbeagle(void) { */ int beaglebone_blue(void) { const char *cmd = "/bin/grep -c 'TI AM335x BeagleBone Blue' /proc/device-tree/model"; - char blue; + // cache the value to avoid poor performance + // in functions that are called frequently like + // gpio_set_value() in source/event_gpio.c + static int initialized = 0; + static int retval = 0; FILE *file = NULL; - file = popen(cmd, "r"); - if (file == NULL) { - fprintf(stderr, "error: beaglebone_blue() failed to run cmd=%s\n", cmd); - syslog(LOG_ERR, "Adafruit_BBIO: error: beaglebone_blue() failed to run cmd=%s\n", cmd); - return -1; + //fprintf(stderr, "beaglebone_blue(): initialized=[%d] retval=[%d]\n", initialized, retval); + if(!initialized) { + initialized = 1; + //fprintf(stderr, "beaglebone_blue(): not initialized\n"); + file = popen(cmd, "r"); + if (file == NULL) { + fprintf(stderr, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd); + syslog(LOG_ERR, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd); + return -1; + } + if( fgetc(file) == '1' ) { + retval = 1; + } + pclose(file); } - blue = fgetc(file); - pclose(file); - if(blue == '1') { - return 1; - } else { - return 0; - } + return retval; }