Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import subprocess
- import time
- import pynma
- from pynma import PyNMA
- ledPin = 18
- buttonPin = 23
- holdTime = 2
- tapTime = 0.01
- nextInterval = 0.0
- lastId = 1
- doorapi = pynma.PyNMA("pynma_api_key_goes_here")
- def tap():
- GPIO.output(ledPin, GPIO.HIGH)
- doorapi.push("Door", "Doorbell", "Ding Dong!", "/home/pinkythepig/HA/Testfile.png")
- time.sleep(3)
- GPIO.output(ledPin, GPIO.LOW)
- def hold():
- GPIO.output(ledPin, GPIO.HIGH)
- doorapi.push("Door", "Doorbell", "Ding Dong! Button was held", "/home/pinkythepig/HA/Testfile.png")
- time.sleep(3)
- GPIO.output(ledPin, GPIO.LOW)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(ledPin, GPIO.OUT)
- GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.output(ledPin, GPIO.HIGH)
- prevButtonState = GPIO.input(buttonPin)
- prevTime = time.time()
- tapEnable = False
- holdEnable = False
- while(True):
- time.sleep(0.15)
- # Poll current button state and time
- buttonState = GPIO.input(buttonPin)
- t = time.time()
- # Has button state changed?
- if buttonState != prevButtonState:
- prevButtonState = buttonState # Yes, save new state/time
- prevTime = t
- else: # Button state unchanged
- if (t - prevTime) >= holdTime: # Button held more than 'holdTime'?
- # Yes it has. Is the hold action as-yet untriggered?
- if holdEnable == True: # Yep!
- hold() # Perform hold action (usu. shutdown)
- holdEnable = False # 1 shot...don't repeat hold action
- tapEnable = False # Don't do tap action on release
- elif (t - prevTime) >= tapTime: # Not holdTime. tapTime elapsed?
- # Yes. Debounced press or release...
- if buttonState == True: # Button released?
- if tapEnable == True: # Ignore if prior hold()
- tap() # Tap triggered (button released)
- tapEnable = False # Disable tap and hold
- holdEnable = False
- else: # Button pressed
- tapEnable = True # Enable tap and hold actions
- holdEnable = True
- # LED blinks while idle, for a brief interval every 2 seconds.
- # Pin 18 is PWM-capable and a "sleep throb" would be nice, but
- # the PWM-related library is a hassle for average users to install
- # right now. Might return to this later when it's more accessible.
- if ((int(t) & 1) == 0) and ((t - int(t)) < 0.15):
- GPIO.output(ledPin, GPIO.HIGH)
- else:
- GPIO.output(ledPin, GPIO.LOW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement