fetapi/tuitest.py
2015-05-18 14:52:20 +02:00

133 lines
3.4 KiB
Python

import RPi.GPIO as gpio
import time
import threading
class FeApPinConfiguration(object):
def __init__(self):
self.pin_nsa = 11
self.pin_nsi = 13
self.pin_gabelschalter = 15
self.pin_schauzeichen = 12
self.pin_wecker_enable = 16
self.pin_wecker_a = 18
self.pin_wecker_b = 19
class FeApUserInterface(object):
def __init__(self, pinconfig):
self.__pinconfig = pinconfig
gpio.setup(self.__pinconfig.pin_nsa, gpio.IN, gpio.PUD_UP)
gpio.setup(self.__pinconfig.pin_nsi, gpio.IN, gpio.PUD_UP)
gpio.setup(self.__pinconfig.pin_gabelschalter, gpio.IN, gpio.PUD_UP)
gpio.setup(self.__pinconfig.pin_schauzeichen, gpio.OUT)
gpio.setup(self.__pinconfig.pin_wecker_enable, gpio.OUT)
gpio.setup(self.__pinconfig.pin_wecker_a, gpio.OUT)
gpio.setup(self.__pinconfig.pin_wecker_b, gpio.OUT)
gpio.add_event_detect(self.__pinconfig.pin_nsa, gpio.BOTH,
self.__on_nsa_change, 20)
gpio.add_event_detect(self.__pinconfig.pin_nsi, gpio.FALLING,
self.__on_nsi_falling, 20)
gpio.add_event_detect(self.__pinconfig.pin_gabelschalter, gpio.BOTH,
self.__on_gabelschalter_change, 20)
#self.__nsa_up_time = 0
self.__nsi_cnt = 0
self.__weckt = False
self.__nummernschalter_callbacks = []
self.__gabelschalter_callbacks = []
def __on_nsa_change(self, pin):
nsastate = gpio.input(self.__pinconfig.pin_nsa)
if nsastate == 0:
#self.__nsa_up_time = time.time()
self.__nsi_cnt = 0
else:
for cb in self.__nummernschalter_callbacks:
cb(self.__nsi_cnt)
def __on_nsi_falling(self, pin):
#print 'nsi'
self.__nsi_cnt += 1
def __on_gabelschalter_change(self, pin):
gbstate = gpio.input(self.__pinconfig.pin_gabelschalter)
print 'gabelschalter:', gbstate
for cb in self.__gabelschalter_callbacks:
cb(gbstate)
def __wecker_thread(self):
while self.__weckt:
c = 0
gpio.output(self.__pinconfig.pin_wecker_enable, 1)
while c < 1000:
gpio.output(self.__pinconfig.pin_wecker_a, 1)
gpio.output(self.__pinconfig.pin_wecker_b, 0)
time.sleep(0.02)
gpio.output(self.__pinconfig.pin_wecker_a, 0)
gpio.output(self.__pinconfig.pin_wecker_b, 1)
time.sleep(0.02)
c += 40
print 'ring'
gpio.output(self.__pinconfig.pin_wecker_enable, 0)
print ''
time.sleep(4)
def add_gabelschalter_callback(self, cb):
self.__gabelschalter_callbacks.append(cb)
def add_nummernschalter_callback(self, cb):
self.__nummernschalter_callbacks.append(cb)
def set_wecker(self, enabled):
if enabled and not self.__weckt:
self.__weckt = True
t = threading.Thread(target=self.__wecker_thread)
t.start()
elif not enabled:
self.__weckt = False
def set_schauzeichen(self, enabled):
gpio.output(self.__pinconfig.pin_schauzeichen, 1 if enabled else 0)
if __name__ == '__main__':
gpio.setmode(gpio.BOARD)
pinconfig = FeApPinConfiguration()
t = FeApUserInterface(pinconfig)
def dailed(num):
print num
t.add_nummernschalter_callback(dailed)
t.set_schauzeichen(True)
time.sleep(0.5)
t.set_schauzeichen(False)
time.sleep(0.5)
t.set_schauzeichen(True)
time.sleep(0.5)
t.set_schauzeichen(False)
time.sleep(0.5)
t.set_schauzeichen(True)
time.sleep(0.5)
t.set_schauzeichen(False)
time.sleep(0.5)
t.set_schauzeichen(True)
time.sleep(0.5)
t.set_schauzeichen(False)
time.sleep(0.5)
t.set_wecker(True)
time.sleep(20)
t.set_wecker(False)
while True:
time.sleep(1)