1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-23 01:06:11 +00:00

Improve code

This commit is contained in:
MatMoul 2017-01-22 00:47:38 +01:00
parent a136c0cc27
commit e8c678a181
2 changed files with 52 additions and 62 deletions

View File

@ -42,8 +42,12 @@ install:
$(DESTDIR)/etc/$(PROGN)/samples \
$(DESTDIR)$(SYSTEMDDIR)/system && \
cp sample_profiles/* $(DESTDIR)/etc/$(PROGN)/samples && \
test -s $(DESTDIR)/etc/$(PROGN)/profile || cp $(DESTDIR)/etc/$(PROGN)/samples/group_keys $(DESTDIR)/etc/$(PROGN)/profile && \
test -s $(DESTDIR)/etc/$(PROGN)/reboot || cp $(DESTDIR)/etc/$(PROGN)/samples/all_off $(DESTDIR)/etc/$(PROGN)/reboot && \
test -s $(DESTDIR)/etc/$(PROGN)/profile || \
cp $(DESTDIR)/etc/$(PROGN)/samples/group_keys $(DESTDIR)/etc/$(PROGN)/profile
@test -s /usr/bin/systemd-run && \
test -s $(DESTDIR)/etc/$(PROGN)/reboot || \
cp $(DESTDIR)/etc/$(PROGN)/samples/all_off $(DESTDIR)/etc/$(PROGN)/reboot
@test -s /usr/bin/systemd-run && \
cp systemd/$(PROGN).service $(DESTDIR)$(SYSTEMDDIR)/system && \
cp systemd/$(PROGN)-reboot.service $(DESTDIR)$(SYSTEMDDIR)/system && \
systemctl daemon-reload && \

View File

@ -47,31 +47,17 @@ bool LedKeyboard::listKeyboards() {
hid_free_enumeration(devs);
hid_exit();
return true;
#elif defined(libusb)
libusb_context *ctx = NULL;
if(libusb_init(&m_ctx) < 0) return false;
libusb_device **devs;
ssize_t cnt = libusb_get_device_list(ctx, &devs);
if(cnt >= 0) {
for(ssize_t i = 0; i < cnt; i++) {
libusb_device *device = devs[i];
libusb_device_descriptor desc = {
0, // bLength
0, // bDescriptorType
0, // bcdUSB
0, // bDeviceClass
0, // bDeviceSubClass
0, // bDeviceProtocol
0, // bMaxPacketSize0
0, // idVendor
0, // idProduct
0, // bcdDevice
0, // iManufacturer
0, // iProduct
0, // iSerialNumber
0 // bNumConfigurations
};
libusb_device_descriptor desc;
libusb_get_device_descriptor(device, &desc);
for (int i=0; i<(int)SuportedKeyboards.size(); i++) {
if (desc.idVendor == SuportedKeyboards[i][0]) {
@ -84,11 +70,12 @@ bool LedKeyboard::listKeyboards() {
}
}
libusb_free_device_list(devs, 1);
libusb_exit(m_ctx);
} else return false;
return true;
#endif
return true;
return false;
}
@ -137,6 +124,10 @@ bool LedKeyboard::open() {
hid_exit();
return false;
}
m_isOpen = true;
return true;
#elif defined(libusb)
if(libusb_init(&m_ctx) < 0) return false;
@ -146,22 +137,7 @@ bool LedKeyboard::open() {
if(cnt >= 0) {
for(ssize_t i = 0; i < cnt; i++) {
libusb_device *device = devs[i];
libusb_device_descriptor desc = {
0, // bLength
0, // bDescriptorType
0, // bcdUSB
0, // bDeviceClass
0, // bDeviceSubClass
0, // bDeviceProtocol
0, // bMaxPacketSize0
0, // idVendor
0, // idProduct
0, // bcdDevice
0, // iManufacturer
0, // iProduct
0, // iSerialNumber
0 // bNumConfigurations
};
libusb_device_descriptor desc;
libusb_get_device_descriptor(device, &desc);
for (int i=0; i<(int)SuportedKeyboards.size(); i++) {
if (desc.idVendor == SuportedKeyboards[i][0]) {
@ -173,9 +149,7 @@ bool LedKeyboard::open() {
}
}
}
if (m_keyboardModel != KeyboardModel::unknown) break;
}
libusb_free_device_list(devs, 1);
}
@ -189,14 +163,12 @@ bool LedKeyboard::open() {
}
m_hidHandle = libusb_open_device_with_vid_pid(m_ctx, m_vendorID, m_productID);
if(m_hidHandle == 0) {
libusb_exit(m_ctx);
m_ctx = NULL;
return false;
}
if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) {
if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) {
libusb_exit(m_ctx);
@ -205,6 +177,7 @@ bool LedKeyboard::open() {
}
m_isKernellDetached = true;
}
if(libusb_claim_interface(m_hidHandle, 1) < 0) {
if(m_isKernellDetached==true) {
libusb_attach_kernel_driver(m_hidHandle, 1);
@ -214,10 +187,13 @@ bool LedKeyboard::open() {
m_ctx = NULL;
return false;
}
#endif
m_isOpen = true;
return true;
#endif
return false;
}
bool LedKeyboard::close() {
@ -228,6 +204,8 @@ bool LedKeyboard::close() {
hid_close(m_hidHandle);
m_hidHandle = 0;
hid_exit();
return true;
#elif defined(libusb)
if(libusb_release_interface(m_hidHandle, 1) != 0) return false;
if(m_isKernellDetached==true) {
@ -238,9 +216,10 @@ bool LedKeyboard::close() {
m_hidHandle = NULL;
libusb_exit(m_ctx);
m_ctx = NULL;
return true;
#endif
return true;
return false;
}
@ -531,19 +510,26 @@ bool LedKeyboard::sendDataInternal(byte_buffer_t &data) {
byte_buffer_t data2;
data2.resize(21, 0x00);
hid_read_timeout(m_hidHandle, const_cast<unsigned char*>(data2.data()), data2.size(), 0);
return true;
#elif defined(libusb)
int r;
if (data.size() > 20) r = libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0212, 1, const_cast<unsigned char*>(data.data()), data.size(), 2000);
else r = libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0211, 1, const_cast<unsigned char*>(data.data()), data.size(), 2000);
if (data.size() > 20) {
if(libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0212, 1,
const_cast<unsigned char*>(data.data()), data.size(), 2000) < 0)
return false;
} else {
if(libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0211, 1,
const_cast<unsigned char*>(data.data()), data.size(), 2000) < 0)
return false;
}
usleep(1000);
if (r < 0) return false;
unsigned char buffer[64];
int len = 0;
r = libusb_interrupt_transfer(m_hidHandle, 0x82, buffer, sizeof(buffer), &len, 1);
libusb_interrupt_transfer(m_hidHandle, 0x82, buffer, sizeof(buffer), &len, 1);
return true;
#endif
}
return true;
return false;
}
LedKeyboard::byte_buffer_t LedKeyboard::getKeyGroupAddress(LedKeyboard::KeyAddressGroup keyAddressGroup) {