summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-27 14:40:42 +0200
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-27 14:40:42 +0200
commit5d8eafe9e8c36c86f9b4c33f32305866dd898f3e (patch)
tree495158ddee0c0eb2ce33111e37ae2a449c24305d
parent25b636c0afd61245765b521dd03255f0ecd4ef8f (diff)
Add state class
-rw-r--r--resources/javatuples-1.2.jarbin0 -> 65508 bytes
-rw-r--r--src/client/MainWindow.java22
-rw-r--r--src/device/Driver.java5
-rw-r--r--src/device/Keyboard.java35
-rw-r--r--src/device/KeyboardState.java55
5 files changed, 94 insertions, 23 deletions
diff --git a/resources/javatuples-1.2.jar b/resources/javatuples-1.2.jar
new file mode 100644
index 0000000..8944308
--- /dev/null
+++ b/resources/javatuples-1.2.jar
Binary files differ
diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java
new file mode 100644
index 0000000..b6020d2
--- /dev/null
+++ b/src/client/MainWindow.java
@@ -0,0 +1,22 @@
+package org.manzerbredes.client;
+
+import java.awt.EventQueue;
+import javax.swing.JFrame;
+
+public class MainWindow extends JFrame {
+
+
+
+ public MainWindow(){
+ this.initUI();
+ this.setVisible(true);
+ }
+
+ private void initUI(){
+ this.setTitle("Open KLM");
+ this.setSize(700, 500);
+ setLocationRelativeTo(null);
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
+ }
+
+} \ No newline at end of file
diff --git a/src/device/Driver.java b/src/device/Driver.java
index d629aa7..a099d95 100644
--- a/src/device/Driver.java
+++ b/src/device/Driver.java
@@ -110,12 +110,15 @@ public class Driver{
// 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.");
+ throw new InstantiationException("Failed to instanciate driver. Device not found or permission denied (try as root)");
}
}
diff --git a/src/device/Keyboard.java b/src/device/Keyboard.java
index 4acf4a1..cc7d8f9 100644
--- a/src/device/Keyboard.java
+++ b/src/device/Keyboard.java
@@ -10,27 +10,20 @@ public class Keyboard{
* Device driver
*/
private Driver device;
-
+
/**
- * Define Keyboard color state
+ * Keyboard State
*/
- private HashMap<Region, Color> KeyboardColor=new HashMap<>();
+ private KeyboardState state;
/**
- * Define Keyboard mode state
- */
- private Mode mode=Mode.NORMAL;
-
- /**
* Build a keyboard access
+ *
+ * @throws InstantiationException Throw if failed to instanciate driver
*/
- public Keyboard(){
- try {
- this.device=new Driver();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ public Keyboard() throws InstantiationException{
+ this.device=new Driver();
+ this.state=new KeyboardState();
}
@@ -42,10 +35,8 @@ public class Keyboard{
* @param intensity Intensity wanted
*/
public void setColor(Color color, Intensity intensity){
- // Save state
- this.KeyboardColor.put(Region.LEFT, color);
- this.KeyboardColor.put(Region.MIDDLE, color);
- this.KeyboardColor.put(Region.RIGHT, color);
+ //Save state
+ this.state.setColor(color, intensity);
// Set color
this.device.setColor(Region.LEFT, color, intensity);
@@ -53,7 +44,7 @@ public class Keyboard{
this.device.setColor(Region.RIGHT, color, intensity);
// Apply color
- this.device.commit(this.mode);
+ this.device.commit(this.state.getMode());
}
@@ -67,13 +58,13 @@ public class Keyboard{
*/
public void setRegionColor(Region region, Color color, Intensity intensity){
// Save state
- this.KeyboardColor.put(region, color);
+ this.state.setRegionColor(region, color, intensity);
// Set color
this.device.setColor(region, color, intensity);
// Apply color
- this.device.commit(this.mode);
+ this.device.commit(this.state.getMode());
}
diff --git a/src/device/KeyboardState.java b/src/device/KeyboardState.java
new file mode 100644
index 0000000..b8cbce4
--- /dev/null
+++ b/src/device/KeyboardState.java
@@ -0,0 +1,55 @@
+package org.manzerbredes.open_klm.device;
+
+import java.util.HashMap;
+
+import org.javatuples.Pair;
+import org.manzerbredes.open_klm.device.Driver.Color;
+import org.manzerbredes.open_klm.device.Driver.Intensity;
+import org.manzerbredes.open_klm.device.Driver.Mode;
+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() {
+ this.mode=Mode.NORMAL;
+ }
+
+ public void setColor(Color color, Intensity intensity){
+ this.KeyboardColor.put(Region.LEFT, new Pair<Driver.Color, Driver.Intensity>(color, intensity));
+ this.KeyboardColor.put(Region.MIDDLE, new Pair<Driver.Color, Driver.Intensity>(color, intensity));
+ this.KeyboardColor.put(Region.RIGHT, new Pair<Driver.Color, Driver.Intensity>(color, intensity));
+ }
+
+ public void setRegionColor(Region region, Color color, Intensity intensity){
+ this.KeyboardColor.put(region, new Pair<Driver.Color, Driver.Intensity>(color, intensity));
+ }
+
+
+ /**
+ * @return the mode
+ */
+ public Mode getMode() {
+ return mode;
+ }
+
+ /**
+ * @param mode the mode to set
+ */
+ public void setMode(Mode mode) {
+ this.mode = mode;
+ }
+
+
+
+}