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

Improve code

This commit is contained in:
MatMoul 2016-06-19 00:38:19 +02:00
parent f7be63cedb
commit 345bcbfcbf
2 changed files with 21 additions and 24 deletions

View File

@ -52,12 +52,15 @@ bool Keyboard::detach() {
bool Keyboard::commit() { bool Keyboard::commit() {
if (m_isAttached == false) return false; if (m_isAttached == false) return false;
bool retval = false;
unsigned char *data = new unsigned char[20-1]; unsigned char *data = new unsigned char[20-1];
data[0] = 0x11; data[0] = 0x11;
data[1] = 0xff; data[1] = 0xff;
data[2] = 0x0c; data[2] = 0x0c;
data[3] = 0x5a; data[3] = 0x5a;
return sendDataInternal(data, 20); retval = sendDataInternal(data, 20);
delete data;
return retval;
} }
bool Keyboard::getKeyAddress(Key key, KeyAddress &keyAddress) { bool Keyboard::getKeyAddress(Key key, KeyAddress &keyAddress) {
@ -377,13 +380,10 @@ bool Keyboard::parseColor(std::string color, KeyColors &colors) {
return true; return true;
} }
// TODO: Need work arround sleep and/or updatekeys ????
bool Keyboard::sendDataInternal(unsigned char *data, int data_size) { bool Keyboard::sendDataInternal(unsigned char *data, int data_size) {
if (m_isAttached == false) return false; if (m_isAttached == false) return false;
int r; int r;
r = libusb_control_transfer(dev_handle, 0x21, 0x09, 0x0211, 1, data, data_size, 0); r = libusb_control_transfer(dev_handle, 0x21, 0x09, 0x0211, 1, data, data_size, 0);
//sleep(0.01);
//sleep(0.5);
if (r < 0) return false; if (r < 0) return false;
return true; return true;
} }
@ -437,8 +437,8 @@ bool Keyboard::populateAddressGroupInternal(KeyAddressGroup addressGroup, unsign
return true; return true;
} }
// TODO: Possible Bug
bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount) { bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount) {
bool retval = false;
unsigned char *data; unsigned char *data;
int data_size; int data_size;
if (addressGroup == KeyAddressGroup::logo) { if (addressGroup == KeyAddressGroup::logo) {
@ -470,10 +470,13 @@ bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[
} }
} }
} }
return sendDataInternal(data, data_size); retval = sendDataInternal(data, data_size);
delete data;
return retval;
} }
bool Keyboard::setPowerOnEffect(PowerOnEffect powerOnEffect) { bool Keyboard::setPowerOnEffect(PowerOnEffect powerOnEffect) {
bool retval = false;
int data_size = 20; int data_size = 20;
unsigned char *data = new unsigned char[data_size - 1]; unsigned char *data = new unsigned char[data_size - 1];
data[0] = 0x11; // Base address data[0] = 0x11; // Base address
@ -491,10 +494,13 @@ bool Keyboard::setPowerOnEffect(PowerOnEffect powerOnEffect) {
break; break;
} }
for(int i = 7; i < data_size; i++) data[i] = 0x00; for(int i = 7; i < data_size; i++) data[i] = 0x00;
return sendDataInternal(data, data_size); retval = sendDataInternal(data, data_size);
delete data;
return retval;
} }
bool Keyboard::setKey(KeyValue keyValue) { bool Keyboard::setKey(KeyValue keyValue) {
bool retval = false;
unsigned char *data; unsigned char *data;
int data_size; int data_size;
if (keyValue.key.addressGroup == KeyAddressGroup::logo) { if (keyValue.key.addressGroup == KeyAddressGroup::logo) {
@ -511,7 +517,9 @@ bool Keyboard::setKey(KeyValue keyValue) {
data[10] = keyValue.colors.green; data[10] = keyValue.colors.green;
data[11] = keyValue.colors.blue; data[11] = keyValue.colors.blue;
for(int i = 12; i < data_size; i++) data[i] = 0x00; for(int i = 12; i < data_size; i++) data[i] = 0x00;
return sendDataInternal(data, data_size); retval = sendDataInternal(data, data_size);
delete data;
return retval;
} }
bool Keyboard::setKey(Key key, KeyColors colors) { bool Keyboard::setKey(Key key, KeyColors colors) {
@ -521,7 +529,6 @@ bool Keyboard::setKey(Key key, KeyColors colors) {
return setKey(keyValue); return setKey(keyValue);
} }
// TODO: maxKeyValueCount = 12 or 13, Why ?
bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) { bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) {
KeyValue logo[5]; KeyValue logo[5];
int logoCount = 0; int logoCount = 0;
@ -549,27 +556,24 @@ bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) {
} }
if (logoCount > 0) setKey(logo[logoCount - 1]); if (logoCount > 0) setKey(logo[logoCount - 1]);
if (indicatorsCount > 0) { if (indicatorsCount > 0) {
for (int i = 0; i < indicatorsCount; i++) { for (int i = 0; i < indicatorsCount; i++) {
setKeysInternal(KeyAddressGroup::indicators, indicators, indicatorsCount); setKeysInternal(KeyAddressGroup::indicators, indicators, indicatorsCount);
commit(); commit();
} }
} }
if (multimediaCount > 0) { if (multimediaCount > 0) {
for (int i = 0; i < multimediaCount; i++) { for (int i = 0; i < multimediaCount; i++) {
setKeysInternal(KeyAddressGroup::multimedia, multimedia, multimediaCount); setKeysInternal(KeyAddressGroup::multimedia, multimedia, multimediaCount);
commit(); commit();
} }
} }
if (keysCount > 0) { if (keysCount > 0) {
int maxKeyValueCount = 2; int maxKeyValueCount = 12; // Normally max 20 or 25 but dont work
//int maxKeyValueCount = 4; for (int i = 0; i < keysCount; i = i + maxKeyValueCount) {
//int maxKeyValueCount = 10;
//int maxKeyValueCount = 12;
//int maxKeyValueCount = 13;
//int maxKeyValueCount = 14; Don't work
//int maxKeyValueCount = 20;
for (int i = 0; i < keysCount; i = i + maxKeyValueCount - 1) {
KeyValue keysBlock[maxKeyValueCount]; KeyValue keysBlock[maxKeyValueCount];
int keysBlockCount = 0; int keysBlockCount = 0;
for (int j = 0; j < maxKeyValueCount; j++) { for (int j = 0; j < maxKeyValueCount; j++) {

View File

@ -1,4 +1,3 @@
//#include <stdlib.h>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <string> #include <string>
@ -60,12 +59,8 @@ int setStartupEffect(string effect) {
g810.setPowerOnEffect(powerOnEffect); g810.setPowerOnEffect(powerOnEffect);
g810.commit(); g810.commit();
g810.detach(); g810.detach();
//free(g810);
//free(powerOnEffect);
return 0; return 0;
} }
//delete[] g810;
//delete powerOnEffect;
return 1; return 1;
} }
@ -111,7 +106,6 @@ int setGroupKeys(string groupKeys, string color, bool commit) {
g810.setGroupKeys(keyGroup, colors); g810.setGroupKeys(keyGroup, colors);
if (commit == true) g810.commit(); if (commit == true) g810.commit();
g810.detach(); g810.detach();
//delete [] g810;
return 0; return 0;
} }
} }
@ -127,7 +121,6 @@ int loadProfile(string profileFile) {
string line; string line;
int lineCount = 1; int lineCount = 1;
int ind; int ind;
//bool commit;
Keyboard g810; Keyboard g810;
Keyboard::KeyGroup keyGroup; Keyboard::KeyGroup keyGroup;