package com.haxademic.sketch.hardware;

import com.haxademic.core.app.P;
import com.haxademic.core.app.PAppletHax;

import themidibus.MidiBus;

public class MidibusTest
extends PAppletHax {
	public static void main(String args[]) { PAppletHax.main(Thread.currentThread().getStackTrace()[1].getClassName()); }

	protected MidiBus midiBus;
	protected int MIDI_IN_INDEX = 0;
	protected int MIDI_OUT_INDEX = 4;

	protected void overridePropsFile() {
		 p.appConfig.setProperty( "midi_device_in", "Launch Control" );
	}

	public void setup() {
		super.setup();
		initMidi();
	}

	protected void initMidi() {
		MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
//		midiBus = new MidiBus(this, MIDI_IN_INDEX, MIDI_OUT_INDEX);
		midiBus = new MidiBus(this);
	}

	public void drawApp() {
		background(0);
	}

	/**
	 * PApplet-level listener for MIDIBUS noteOn call
	 */
	public void noteOn(int channel, int  pitch, int velocity) {
		P.println(channel, pitch, velocity);
		if( midi != null ) { 
			if( midi.midiNoteIsOn( pitch ) == 0 ) {
				midi.noteOn( channel, pitch, velocity );
				try{ 
					handleInput( true );
				}
				catch( ArrayIndexOutOfBoundsException e ){println("noteOn BROKE!");}
			}
		}
	}
	
	/**
	 * PApplet-level listener for MIDIBUS noteOff call
	 */
	public void noteOff(int channel, int  pitch, int velocity) {
		P.println(channel, pitch, velocity);
		if( midi != null ) midi.noteOff( channel, pitch, velocity );
	}
	
	/**
	 * PApplet-level listener for MIDIBUS CC signal
	 */
	public void controllerChange(int channel, int number, int value) {
		if( midi != null ) midi.controllerChange( channel, number, value );
		P.println(channel, number, value);
	}
	
	/**
	 * Test sending a signal out
	 */
	public void keyPressed() {
		super.keyPressed();
		if(p.key == ' ') {
			P.println("sending midi");
//			midiHandler.sendMidiOut(true, 1, 58, 127);
			p.midi.sendMidiOut(true, 1, 58, 127);
		}
	}

	
}
@@ -33,16 +33,43 @@
 dware;%0A%0A
+import java.util.HashMap;%0A%0A
 import c
@@ -426,16 +426,93 @@
 DEX = 4;
+%0A%09protected HashMap%3CString, Number%3E midiMap = new HashMap%3CString, Number%3E(); 
 %0A%0A%09prote
@@ -809,18 +809,16 @@
 d name.%0A
-//
 %09%09midiBu
@@ -871,16 +871,18 @@
 INDEX);%0A
+//
 %09%09midiBu
@@ -950,16 +950,243 @@
 und(0);%0A
+%09%09%0A%09%09// debug str%0A%09%09p.noStroke();%0A%09%09p.fill(255);%0A%09%09String debugStr = %22%22;%0A%09%09for (String key : midiMap.keySet()) %7B%0A%09%09%09debugStr += key + %22: %22 + midiMap.get(key) + %22%5Cn%22;%0A%09%09%7D%0A%09%09p.text(debugStr, 20, 20, p.width - 40, p.height - 40);%0A
 %09%7D%0A%0A%09/**
@@ -1305,33 +1305,35 @@
 ty) %7B%0A%09%09
-P.println
+midiMap.put
 (channel
 , pitch,
@@ -1316,34 +1316,37 @@
 iMap.put(channel
-, 
++%22-%22+
 pitch, velocity)
@@ -1714,33 +1714,35 @@
 ty) %7B%0A%09%09
-P.println
+midiMap.put
 (channel
 , pitch,
@@ -1729,26 +1729,29 @@
 .put(channel
-, 
++%22-%22+
 pitch, veloc
@@ -1951,16 +1951,58 @@
 alue) %7B%0A
+%09%09midiMap.put(channel+%22-%22+number, value);%0A
 %09%09if( mi
@@ -2067,45 +2067,8 @@
  );%0A
-%09%09P.println(channel, number, value);%0A
 %09%7D%0A%09

Add debug text on-screen