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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/beagleboard/4e171c14-b291-4954-a482-acb8cc4042c0%40googlegroups.com.