(LED code) Cleanups

This commit is contained in:
twinaphex 2017-12-27 21:17:47 +01:00
parent c8a134818a
commit eb62b610cb
5 changed files with 211 additions and 122 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __OUTPUT_DEFINES__H #ifndef __OUTPUT_DEFINES__H
#define __OUTPUT_DEFINES__H #define __OUTPUT_DEFINES__H

View File

@ -1,56 +1,58 @@
#include <stdio.h> /* RetroArch - A frontend for libretro.
#include "led_driver.h" *
#include "configuration.h" * RetroArch is free software: you can redistribute it and/or modify it under the terms
#include "verbosity.h" * 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 <http://www.gnu.org/licenses/>.
*/
extern led_driver_t *null_led_driver; #include <stdio.h>
#if HAVE_RPILED #include <string/stdstring.h>
extern led_driver_t *rpi_led_driver;
#endif #include "led_driver.h"
led_driver_t *current_led_driver = NULL; #include "../configuration.h"
#include "../verbosity.h"
static led_driver_t *current_led_driver = NULL;
bool led_driver_init(void) bool led_driver_init(void)
{ {
char *drivername = NULL; settings_t *settings = config_get_ptr();
settings_t *settings = config_get_ptr(); char *drivername = settings ? settings->arrays.led_driver : NULL;
drivername = settings->arrays.led_driver;
if(!drivername)
if(drivername == NULL) drivername = "null";
drivername = "null";
current_led_driver = null_led_driver;
#if HAVE_RPILED #if HAVE_RPILED
if(!strcmp("rpi",drivername)) if(string_is_equal("rpi", drivername))
{ current_led_driver = rpi_led_driver;
current_led_driver = rpi_led_driver;
}
else
#endif #endif
{
current_led_driver = null_led_driver;
}
RARCH_LOG("[LED]: LED driver = '%s' %p\n",drivername,current_led_driver); RARCH_LOG("[LED]: LED driver = '%s' %p\n",
drivername,current_led_driver);
if(current_led_driver != NULL)
{ if(current_led_driver)
(*current_led_driver->init)(); (*current_led_driver->init)();
}
return true;
return true;
} }
void led_driver_free(void) void led_driver_free(void)
{ {
if(current_led_driver != NULL) if(current_led_driver)
{ (*current_led_driver->free)();
(*current_led_driver->free)();
}
} }
void led_driver_set_led(int led,int value) void led_driver_set_led(int led,int value)
{ {
if(current_led_driver != NULL) if(current_led_driver)
{ (*current_led_driver->set_led)(led,value);
(*current_led_driver->set_led)(led,value);
}
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __LED_DRIVER__H #ifndef __LED_DRIVER__H
#define __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); void led_driver_set_led(int led,int value);
extern led_driver_t *null_led_driver;
extern led_driver_t *rpi_led_driver;
#endif #endif

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "led_driver.h" #include "led_driver.h"
#include "verbosity.h" #include "../verbosity.h"
static void null_init(void) static void null_init(void) { }
{ static void null_free(void) { }
RARCH_LOG("[LED]: using null LED driver\n"); static void null_set(int led,int state) { }
}
static void null_free(void) {} static led_driver_t null_led_driver_ins = {
static void null_set(int led,int state) {} 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; led_driver_t *null_led_driver = &null_led_driver_ins;

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> #include <stdio.h>
#include "led_driver.h" #include "led_driver.h"
#include "led_defines.h" #include "led_defines.h"
#include "configuration.h" #include "../configuration.h"
#include "verbosity.h" #include "../verbosity.h"
typedef struct typedef struct
{ {
int setup[MAX_LEDS]; int setup[MAX_LEDS];
int map[MAX_LEDS]; int map[MAX_LEDS];
} rpiled_t; } rpiled_t;
static rpiled_t curins; static rpiled_t curins;
@ -16,92 +31,116 @@ static rpiled_t *cur = &curins;
static void rpi_init(void) static void rpi_init(void)
{ {
int i; int i;
settings_t *settings = config_get_ptr(); settings_t *settings = config_get_ptr();
RARCH_LOG("[LED]: rpi LED driver init\n");
for(i=0;i<MAX_LEDS;i++) { if (!settings)
cur->setup[i] = 0; return;
cur->map[i] = settings->uints.led_map[i];
RARCH_LOG("[LED]: rpi map[%d]=%d\n",i,cur->map[i]); 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) static void rpi_free(void)
{ {
RARCH_LOG("[LED]: rpi LED driver free\n");
} }
static int set_gpio(int gpio,int value) static int set_gpio(int gpio,int value)
{ {
FILE *fp; FILE *fp;
char buf[256]; char buf[256];
sprintf(buf,"/sys/class/gpio/%d/value",gpio); snprintf(buf, sizeof(buf), "/sys/class/gpio/%d/value", gpio);
fp = fopen(buf,"w"); fp = fopen(buf,"w");
if(fp == NULL)
{ if(!fp)
RARCH_WARN("[LED]: failed to set GPIO %d\n",gpio); {
return -1; RARCH_WARN("[LED]: failed to set GPIO %d\n",gpio);
} return -1;
fprintf(fp,"%d\n",value?1:0); }
fclose(fp);
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) { if(!fp)
FILE *fp; {
char buf[256]; snprintf(buf, sizeof(buf), "/sys/class/gpio/export");
sprintf(buf,"/sys/class/gpio/%d/direction",gpio); fp = fopen(buf,"w");
fp = fopen(buf,"w");
if(fp == NULL) { if(!fp)
sprintf(buf,"/sys/class/gpio/export"); {
fp = fopen(buf,"w"); RARCH_WARN("[LED]: failed to export GPIO %d\n",gpio);
if(fp == NULL) return -1;
{ }
RARCH_WARN("[LED]: failed to export GPIO %d\n",gpio);
return -1; fprintf(fp,"%d\n",gpio);
} fclose(fp);
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) static void rpi_set(int led,int state)
{ {
int gpio = 0; 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;
if(cur->setup[led]==0) if((led < 0) || (led >= MAX_LEDS))
{ {
RARCH_LOG("[LED]: rpi setup led %d gpio %d\n",led,gpio,state); RARCH_WARN("[LED]: invalid led %d\n",led);
cur->setup[led] = setup_gpio(gpio); return;
if(cur->setup[led] <= 0) }
{
RARCH_WARN("[LED]: failed to setup led %d gpio %d\n",led,gpio); gpio = cur->map[led];
} if(gpio <= 0)
} return;
if(cur->setup[led] > 0)
{ 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); 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; led_driver_t *rpi_led_driver = &rpi_led_driver_ins;