]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
Add pin_alias
authorLeo C <erbl259-lmu@yahoo.de>
Wed, 15 Oct 2014 19:20:41 +0000 (21:20 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Wed, 15 Oct 2014 19:20:41 +0000 (21:20 +0200)
avr/cmd_boot.c
avr/main.c
include/config.h

index 26855f1e6099e7858f0c6a8136417f22d198e5e6..1213365ee91eefa60fffaeeee029d3c901f4725e 100644 (file)
@@ -6,11 +6,13 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <ctype.h>
+#include <string.h>
 #include <util/delay.h>
 #include <avr/pgmspace.h>
 
 #include "command.h"
 #include "getopt-min.h"
+#include "env.h"
 #include "z80-if.h"
 #include "pin.h"
 #include "debug.h"
@@ -268,7 +270,41 @@ command_ret_t do_clock2(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]
 }
 #endif
 
-// {INPUT, INPUT_PULLUP, OUTPUT, OUTPUT_TIMER} pinmode_t;
+
+static const int namestr = PIN_MAX;
+static char *pin_names[PIN_MAX+1];
+static uint_least8_t pin_names_width;
+
+void pinnames_get(void)
+{
+       static const FLASH char delim1[] = {":= "};
+       static const FLASH char delim2[] = {", "};
+       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;
+
+       if ((lp = getenv(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)) < PIN_MAX) &&
+                                       lp != ptr &&
+                                       (ptr = strtok_P(NULL, delim2)) != NULL ) {
+                               pin_names[i] = ptr;
+                               ptr = strtok_P(NULL, delim1);
+                       }
+               }
+
+               for (i = 0; i < PIN_MAX; i++)
+                       if (strlen(pin_names[i]) > pin_names_width)
+                               pin_names_width = strlen(pin_names[i]);
+       }
+}
 
 
 static void print_blanks(uint_fast8_t count)
@@ -277,6 +313,14 @@ static void print_blanks(uint_fast8_t count)
                putchar(' ');
 }
 
+static int xstrlen(char *s)
+{
+       if (s == NULL)
+               return 0;
+       else
+               return strlen(s);
+}
+
 static const FLASH char * const FLASH pinconf_str[] = {
                        FSTR("?"),
                        FSTR("Input"),
@@ -305,16 +349,20 @@ int print_pin(int pin, int multi)
                levelp = pinlevel_str[pin_read(pin)];
 
        if (multi) {
-               printf_P(PSTR("%3d  "), pin);
+               printf_P(PSTR("%3d "), pin);
+               if (pin_names_width) {
+                       printf_P(PSTR("%s "), pin_names[pin]);
+                       print_blanks(pin_names_width - xstrlen(pin_names[pin]));
+               }
                my_puts_P(pinconf_str[pinconf]);
-               print_blanks(8 - strlen_P(pinconf_str[pinconf]));
+               print_blanks(7 - strlen_P(pinconf_str[pinconf]));
                my_puts_P(levelp);
-               print_blanks(6 - strlen_P(levelp));
+               print_blanks(5 - strlen_P(levelp));
                if (pinconf == OUTPUT_TIMER)
-                       printf_P(PSTR("%7ld   %8ld"), 
+                       printf_P(PSTR("%8ld  %8ld"),
                                div, F_CPU/div);
        } else {
-               printf_P(PSTR("Pin %d: "), pin);
+               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(levelp);
@@ -409,11 +457,13 @@ command_ret_t do_pin(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
        /* remaining arguments */
        argc -= optind;
 
-       if (argc == 0)
+       pinnames_get();
+
+       if (argc == 0) {
                /* print cofig of all pins */
                for (pinargc = 0; pinargc < PIN_MAX; pinargc++)
                        pinarg[pinargc] = pinargc;
-       else {
+       else {
                /* get first arg */
                pinargc = pinarg_get(argv[optind++], pinarg);
                if (pinargc == 0)
@@ -427,9 +477,21 @@ command_ret_t do_pin(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
                if (pinargc == 1)
                        print_pin(pinarg[0], 0);
                else {
-                       if (printheader)
-                               printf_P(PSTR("Pin  Config  Level  Divider  Frequency/Hz\n"
-                                       "-----------------------------------------\n"));
+                       if (printheader) {
+                               if (pin_names_width > 0) {
+                                       if ( strlen("Name") > pin_names_width)
+                                               pin_names_width = strlen("Name");
+                                       char s[pin_names_width+1];
+                                       memset(s, ' ', pin_names_width);
+                                       s[pin_names_width] = '\0';
+                                       strncpy_P(s, PSTR("Name"), 4);
+                                       printf_P(PSTR("Pin %s Config Level Divider  Frequency/Hz\n"),s);
+                                       memset(s, '-', pin_names_width);
+                                       printf_P(PSTR("----%s-----------------------------------\n"), s);
+                               } else
+                                       printf_P(PSTR("Pin Config Level Divider  Frequency/Hz\n"
+                                                     "--------------------------------------\n"));
+                       }
                        for (int i = 0; i < pinargc; i++)
                                print_pin(pinarg[i], 1);
                }
index e7ebd5d30344192f2a92ea2c4d4078cfc3ab4d5c..ba8a6729c2612d01951c178aec27cde74d6721c3 100644 (file)
@@ -168,13 +168,13 @@ const char *bootdelay_process(void)
        char *s;
        int bootdelay;
 
-       bootdelay = (int) getenv_ulong(PSTR("bootdelay"), 10, CONFIG_BOOTDELAY);
+       bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
 
 
        debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
        _delay_ms(20);
 
-       s = getenv(PSTR("bootcmd"));
+       s = getenv(PSTR(ENV_BOOTCMD));
        stored_bootdelay = bootdelay;
        return s;
 }
@@ -212,7 +212,7 @@ int main(void)
        if (reset_reason_is_power_on())
                _delay_ms(CONFIG_PWRON_DELAY);
 
-       serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE));
+       serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
        sei();
 
 #if DEBUG
index 4093a6711bf0ee3bd1beffe119fa9730fc50289a..c0777a8e36deaa9327bb7a682dccc09ff3f5551b 100644 (file)
@@ -1,6 +1,13 @@
 #ifndef CONFIG_H               
 #define CONFIG_H
 
+/* Environment variables */
+
+#define ENV_BAUDRATE   "baudrate"
+#define ENV_BOOTDELAY  "bootdelay"
+#define ENV_BOOTCMD    "bootcmd"
+#define ENV_PINALIAS   "pin_alias"
+
 #define CONFIG_ENV_SIZE                1600
 #define CONFIG_ENV_OFFSET      0
 #define CONFIG_ENVVAR_MAX      20