-
Notifications
You must be signed in to change notification settings - Fork 0
/
procedural_colorgame.py
117 lines (91 loc) · 2.24 KB
/
procedural_colorgame.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
from gpiozero import Button, RGBLED
from signal import pause
from collections import namedtuple
import rx
from rx import Observable
from rx import operators as ops
KEYPAD = [
["1", "2", "3"],
["4", "5", "6"],
["7", "8", "9"],
["*", "0", "#"]
]
ROW_PINS = [19, 16, 6, 20]
COL_PINS = [11, 25, 9]
led = RGBLED(red=4, green=14, blue=15)
# loading : red
led.color = (1, 0, 0)
def say_hello():
led.color = (1,1,1)
def say_goodbye():
led.color = (0,0,0)
comb = Button(3)
comb.when_pressed = say_hello
comb.when_released = say_goodbye
Row = namedtuple('Row', ['n'])
Col = namedtuple('Col', ['n'])
buttons = {}
def debug_pressed(v):
print(v)
def observeRowPins(pins):
def handle(observer, scheduler):
def _handle(b):
observer.on_next(Row(n=pins.index(b.pin.number)))
for pin in pins:
buttons[pin] = Button(pin)
buttons[pin].when_pressed = _handle
return handle
def observeColPins(pins):
def handle(observer, scheduler):
def _handle(b):
observer.on_next(Col(n=pins.index(b.pin.number)))
for pin in pins:
buttons[pin] = Button(pin)
buttons[pin].when_pressed = _handle
return handle
colStream = rx.create(observeColPins(COL_PINS))
rowStream = rx.create(observeRowPins(ROW_PINS))
#colStream.subscribe(on_next=lambda i: print(i))
#rowStream.subscribe(on_next=lambda i: print(i))
def findKey(xy):
row, col = xy
return KEYPAD[row.n][col.n]
keyStream = rowStream.pipe(
ops.join(
colStream,
lambda l: rx.timer(.01),
lambda r: rx.timer(.01)
),
ops.map(findKey),
)
def makeColor(i):
print(i)
if i == 1:
led.red = 1
if i == 2:
led.green = 1
if i == 3:
led.blue = 1
if i == 4:
led.red = 0.66
if i == 5:
led.green = 0.66
if i == 6:
led.blue = 0.66
if i == 7:
led.red = 0.33
if i == 8:
led.green = 0.33
if i == 9:
led.blue = 0.33
if i == 0:
led.green = 0
if i == "*":
led.red = 0
if i == "#":
led.blue = 0
# keyStream.subscribe(on_next=lambda i: print(i))
keyStream.subscribe(on_next=lambda i: makeColor(i))
# ready : green
led.color = (0, 1, 0)
pause()