Hi Ken, That code looks like it would work. If you want to have command line arguments, the int main(void) gets replaced by something like: int main(int argc, char** argv) and then you can inspect the argc value to see the number of arguments, and the text for each argument will be pointed to by the array of pointers argv. However, personally I find that once you start wanting more than the most basic command line, then it's sometimes better to bite the bullet and use a command line library, rather than manually try to read and parse the arguments. An easy-to-use one is called argp. Others may have different favorites. I'm curious what the reason is to use C though, if the bash script works. Python is preferable to C sometimes too. Depending on how much lily-guilding is needed, another feature could be some mapping between friendly names and GPIO names, in (say) a JSON file. So, then the command to switch on one relay could be: relay mylamp 1 That's incidentally easier in Python, because it's easy to read config files and write them, and so on. But if you've already got working code, it can be better to use that sometimes too.
________________________________ From: beagleboard@googlegroups.com <beagleboard@googlegroups.com> on behalf of KenUnix <ken.unix....@gmail.com> Sent: 09 April 2020 00:32 To: BeagleBoard <beagleboard@googlegroups.com> Subject: Re: [beagleboard] Re: 4 Relay relay Cape 'c' code Snabaz, If I want to simply control a relay 1. Tthis simple 'c' would work --Start Code-- #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { int fd = open( "/sys/class/gpio/gpio20/value", O_RDWR | O_CLOEXEC ); // Relay 1 if( fd < 0 ) { fprintf( stderr, "open: %m\n" ); exit(1); } char c = '0'; // 0 = off, 1 == on write( fd, &c, 1 ); close( fd ); return 0; } --End Code-- Being away from 'c' for some time I would like to add 2 command line arguments. Example command 1 20 where as command 1 30 would fail. 1. 0 or 1 for on or off 2. gpio # 20, 7,112 and 115. And validate input was 20, 7, 112 or 115. If you want more control on, off, state, label this bash script will do it --Start Code-- #!/bin/bash # # 4-7-2020 Ken # # cmd [state|label|on|off|in|out] gpio # # Example : cmd state 112 # if [ "$1" == "--help" ] then echo -e "\ngpio relay cape tool. 4-7-2020 KenUnix\n" echo "state Get state of gpio number" echo "label Display associated P number" echo "on Set relay # to on" echo "off Turn relay off" echo -e "out Set gpio to out\n" echo "Example: cmd status 115 Will display the state of gpio 115" echo -e " cmd on 20 Will turn relay 1 on for gpio 20\n" exit fi case $2 in 20|7|112|115) ;; *) echo "Invalid gpio $2" echo "Vaild numbers are 20, 7, 112 or 115" echo -e "Relay 1 20, relay 2 7, relay 3 112, relay 4 115\007" exit 1 ;; esac case $1 in "state") direction=`cat /sys/class/gpio/gpio$2/direction` echo -n "Direction $direction, State " state=`cat /sys/class/gpio/gpio$2/value` if [ "$state" == "0" ]; then echo "off"; fi if [ "$state" == "1" ]; then echo "on"; fi exit ;; "label") echo -n "Physical header pin number " cat /sys/class/gpio/gpio$2/label ; exit ;; "on") echo 1 >/sys/class/gpio/gpio$2/value exit ;; "off") echo 0 >/sys/class/gpio/gpio$2/value exit ;; "out") echo "out" > /sys/class/gpio/gpio$2/direction ; exit ;; "in") echo "in" > /sys/class/gpio/gpio$2/direction ; exit ;; *) echo -e "Invalid operation $1. Try cmd --help\007" ; exit 1 ;; esac --End Code-- Feel free to use the bash script. If you down load it don't forget to chmod 755 filename that way you can execute by ./filename Ken -- For more options, visit http://beagleboard.org/discuss --- You received this message because you are subscribed to the Google Groups "BeagleBoard" group. To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard+unsubscr...@googlegroups.com<mailto:beagleboard+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/4e171c14-b291-4954-a482-acb8cc4042c0%40googlegroups.com<https://groups.google.com/d/msgid/beagleboard/4e171c14-b291-4954-a482-acb8cc4042c0%40googlegroups.com?utm_medium=email&utm_source=footer>. -- For more options, visit http://beagleboard.org/discuss --- You received this message because you are subscribed to the Google Groups "BeagleBoard" group. To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/DB6P18901MB0214A34684727E0BCC6B483384C00%40DB6P18901MB0214.EURP189.PROD.OUTLOOK.COM.