Skip to content

Commit

Permalink
cache board type to avoid poor performance (#196)
Browse files Browse the repository at this point in the history
cache board type to avoid poor performance
in functions that are called frequently like
gpio_set_value() in source/event_gpio.c

Signed-off-by: Drew Fustini <[email protected]>
  • Loading branch information
pdp7 committed Nov 19, 2017
1 parent b328cdd commit 3281479
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down

0 comments on commit 3281479

Please sign in to comment.