1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
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 1770 ff00
* using HIDAPI.
*
* @author Manzerbredes
*
*/
public class Driver_1770_ff00 implements Driver, DriverTypeA{
/**
* Device entry
*/
HIDDevice device;
/**
* Define Keyboard primary color state
*/
private HashMap<Region, Pair<Color,Intensity>> primaryColorsState=new HashMap<>();
/**
* Define Keyboard secondary color state (for wave)
*/
private HashMap<Region, Pair<Color,Intensity>> secondaryColorsState=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){
// Init primary color state
this.primaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.HIGH));
this.primaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.HIGH));
this.primaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.HIGH));
// Init secondary color state
this.secondaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.LOW));
this.secondaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.LOW));
this.secondaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.OFF, Intensity.LOW));
// Init mode
this.mode=Mode.NORMAL;
// Return true (init successfully done)
return true;
}
}
catch(Exception e){
System.err.println(e.getMessage());
}
return false;
}
/**
* Build a byte[] report for convenience.
*
* @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[] report={(byte) a,(byte) b,(byte) c,(byte) d,(byte) e,(byte) f,(byte) g,(byte) h};
return report;
}
/**
* 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) {
e.printStackTrace();
}
}
@Override
public void setRegionColor(Region region, Color color, Intensity intensity) {
try {
// Set the color of the region
this.device.sendFeatureReport(this.getReport(1,2,66,region.intValue(),color.intValue(),intensity.intValue(),0,236));
this.commit();
// Save the color of the region
this.primaryColorsState.put(region, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void setColor(Color color, Intensity intensity) {
try {
// Apply color to all region
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();
// Save the color of all region
this.primaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
this.primaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
this.primaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void setMode(Mode mode) {
this.mode=mode;
// Apply wave mode
if(this.mode==Mode.WAVE || this.mode==Mode.BREATHE){
for(int i=0;i<Region.values().length;i++){
int entry=i*3; // 3 entry for each region (left:1,2,3 -- middle:4,5,6 -- right:7,8,9)
try {
// Set primary color (with 2 of intensity for speed problem)
// TODO Check intensity (fixed for speed) :
this.device.sendFeatureReport(this.getReport(1,2,67,entry+1 ,this.primaryColorsState.get(Region.values()[i]).getValue0().intValue(),2,0,236));
// Set secondary color
this.device.sendFeatureReport(this.getReport(1,2,67,entry+2 ,this.secondaryColorsState.get(Region.values()[i]).getValue0().intValue(),this.secondaryColorsState.get(Region.values()[i]).getValue1().intValue(),0,236));
// Set period
// TODO Check period :
this.device.sendFeatureReport(this.getReport(1,2,67,entry+3 ,2,2,0,236));
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Apply mode
this.commit();
}
@Override
public Class<?> getType() {
return DriverTypeA.class;
}
@Override
public void setSecondaryColor(Color color, Intensity intensity) {
// Set the secondary color of al regions
this.secondaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
this.secondaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
this.secondaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
}
@Override
public void setSecondaryRegionColor(Region region, Color color, Intensity intensity) {
// Set the secondary color of the region
this.secondaryColorsState.put(region, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(color, intensity));
}
}
|