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 Driver_1770_ff00 implements Driver, DriverTypeA{ /** * Device entry */ HIDDevice device; /** * Define Keyboard color state */ private HashMap> 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[] 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) { // 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; } }