/
retrocog.spin2
208 lines (154 loc) · 3.99 KB
/
retrocog.spin2
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
204
205
206
207
208
' A retromachine cog
' The purpose: to read/send and process data from RPi and Midi Shield
' v. 0.01 pik33@o2.pl
'
con
rxpin=32
txpin=33
midiin=29
midiout=30
srxpin=63
stxpin=62
baudrate=1920000
mbaudrate=31250
sbaudrate=2000000
var
long kbdfront,kbdtail
long mousefront,mousetail
long midifront,miditail
long serialfront,serialtail
long kbdbuf[32]
long mousebuf[128]
long midibuf[32]
byte serialbuf[32]
long serialstack[64]
pub dummy()
repeat
'this is the object and not a program
pub start :cog
cog:=cogspin(16,serialcog(),@serialstack)
return cog
pub serialcog()| rr, mrr, srr, b, mb, midireport, kbmreport
'start serial and midi
serial_start(rxpin, txpin, baudrate)
serial_start(midiin,midiout,mbaudrate)
serial_start(srxpin,stxpin,sbaudrate)
mb:=(-1)
b:=(-1)
mousefront:=0
mousetail:=0
kbdfront:=0
kbdtail:=0
midifront:=0
miditail:=0
serialfront:=0
serialtail:=0
long[$30]:=0
long[$34]:=0
long[$38]:=0
long[$3C]:=0
repeat
' repeat
rr:=rxcheck(rxpin)
mrr:=rxcheck(midiin)
srr:=rxcheck(srxpin)
' until rr>=0 || mrr>=0 || srr>=0
' midi. Must be >= $80 for a command
'$80-$8F - 3 bytes note off. note, velocity
'$90-$9F - 3 bytes note on, note, velocity
'$A0-$AF - 3 bytes poly aftertouch. note, value
'$B0-$BF - 3 bytes control change, controller-value,
'$C0-$CF - 2 bytes program change
'$D0-$DF - 2 bytes mono aftertouch
'$E0-$EF - 3 bytes pitch bend, $2000 - center
'$F0-$FF - 1 byte
if srr>=0
if serialfront<>((serialtail-1) //32)
serialbuf[serialfront]:=srr
serialfront+=1
serialfront:=serialfront // 32
if ((mrr>=$80) && (mrr<=$BF)) || ((mrr>=$E0) && (mrr<=$EF))
mb:=2
midireport:=mrr<<24
if (mrr>=$C0) && (mrr<=$DF)
mb:=1
midireport:=mrr<<24
if (mrr>=$F0) 'TODO: do something with SYSEX, now a workaround for Novation Impulse
mb:=4
midireport:=mrr<<24
if (mrr<$80) && (mb>0) && (mrr>=0)
mb--
if mb<3
midireport+=mrr<<(mb<<3)
if mb==0
if midifront<>((miditail-1) //32)
midibuf[midifront]:=midireport
midifront+=1
midifront:=midifront // 32
mb:=(-1)
'Keyboard and mouse
if (rr>=$80) && (rr<>$FF)
b:=3
kbmreport:=rr<<24
if (rr<$80) && (b>0) && (rr>=0)
b--
kbmreport+=rr<<(b<<3)
if (rr==$FF)
b:=0
if b==0
if (kbmreport +>= $80000000) && (kbmreport +< $87000000) 'mouse
if mousefront<>((mousetail-1) //128)
mousebuf[mousefront]:=kbmreport
mousefront+=1
mousefront:=mousefront // 128
b:=(-1)
if (kbmreport +>= $87000000) && (kbmreport +< $8a000000) 'keyboard
if kbdfront<>((kbdtail-1) //32)
kbdbuf[kbdfront]:=kbmreport
kbdfront+=1
kbdfront:=kbdfront // 32
b:=(-1)
if long[$30]==0
if kbdfront<>kbdtail
long[$30]:=kbdbuf[kbdtail]
kbdtail++
kbdtail:= kbdtail // 32
if long[$34]==0
if mousefront<>mousetail
long[$34]:=mousebuf[mousetail]
mousetail++
mousetail:= mousetail // 128
if long[$38]==0
if midifront<>miditail
long[$38]:=midibuf[miditail]
miditail++
miditail:= miditail // 32
if long[$3c]==0
if serialfront<>serialtail
byte[$3c]:=255
byte[$3D]:=serialbuf[serialtail]
serialtail++
serialtail:= serialtail // 32
''---------------------- Serial functions from jm_serial.spin2, modified
pub serial_start(rxpin, txpin, baud) | bitmode
bitmode := muldiv64(clkfreq, $1_0000, baud) & $FFFFFC00 ' set bit timing
bitmode |= 7 ' set bits (8)
pinstart(rxpin,P_HIGH_15K|P_ASYNC_RX,bitmode,0)
pinstart(txpin,P_ASYNC_TX|P_OE,bitmode,0)
pinhigh(rxpin)
pub rxcheck(pin) : rxbyte | check
'' Check for serial input
'' -- returns -1 if nothing available
rxbyte := -1
check := pinr(pin)
if (check)
rxbyte := rdpin(pin) >> 24
pub tx(pin,b)
'' Emit byte
wypin(pin, b)
txflush(pin)
pub txflush(pin) | check
'' Wait until last byte has finished
repeat
check := pinr(pin)
while (check == 0)