summaryrefslogtreecommitdiff
path: root/avr/cmd_gpio.c
diff options
context:
space:
mode:
authorLeo C2018-07-22 17:24:28 +0200
committerLeo C2018-07-22 17:24:28 +0200
commit972e6e9bc05f0d3d868357029a6da2354f2cdb36 (patch)
treed8a1c1bc6a0016696050dc241761550e6b3be66c /avr/cmd_gpio.c
parent81a43faf77dc6b8aaa9f3b9cddb89dc1e9192ebd (diff)
downloadz180-stamp-972e6e9bc05f0d3d868357029a6da2354f2cdb36.zip
cmd_gpio.c: eliminate static vars, free heap after use.
Diffstat (limited to 'avr/cmd_gpio.c')
-rw-r--r--avr/cmd_gpio.c85
1 files changed, 41 insertions, 44 deletions
diff --git a/avr/cmd_gpio.c b/avr/cmd_gpio.c
index 8f50ca3..65a71be 100644
--- a/avr/cmd_gpio.c
+++ b/avr/cmd_gpio.c
@@ -14,42 +14,38 @@
//#include "debug.h"
-
-static const int namestr = GPIO_MAX;
-static char *pin_names[GPIO_MAX+1];
-static uint_least8_t pin_names_width;
-
-static void pinnames_get(void)
+static uint_fast8_t pinnames_get(char *pin_names[GPIO_MAX+1])
{
static const FLASH char delim1[] = {":= "};
static const FLASH char delim2[] = {", "};
+ uint_fast8_t width = 0;
char *lp;
- char *ptr;
uint_fast8_t i;
- if (pin_names[namestr] != NULL)
- free(pin_names[namestr]);
- memset(pin_names, 0, sizeof(pin_names));
- pin_names_width = 0;
+ memset(pin_names, 0, (GPIO_MAX+1) * sizeof(char *));
/* TODO: enters endless loop on wrong parameters */
- if ((lp = getenv_str(PSTR(ENV_PINALIAS))) != NULL) {
- pin_names[namestr] = strdup(lp);
- ptr = strtok_P(pin_names[namestr], delim1);
- while (ptr != NULL) {
- if (((i = strtoul(ptr, &lp, 10)) < GPIO_MAX) &&
- lp != ptr &&
- (ptr = strtok_P(NULL, delim2)) != NULL ) {
- pin_names[i] = ptr;
- ptr = strtok_P(NULL, delim1);
+ lp = getenv_str(PSTR(ENV_PINALIAS));
+ if (lp != NULL) {
+ pin_names[GPIO_MAX] = strdup(lp);
+ if (pin_names[GPIO_MAX] != NULL) {
+ char *ptr = strtok_P(pin_names[GPIO_MAX], delim1);
+ while (ptr != NULL) {
+ if (((i = strtoul(ptr, &lp, 10)) < GPIO_MAX) &&
+ lp != ptr &&
+ (ptr = strtok_P(NULL, delim2)) != NULL ) {
+ pin_names[i] = ptr;
+ ptr = strtok_P(NULL, delim1);
+ }
}
- }
- for (i = 0; i < GPIO_MAX; i++)
- if (strlen(pin_names[i]) > pin_names_width)
- pin_names_width = strlen(pin_names[i]);
+ for (i = 0; i < GPIO_MAX; i++)
+ if (strlen(pin_names[i]) > width)
+ width = strlen(pin_names[i]);
+ }
}
+ return width;
}
@@ -75,9 +71,9 @@ static const FLASH char * const FLASH pinlevel_str[] = {
FSTR(""),
};
-static int print_pin(int pin, int multi)
+static void print_pin(uint8_t pin, uint_fast8_t multi, char *pin_names[], uint_fast8_t pn_width)
{
- int pinconf;
+ gpiomode_t pinconf;
const FLASH char *levelp;
long div;
@@ -90,9 +86,9 @@ static int print_pin(int pin, int multi)
if (multi) {
printf_P(PSTR("%3d "), pin);
- if (pin_names_width) {
+ if (pn_width) {
printf_P(PSTR("%s "), pin_names[pin]);
- print_blanks(pin_names_width - xstrlen(pin_names[pin]));
+ print_blanks(pn_width - xstrlen(pin_names[pin]));
}
my_puts_P(pinconf_str[pinconf]);
print_blanks(7 - strlen_P(pinconf_str[pinconf]));
@@ -104,23 +100,21 @@ static int print_pin(int pin, int multi)
} else {
printf_P(PSTR("%d: \"%s\", "), pin, pin_names[pin] ? pin_names[pin] : 0);
my_puts_P(pinconf_str[pinconf]);
- printf_P(PSTR(", "));
+ my_puts_P(PSTR(", "));
my_puts_P(levelp);
if (pinconf == OUTPUT_TIMER)
printf_P(PSTR("divide by %ld (%ldHz)"),
div, F_CPU/div);
}
- printf_P(PSTR("\n"));
-
- return 0;
+ my_puts_P(PSTR("\n"));
}
-static int_fast8_t pinarg_insert(int pin, uint_fast8_t count, uint_fast8_t pinarg[])
+static int_fast8_t pinarg_insert(uint8_t pin, uint_fast8_t count, uint_fast8_t pinarg[])
{
uint_fast8_t pos;
- if (pin < 0 || pin >= GPIO_MAX)
+ if (pin >= GPIO_MAX)
return -1;
for (pos = 0; pos < count; pos++) {
@@ -173,14 +167,12 @@ static uint_fast8_t pinarg_get(char * arg, uint_fast8_t pinarg[])
}
-command_ret_t do_gpio(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char *const argv[])
+command_ret_t do_gpio(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char *const argv[])
{
- char printheader = 1;
+ uint_fast8_t printheader = 1;
uint_fast8_t pinarg[GPIO_MAX];
uint_fast8_t pinargc;
- (void) cmdtp; (void) flag;
-
/* reset getopt() */
optind = 0;
@@ -198,10 +190,8 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char *const
/* remaining arguments */
argc -= optind;
- pinnames_get();
-
if (argc == 0) {
- /* print cofig of all pins */
+ /* print config of all pins */
for (pinargc = 0; pinargc < GPIO_MAX; pinargc++)
pinarg[pinargc] = pinargc;
} else {
@@ -213,10 +203,15 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char *const
argc--;
}
+
if (argc == 0) {
- /* no more args, print config */
+ /* no more args, print pin config */
+
+ char *pin_names[GPIO_MAX + 1];
+ uint_fast8_t pin_names_width = pinnames_get(pin_names);
+
if (pinargc == 1)
- print_pin(pinarg[0], 0);
+ print_pin(pinarg[0], 0, pin_names, pin_names_width);
else {
if (printheader) {
if (pin_names_width > 0) {
@@ -234,8 +229,10 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char *const
"--------------------------------------\n"));
}
for (uint_fast8_t i = 0; i < pinargc; i++)
- print_pin(pinarg[i], 1);
+ print_pin(pinarg[i], 1, pin_names, pin_names_width);
}
+ if (pin_names[GPIO_MAX] != NULL)
+ free(pin_names[GPIO_MAX]);
return CMD_RET_SUCCESS;
}