-
Notifications
You must be signed in to change notification settings - Fork 1
/
circuitpython.py
203 lines (147 loc) · 5.15 KB
/
circuitpython.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import board
import busio
import digitalio
import usb_midi
import time
import random
from spi_device import SPIDevice
midi_out = usb_midi.ports[1]
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
uart = busio.UART(board.TX, board.RX, baudrate=57600, timeout=0.01)
_spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi = SPIDevice(_spi, digitalio.DigitalInOut(board.D9), phase=1, polarity=0, baudrate=8000000)
def process_serial():
line = uart.readline()
if line is not None:
led.value = True
line = line.decode('ascii')
print(line, end='')
led.value = False
def _wait_for_byte(io, byte, timeout=10000000):
start = time.monotonic_ns()
buf = bytearray(1)
while True:
if time.monotonic_ns() - start > timeout:
return False
io.readinto(buf)
if buf[0] == byte:
return True
class HostessSpiCommands:
START_REQUEST = 0x81
REQUEST_MIDI_READ_EVENT = 0x83
REQUEST_MIDI_WRITE_EVENT = 0x84
REQUEST_KB_STRING = 0x85
REQUEST_KB_EVENT = 0x86
START_RESPONSE = 0xA1
RESPONSE_EMPTY = 0xA2
RESPONSE_MIDI_READ_EVENT = 0xA3
RESPONSE_MIDI_WRITE_EVENT = 0xA4
RESPONSE_KB_STRING = 0xA5
RESPONSE_KB_EVENT = 0xA6
class HostessMidiPortIn:
def __init__(self, spi):
self.spi = spi
def read(self):
with self.spi as io:
# Read MIDI byte command
io.write(bytearray([
HostessSpiCommands.START_REQUEST,
HostessSpiCommands.REQUEST_MIDI_READ_EVENT
]))
# Wait for response byte
if not _wait_for_byte(io, HostessSpiCommands.START_RESPONSE):
print("timeout")
return None
spi_result = bytearray(8)
io.readinto(spi_result)
if spi_result[0] == HostessSpiCommands.RESPONSE_EMPTY:
return None
if spi_result[0] != HostessSpiCommands.RESPONSE_MIDI_READ_EVENT:
print("Got unexpected response")
return None
if spi_result[1] != 0:
return spi_result[1:]
print(spi_result)
return None
def write(self, data):
with self.spi as io:
# Write MIDI byte command
io.write(bytearray([
HostessSpiCommands.START_REQUEST,
HostessSpiCommands.REQUEST_MIDI_WRITE_EVENT
]) + data)
# Wait for response byte
if not _wait_for_byte(io, HostessSpiCommands.START_RESPONSE):
print("Didn't get start response")
return False
# Wait for response byte
if not _wait_for_byte(io, HostessSpiCommands.RESPONSE_MIDI_WRITE_EVENT):
print("Didn't get ack")
return False
return True
class HostessKeyboard:
def __init__(self, spi):
self.spi = spi
def read(self):
with self.spi as io:
# Read MIDI byte command
io.write(bytearray([
HostessSpiCommands.START_REQUEST,
HostessSpiCommands.REQUEST_KB_STRING,
]))
# Wait for response byte
if not _wait_for_byte(io, HostessSpiCommands.START_RESPONSE):
return None
spi_result = bytearray(2)
io.readinto(spi_result)
if spi_result[0] == HostessSpiCommands.RESPONSE_EMPTY:
return None
if spi_result[0] != HostessSpiCommands.RESPONSE_KB_STRING:
print("Got unexpected response")
return None
return spi_result[1]
return None
class HostessKeyboardEvents:
def __init__(self, spi):
self.spi = spi
def read(self):
with self.spi as io:
# Read MIDI byte command
io.write(bytearray([
HostessSpiCommands.START_REQUEST,
HostessSpiCommands.REQUEST_KB_EVENT,
]))
# Wait for response byte
if not _wait_for_byte(io, HostessSpiCommands.START_RESPONSE):
return None
spi_result = bytearray(4)
io.readinto(spi_result)
if spi_result[0] == HostessSpiCommands.RESPONSE_EMPTY:
return None
if spi_result[0] != HostessSpiCommands.RESPONSE_KB_EVENT:
print("Got unexpected response")
return None
return spi_result[1:]
return None
hostess_midi_in = HostessMidiPortIn(spi)
hostess_kb = HostessKeyboard(spi)
hostess_kb_events = HostessKeyboardEvents(spi)
upstream_midi_out = usb_midi.ports[1]
while True:
#process_serial()
### MIDI IN test
midi_event = hostess_midi_in.read()
if midi_event is not None:
# Send the MIDI event to the computer
# over this Feather's USB MIDI connection
upstream_midi_out.write(midi_event[1:])
print(midi_event)
### Keyboard event test
# event = hostess_kb_events.read()
# if event is not None:
# print(event)
### Keyboard string test
#char = hostess_kb.read()
#if char is not None:
#print(chr(char), end='')