]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_gpio.c
reset optind before executing command
[z180-stamp.git] / avr / cmd_gpio.c
index 8f50ca3a2cfd9c59fd2a4bd86e9e93534e0ec599..549654fdf1c6e24da59512d87bff73ab2d71ba11 100644 (file)
 //#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,17 +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;
-
        int opt;
        while ((opt = getopt(argc, argv, PSTR("s"))) != -1) {
                switch (opt) {
@@ -198,10 +187,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 +200,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 +226,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;
        }