From eb62b610cbdefa42af014730484d703ab53a89ab Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 27 Dec 2017 21:17:47 +0100 Subject: [PATCH] (LED code) Cleanups --- led/led_defines.h | 14 ++++ led/led_driver.c | 80 +++++++++--------- led/led_driver.h | 17 ++++ led/null_led_driver.c | 33 ++++++-- led/rpi_led_driver.c | 189 +++++++++++++++++++++++++----------------- 5 files changed, 211 insertions(+), 122 deletions(-) diff --git a/led/led_defines.h b/led/led_defines.h index 720a887838..3789886c5a 100644 --- a/led/led_defines.h +++ b/led/led_defines.h @@ -1,3 +1,17 @@ +/* RetroArch - A frontend for libretro. + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + #ifndef __OUTPUT_DEFINES__H #define __OUTPUT_DEFINES__H diff --git a/led/led_driver.c b/led/led_driver.c index 522218cc03..01f605b9aa 100644 --- a/led/led_driver.c +++ b/led/led_driver.c @@ -1,56 +1,58 @@ -#include -#include "led_driver.h" -#include "configuration.h" -#include "verbosity.h" +/* RetroArch - A frontend for libretro. + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ -extern led_driver_t *null_led_driver; -#if HAVE_RPILED -extern led_driver_t *rpi_led_driver; -#endif -led_driver_t *current_led_driver = NULL; +#include +#include + +#include "led_driver.h" +#include "../configuration.h" +#include "../verbosity.h" + +static led_driver_t *current_led_driver = NULL; bool led_driver_init(void) { - char *drivername = NULL; - settings_t *settings = config_get_ptr(); - drivername = settings->arrays.led_driver; - - if(drivername == NULL) - drivername = "null"; + settings_t *settings = config_get_ptr(); + char *drivername = settings ? settings->arrays.led_driver : NULL; + + if(!drivername) + drivername = "null"; + + current_led_driver = null_led_driver; #if HAVE_RPILED - if(!strcmp("rpi",drivername)) - { - current_led_driver = rpi_led_driver; - } - else + if(string_is_equal("rpi", drivername)) + current_led_driver = rpi_led_driver; #endif - { - current_led_driver = null_led_driver; - } - RARCH_LOG("[LED]: LED driver = '%s' %p\n",drivername,current_led_driver); - - if(current_led_driver != NULL) - { - (*current_led_driver->init)(); - } - - return true; + RARCH_LOG("[LED]: LED driver = '%s' %p\n", + drivername,current_led_driver); + + if(current_led_driver) + (*current_led_driver->init)(); + + return true; } void led_driver_free(void) { - if(current_led_driver != NULL) - { - (*current_led_driver->free)(); - } + if(current_led_driver) + (*current_led_driver->free)(); } void led_driver_set_led(int led,int value) { - if(current_led_driver != NULL) - { - (*current_led_driver->set_led)(led,value); - } + if(current_led_driver) + (*current_led_driver->set_led)(led,value); } diff --git a/led/led_driver.h b/led/led_driver.h index ba79f5b3a0..d86e368a3a 100644 --- a/led/led_driver.h +++ b/led/led_driver.h @@ -1,3 +1,17 @@ +/* RetroArch - A frontend for libretro. + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + #ifndef __LED_DRIVER__H #define __LED_DRIVER__H @@ -32,4 +46,7 @@ void led_driver_free(void); void led_driver_set_led(int led,int value); +extern led_driver_t *null_led_driver; +extern led_driver_t *rpi_led_driver; + #endif diff --git a/led/null_led_driver.c b/led/null_led_driver.c index 0efbe34a95..08827e7782 100644 --- a/led/null_led_driver.c +++ b/led/null_led_driver.c @@ -1,12 +1,29 @@ +/* RetroArch - A frontend for libretro. + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + #include "led_driver.h" -#include "verbosity.h" +#include "../verbosity.h" -static void null_init(void) -{ - RARCH_LOG("[LED]: using null LED driver\n"); -} -static void null_free(void) {} -static void null_set(int led,int state) {} +static void null_init(void) { } +static void null_free(void) { } +static void null_set(int led,int state) { } + +static led_driver_t null_led_driver_ins = { + null_init, + null_free, + null_set +}; -static led_driver_t null_led_driver_ins = { null_init, null_free, null_set }; led_driver_t *null_led_driver = &null_led_driver_ins; + diff --git a/led/rpi_led_driver.c b/led/rpi_led_driver.c index 9f59b5bb8e..ae39bb2769 100644 --- a/led/rpi_led_driver.c +++ b/led/rpi_led_driver.c @@ -1,14 +1,29 @@ +/* RetroArch - A frontend for libretro. + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + #include + #include "led_driver.h" #include "led_defines.h" -#include "configuration.h" -#include "verbosity.h" +#include "../configuration.h" +#include "../verbosity.h" typedef struct { - int setup[MAX_LEDS]; - int map[MAX_LEDS]; + int setup[MAX_LEDS]; + int map[MAX_LEDS]; } rpiled_t; static rpiled_t curins; @@ -16,92 +31,116 @@ static rpiled_t *cur = &curins; static void rpi_init(void) { - int i; - settings_t *settings = config_get_ptr(); - RARCH_LOG("[LED]: rpi LED driver init\n"); - for(i=0;isetup[i] = 0; - cur->map[i] = settings->uints.led_map[i]; - RARCH_LOG("[LED]: rpi map[%d]=%d\n",i,cur->map[i]); - } + int i; + settings_t *settings = config_get_ptr(); + + if (!settings) + return; + + for(i = 0; i < MAX_LEDS; i++) + { + cur->setup[i] = 0; + cur->map[i] = settings->uints.led_map[i]; + RARCH_LOG("[LED]: rpi map[%d]=%d\n",i,cur->map[i]); + } } + static void rpi_free(void) { - RARCH_LOG("[LED]: rpi LED driver free\n"); } static int set_gpio(int gpio,int value) { - FILE *fp; - char buf[256]; - sprintf(buf,"/sys/class/gpio/%d/value",gpio); - fp = fopen(buf,"w"); - if(fp == NULL) - { - RARCH_WARN("[LED]: failed to set GPIO %d\n",gpio); - return -1; - } - fprintf(fp,"%d\n",value?1:0); - fclose(fp); - return 1; + FILE *fp; + char buf[256]; + snprintf(buf, sizeof(buf), "/sys/class/gpio/%d/value", gpio); + fp = fopen(buf,"w"); + + if(!fp) + { + RARCH_WARN("[LED]: failed to set GPIO %d\n",gpio); + return -1; + } + + fprintf(fp,"%d\n",value?1:0); + fclose(fp); + return 1; } +static int setup_gpio(int gpio) +{ + FILE *fp; + char buf[256]; + snprintf(buf, sizeof(buf), "/sys/class/gpio/%d/direction", gpio); + fp = fopen(buf,"w"); -static int setup_gpio(int gpio) { - FILE *fp; - char buf[256]; - sprintf(buf,"/sys/class/gpio/%d/direction",gpio); - fp = fopen(buf,"w"); - if(fp == NULL) { - sprintf(buf,"/sys/class/gpio/export"); - fp = fopen(buf,"w"); - if(fp == NULL) - { - RARCH_WARN("[LED]: failed to export GPIO %d\n",gpio); - return -1; - } - fprintf(fp,"%d\n",gpio); - fclose(fp); + if(!fp) + { + snprintf(buf, sizeof(buf), "/sys/class/gpio/export"); + fp = fopen(buf,"w"); + + if(!fp) + { + RARCH_WARN("[LED]: failed to export GPIO %d\n",gpio); + return -1; + } + + fprintf(fp,"%d\n",gpio); + fclose(fp); + + snprintf(buf, sizeof(buf), "/sys/class/gpio/%d/direction",gpio); + fp = fopen(buf,"w"); + } + + if(!fp) + { + RARCH_WARN("[LED]: failed to set direction GPIO %d\n", + gpio); + return -1; + } + + fprintf(fp,"out\n"); + fclose(fp); + return 1; - sprintf(buf,"/sys/class/gpio/%d/direction",gpio); - fp = fopen(buf,"w"); - } - if(fp == NULL) - { - RARCH_WARN("[LED]: failed to set direction GPIO %d\n",gpio); - return -1; - } - fprintf(fp,"out\n"); - fclose(fp); - return 1; - } static void rpi_set(int led,int state) { - int gpio = 0; - if((led < 0) || (led >= MAX_LEDS)) - { - RARCH_WARN("[LED]: invalid led %d\n",led); - return; - } - gpio = cur->map[led]; - if(gpio <= 0) return; + int gpio = 0; - if(cur->setup[led]==0) - { - RARCH_LOG("[LED]: rpi setup led %d gpio %d\n",led,gpio,state); - cur->setup[led] = setup_gpio(gpio); - if(cur->setup[led] <= 0) - { - RARCH_WARN("[LED]: failed to setup led %d gpio %d\n",led,gpio); - } - } - if(cur->setup[led] > 0) - { - RARCH_LOG("[LED]: rpi LED driver set led %d gpio %d = %d\n",led,gpio,state); - set_gpio(gpio,state); - } + if((led < 0) || (led >= MAX_LEDS)) + { + RARCH_WARN("[LED]: invalid led %d\n",led); + return; + } + + gpio = cur->map[led]; + if(gpio <= 0) + return; + + if(cur->setup[led]==0) + { + RARCH_LOG("[LED]: rpi setup led %d gpio %d\n", + led, gpio, state); + cur->setup[led] = setup_gpio(gpio); + if(cur->setup[led] <= 0) + { + RARCH_WARN("[LED]: failed to setup led %d gpio %d\n", + led, gpio); + } + } + if(cur->setup[led] > 0) + { + RARCH_LOG("[LED]: rpi LED driver set led %d gpio %d = %d\n", + led, gpio, state); + set_gpio(gpio,state); + } } -static led_driver_t rpi_led_driver_ins = { rpi_init, rpi_free, rpi_set }; +static led_driver_t rpi_led_driver_ins = { + rpi_init, + rpi_free, + rpi_set +}; + led_driver_t *rpi_led_driver = &rpi_led_driver_ins;