From 05acfd5654837d0890ef2385ccd2d2724d5605f4 Mon Sep 17 00:00:00 2001 From: Sven Bentfeld Date: Fri, 12 Feb 2016 23:04:56 +0100 Subject: [PATCH] Fader und Midi, refs #3, #5 --- DMXSoftware/Data/DeviceTypes.xml | 23 ++-- DMXSoftware/Data/Devices.xml | 6 +- .../Communicate/MidiConnection.java | 101 ++++++++++++++++++ .../dmxsoftware/Database/Device.java | 4 + .../dmxsoftware/HelloWorldMain.java | 10 +- .../dmxsoftware/Processing/Manage.java | 52 +++++++++ .../dmxsoftware/Surface/ControlSurface.java | 56 +++++++--- .../dmxsoftware/Surface/Fader.java | 8 +- .../dmxsoftware/Surface/FaderListener.java | 17 ++- 9 files changed, 243 insertions(+), 34 deletions(-) create mode 100644 DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Communicate/MidiConnection.java create mode 100644 DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Processing/Manage.java diff --git a/DMXSoftware/Data/DeviceTypes.xml b/DMXSoftware/Data/DeviceTypes.xml index d328f2d..25802a6 100644 --- a/DMXSoftware/Data/DeviceTypes.xml +++ b/DMXSoftware/Data/DeviceTypes.xml @@ -129,14 +129,23 @@ - "Renkforce RDM Par_5" + "Renkforce RDM Par_10" "Par" "60" "Data/Pictures/RenkforceRDM_5.png" - + - + + "Dimmer" + + + + "1:1" + + + + "Red" @@ -145,7 +154,7 @@ - + "Green" @@ -153,7 +162,7 @@ "1:1" - + "Blue" @@ -161,7 +170,7 @@ "1:1" - + "White" @@ -169,7 +178,7 @@ "1:1" - + "Amber" diff --git a/DMXSoftware/Data/Devices.xml b/DMXSoftware/Data/Devices.xml index 381b2c7..722a3a6 100644 --- a/DMXSoftware/Data/Devices.xml +++ b/DMXSoftware/Data/Devices.xml @@ -19,17 +19,17 @@ "BAR 1" "Kuhstall Tor-Seite Mistfall" - + "BAR 2" "Kuhstall Tor-Seite Deele" - + "BAR 3" "Kuhstall Haus-Seite Mistfall" - + "BAR 4" "Kuhstall Haus-Seite Deele" diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Communicate/MidiConnection.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Communicate/MidiConnection.java new file mode 100644 index 0000000..6977e53 --- /dev/null +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Communicate/MidiConnection.java @@ -0,0 +1,101 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package SnowballThrower.dmxsoftware.Communicate; + +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.sound.midi.InvalidMidiDataException; +import javax.sound.midi.MidiDevice; +import javax.sound.midi.MidiDevice.Info; +import javax.sound.midi.MidiMessage; +import javax.sound.midi.MidiSystem; +import javax.sound.midi.MidiUnavailableException; +import javax.sound.midi.ShortMessage; +import static javax.sound.midi.ShortMessage.CONTROL_CHANGE; + +/** + * + * @author Sven + */ +public class MidiConnection extends Thread { + + static final int MAX_CH = 1000; + MidiDevice dmxController; + private int[] valOld; + int[] valNew; + boolean stop; + + public MidiConnection() { + stop = false; + valOld = new int[MAX_CH]; + valNew = new int[MAX_CH]; + Info[] infos = MidiSystem.getMidiDeviceInfo(); + for (int i = 0; i < infos.length; i++) { + System.out.println(infos[i].getName()); + if (infos[i].getName().contains("Cable")) { + try { + dmxController = MidiSystem.getMidiDevice(infos[i]); + } catch (MidiUnavailableException ex) { + Logger.getLogger(MidiConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + if (dmxController != null) { + if (!dmxController.isOpen()) { + try { + dmxController.open(); + } catch (MidiUnavailableException ex) { + Logger.getLogger(MidiConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } + + public void change(int channel, int value) { + if (value < 256 && value >= 0 && channel >= 0 && channel < MAX_CH) { + valNew[channel] = value; + } + } + + private void send(int channel, int value) { + System.out.println("Midi send: " + channel + " ," + value / 2); + System.out.println("Midi send: " + (CONTROL_CHANGE + channel / 128) + " ," + channel % 128 + " ," + value / 2); + try { + ShortMessage message = new ShortMessage(CONTROL_CHANGE, channel / 128, channel % 128, value / 2); + try { + dmxController.getReceiver().send(message, -1); + } catch (MidiUnavailableException ex) { + System.out.println("No Midi " + channel + " ," + value); + //stop = true; + //stop(); + } catch (NullPointerException np) { + System.out.println("midi=null"); + } + } catch (InvalidMidiDataException ex) { + Logger.getLogger(MidiConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void loop() { + int i; + for (i = 0; i < MAX_CH; i++) { + if (valOld[i] != valNew[i]) { + send(i, valNew[i]); + valOld[i] = valNew[i]; + } + } + } + + @Override + public void run() { + while (!stop) { + loop(); + } + if (dmxController != null) { + dmxController.close(); + } + } +} diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Database/Device.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Database/Device.java index 0a96de1..a4ebccf 100644 --- a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Database/Device.java +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Database/Device.java @@ -50,4 +50,8 @@ public DeviceType getType() { public String getImagePath() { return type.picturePath; } + + public int getStartCh() { + return this.startChannel; + } } diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/HelloWorldMain.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/HelloWorldMain.java index 92faf50..813bfb5 100644 --- a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/HelloWorldMain.java +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/HelloWorldMain.java @@ -5,16 +5,12 @@ */ package SnowballThrower.dmxsoftware; -import SnowballThrower.dmxsoftware.Database.Function; import SnowballThrower.dmxsoftware.Processing.Devices; -import SnowballThrower.dmxsoftware.Database.XMLReader; +import SnowballThrower.dmxsoftware.Processing.Manage; import SnowballThrower.dmxsoftware.Surface.ControlSurface; -import SnowballThrower.dmxsoftware.Surface.Fader; -import SnowballThrower.dmxsoftware.Surface.FaderListener; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; -import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; @@ -28,6 +24,7 @@ public class HelloWorldMain extends Application { @Override public void start(Stage primaryStage) { + Manage mng = new Manage(); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() { @@ -35,8 +32,9 @@ public void start(Stage primaryStage) { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); + mng.startMidi(); Devices devices = new Devices(); - new ControlSurface(devices.getDevices()); + ControlSurface cs = new ControlSurface(mng, devices.getDevices()); } }); diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Processing/Manage.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Processing/Manage.java new file mode 100644 index 0000000..3b71b4f --- /dev/null +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Processing/Manage.java @@ -0,0 +1,52 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package SnowballThrower.dmxsoftware.Processing; + +import SnowballThrower.dmxsoftware.Communicate.MidiConnection; +import SnowballThrower.dmxsoftware.Communicate.SerialConnection; +import SnowballThrower.dmxsoftware.Surface.ControlSurface; + +/** + * + * @author Sven + */ +public class Manage { + + ControlSurface cs; + MidiConnection mc; + SerialConnection sc; + + public Manage() { + mc = new MidiConnection(); + } + + public void send(int channel, int value) { + if (channel < 16 * 12 && channel > 0 && value > 0 && value < 256) { + mc.change(channel, value); + } + } + + public void handle(String id, int value) { + try { + int ch = Integer.parseInt(id); + if (ch < 1000) { + System.out.println("Change: " + id + " ," + value); + send(ch - 1, value); + return; + } + } catch (Exception ex) { + + } + } + + public void startMidi() { + try { + mc.start(); + } catch (Exception ex) { + System.out.println("Error in startMidi"); + } + } +} diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/ControlSurface.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/ControlSurface.java index 98b5770..2e3fb1a 100644 --- a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/ControlSurface.java +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/ControlSurface.java @@ -7,6 +7,7 @@ import SnowballThrower.dmxsoftware.Database.Channel; import SnowballThrower.dmxsoftware.Database.Device; +import SnowballThrower.dmxsoftware.Processing.Manage; import java.util.List; import javafx.application.Application; import javafx.scene.Group; @@ -14,6 +15,8 @@ import javafx.scene.control.ScrollPane; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.input.InputEvent; +import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; @@ -35,16 +38,36 @@ public class ControlSurface extends Application { List devices; double windowHeight; double windowWidth; + Manage manager; - public ControlSurface(List devices) { + public ControlSurface(Manage mng, List devices) { + manager = mng; + this.devices = devices; + stage = new Stage(); + ScrollPane pane = allDevs(); + pane.setLayoutY(50); + Group buttons = new Group(); + StackPane devSceneGroup = new StackPane(pane, buttons); + faders = new Scene(devSceneGroup, 1000, 800); + stage.setScene(faders); + //pane.setMaxHeight(stage.getHeight()); + stage.show(); + } + ScrollPane allDevs(){ + + int dev = 0; double sizeX = 70; double sizeY = 200; double distY = 20; - this.devices = devices; - int dev = 0; ScrollPane pane = new ScrollPane(); pane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); - FaderListener fl = new FaderListener(); + pane.addEventFilter(InputEvent.ANY, (event) -> { + if (event.getEventType().toString().contains("SCROLL")) { + //System.out.println(event.getEventType()); + event.consume(); + } + }); + FaderListener fl = new FaderListener(manager); Group faderGroup = new Group(); for (Device device : devices) { Group picture; @@ -52,13 +75,13 @@ public ControlSurface(List devices) { try { Image image = new Image(device.getImagePath()); ImageView imV = new ImageView(image); - imV.setFitHeight(2*sizeX); - imV.setFitWidth(2*sizeX); + imV.setFitHeight(2 * sizeX); + imV.setFitWidth(2 * sizeX); imV.setLayoutY(dev * (sizeY + distY)); picture = new Group(imV); - } catch (Exception ex){ + } catch (Exception ex) { System.out.println(ex); - Rectangle rect = new Rectangle(0,dev * (sizeY + distY),sizeX*2,sizeX*2); + Rectangle rect = new Rectangle(0, dev * (sizeY + distY), sizeX * 2, sizeX * 2); rect.setFill(Color.WHITE); picture = new Group(rect); } @@ -67,7 +90,8 @@ public ControlSurface(List devices) { System.out.println(device.getName()); for (Channel channel : device.getType().getChannels()) { if (channel != null) { - Fader fader = new Fader(ch * sizeX, dev * (sizeY + distY), sizeX, sizeY, channel); + Fader fader = new Fader("" + (device.getStartCh() + ch - 2), + ch * sizeX, dev * (sizeY + distY), sizeX, sizeY, channel); fl.addFader(fader); faderGroup.getChildren().add(fader.getFader()); } @@ -77,17 +101,21 @@ public ControlSurface(List devices) { } Group group = new Group(faderGroup); pane.setContent(group); - Scene faders = new Scene(pane, 1000, 800); group.setOnMouseClicked(fl); group.setOnMousePressed(fl); group.setOnMouseDragged(fl); - stage = new Stage(); - stage.setScene(faders); - stage.show(); + return pane; } - @Override public void start(Stage primaryStage) throws Exception { System.out.println("Start"); } + + public void setManager(Manage mng) { + manager = mng; + } + + public void send(int channel, int value) { + + } } diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/Fader.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/Fader.java index 69868c1..a010ee4 100644 --- a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/Fader.java +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/Fader.java @@ -30,8 +30,9 @@ public class Fader { Group head; Group fader; int max = 255; + private String Id; - public Fader(double posX, double posY, double width, double height, Channel channel) { + public Fader(String id, double posX, double posY, double width, double height, Channel channel) { this.function = channel.getFunction(); this.width = width; this.height = height; @@ -44,6 +45,7 @@ public Fader(double posX, double posY, double width, double height, Channel chan fader = new Group(background, head); fader.setLayoutX(posX); fader.setLayoutY(posY); + this.Id = id; } public void setValue(int value) { @@ -122,4 +124,8 @@ private Group getBackGround() { double getHeight() { return height; } + + String getID() { + return Id; + } } diff --git a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/FaderListener.java b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/FaderListener.java index 1d19498..40e86d2 100644 --- a/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/FaderListener.java +++ b/DMXSoftware/src/main/java/SnowballThrower/dmxsoftware/Surface/FaderListener.java @@ -5,6 +5,7 @@ */ package SnowballThrower.dmxsoftware.Surface; +import SnowballThrower.dmxsoftware.Processing.Manage; import java.util.LinkedList; import java.util.List; import javafx.event.EventHandler; @@ -17,13 +18,21 @@ public class FaderListener implements EventHandler { List faders; + private Manage manager; public FaderListener() { faders = new LinkedList(); } + FaderListener(Manage manager) { + this.manager = manager; + faders = new LinkedList(); + } + public void addFader(Fader fader) { - faders.add(fader); + if (fader != null) { + faders.add(fader); + } } public void removeFader(Fader fader) { @@ -39,6 +48,7 @@ public void mouseClicked(MouseEvent e) { fader.setValue(constrain((int) ((fader.getHeight() - fader.getFrameY() - e.getY() + fader.getPosY()) * 255 / (fader.getHeight() - 2 * fader.getFrameY())))); + manager.handle(fader.getID(), fader.getValue()); } } } @@ -48,9 +58,10 @@ public void handle(MouseEvent event) { mouseClicked(event); } - public void translate(double x, double y){ - + public void translate(double x, double y) { + } + private int constrain(int i) { if (i > 255) { return 255;