This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
forked from FRC2714/NovationLaunchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchpad_mk2.py
96 lines (70 loc) · 1.96 KB
/
launchpad_mk2.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
import sys
import time
import asyncio
from threading import Thread
try:
import launchpad_py as launchpad
except ImportError:
try:
import launchpad
except ImportError:
sys.exit("ERROR: loading launchpad.py failed")
import random
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
from tornado.ioloop import PeriodicCallback
import tornado.web
class WSHandler(tornado.websocket.WebSocketHandler):
def initialize(self, lp):
self.lp = lp
def open(self):
self.callback = PeriodicCallback(self.send_hello, 1)
self.callback.start()
def send_hello(self):
events = self.lp.ButtonStateXY()
if events:
print("sending")
self.write_message(str(events[0]) + ":" + str(events[1]) + ":" + str(events[2]))
def on_message(self, message):
pass
def on_close(self):
self.callback.stop()
def main():
# 60, 23, 10
# some basic info
print( "\nRunning..." )
print( " - Python " + str( sys.version.split()[0] ) )
ledCodes = [[1,1,0,0,0,5,0,5,5],
[90,90,0,82,82,0,0,0,9],
[90,90,0,82,82,0,5,5,0],
[0,0,0,0,0,0,0,0,13],
[126,126,0,109,109,0,9,9,0],
[126,126,0,109,109,0,0,0,82],
[0,0,0,0,0,0,13,13,90],
[9,9,0,13,13,0,0,0,5],
[9,9,0,13,13,0,21,21,21]]
# create an instance
lp = launchpad.LaunchpadMk2()
# open the first Launchpad Mk2
if lp.Open( 0, "mk2" ):
print( " - Launchpad Mk2: OK" )
else:
print( " - Launchpad Mk2: ERROR" )
return
# Clear the buffer because the Launchpad remembers everything
lp.ButtonFlush()
for i in range(9):
for j in range(9):
lp.LedCtrlXYByCode(i, j, ledCodes[j][i])
# buttonThread = Thread(target = process_buttons, args = (lp, ))
# buttonThread.start()
application = tornado.web.Application([(r'/', WSHandler,{"lp": lp})],)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(5802)
tornado.ioloop.IOLoop.instance().start()
# close this instance
print( " - More to come, goodbye...\n" )
lp.Close()
if __name__ == '__main__':
main()