From fd09e7c0242e1793ccb8fc036ebbb3f16ed61ed5 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Tue, 2 Apr 2019 17:49:56 -0400 Subject: [PATCH] Add a processing sketch which connects to basic --- processing/mcopy/cfg.json | 152 +++++++++++++++++++++++++++ processing/mcopy/mcopy.pde | 205 +++++++++++++++++++++++++++++++++++++ scripts/build.sh | 3 +- 3 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 processing/mcopy/cfg.json create mode 100644 processing/mcopy/mcopy.pde diff --git a/processing/mcopy/cfg.json b/processing/mcopy/cfg.json new file mode 100644 index 0000000..85f19de --- /dev/null +++ b/processing/mcopy/cfg.json @@ -0,0 +1,152 @@ +{ + "version": "1.4.0", + "ext_port": 1111, + "profiles": { + "mcopy": { + "label": "Default mcopy profile", + "cam": { + "time": 850, + "delay": 50, + "momentary": 0 + }, + "proj": { + "time": 1400, + "delay": 50, + "momentary": 0 + }, + "black": { + "before": 0, + "after": 0 + } + }, + "jk103": { + "label": "JK103", + "cam": { + "time": 600, + "delay": 50, + "momentary": 240 + }, + "proj": { + "time": 950, + "delay": 50, + "momentary": 240 + }, + "black": { + "before": 0, + "after": 0 + }, + "light": false + }, + "jk_original": { + "label": "JK45 profile", + "cam": { + "time": 750, + "delay": 50, + "momentary": 300 + }, + "proj": { + "time": 1300, + "delay": 50, + "momentary": 300 + }, + "black": { + "before": 250, + "after": 250 + } + }, + "intval3": { + "label": "INTVAL3", + "cam": { + "time": 630, + "delay": 50, + "momentary": 0 + } + }, + "jk_mono": { + "label": "MONO's JK", + "cam": { + "time": 750, + "delay": 50, + "momentary": 300 + }, + "proj": { + "time": 1300, + "delay": 50, + "momentary": 300 + }, + "light": false, + "projector_second": true + } + }, + "cmd": { + "camera_forward": "CF", + "camera_backward": "CB", + "projector_forward": "PF", + "projector_backward": "PB", + "black_forward": "BF", + "black_backward": "BB", + "camera_second_forward": "C2F", + "camera_second_backward": "C2B", + "cameras_forward": "CCB", + "camera_forward_camera_second_backward": "CFCB", + "camera_backward_camera_second_forward": "CBCF", + "projector_second_forward": "P2F", + "projector_second_backward": "P2B", + "projectors_forward": "PPF", + "projectors_backward": "PPB", + "projector_forward_projector_second_backward": "PFPB", + "projector_backward_projector_second_forward": "PBPF" + }, + "arduino": { + "baud": 57600, + "board": "uno", + "serialDelay": 20, + "sequenceDelay": 100, + "cam": { + "time": 850, + "delay": 50, + "momentary": 300 + }, + "proj": { + "time": 1300, + "delay": 50, + "momentary": 300 + }, + "black": { + "before": 250, + "after": 250 + }, + "cmd": { + "debug": "d", + "connect": "i", + "light": "l", + "camera": "c", + "projector": "p", + "black": "b", + "camera_forward": "e", + "camera_backward": "f", + "projector_forward": "g", + "projector_backward": "h", + "projector_identifier": "j", + "camera_identifier": "k", + "mcopy_identifier": "m", + "camera_timed": "n", + "light_identifier": "o", + "projector_light_identifier": "q", + "projector_camera_light_identifier": "r", + "projector_camera_identifier": "s", + "projector_second_identifier": "t", + "projectors_identifier": "d", + "projector_second_forward": "u", + "projector_second_backward": "v", + "projector_second": "w", + "projectors": "x", + "camera_second_identifier": "y", + "cameras_identifier": "a", + "camera_second_forward": "1", + "camera_second_backward": "2", + "camera_second": "3", + "cameras": "4" + } + } +} diff --git a/processing/mcopy/mcopy.pde b/processing/mcopy/mcopy.pde new file mode 100644 index 0000000..77a84a8 --- /dev/null +++ b/processing/mcopy/mcopy.pde @@ -0,0 +1,205 @@ +import processing.serial.*; + +String CFG_FILE = "cfg.json"; +int BAUD = 56700; + +JSONObject cfg; +JSONObject cfgArduino; +JSONObject cmds; + +String cameraString = ""; +String projectorString = ""; + +// The serial port: +Serial connectPort; +Serial cameraPort; +Serial projectorPort; + +void configure () { + cfg = loadJSONObject(CFG_FILE); + cfgArduino = cfg.getJSONObject("arduino"); + cmds = cfgArduino.getJSONObject("cmd"); + BAUD = cfgArduino.getInt("baud"); + println("Using mcopy " + CFG_FILE + " version " + cfg.getString("version")); +} + +void connect () { + String[] ports = Serial.list(); + String[] matches = {}; + String connectCmd = cmds.getString("connect"); + String identifyCmd = cmds.getString("mcopy_identifier"); + boolean connected = false; + String deviceIdentifier; + + //println("Found serial ports:"); + //printArray(ports); + + for (int i = 0; i < ports.length; i++) { + if (ports[i].toLowerCase().indexOf("tty.") == -1) { + continue; + } + if (ports[i].toLowerCase().indexOf("arduino") > -1) { + matches = append(matches, ports[i]); + } else if (ports[i].toLowerCase().indexOf("usbserial") > -1) { + matches = append(matches, ports[i]); + } else if (ports[i].toLowerCase().indexOf("usbmodem") > -1) { + matches = append(matches, ports[i]); + } + } + + println("Found " + matches.length + " arduino device matches"); + printArray(matches); + + for (int i = 0; i < matches.length; i++) { + println("Connecting to matches["+i+"] "+ matches[i] +" at baud " + BAUD); + connectPort = new Serial(this, matches[i], BAUD); + + println("Waiting for connection..."); + delay(2000); + + println("Sending mcopy connect command " + connectCmd); + write(connectPort, connectCmd); + waitFor(connectPort); + connected = onConnect(); + if (connected) { + delay(1000); + println("Sending mcopy identifier command " + identifyCmd); + connectPort.write(identifyCmd); + waitFor(connectPort); + onIdentify(matches[i]); + delay(1000); + } + connectPort.stop(); + } + if (!cameraString.equals("")) { + connectCamera(); + } + if (!projectorString.equals("")) { + connectProjector(); + } + + onReady(); +} + +void waitFor (Serial serialPort) { + int limit = 5000; + int count = 0; + while (serialPort.available() == 0 || count >= limit) { + delay(1); + count++; + } + return; +} + +void write (Serial serialPort, String cmd) { + serialPort.write(cmd); + return; +} + +String read (Serial serialPort) { + String inBuffer = serialPort.readStringUntil(13); + if (inBuffer == null) { + return ""; + } + serialPort.clear(); + return inBuffer.trim(); +} + +boolean onConnect () { + boolean connected = false; + String res = read(connectPort); + String connectCmd = cmds.getString("connect"); + if (res.equals(connectCmd)) { + println("Firmware confirmed. " + res + " == " + connectCmd); + connected = true; + } else { + println("Device was not confirmed"); + } + delay(10); + return connected; +} + +/* +projector_identifier +camera_identifier +#mcopy_identifier +light_identifier +projector_light_identifier +projector_camera_light_identifier +projector_camera_identifier +projector_second_identifier +projectors_identifier +camera_second_identifier +*/ +boolean onIdentify (String serialString) { + String res = read(connectPort); + String device = ""; + boolean identified = true; + if (!res.equals("")) { + device = whichDevice(res); + if (device.equals("projector_identifier")) { + projectorString = serialString; + } else if (device.equals("camera_identifier")) { + cameraString = serialString; + } else if (device.equals("projector_camera_identifier")) { + //Oh no... + cameraString = serialString; + projectorString = serialString; + } else { + identified = false; + } + + } + return identified; +} + +String whichDevice (String res) { + String[] identifiers = {}; + String[] keys = (String[]) cmds.keys().toArray(new String[cmds.size()]); + String id = ""; + + for (int i = 0; i < keys.length;i++) { + if (keys[i].indexOf("_identifier") > -1) { + identifiers = append(identifiers, keys[i]); + } + } + + for (int i = 0; i < identifiers.length; i++) { + if ( res.equals( cmds.getString(identifiers[i]) ) ) { + println("Device identified with " + res + " response as " + identifiers[i]); + id = identifiers[i]; + break; + } + } + + return id; +} + +void connectCamera () { + println("Connecting to " + cameraString + " as camera..."); + cameraPort = new Serial(this, cameraString, BAUD); + delay(1000); + println("Camera connected."); +} +void connectProjector () { + println("Connecting to " + projectorString + " as projector..."); + projectorPort = new Serial(this, projectorString, BAUD); + delay(1000); + println("Projector connected."); +} + +void onReady(){ + +} + +void setup() { + configure(); + connect(); + size(640, 360); + noStroke(); +} + +void draw() { + background(0); + +} diff --git a/scripts/build.sh b/scripts/build.sh index a905118..27297c8 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -8,4 +8,5 @@ cp -r ./lib/* ./app/lib/ cp -r ./lib/* ./cli/lib/ cp ./data/cfg.json ./app/data/ -cp ./data/cfg.json ./cli/data/ \ No newline at end of file +cp ./data/cfg.json ./cli/data/ +cp ./data/cfg.json ./processing/mcopy/ \ No newline at end of file