Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Rotary encoder guru ?
#9
(09-14-2018, 03:29 AM)fedormil Wrote: Я использую
Code:
#!/usr/bin/env python3.5

# Key press detect code, uses gpio 27 and ground to a momentary switch. If pressed for more
# than 1sec but (less than 5) it will force a reboot.
# If pressed for more than 5 seconds it will force a shutdown
# Code taken from sample posted on the Foundation's forum
# by paulv >> Fri Jun 28, 2013 2:58 pm
# https://www.raspberrypi.org/forums/viewtopic.php?t=48455&p=379280
# Should be initiated in /etc/rc.local with following line:
# python /home/pi/tools/ButtonPress.py&


from time import sleep
import subprocess
import RPi.GPIO as GPIO

# GPIO channel 27 is on pin 13 of 40way connector
# with GND on pin14
CHANNEL = 15

GPIO.setmode(GPIO.BCM)
GPIO.setup(CHANNEL, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# setup the channel as input with a 50K Ohm pull up. A push button will ground the pin,
# creating a falling edge.


def system_action(CHANNEL):
   print('Button press = negative edge detected on channel %s'%CHANNEL)
   button_press_timer = 0
   while True:
           if (GPIO.input(CHANNEL) == False) : # while button is still pressed down
               button_press_timer += 1 # keep counting until button is released
           else: # button is released, figure out for how long
               if (button_press_timer > 5) : # pressed for > 5 seconds
                   print "long press > 5 : ", button_press_timer
                   # do what you need to do before halting
                   subprocess.call(['shutdown -h now "System halted by GPIO action" &'], shell=True)
               elif (button_press_timer > 1) : # press for > 1 < 5 seconds
                   print "short press > 1 < 5 : ", button_press_timer
                   # do what you need to do before a reboot
                   subprocess.call(['sudo reboot &'], shell=True)
        elif (button_press_timer > 0,1) : # press for > 0,1 < 1 seconds
                   print "short press > 0,1 < 1 : ", button_press_timer
                   # do what you need to do before a reboot
                   subprocess.call(["mpc", "toggle"], shell=False)
            break
               button_press_timer = 0
           sleep(1)

GPIO.add_event_detect(CHANNEL, GPIO.FALLING, callback=system_action, bouncetime=200)
# setup the thread, detect a falling edge on gpio and debounce it with 200mSec

# assume this is the main code...
try:
   while True:
       # do whatever
       # while "waiting" for falling edge on gpio
       sleep (2)

except KeyboardInterrupt:
     GPIO.setwarnings(False)
     GPIO.cleanup()  # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

Hi @fedormil,

"Я использую"

Please post in English so we can all understand your posts, otherwise I'll need to moderate the posts.

-Tim
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply


Messages In This Thread
Rotary encoder guru ? - by DRONE7 - 09-06-2018, 06:43 AM
RE: Rotary encoder guru ? - by AnnaBlume - 09-06-2018, 09:50 AM
RE: Rotary encoder guru ? - by AnnaBlume - 09-06-2018, 01:13 PM
RE: Rotary encoder guru ? - by Tim Curtis - 09-06-2018, 11:40 PM
RE: Rotary encoder guru ? - by Tim Curtis - 09-07-2018, 01:13 AM
RE: Rotary encoder guru ? - by DRONE7 - 09-07-2018, 03:18 AM
RE: Rotary encoder guru ? - by Tim Curtis - 09-07-2018, 03:42 AM
RE: Rotary encoder guru ? - by fedormil - 09-14-2018, 03:29 AM
RE: Rotary encoder guru ? - by Tim Curtis - 09-14-2018, 04:35 PM
RE: Rotary encoder guru ? - by fedormil - 09-14-2018, 07:01 PM
RE: Rotary encoder guru ? - by Zootalaws - 09-12-2020, 11:43 PM
RE: Rotary encoder guru ? - by Johnathan W. - 02-27-2024, 04:51 PM
RE: Rotary encoder guru ? - by v12nut - 03-03-2024, 12:06 PM
RE: Rotary encoder guru ? - by the_bertrum - 03-04-2024, 09:31 AM
RE: Rotary encoder guru ? - by Johnathan W. - 03-05-2024, 05:09 AM
RE: Rotary encoder guru ? - by the_bertrum - 03-05-2024, 07:51 AM
RE: Rotary encoder guru ? - by steve4star - 03-05-2024, 08:03 AM

Forum Jump: