summaryrefslogtreecommitdiff
path: root/echtgeld.py
diff options
context:
space:
mode:
Diffstat (limited to 'echtgeld.py')
-rw-r--r--echtgeld.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/echtgeld.py b/echtgeld.py
new file mode 100644
index 0000000..8e23124
--- /dev/null
+++ b/echtgeld.py
@@ -0,0 +1,56 @@
+import threading
+import RPi.GPIO as g
+
+INHIBIT_PIN=40
+MONEY_PIN=38
+AMOUNT_PER_PULSE=5
+TIMEOUT = 30
+
+g.setmode(g.BOARD)
+
+g.setup(INHIBIT_PIN, g.OUT)
+g.output(INHIBIT_PIN, True)
+
+g.setup(MONEY_PIN, g.IN, pull_up_down = g.PUD_UP)
+
+
+class MoneyTransaction:
+ def __init__(self, end_callback, add_money_callback) -> None:
+ self.end_callback = end_callback
+ self.add_money_callback = add_money_callback
+ self.lock = threading.Lock()
+ self.lock.acquire_lock(blocking=True)
+ self.timer = None
+ self.timer_lock = threading.Lock()
+
+ def __end_callback(self, _ = None):
+ print("End Callback")
+ self.end_callback()
+ g.output(INHIBIT_PIN, True) # Disable Bill Acceptor
+ try:
+ g.remove_event_detect(MONEY_PIN)
+ except Exception:
+ pass
+ with self.timer_lock:
+ if self.timer is not None:
+ self.timer.cancel()
+ self.timer = None
+ self.lock.release_lock()
+
+ def __add_money_callback(self, _ = None):
+ with self.timer_lock:
+ if self.timer is not None:
+ self.timer.cancel()
+ self.timer = threading.Timer(TIMEOUT, self.__end_callback) # create Timeout
+ self.timer.start()
+ self.add_money_callback(AMOUNT_PER_PULSE)
+
+ def start(self):
+ print("Start Transaction")
+ g.output(INHIBIT_PIN, False) # Enable Bill Acceptor
+ with self.timer_lock:
+ self.timer = threading.Timer(TIMEOUT, self.__end_callback) # create Timeout
+ self.timer.start()
+ g.add_event_detect(MONEY_PIN, g.FALLING, callback = self.__add_money_callback, bouncetime=130)
+ def stop(self):
+ self.__end_callback()