From 0ad54223e2fac6ba8da8510a6b7a574d278bc71b Mon Sep 17 00:00:00 2001 From: Byron Batteson Date: Wed, 14 Dec 2016 14:00:54 +0200 Subject: [PATCH] Added a method for reading the current mode of a given pin. --- gpio/gpio.c | 59 ++++++++++++++++++++++++++++++----------------------- gpio/readall.c | 13 ++++-------- wiringPi/wiringPi.c | 9 ++++++++ wiringPi/wiringPi.h | 1 + 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 6162090..0237613 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -70,6 +70,8 @@ char *usage = "Usage: gpio -v\n" " gpio export/edge/unexport ...\n" " gpio wfi \n" " gpio drive \n" + " gpio mode \n" + " gpio mode \n" " gpio pwm-bal/pwm-ms \n" " gpio pwmr \n" " gpio pwmc \n" @@ -678,6 +680,7 @@ static void doReset (char *progName) /* * doMode: * gpio mode pin mode ... + * or, gpio mode pin ********************************************************************************* */ @@ -686,37 +689,43 @@ void doMode (int argc, char *argv []) int pin ; char *mode ; - if (argc != 4) + if (argc > 4) { fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ; exit (1) ; + } else if(argc < 3) { + fprintf (stderr, "Usage: %s mode pin\n", argv [0]) ; + exit (1) ; } pin = atoi (argv [2]) ; - - mode = argv [3] ; - - /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; - else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ; - else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; - else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; - else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; - else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; - else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; - else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; - else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; - else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; - else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ; - else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ; - else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ; - else - { - fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; - exit (1) ; + if(argc == 4) { + mode = argv [3] ; + + /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ; + else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; + else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; + else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; + else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; + else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; + else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; + else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ; + else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ; + else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ; + else + { + fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; + exit (1) ; + } + } else { + printf("%s\n", getAltText(pin)); } } diff --git a/gpio/readall.c b/gpio/readall.c index cb7e18f..4f4c527 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -75,11 +75,6 @@ static void doReadallExternal (void) ********************************************************************************* */ -static char *alts [] = -{ - "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" -} ; - static int physToWpi [64] = { -1, // 0 @@ -177,7 +172,7 @@ static void readallPhys (int physPin) else pin = physToWpi [physPin] ; - printf (" | %4s", alts [getAlt (pin)]) ; + printf (" | %4s", getAltText(pin)) ; printf (" | %d", digitalRead (pin)) ; } @@ -201,7 +196,7 @@ static void readallPhys (int physPin) pin = physToWpi [physPin] ; printf (" | %d", digitalRead (pin)) ; - printf (" | %-4s", alts [getAlt (pin)]) ; + printf (" | %-4s", getAltText(pin)) ; } printf (" | %-5s", physNames [physPin]) ; @@ -233,11 +228,11 @@ static void allReadall (void) for (pin = 0 ; pin < 27 ; ++pin) { printf ("| %3d ", pin) ; - printf ("| %-4s ", alts [getAlt (pin)]) ; + printf ("| %-4s ", getAltText(pin)) ; printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ; printf ("| ") ; printf ("| %3d ", pin + 27) ; - printf ("| %-4s ", alts [getAlt (pin + 27)]) ; + printf ("| %-4s ", getAltText(pin + 27)) ; printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ; printf ("|\n") ; } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index dfd5de4..824528c 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1068,6 +1068,15 @@ int getAlt (int pin) return alt ; } +char* getAltText (int pin) +{ + static char *alts [] = { + "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" + }; + + return alts[getAlt (pin)]; +} + /* * pwmSetMode: diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index e11a0be..849e990 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -196,6 +196,7 @@ extern int wpiPinToGpio (int wpiPin) ; extern int physPinToGpio (int physPin) ; extern void setPadDrive (int group, int value) ; extern int getAlt (int pin) ; +extern const char* getAltText (int pin) ; extern void pwmToneWrite (int pin, int freq) ; extern void digitalWriteByte (int value) ; extern unsigned int digitalReadByte (void) ;