User avatar
Sinisa
Posts: 5
Joined: Wed Jun 15, 2016 8:06 am
Location: Novi Sad, Serbia

Switch button on GPIO, short press / long press

Mon Feb 20, 2017 11:11 am

Hi,

I need to run one script file after short press of switch button and another one after long press.
GPIO pins that i'm using are 25 and 26.
I have managed to run this Python code and it works well for just one script:

Code: Select all

#!/bin/bash

exec > >(tee -a /home/pi/Skripta/log.txt)
exec 2>&1
echo Restart  `date` 
sudo python - <<END
import time
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_UP)
print "wait for 2 seconds"
buttonReleased = True
while buttonReleased:
    gpio.wait_for_edge(7, gpio.FALLING)
    buttonReleased = False
    for i in range(20):
        time.sleep(0.1)
        if gpio.input(7):
            buttonReleased = True
            break
gpio.cleanup()

END

echo `date`. reboot...
sudo sh pi/Desktop/script1.sh
How can i expand it for short/long press?
Signature

User avatar
thagrol
Posts: 2096
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Switch button on GPIO, short press / long press

Mon Feb 20, 2017 3:58 pm

I had a similar requirement and did it like this: (scroll down for example of using the class)

Code: Select all

#!/usr/bin/env python

import gpiozero
import time

class cmdbutton(object):

    def __init__(self, pin, press, hold):
        self.__button = gpiozero.Button(pin,
                                        pull_up=True,
                                        bounce_time=0.050,
                                        hold_time=2.0)
        self.__press = press
        self.__hold = hold
        self.__press_time = None
        self.__sw = switch

        self.__button.when_held = self.__on_hold
        self.__button.when_pressed = self.__on_press
        self.__button.when_released = self.__on_release
    
    def __on_press(self):
        # button pressed
        self.__press_time = time.time() - self.__button.active_time

    def __on_release(self):
        # short press
        if time.time() - self.__press_time < 0.5:
            os.system(self.__press)

    def __on_hold(self):
        # button held
        os.system(self.__hold)

    def close(self):
        self.__button.close()

if __name__ == '__main__':
    btn = cmdbutton(pin=25,
                    press='echo foo',
                    hold='echo bar')
    try:
        while True:
            time.sleep(1)
    finally:
        btn.close()
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

User avatar
B.Goode
Posts: 9014
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Switch button on GPIO, short press / long press

Mon Feb 20, 2017 4:16 pm

The gpiozero python library can help you do this.

See: viewtopic.php?f=32&t=152046

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Switch button on GPIO, short press / long press

Tue Feb 21, 2017 8:02 am

Sinisa wrote:Hi,

I need to run one script file after short press of switch button and another one after long press.
GPIO pins that i'm using are 25 and 26.
I have managed to run this Python code and it works well for just one script:

Code: Select all

#!/bin/bash

exec > >(tee -a /home/pi/Skripta/log.txt)
exec 2>&1
echo Restart  `date` 
sudo python - <<END
import time
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_UP)
print "wait for 2 seconds"
buttonReleased = True
while buttonReleased:
    gpio.wait_for_edge(7, gpio.FALLING)
    buttonReleased = False
    for i in range(20):
        time.sleep(0.1)
        if gpio.input(7):
            buttonReleased = True
            break
gpio.cleanup()

END

echo `date`. reboot...
sudo sh pi/Desktop/script1.sh
How can i expand it for short/long press?
Not sure where this code came from, although, yes it is python, the shebang on the first line isn't correct - see thagrols correct example.
https://en.wikipedia.org/wiki/Shebang_(Unix)

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
thagrol
Posts: 2096
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Switch button on GPIO, short press / long press

Tue Feb 21, 2017 1:45 pm

Hate to disagree with you texy, but the op's shebang is correct. That code is bash with embedded python (via a here document).

Seems a convoluted way of ding things to me. Especially when it could probably be done either in bash or python.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Switch button on GPIO, short press / long press

Tue Feb 21, 2017 2:08 pm

:oops:
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Sinisa
Posts: 5
Joined: Wed Jun 15, 2016 8:06 am
Location: Novi Sad, Serbia

Re: Switch button on GPIO, short press / long press

Fri Feb 24, 2017 9:07 am

Thank you for answers.
I didn't found a way to run it just from bash.
Could anyone give me an example for bash, please?
Signature

User avatar
Sinisa
Posts: 5
Joined: Wed Jun 15, 2016 8:06 am
Location: Novi Sad, Serbia

Re: Switch button on GPIO, short press / long press

Mon Mar 20, 2017 11:58 am

Code: Select all

X.Org X Server 1.17.2
Release Date: 2015-06-16
X Protocol Version 11, Revision 0
Build Operating System: Linux 3.8.11-2-ARCH armv7l Raspbian
Current Operating System: Linux raspberrypi 4.4.9-v7+ #884 SMP Fri May 6 17:28:59 BST 2016 armv7l
Kernel command line: 8250.nr_uarts=1 dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa02082 bcm2709.serial=0xf95611a3 smsc95xx.macaddr=B8:27:EB:56:11:A3 bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Build Date: 11 January 2016  01:32:44PM
xorg-server 2:1.17.2-1+rpi1 (http://www.debian.org/support)
Current version of pixman: 0.33.3
    Before reporting problems, check http://wiki.x.org
    to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
    (++) from command line, (!!) notice, (II) informational,
    (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.0.log", Time: Fri Mar 10 15:21:19 2017
(==) Using system config directory "/usr/share/X11/xorg.conf.d"
modprobe: FATAL: Module g2d_23 not found.
This is error that i get after short or long press in terminal. Could anyone help me solve this, please?
Signature

Return to “Beginners”