package com.haxademic.sketch.hardware;

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

import dmxP512.DmxP512;

public class DmxUSBProTest
extends PAppletHax {
	public static void main(String args[]) { PAppletHax.main(Thread.currentThread().getStackTrace()[1].getClassName()); }
	
	DmxP512 dmx;
	
	// On Windows, port should be an actual serial port, and probably needs to be uppercase
	// On OS X, port will likely be a virtual serial port via USB, looking like "/dev/tty.usbserial-EN158815"
	// - To make this work, you need to install something like the Plugable driver: 
	// - https://plugable.com/2011/07/12/installing-a-usb-serial-adapter-on-mac-os-x/
	
	String DMXPRO_PORT = "DMXPRO_PORT";
	String DMXPRO_BAUDRATE = "DMXPRO_BAUDRATE";
	String DMXPRO_UNIVERSE_SIZE = "DMXPRO_UNIVERSE_SIZE";

	protected void overridePropsFile() {
		p.appConfig.setProperty(DMXPRO_PORT, "/dev/tty.usbserial-EN158815");
		p.appConfig.setProperty(DMXPRO_BAUDRATE, 115000);
		p.appConfig.setProperty(DMXPRO_UNIVERSE_SIZE, 128);
	}

	public void setupFirstFrame() {
		  dmx = new DmxP512(P.p, p.appConfig.getInt(DMXPRO_UNIVERSE_SIZE, 115000), false);
		  dmx.setupDmxPro(p.appConfig.getString(DMXPRO_PORT, "COM4"), p.appConfig.getInt(DMXPRO_BAUDRATE, 115000));
	}

	public void drawApp() {
		background(0);
		// color cycle
//		dmxOutput.set(1, round(127 + 127 * P.sin(p.frameCount * 0.2f)));
//		dmxOutput.set(2, round(127 + 127 * P.sin(p.frameCount * 0.08f)));
//		dmxOutput.set(3, round(127 + 127 * P.sin(p.frameCount * 0.02f)));
		// audio eq
		dmx.set(1, P.round(255 * p._audioInput.getFFT().spectrum[10]));
		dmx.set(2, P.round(255 * p._audioInput.getFFT().spectrum[20]));
		dmx.set(3, P.round(255 * p._audioInput.getFFT().spectrum[40]));
	}
}






@@ -407,16 +407,40 @@
 ppercase
+ - something like %22COM1%22
 %0A%09// On 
@@ -842,16 +842,58 @@
 E_SIZE%22;
+%0A%09%0A%09protected boolean audioActive = false;
 %0A%0A%09prote
@@ -999,114 +999,8 @@
 %22);%0A
-%09%09p.appConfig.setProperty(DMXPRO_BAUDRATE, 115000);%0A%09%09p.appConfig.setProperty(DMXPRO_UNIVERSE_SIZE, 128);%0A
 %09%7D%0A%0A
@@ -1101,21 +1101,18 @@
 _SIZE, 1
-15000
+28
 ), false
@@ -1173,17 +1173,17 @@
 RT, %22COM
-4
+1
 %22), p.ap
@@ -1276,44 +1276,55 @@
 ;%0A%09%09
-// color cycle%0A//%09%09dmxOutput
+if(audioActive) %7B%0A%09%09%09// audio eq%0A%09%09%09dmx
 .set(1, 
 roun
@@ -1323,75 +1323,236 @@
 (1, 
+P.
 round(
-127 + 127 * P.sin(p.frameCount * 0.2f)));%0A//%09%09dmxOutput
+255 * p._audioInput.getFFT().spectrum%5B10%5D));%0A%09%09%09dmx.set(2, P.round(255 * p._audioInput.getFFT().spectrum%5B20%5D));%0A%09%09%09dmx.set(3, P.round(255 * p._audioInput.getFFT().spectrum%5B40%5D));%0A%09%09%7D else %7B%0A%09%09%09// color cycle%0A%09%09%09dmx
 .set(
-2
+1
 , ro
@@ -1594,35 +1594,27 @@
 * 0.
-08
+2
 f)));%0A
-//
+%09
 %09%09dmx
-Output
 .set(
-3
+2
 , ro
@@ -1657,29 +1657,16 @@
  0.0
-2
+8
 f)));%0A%09
-%09// audio eq%0A
 %09%09dm
@@ -1675,194 +1675,164 @@
 set(
-1
+3
 , 
-P.
 round(
-255 * p._audioInput.getFFT().spectrum%5B10%5D
+127 + 127 * P.sin(p.frameCount * 0.02f
 ))
+)
 ;%0A%09%09
-dmx.set(2, P.round(255 * p._audioInput.getFFT().spectrum%5B20%5D));%0A%09%09dmx.set(3, P.round(255 * p._audioInput.getFFT().spectrum%5B40%5D))
+%7D%0A%09%7D%0A%09%0A%09public void keyPressed() %7B%0A%09%09super.keyPressed();%0A%09%09if(p.key == ' ') audioActive = !audioActive
 ;%0A%09%7D

Fix up DMX demo