forked from jarret/raspi-uart-waveshare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_twisted_io.py
executable file
·157 lines (125 loc) · 4.41 KB
/
test_twisted_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
# Copyright (c) 2019 Jarret Dyrbye
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php
import time
import RPi.GPIO as GPIO
from twisted.internet import reactor, threads
from twisted.internet.task import LoopingCall
from waveshare.epaper import EPaper
from lib.invoicedisplay import InvoiceDisplay
from lib.selections import SELECTIONS
# the order of buttons to GPIO pin connection on my breadboard. YMMV
BUTTON_1 = 11
BUTTON_2 = 12
BUTTON_3 = 13
BUTTON_4 = 15
LED_1 = 16
LED_2 = 18
LED_3 = 19
LED_4 = 21
MAPPING = {BUTTON_1: LED_2,
BUTTON_2: LED_1,
BUTTON_3: LED_3,
BUTTON_4: LED_4}
STATE = {LED_1: None,
LED_2: None,
LED_3: None,
LED_4: None}
SELECTION_MAPPING = {BUTTON_1: SELECTIONS[0],
BUTTON_2: SELECTIONS[1],
BUTTON_3: SELECTIONS[2],
BUTTON_4: SELECTIONS[3]}
class ButtonDrive(object):
def __init__(self, button_event):
self.button_event = button_event
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED_1, GPIO.OUT)
GPIO.setup(LED_2, GPIO.OUT)
GPIO.setup(LED_3, GPIO.OUT)
GPIO.setup(LED_4, GPIO.OUT)
self.state = {LED_1: False,
LED_2: False,
LED_3: False,
LED_4: False}
GPIO.output(LED_1, GPIO.LOW)
GPIO.output(LED_2, GPIO.LOW)
GPIO.output(LED_3, GPIO.LOW)
GPIO.output(LED_4, GPIO.LOW)
def button(self, button_no):
print("button: %d" % button_no)
#l = MAPPING[button_no]
#if self.state[l]:
# GPIO.output(l, GPIO.LOW)
# self.state[l] = False
#else:
# GPIO.output(l, GPIO.HIGH)
# self.state[l] = True
reactor.callFromThread(self.button_event, button_no)
class ButtonEInkUI(object):
def __init__(self):
self.bd = ButtonDrive(self.button_event)
self.leds_on()
paper = EPaper()
self.display = InvoiceDisplay(paper, refresh_cb=self.refresh_cb)
self.drawing = False
self.blink = None
self.led_state = None
self.leds_off()
def button_thread(self):
GPIO.add_event_detect(BUTTON_1, GPIO.FALLING, callback=self.bd.button,
bouncetime=150)
GPIO.add_event_detect(BUTTON_2, GPIO.FALLING, callback=self.bd.button,
bouncetime=150)
GPIO.add_event_detect(BUTTON_3, GPIO.FALLING, callback=self.bd.button,
bouncetime=150)
GPIO.add_event_detect(BUTTON_4, GPIO.FALLING, callback=self.bd.button,
bouncetime=150)
def refresh_cb(self):
self.blink.stop()
self.leds_on()
print("ok, refreshing")
def leds_on(self):
GPIO.output(LED_1, GPIO.HIGH)
GPIO.output(LED_2, GPIO.HIGH)
GPIO.output(LED_3, GPIO.HIGH)
GPIO.output(LED_4, GPIO.HIGH)
self.led_state = True
def leds_off(self):
GPIO.output(LED_1, GPIO.LOW)
GPIO.output(LED_2, GPIO.LOW)
GPIO.output(LED_3, GPIO.LOW)
GPIO.output(LED_4, GPIO.LOW)
self.led_state = False
def leds_flip(self):
if self.led_state:
self.leds_off()
else:
self.leds_on()
def button_event(self, button):
if self.drawing:
print("already drawing, dropping on floor")
return
print("got button: %s" % button)
self.drawing = True
self.leds_on()
print("kicking off draw")
d = threads.deferToThread(self.display.draw_selection,
SELECTION_MAPPING[button])
d.addCallback(self.finish_drawing)
self.blink = LoopingCall(self.leds_flip)
self.blink.start(0.2, now=False)
def finish_drawing(self, result):
self.drawing = False
self.leds_off()
print("finished_drawing")
if __name__ == '__main__':
bei = ButtonEInkUI()
reactor.callInThread(bei.button_thread)
reactor.run()
print("cleaning up")
GPIO.cleanup()