From fee23aed22ebdc81eb4d0d807f8d17712a71bb43 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Tue, 29 Mar 2016 08:28:01 +0200 Subject: Change project structure and driver modularity --- src/app/App.java | 7 +- src/client/MainWindow.java | 22 ++++-- src/drivers/Device_1770_ff00.java | 139 ------------------------------------ src/drivers/Driver.java | 25 +++++++ src/drivers/DriverManager.java | 48 +++++++++++++ src/drivers/DriverTypeA.java | 2 +- src/drivers/Driver_1770_ff00.java | 144 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 238 insertions(+), 149 deletions(-) delete mode 100644 src/drivers/Device_1770_ff00.java create mode 100644 src/drivers/Driver.java create mode 100644 src/drivers/DriverManager.java create mode 100644 src/drivers/Driver_1770_ff00.java diff --git a/src/app/App.java b/src/app/App.java index c509962..8de6d69 100644 --- a/src/app/App.java +++ b/src/app/App.java @@ -24,7 +24,8 @@ public class App public void parseArguments(){ if(this.leftColor!=null || this.rightColor!=null || this.middleColor!=null){ try { - DriverTypeA device=new Device_1770_ff00(); + DriverTypeA device=new Driver_1770_ff00(); + if(this.leftColor==null) this.leftColor="OFF"; if(this.middleColor==null) @@ -35,7 +36,7 @@ public class App device.setRegionColor(Region.MIDDLE, Color.valueOf(middleColor), Intensity.HIGH); device.setRegionColor(Region.RIGHT, Color.valueOf(rightColor), Intensity.HIGH); - } catch (InstantiationException e) { + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -51,6 +52,6 @@ public class App parser.parseArgument(args); app.parseArguments(); new MainWindow(); - + } } diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java index b60b9ec..1f24f64 100644 --- a/src/client/MainWindow.java +++ b/src/client/MainWindow.java @@ -23,16 +23,26 @@ public class MainWindow extends JFrame { private JComboBox right; private JButton apply=new JButton("Apply"); - private DriverTypeA keyboard; + private DriverTypeA keyboardTypeA; + private Class driverType; + private DriverManager drvMan=new DriverManager(); public MainWindow() throws InstantiationException{ this.initUI(); - this.keyboard=new Device_1770_ff00(); + Driver drv=drvMan.getDevice(); + if(drv==null){ + System.err.println("No driver avalaible (try as root)"); + System.exit(1); + } + else{ + this.driverType=drv.getType(); + this.keyboardTypeA=(DriverTypeA) drv; + } + this.left=new JComboBox<>(Color.values()); this.middle=new JComboBox<>(Color.values()); this.right=new JComboBox<>(Color.values()); - this.apply.addActionListener(new ActionListener() { @Override @@ -41,9 +51,9 @@ public class MainWindow extends JFrame { Color middleRegion=(Color) middle.getSelectedItem(); Color rightRegion=(Color) right.getSelectedItem(); - keyboard.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH); - keyboard.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH); - keyboard.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH); + keyboardTypeA.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH); + keyboardTypeA.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH); + keyboardTypeA.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH); } diff --git a/src/drivers/Device_1770_ff00.java b/src/drivers/Device_1770_ff00.java deleted file mode 100644 index 77ee461..0000000 --- a/src/drivers/Device_1770_ff00.java +++ /dev/null @@ -1,139 +0,0 @@ -package org.manzerbredes.open_klm.drivers; - -import java.io.IOException; -import java.util.HashMap; -import org.javatuples.Pair; -import com.codeminders.hidapi.*; - - -/** - * - * Driver to communicate with the keyboard device - * using HIDAPI. - * - * @author Manzerbredes - * - */ -public class Device_1770_ff00 implements DriverTypeA{ - - - - /** - * Device entry - */ - HIDDevice device; - - /** - * Define Keyboard color state - */ - private HashMap> KeyboardColor=new HashMap<>(); - - /** - * Define Keyboard mode state - */ - private Mode mode=Mode.NORMAL; - - - - - - /** - * Init driver and HIDAPI library - * - * @throws InstantiationException - */ - public Device_1770_ff00() throws InstantiationException{ - // Init HIDAPI Library - com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary(); - - // Try not bind the device - try { - HIDManager man=HIDManager.getInstance(); - this.device=man.openById(0x1770, 0xff00, null); - if(this.device==null) - throw new Exception(); - } - catch(Exception e){ - throw new InstantiationException("Failed to instanciate driver. Device not found or permission denied (try as root)"); - } - - } - - - /** - * Build a byte[] report - * - * @param a - * @param b - * @param c - * @param d - * @param e - * @param f - * @param g - * @param h - * @return - */ - private byte[] getReport(int a, int b, int c, int d, int e, int f, int g, int h){ - byte[] message = new byte[8]; - message[0] = (byte) a; - message[1] = (byte) b; - message[2] = (byte) c; - message[3] = (byte) d; - message[4] = (byte) e; - message[5] = (byte) f; - message[6] = (byte) g; - message[7] = (byte) h; - return message; - } - - - - - - - private void commit(){ - try { - this.device.sendFeatureReport(this.getReport(1,2,65,this.mode.intValue(),0,0,0,236)); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - - @Override - public void setRegionColor(Region region, Color color, Intensity intensity) { - try { - this.device.sendFeatureReport(this.getReport(1,2,66,region.intValue(),color.intValue(),intensity.intValue(),0,236)); - this.commit(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - } - - - @Override - public void setColor(Color color, Intensity intensity) { - try { - this.device.sendFeatureReport(this.getReport(1,2,66,Region.LEFT.intValue(),color.intValue(),intensity.intValue(),0,236)); - this.device.sendFeatureReport(this.getReport(1,2,66,Region.MIDDLE.intValue(),color.intValue(),intensity.intValue(),0,236)); - this.device.sendFeatureReport(this.getReport(1,2,66,Region.RIGHT.intValue(),color.intValue(),intensity.intValue(),0,236)); - this.commit(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - - @Override - public void setMode(Mode mode) { - this.mode=mode; - this.commit(); - } - -} \ No newline at end of file diff --git a/src/drivers/Driver.java b/src/drivers/Driver.java new file mode 100644 index 0000000..d362d19 --- /dev/null +++ b/src/drivers/Driver.java @@ -0,0 +1,25 @@ +package org.manzerbredes.open_klm.drivers; + + +/** + * Driver must implement this interface + * + * @author Manzerbredes + * + */ +public interface Driver{ + + /** + * Get the type of the driver + * + * @return class that represent the type of the driver (DriverTypeA.class for example) + */ + public Class getType(); + + /** + * Initialise the driver (do not initialise anything in the constructor). + * + * @return true if success (device is present and accessible) false else. + */ + public boolean initDriver(); +} \ No newline at end of file diff --git a/src/drivers/DriverManager.java b/src/drivers/DriverManager.java new file mode 100644 index 0000000..9a3c37c --- /dev/null +++ b/src/drivers/DriverManager.java @@ -0,0 +1,48 @@ +package org.manzerbredes.open_klm.drivers; + + +/** + * Driver Manager + * + * @author Manzerbredes + * + */ +public class DriverManager{ + + /** + * List of avalaible drivers + */ + private Class[] drivers={ + Driver_1770_ff00.class + }; + + + + /** + * Get a successfully loaded driver + * + * @return Driver the loaded driver. + */ + public Driver getDevice(){ + // Walk on driver list + for(int i=0;i> KeyboardColor=new HashMap<>(); + + /** + * Define Keyboard mode state + */ + private Mode mode=Mode.NORMAL; + + + @Override + public boolean initDriver(){ + // Init HIDAPI Library + com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary(); + + // Try not bind the device + try { + HIDManager man=HIDManager.getInstance(); + this.device=man.openById(0x1770, 0xff00, null); + if(this.device!=null) + return true; + } + catch(Exception e){ + System.err.println(e.getMessage()); + } + return false; + } + + + /** + * Build a byte[] report + * + * @param a + * @param b + * @param c + * @param d + * @param e + * @param f + * @param g + * @param h + * @return + */ + private byte[] getReport(int a, int b, int c, int d, int e, int f, int g, int h){ + byte[] message = new byte[8]; + message[0] = (byte) a; + message[1] = (byte) b; + message[2] = (byte) c; + message[3] = (byte) d; + message[4] = (byte) e; + message[5] = (byte) f; + message[6] = (byte) g; + message[7] = (byte) h; + return message; + } + + + + + + /** + * Commit (apply current mode to update the color) + */ + private void commit(){ + try { + this.device.sendFeatureReport(this.getReport(1,2,65,this.mode.intValue(),0,0,0,236)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + @Override + public void setRegionColor(Region region, Color color, Intensity intensity) { + try { + this.device.sendFeatureReport(this.getReport(1,2,66,region.intValue(),color.intValue(),intensity.intValue(),0,236)); + this.commit(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + + + @Override + public void setColor(Color color, Intensity intensity) { + try { + this.device.sendFeatureReport(this.getReport(1,2,66,Region.LEFT.intValue(),color.intValue(),intensity.intValue(),0,236)); + this.device.sendFeatureReport(this.getReport(1,2,66,Region.MIDDLE.intValue(),color.intValue(),intensity.intValue(),0,236)); + this.device.sendFeatureReport(this.getReport(1,2,66,Region.RIGHT.intValue(),color.intValue(),intensity.intValue(),0,236)); + this.commit(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + + @Override + public void setMode(Mode mode) { + this.mode=mode; + this.commit(); + } + + + @Override + public Class getType() { + return DriverTypeA.class; + } + + + + + +} \ No newline at end of file -- cgit v1.2.3