11/* global Scratch, jwArray */
2+ //added joystick support yay
23
34let jwArray = {
45 Type : class { } ,
@@ -33,6 +34,10 @@ class MIDI {
3334
3435 this . pitchBend = 0 ;
3536 this . modWheel = 0 ;
37+
38+ // MPK Mini Plus Joystick State
39+ this . joystickX = 64 ;
40+ this . joystickY = 64 ;
3641
3742 this . sequenceQueue = [ ] ;
3843
@@ -83,6 +88,10 @@ class MIDI {
8388
8489 { opcode : "getPitchBend" , blockType : Scratch . BlockType . REPORTER , text : "pitch bend" } ,
8590 { opcode : "getModWheel" , blockType : Scratch . BlockType . REPORTER , text : "mod wheel" } ,
91+
92+ // Joystick reporters
93+ { opcode : "getJoystickX" , blockType : Scratch . BlockType . REPORTER , text : "joystick tilt x" } ,
94+ { opcode : "getJoystickY" , blockType : Scratch . BlockType . REPORTER , text : "joystick tilt y" } ,
8695
8796 { opcode : "lastNNotesPressed" , blockType : Scratch . BlockType . REPORTER , text : "last [N] notes pressed" ,
8897 arguments : { N : { type : Scratch . ArgumentType . NUMBER , defaultValue : 5 } } , ...jwArray . Block } ,
@@ -130,6 +139,8 @@ class MIDI {
130139 this . lastChannel = - 1 ;
131140 this . lastPad = "" ;
132141 this . lastPadVelocity = 0 ;
142+ this . joystickX = 64 ;
143+ this . joystickY = 64 ;
133144 this . updateVisualizer ( ) ;
134145 }
135146
@@ -145,8 +156,18 @@ class MIDI {
145156 const command = status & 0xf0 ;
146157 const channel = ( status & 0x0f ) + 1 ;
147158
148- if ( command === CONTROL_CHANGE && note === 1 ) this . modWheel = velocity ;
149- if ( command === PITCH_BEND ) this . pitchBend = ( ( velocity << 7 ) | note ) - 8192 ;
159+ if ( command === CONTROL_CHANGE ) {
160+ // Your MPK reported CC#002 and CC#012
161+ if ( note === 2 ) this . joystickX = velocity ;
162+ if ( note === 12 ) this . joystickY = velocity ;
163+
164+ // Standard Mod Wheel remains on CC 1
165+ if ( note === 1 ) this . modWheel = velocity ;
166+ }
167+
168+ if ( command === PITCH_BEND ) {
169+ this . pitchBend = ( ( velocity << 7 ) | note ) - 8192 ;
170+ }
150171
151172 const isPad = note >= PAD_NOTE_MIN && note <= PAD_NOTE_MAX ;
152173 const isNoteOn = command === MIDI_NOTE_ON && velocity > 0 ;
@@ -174,6 +195,12 @@ class MIDI {
174195 }
175196 }
176197
198+ // Boilerplate reporters
199+ getPitchBend ( ) { return this . pitchBend ; }
200+ getModWheel ( ) { return this . modWheel ; }
201+ getJoystickX ( ) { return this . joystickX ; }
202+ getJoystickY ( ) { return this . joystickY ; }
203+
177204 whenSequencePlayed ( args ) {
178205 const target = args . SEQUENCE . trim ( ) . split ( " " ) . map ( Number ) ;
179206 const qlen = this . sequenceQueue . length ;
@@ -199,9 +226,6 @@ class MIDI {
199226 return false ;
200227 }
201228
202- getPitchBend ( ) { return this . pitchBend ; }
203- getModWheel ( ) { return this . modWheel ; }
204-
205229 lastNNotesPressed ( args ) { return new jwArray . Type ( this . lastNNotes . slice ( - args . N ) ) ; }
206230
207231 noteHeldTime ( args ) {
@@ -247,4 +271,4 @@ class MIDI {
247271}
248272
249273Scratch . extensions . register ( new MIDI ( ) ) ;
250- // Howdy! I'm Flowey! Flowey the Flower!
274+ // but nobody came...
0 commit comments