LN-Beertap_pi/Button.py

229 lines
7.4 KiB
Python

import RPi.GPIO as GPIO
import subprocess, time, threading
import Tap, Beertap
BUTTON_GPIO = 16
LED_GPIO = 21
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LED_GPIO, GPIO.OUT) # Set pin mode as output
GPIO.output(LED_GPIO, GPIO.LOW) # Set pin to low(0V)
pwm = GPIO.PWM(LED_GPIO, 1000) # set Frequece to 1KHz
pwm.start(0) # Start PWM output, Duty Cycle = 0
counter = 0
manualTapping = False
tapping = False
led_breath = False
led_doubleblink = False
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def breath_led():
pwm.start(0)
GPIO.output(LED_GPIO,GPIO.HIGH)
while led_breath:
for dc in range(0, 101, 5): # Increase duty cycle: 0~100
pwm.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.015)
time.sleep(1.5)
for dc in range(100, -1, -5): # Decrease duty cycle: 100~0
pwm.ChangeDutyCycle(dc)
time.sleep(0.015)
def double_blink():
pwm.stop()
GPIO.output(LED_GPIO,GPIO.LOW)
while led_doubleblink:
GPIO.output(LED_GPIO,GPIO.HIGH)
time.sleep(0.10)
GPIO.output(LED_GPIO,GPIO.LOW)
time.sleep(0.10)
GPIO.output(LED_GPIO,GPIO.HIGH)
time.sleep(0.10)
GPIO.output(LED_GPIO,GPIO.LOW)
time.sleep(1.5)
def button_press(channel):
global counter
counter = counter + 1
GPIO.add_event_detect(BUTTON_GPIO, GPIO.RISING, callback = button_press, bouncetime = 200)
ledThread_doubleblink = StoppableThread(target=double_blink, daemon=True)
ledThread_breath = StoppableThread(target=breath_led, daemon=True)
def CheckStatus():
time.sleep(.1)
return subprocess.call(["systemctl", "is-active", "--quiet", "beertap"])
def LedSwitch(toggle):
if toggle:
pwm.stop()
GPIO.output(LED_GPIO,GPIO.HIGH)
else:
pwm.stop()
GPIO.output(LED_GPIO,GPIO.LOW)
def LedBreath(toggle):
global ledThread_doubleblink
global ledThread_breath
global led_breath
global led_doubleblink
if toggle:
if ledThread_doubleblink.is_alive():
led_doubleblink = False
ledThread_doubleblink.stop()
LedSwitch(False)
ledThread_doubleblink = StoppableThread(target=double_blink, daemon=True)
if not ledThread_breath.is_alive():
led_breath = True
ledThread_breath.start()
else:
if ledThread_breath.is_alive():
led_breath = False
ledThread_breath.stop()
LedSwitch(False)
ledThread_breath = StoppableThread(target=breath_led, daemon=True)
def LedDoubleBlink(toggle):
global ledThread_doubleblink
global ledThread_breath
global led_breath
global led_doubleblink
if toggle:
if ledThread_breath.is_alive():
led_breath = False
ledThread_breath.stop()
LedSwitch(False)
ledThread_breath = StoppableThread(target=breath_led, daemon=True)
if not ledThread_doubleblink.is_alive():
led_doubleblink = True
ledThread_doubleblink.start()
else:
if ledThread_doubleblink.is_alive():
led_doubleblink = False
ledThread_doubleblink.stop()
LedSwitch(False)
ledThread_doubleblink = StoppableThread(target=double_blink, daemon=True)
def WriteDuration(duration):
with open('track/TapDurations.txt','a',encoding = 'utf-8') as f:
f.write(str(duration)+"\n")
def ManualTapping():
global counter
global manualTapping
tapping=False
while manualTapping:
if counter == 1:
if not tapping:
LedDoubleBlink(False)
LedSwitch(True)
print("open")
Tap.OpenTapManually()
t0 = time.time()
tapping = True
elif counter >= 2:
t1 = time.time()
tappingDuration = t1-t0
WriteDuration(tappingDuration)
LedSwitch(False)
print("close\nTapping Duration: "+ str(tappingDuration))
Tap.CloseTapManually()
counter = 0
time.sleep(1)
LedBreath(True)
manualTapping = False
#tag_emulation = True
def main():
global counter
global manualTapping
manualtapping_thread = StoppableThread(target=ManualTapping, daemon=True)
while True:
time.sleep(.02)
if counter >= 1 and not manualTapping:
time.sleep(.8)
if counter == 1:
if(CheckStatus() != 0):
print("Start BeertapService")
subprocess.call(["sudo", "systemctl", "start", "beertap"])
LedBreath(False)
LedDoubleBlink(False)
# else:
# if not tag_emulation:
# print("BeertapService already running - Emulate Tag")
# # Hide QR
# # Show NFC Logo
# # Stop NFC.service
# # Start python tagemulation
# tag_emulation = True
# else:
# print("BeertapService already running - Show QR")
# # Hide NFC Logo
# # Show QR
# # Stop tagemulation
# # Start NFC-Service
# tag_emulation = False
elif counter == 2:
if CheckStatus() != 0:
LedSwitch(False)
print("Start manual Tapping")
LedDoubleBlink(True)
manualTapping = True
counter = 0
if manualtapping_thread.is_alive():
manualtapping_thread.stop()
else:
manualtapping_thread = StoppableThread(target=ManualTapping, daemon=True)
manualtapping_thread.start()
elif counter == 3:
print("Stop BeertapService")
if(CheckStatus() == 0):
Beertap.kill()
time.sleep(1)
subprocess.call(["sudo", "systemctl", "stop", "beertap"])
else:
print("Can't stop BeertapService - it isn't running")
time.sleep(1)
if(CheckStatus() != 0):
LedSwitch(False)
LedDoubleBlink(False)
LedBreath(True)
#elif counter > 3:
#print("more")
counter = 0
if (CheckStatus() == 0):
LedBreath(False)
LedDoubleBlink(False)
LedSwitch(False)
else:
LedBreath(True)
main_thread = StoppableThread(target=main)
main_thread.start()