summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-27 18:37:12 +0200
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-27 18:37:12 +0200
commit6500028632b515c99a1d057478896fe66955fe29 (patch)
treebd7e4134b8ed17cabe3c073563680737fec6ca89
parent91430fcc06d1f7aff7ff7ca3babd3ec2c745ab73 (diff)
Add driver modularity
-rw-r--r--src/client/MainWindow.java13
-rw-r--r--src/device/Driver.java184
-rw-r--r--src/device/KeyboardState.java10
-rw-r--r--src/drivers/BasicDriver.java110
-rw-r--r--src/drivers/Device_1770_ff00.java139
5 files changed, 256 insertions, 200 deletions
diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java
index b98cca1..cd73757 100644
--- a/src/client/MainWindow.java
+++ b/src/client/MainWindow.java
@@ -1,4 +1,4 @@
-package org.manzerbredes.client;
+package org.manzerbredes.open_klm.client;
import java.awt.EventQueue;
import java.awt.GridLayout;
@@ -12,10 +12,9 @@ import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
-import org.manzerbredes.open_klm.device.Driver.Color;
-import org.manzerbredes.open_klm.device.Driver.Intensity;
-import org.manzerbredes.open_klm.device.Driver.Region;
-import org.manzerbredes.open_klm.device.Keyboard;
+import org.manzerbredes.open_klm.drivers.BasicDriver;
+import org.manzerbredes.open_klm.drivers.BasicDriver.*;
+import org.manzerbredes.open_klm.drivers.Device_1770_ff00;
public class MainWindow extends JFrame {
@@ -25,11 +24,11 @@ public class MainWindow extends JFrame {
private JComboBox<Color> right;
private JButton apply=new JButton("Apply");
- private Keyboard keyboard;
+ private BasicDriver keyboard;
public MainWindow() throws InstantiationException{
this.initUI();
- this.keyboard=new Keyboard();
+ this.keyboard=new Device_1770_ff00();
this.left=new JComboBox<>(Color.values());
this.middle=new JComboBox<>(Color.values());
this.right=new JComboBox<>(Color.values());
diff --git a/src/device/Driver.java b/src/device/Driver.java
deleted file mode 100644
index a099d95..0000000
--- a/src/device/Driver.java
+++ /dev/null
@@ -1,184 +0,0 @@
-package org.manzerbredes.open_klm.device;
-
-import java.io.IOException;
-
-import com.codeminders.hidapi.HIDDevice;
-import com.codeminders.hidapi.HIDManager;
-
-/**
- *
- * Driver to communicate with the keyboard device
- * using HIDAPI.
- *
- * @author Manzerbredes
- *
- */
-public class Driver{
-
- /**
- * Defined Region Helper
- *
- * @author Manzerbredes
- *
- */
- public enum Region{
- LEFT(1), MIDDLE(2), RIGHT(3);
-
- private int current;
-
- Region(int current){
- this.current=current;
- }
-
- public int intValue(){
- return this.current;
- }
- }
-
- /**
- * Defined Color Helper
- *
- * @author Manzerbredes
- *
- */
- public enum Color{
- OFF(0),RED(1),ORANGE(2),YELLOW(3),GREEN(4),SKY(5), BLUE(6),PURPLE(7),WHITE(8);
-
- private int current;
-
- Color(int current){
- this.current=current;
- }
-
- public int intValue(){
- return this.current;
- }
- }
-
- /**
- * Defined Level Helper
- *
- * @author Manzerbredes
- *
- */
- public enum Intensity{
- HIGH(0), MEDIUM(1), LOW(2), LIGHT(3);
-
- private int current;
-
- Intensity(int current){
- this.current=current;
- }
-
- public int intValue(){
- return this.current;
- }
- }
-
- /**
- * Defined Mode Helper
- *
- * @author Manzerbredes
- *
- */
- public enum Mode{
- NORMAL(1), GAMING(2), BREATHE(3), DEMO(4), WAVE(5);
-
- private int current;
-
- Mode(int current){
- this.current=current;
- }
-
- public int intValue(){
- return this.current;
- }
- }
-
- /**
- * Device entry
- */
- HIDDevice device;
-
-
- /**
- * Init driver and HIDAPI library
- *
- * @throws InstantiationException
- */
- public Driver() 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;
- }
-
-
- /**
- * Set Color of region
- *
- * @param region Region to apply
- * @param color Color to apply
- * @param intensity Intensity wanted
- */
- public void setColor(Region region, Color color, Intensity intensity){
- try {
- this.device.sendFeatureReport(this.getReport(1,2,66,region.intValue(),color.intValue(),intensity.intValue(),0,236));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /**
- * Commit change
- *
- * @param mode Mode wanted
- */
- public void commit(Mode mode){
- try {
- this.device.sendFeatureReport(this.getReport(1,2,65,mode.intValue(),0,0,0,236));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
-} \ No newline at end of file
diff --git a/src/device/KeyboardState.java b/src/device/KeyboardState.java
index b8cbce4..3f31cd4 100644
--- a/src/device/KeyboardState.java
+++ b/src/device/KeyboardState.java
@@ -10,15 +10,7 @@ import org.manzerbredes.open_klm.device.Driver.Region;
public class KeyboardState {
- /**
- * Define Keyboard color state
- */
- private HashMap<Region, Pair<Color,Intensity>> KeyboardColor=new HashMap<>();
-
- /**
- * Define Keyboard mode state
- */
- private Mode mode;
+
public KeyboardState() {
diff --git a/src/drivers/BasicDriver.java b/src/drivers/BasicDriver.java
new file mode 100644
index 0000000..9ec7c13
--- /dev/null
+++ b/src/drivers/BasicDriver.java
@@ -0,0 +1,110 @@
+package org.manzerbredes.open_klm.drivers;
+
+
+public interface BasicDriver{
+
+
+ /**
+ * Defined Region Helper
+ *
+ * @author Manzerbredes
+ *
+ */
+ public enum Region{
+ LEFT(1), MIDDLE(2), RIGHT(3);
+
+ private int current;
+
+ Region(int current){
+ this.current=current;
+ }
+
+ public int intValue(){
+ return this.current;
+ }
+ }
+
+ /**
+ * Defined Color Helper
+ *
+ * @author Manzerbredes
+ *
+ */
+ public enum Color{
+ OFF(0),RED(1),ORANGE(2),YELLOW(3),GREEN(4),SKY(5), BLUE(6),PURPLE(7),WHITE(8);
+
+ private int current;
+
+ Color(int current){
+ this.current=current;
+ }
+
+ public int intValue(){
+ return this.current;
+ }
+ }
+
+ /**
+ * Defined Level Helper
+ *
+ * @author Manzerbredes
+ *
+ */
+ public enum Intensity{
+ HIGH(0), MEDIUM(1), LOW(2), LIGHT(3);
+
+ private int current;
+
+ Intensity(int current){
+ this.current=current;
+ }
+
+ public int intValue(){
+ return this.current;
+ }
+ }
+
+ /**
+ * Defined Mode Helper
+ *
+ * @author Manzerbredes
+ *
+ */
+ public enum Mode{
+ NORMAL(1), GAMING(2), BREATHE(3), DEMO(4), WAVE(5);
+
+ private int current;
+
+ Mode(int current){
+ this.current=current;
+ }
+
+ public int intValue(){
+ return this.current;
+ }
+ }
+
+
+
+ /**
+ * Set color of the region
+ * @param region
+ * @param color
+ * @param intensity
+ */
+ public void setRegionColor(Region region, Color color, Intensity intensity);
+
+ /**
+ * Set global keyboard color
+ * @param color
+ * @param intensity
+ */
+ public void setColor(Color color, Intensity intensity);
+
+ /**
+ * Set keyboard mode
+ * @param mode
+ */
+ public void setMode(Mode mode);
+
+} \ No newline at end of file
diff --git a/src/drivers/Device_1770_ff00.java b/src/drivers/Device_1770_ff00.java
new file mode 100644
index 0000000..ec942d5
--- /dev/null
+++ b/src/drivers/Device_1770_ff00.java
@@ -0,0 +1,139 @@
+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 BasicDriver{
+
+
+
+ /**
+ * Device entry
+ */
+ HIDDevice device;
+
+ /**
+ * Define Keyboard color state
+ */
+ private HashMap<Region, Pair<Color,Intensity>> 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