WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: GPIO usage  (Read 1553 times)

Offline linvincible

  • WikiUser
  • *
  • Posts: 3
GPIO usage
« on: September 03, 2014, 01:57:38 PM »
Hello,
am new here and DID search before posting ;o)
but answers were above my level of understanding...
I use tinycore on a Raspberry Pi and added an IR card capable of starting and cutting the power to the pi.
it's from newbielink:http://www.msldigital.com/ [nonactive] and they provide sh scripts for other releases here newbielink:http://www.msldigital.com/pages/more-information/ [nonactive], that I tried
I get the error: "can't open '/sys/class/gpio/gpio22/value"
I guess the GPIO query doesn't work the same way than on the proposals...
How can I make it work?
Thanks in advance!
Cheers,
Charles

Offline linvincible

  • WikiUser
  • *
  • Posts: 3
Re: GPIO usage
« Reply #1 on: September 06, 2014, 11:04:40 AM »
I think my issue was with accessing the folders/files
new scripts sets ownership to all touched files to tc before each access and it works now
I'll post it after it's validated by the guys at msldigital.com

Offline linvincible

  • WikiUser
  • *
  • Posts: 3
Re: GPIO usage
« Reply #2 on: September 07, 2014, 08:20:49 AM »
script looks ok to the msldigital.com guys and I received a tip to use a graceful shutdown script included in the picoreplayer release
If it's not in the default tinycore just use your shutdown script
I tried with less sudo and chown commands but I really had to do them all for all the commands to be accepted!
script can be placed in tc''s home directory and called from .profile


#! /bin/sh
# 22 is the GPIO pin receiving the shutdown signal

sudo chown tc /sys/class/gpio/export
echo "22" > /sys/class/gpio/export

sudo chown tc /sys/class/gpio/gpio22/direction
sudo chown tc /sys/class/gpio/gpio22/value

echo "in" > /sys/class/gpio/gpio22/direction

while true; do
 sleep 1
 power=$(cat /sys/class/gpio/gpio22/value)
 if [ $power != 0 ]; then
    echo "out" > /sys/class/gpio/gpio22/direction
    echo "1" > /sys/class/gpio/gpio22/value
    sleep 5
  exitcheck.sh
 fi
done
« Last Edit: September 08, 2014, 12:22:40 AM by linvincible »

Offline ahhh

  • WikiUser
  • *
  • Posts: 1
Re: GPIO usage
« Reply #3 on: August 29, 2017, 12:37:47 AM »
1. THX
2. further example (mainly 4 me to record)
(don't forget to make executable by
$ chmod +x led.sh )

Code: newbielink:javascript:void(0); [nonactive]
#! /bin/sh
# The script sets an GPIO pin pnNr from cli to state for some secs (<10000) or unlimited
#
# Usage: $ ./led.sh pinNr [state [sec]]
# # Example: turn pin 24 ON
#       $ ./led.sh 24 1
#     or (STATE defaults to 1)
#       $ ./led.sh 24
# Example2: turn pin 24 OFF 10sec
#       $ ./led.sh 24 0 10

STATE="1" # default 1 vs 0
UNSTATE="0"

if [ "$2" != "" ] && [ $2 -eq 0 ]
then
        STATE="0"
        UNSTATE="1"
fi

if [ "$1" != "" ]
then
        if [ ! -d /sys/class/gpio/gpio$1 ]
        then
                sudo chown tc /sys/class/gpio/export
                echo "$1" > /sys/class/gpio/export
        fi

        if [ -d /sys/class/gpio/gpio$1 ]
        then
                sudo chown tc /sys/class/gpio/gpio$1/direction
                sudo chown tc /sys/class/gpio/gpio$1/value

                echo "out" > /sys/class/gpio/gpio$1/direction
                echo "$STATE" > /sys/class/gpio/gpio$1/value

                if [ "$3" != "" ] && [ $3 -gt 0 ] && [ $3 -lt 10000 ]
                then
                        sleep $3
                        echo "$UNSTATE" > /sys/class/gpio/gpio$1/value

                        if [ -d /sys/class/gpio/gpio$1 ]
                        then
                                sudo chown tc /sys/class/gpio/unexport
                                echo "$1" > /sys/class/gpio/unexport
                        fi
                fi
        fi
else
        echo $'Usage: \n$ ./led.sh pinNr [state [sec]]\nExample: turn pin 24 ON 10sec \n$ ./led.sh 24 1 10'
fi

((uups))
« Last Edit: August 29, 2017, 12:49:10 AM by ahhh »