-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyml_badge.py
172 lines (134 loc) · 4.67 KB
/
pyml_badge.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import ugfx
import badge
import wifi
import network
from time import sleep
import usocket as socket
state_map = {
'up': 0,
'down': 1,
'left': 2,
'right': 3,
'a': 4,
'b': 5,
'start': 8,
'select': 9,
}
states = [0 for _ in range(14)]
def handle_key(id, pressed):
states[id] = pressed
connection.send_key_states(states)
def handle_up(pressed):
handle_key(state_map['up'], int(pressed))
def handle_down(pressed):
handle_key(state_map['down'], int(pressed))
def handle_left(pressed):
handle_key(state_map['left'], int(pressed))
def handle_right(pressed):
handle_key(state_map['right'], int(pressed))
def connect_to_wifi(ssid='pymlbadge', password='pymlbadge'):
show_message("Waiting for wifi...")
wlan = network.WLAN(network.STA_IF)
if not wlan.active() or not wlan.isconnected():
wlan.active(True)
print('connecting to:', ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
sleep(0.1)
print('network config:', wlan.ifconfig())
show_message("Connected")
def init_badge():
badge.init()
ugfx.init()
wifi.init()
connect_to_wifi()
def show_message(message):
ugfx.clear(ugfx.WHITE)
ugfx.string(10, 10, message, "Roboto_Regular12", 0)
ugfx.flush()
class Connection:
def __init__(self, listen_port, control_addr, control_port):
self.uid = None
self.listen_port = listen_port
self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
self.listen_sock.setblocking(False)
# self.listen_sock.bind(('0.0.0.0', self.listen_port))
addr = socket.getaddrinfo('0.0.0.0', listen_port)
self.listen_sock.bind(addr[0][-1])
self.control_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
self.control_dest = []
while len(self.control_dest) == 0:
self.control_dest = socket.getaddrinfo(control_addr, control_port)
self.control_dest = self.control_dest[0][-1]
print("registering")
self.register()
def ready(self):
return self.uid is not None
def register(self):
command = '/controller/new/{port}'.format(port=self.listen_port)
try:
self.control_sock.sendto(command.encode('utf-8'), self.control_dest)
except Exception as ex:
print("failed to register controller: {}".format(ex))
def handle_read(self, data):
data = data.decode('utf-8')
if '/' not in data: # bad, malicous data!!
return
command, data = data.rsplit('/', 1)
if command.startswith('/uid'):
self.handle_uid(data)
elif command.startswith('/rumble'):
# self.handle_rumble(data)
pass
elif command.startswith('/message'):
# self.handle_message(data)
pass
elif command.startswith('/download'):
# self.handle_download(data)
pass
elif command.startswith('/play'):
# self.handle_play(data)
pass
def handle_uid(self, data):
self.uid = data
print("Got UID {}".format(data))
self.init_inputs()
def start_listening(self):
self.listening = True
self._listener_loop()
def stop_listening(self):
self.listening = False
def _listener_loop(self):
while self.listening:
try:
data, addr = self.listen_sock.recvfrom(1024)
self.handle_read(data)
except:
pass
sleep(0.01)
def init_inputs(self):
print("initializing input callbacks")
ugfx.input_init()
ugfx.input_attach(ugfx.JOY_UP, handle_up)
ugfx.input_attach(ugfx.JOY_DOWN, handle_down)
ugfx.input_attach(ugfx.JOY_LEFT, handle_left)
ugfx.input_attach(ugfx.JOY_RIGHT, handle_right)
ugfx.input_attach(ugfx.BTN_A, handle_up)
ugfx.input_attach(ugfx.BTN_B, handle_up)
ugfx.input_attach(ugfx.BTN_SELECT, handle_up)
ugfx.input_attach(ugfx.BTN_START, handle_up)
def ping(self):
command = '/controller/{uid}/ping/{port}'.format(
uid=self.uid,
port=self.port
)
socket.sendto(command.encode('utf-8'), self.control_dest)
def send_key_states(self, states):
command = '/controller/{uid}/states/{states}'.format(
uid=self.uid, states=''.join(map(str, states)))
self.listen_sock.sendto(command.encode('utf-8'), self.control_dest)
init_badge()
destination = 'control.ilexlux.xyz'
show_message("Connecting to {}".format(destination))
connection = Connection(1338, destination, 1338)
connection.start_listening()