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

Multiple changes of datatypes being past overflowing constants (most char needed to be changed to unsigned char or uint8_t). Also changed the include style of the <libusb.h> to be controlled through the makefile setting

This commit is contained in:
Charles 2016-12-12 14:06:45 -06:00
parent ecef158d35
commit 54897d9a5a
3 changed files with 45 additions and 33 deletions

View File

@ -1,5 +1,6 @@
CC=g++
CFLAGS=-Wall -O2 -std=gnu++11
LIBUSB_INC?=-I/usr/include/libusb-1.0
LDFLAGS=-lusb-1.0
PROGN=g810-led
@ -7,11 +8,11 @@ PROGN=g810-led
all: bin/$(PROGN)
bin/$(PROGN): src/main.cpp src/classes/*.cpp
bin/$(PROGN): src/main.cpp src/classes/*.cpp src/classes/*.h
@mkdir -p bin
$(CC) -o $@ $(CFLAGS) $^ $(LDFLAGS)
$(CC) $(CFLAGS) $(LIBUSB_INC) -o $@ $^ $(LDFLAGS)
debug: CFLAGS += -g
debug: CFLAGS += -g -Wextra -pedantic
debug: bin/$(PROGN)
clean:

View File

@ -21,7 +21,20 @@ bool Keyboard::attach() {
int pid = 0;
for(ssize_t i = 0; i < cnt; i++) {
libusb_device *device = devs[i];
libusb_device_descriptor desc = {0};
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_get_device_descriptor(device, &desc);
if (desc.idVendor == 0x046d) {
if (desc.idProduct == 0xc331) { pid = desc.idProduct; break; } // G810 spectrum
@ -476,7 +489,7 @@ bool Keyboard::parseColor(std::string color, KeyColors &colors) {
return true;
}
bool Keyboard::sendDataInternal(unsigned char *data, int data_size) {
bool Keyboard::sendDataInternal(unsigned char *data, uint16_t data_size) {
if (m_isAttached == false) return false;
int r;
if (data_size > 20) r = libusb_control_transfer(dev_handle, 0x21, 0x09, 0x0212, 1, data, data_size, 2000);
@ -589,17 +602,15 @@ bool Keyboard::populateAddressGroupInternal(KeyAddressGroup addressGroup, unsign
return true;
}
bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount) {
bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], size_t keyValueCount) {
bool retval = false;
unsigned char *data;
int data_size;
int maxKeyValueCount = 0;
if (addressGroup == KeyAddressGroup::logo) data_size = 20;
else data_size = 64;
data = new unsigned char[data_size];
unsigned char *data = new unsigned char[data_size];
const size_t maxKeyValueCount = (data_size - 8) / 4;
populateAddressGroupInternal(addressGroup, data);
maxKeyValueCount = (data_size - 8) / 4;
for(int i = 0; i < maxKeyValueCount; i++) {
for(size_t i = 0; i < maxKeyValueCount; i++) {
if (i < keyValueCount) {
data[8 + i * 4 + 0] = keyValues[i].key.id;
data[8 + i * 4 + 1] = keyValues[i].colors.red;
@ -671,24 +682,24 @@ bool Keyboard::setKey(Key key, KeyColors colors) {
return setKey(keyValue);
}
bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) {
int maxLogoKeys = 5;
int logoCount = 0;
bool Keyboard::setKeys(KeyValue keyValue[], size_t keyValueCount) {
const size_t maxLogoKeys = 5;
const size_t maxIndicatorsKeys = 25;
const size_t maxMultimediaKeys = 25;
const size_t maxKeys = 200;
const size_t maxGKeys = 25;
size_t logoCount = 0;
size_t indicatorsCount = 0;
size_t multimediaCount = 0;
size_t keysCount = 0;
size_t gkeysCount = 0;
KeyValue logo[maxLogoKeys];
int maxIndicatorsKeys = 25;
int indicatorsCount = 0;
KeyValue indicators[maxIndicatorsKeys];
int maxMultimediaKeys = 25;
int multimediaCount = 0;
KeyValue multimedia[maxMultimediaKeys];
int maxKeys = 200;
int keysCount = 0;
KeyValue keys[maxKeys];
int maxGKeys = 25;
int gkeysCount = 0;
KeyValue gkeys[maxGKeys];
for (int i = 0; i < keyValueCount; i++) {
for (size_t i = 0; i < keyValueCount; i++) {
if(keyValue[i].key.addressGroup == KeyAddressGroup::logo && logoCount <= maxLogoKeys) {
logo[logoCount] = keyValue[i];
logoCount++;
@ -714,11 +725,11 @@ bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) {
if (multimediaCount > 0) setKeysInternal(KeyAddressGroup::multimedia, multimedia, multimediaCount);
if (keysCount > 0) {
int maxKeyValueCount = 14;
for (int i = 0; i < keysCount; i = i + maxKeyValueCount) {
const size_t maxKeyValueCount = 14;
for (size_t i = 0; i < keysCount; i = i + maxKeyValueCount) {
KeyValue keysBlock[maxKeyValueCount];
int keysBlockCount = 0;
for (int j = 0; j < maxKeyValueCount; j++) {
size_t keysBlockCount = 0;
for (size_t j = 0; j < maxKeyValueCount; j++) {
if((i + j) < keysCount ) {
keysBlock[j] = keys[i + j];
keysBlockCount++;

View File

@ -2,7 +2,7 @@
#define DEF_KEYBOARD
#include <iostream>
#include "/usr/include/libusb-1.0/libusb.h"
#include <libusb.h>
class Keyboard {
@ -32,8 +32,8 @@ class Keyboard {
};
enum class KeyGroup { logo, indicators, multimedia, fkeys, modifiers, arrows, numeric, functions, keys, gkeys};
struct KeyColors { char red; char green; char blue; };
struct KeyAddress { KeyAddressGroup addressGroup; char id; };
struct KeyColors { uint8_t red; uint8_t green; uint8_t blue; };
struct KeyAddress { KeyAddressGroup addressGroup; uint8_t id; };
struct KeyValue { KeyAddress key; KeyColors colors; };
bool isAttached();
@ -48,7 +48,7 @@ class Keyboard {
bool setPowerOnEffect(PowerOnEffect powerOnEffect);
bool setKey(KeyValue keyValue);
bool setKey(Key key, KeyColors colors);
bool setKeys(KeyValue keyValue[], int keyValueCount);
bool setKeys(KeyValue keyValue[], size_t keyValueCount);
bool setAllKeys(KeyColors colors);
bool setGroupKeys(KeyGroup keyGroup, KeyColors colors);
@ -63,8 +63,8 @@ class Keyboard {
libusb_context *ctx = NULL;
bool populateAddressGroupInternal(KeyAddressGroup addressGroup, unsigned char *data);
bool sendDataInternal(unsigned char *data, int data_size);
bool setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount);
bool sendDataInternal(unsigned char *data, uint16_t data_size);
bool setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], size_t keyValueCount);
};