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

Merge pull request #160 from DanEble/issue-156-effect-period-ms

Set effect period in ms or s (Issue #156)
This commit is contained in:
MatMoul 2019-01-02 01:49:10 +01:00 committed by GitHub
commit 7a23f23b66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 64 deletions

View File

@ -568,7 +568,7 @@ bool LedKeyboard::setAllKeys(LedKeyboard::Color color) {
for (uint8_t rIndex=0x01; rIndex <= 0x05; rIndex++) if (! setRegion(rIndex, color)) return false;
return true;
case KeyboardModel::g413:
setNativeEffect(NativeEffect::color, NativeEffectPart::keys, 0, color);
setNativeEffect(NativeEffect::color, NativeEffectPart::keys, std::chrono::seconds(0), color);
return true;
case KeyboardModel::g410:
case KeyboardModel::g513:
@ -697,7 +697,7 @@ bool LedKeyboard::setStartupMode(StartupMode startupMode) {
}
bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, uint8_t speed, Color color) {
bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, std::chrono::duration<uint16_t, std::milli> period, Color color) {
uint8_t protocolByte = 0;
NativeEffectGroup effectGroup = static_cast<NativeEffectGroup>(static_cast<uint16_t>(effect) >> 8);
@ -722,8 +722,8 @@ bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, ui
break;
}
return (
setNativeEffect(effect, LedKeyboard::NativeEffectPart::keys, speed, color) &&
setNativeEffect(effect, LedKeyboard::NativeEffectPart::logo, speed, color));
setNativeEffect(effect, LedKeyboard::NativeEffectPart::keys, period, color) &&
setNativeEffect(effect, LedKeyboard::NativeEffectPart::logo, period, color));
}
switch (currentDevice.model) {
@ -746,47 +746,28 @@ bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, ui
return false;
}
byte_buffer_t data;
switch (effectGroup) {
case NativeEffectGroup::color:
data = { 0x11, 0xff, protocolByte, 0x3c, (uint8_t)part, 0x01, color.red, color.green, color.blue, 0x02 };
break;
case NativeEffectGroup::breathing:
data = {
0x11, 0xff, protocolByte, 0x3c, (uint8_t)part, 0x02,
color.red, color.green, color.blue, speed,
0x10, 0x00, 0x64
};
break;
case NativeEffectGroup::cycle:
data = {
0x11, 0xff, protocolByte, 0x3c, (uint8_t)part, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, speed, 0x00, 0x00, 0x64
};
break;
case NativeEffectGroup::waves:
switch (part) {
case NativeEffectPart::logo:
setNativeEffect(NativeEffect::color, part, 0, Color({0x00, 0xff, 0xff}));
break;
default:
data = {
0x11, 0xff, protocolByte, 0x3c, (uint8_t)part, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88,
static_cast<uint8_t>(static_cast<uint16_t>(effect) & 0xff),
0x64, speed
};
break;
}
break;
default:
return false;
if ((effectGroup == NativeEffectGroup::waves) && (part == NativeEffectPart::logo)) {
return setNativeEffect(NativeEffect::color, part, std::chrono::seconds(0), Color({0x00, 0xff, 0xff}));
}
data.resize(20, 0x00);
byte_buffer_t data = {
0x11, 0xff, protocolByte, 0x3c,
(uint8_t)part, static_cast<uint8_t>(effectGroup),
// color of static-color and breathing effects
color.red, color.green, color.blue,
// period of breathing effect (ms)
static_cast<uint8_t>(period.count() >> 8), static_cast<uint8_t>(period.count() & 0xff),
// period of cycle effect (ms)
static_cast<uint8_t>(period.count() >> 8), static_cast<uint8_t>(period.count() & 0xff),
static_cast<uint8_t>(static_cast<uint16_t>(effect) & 0xff), // wave variation (e.g. horizontal)
0x64, // unused?
// period of wave effect (ms)
static_cast<uint8_t>(period.count() >> 8), // LSB is shared with cycle effect above
0, // change to 1 to store this effect as the user effect (issue #157)
0, // unused?
0, // unused?
0, // unused?
};
return sendDataInternal(data);
}

View File

@ -1,6 +1,7 @@
#ifndef KEYBOARD_CLASS
#define KEYBOARD_CLASS
#include <chrono>
#include <iostream>
#include <vector>
@ -171,7 +172,8 @@ class LedKeyboard {
bool setRegion(uint8_t region, Color color);
bool setStartupMode(StartupMode startupMode);
bool setNativeEffect(NativeEffect effect, NativeEffectPart part, uint8_t speed, Color color);
bool setNativeEffect(NativeEffect effect, NativeEffectPart part,
std::chrono::duration<uint16_t, std::milli> period, Color color);
private:

View File

@ -89,7 +89,9 @@ namespace help {
cout<<" color formats :\t\t\tII (hex value for intensity)"<<endl;
if((features | KeyboardFeatures::setregion) == features)
cout<<" region formats :\t\t\tRN (integer value for region, 1 to 5)"<<endl;
cout<<" speed formats :\t\t\tSS (hex value for speed 01 to ff)"<<endl;
cout<<" period formats :\t\t\tDms (decimal integer; units of milliseconds)"<<endl;
cout<<" \t\t\tDs (decimal integer; units of seconds)"<<endl;
cout<<" \t\t\tSS (hex value 01 to ff; units of 256ms)"<<endl;
cout<<endl;
if((features | KeyboardFeatures::setkey) == features)
cout<<" key values :\t\t\t\tabc... 123... and other (use --help-keys for more detail)"<<endl;
@ -241,17 +243,17 @@ namespace help {
cout<<cmdName<<" Effects"<<endl;
cout<<"----------------"<<endl;
cout<<endl;
cout<<"At this time, FX are only tested on g810 and g512 !"<<endl;
cout<<"At this time, FX are only tested on g512, g810, and gpro !"<<endl;
cout<<endl;
cout<<" -fx {effect} {target}"<<endl;
cout<<endl;
cout<<" -fx color {target} {color}"<<endl;
cout<<" -fx breathing {target} {color} {speed}"<<endl;
cout<<" -fx cycle {target} {speed}"<<endl;
cout<<" -fx waves {target} {speed}"<<endl;
cout<<" -fx hwave {target} {speed}"<<endl;
cout<<" -fx vwave {target} {speed}"<<endl;
cout<<" -fx cwave {target} {speed}"<<endl;
cout<<" -fx breathing {target} {color} {period}"<<endl;
cout<<" -fx cycle {target} {period}"<<endl;
cout<<" -fx waves {target} {period}"<<endl;
cout<<" -fx hwave {target} {period}"<<endl;
cout<<" -fx vwave {target} {period}"<<endl;
cout<<" -fx cwave {target} {period}"<<endl;
cout<<endl;
if((features | KeyboardFeatures::logo1) == features)
cout<<"target value :\t\t\t\tall, keys, logo"<<endl;
@ -261,7 +263,9 @@ namespace help {
cout<<"color formats :\t\t\t\tRRGGBB (hex value for red, green and blue)"<<endl;
else if((features | KeyboardFeatures::rgb) == features)
cout<<"color formats :\t\t\t\tII (hex value for intensity)"<<endl;
cout<<"speed formats :\t\t\t\tSS (hex value for speed 01 to ff)"<<endl;
cout<<"period formats :\t\t\tDms (decimal integer; units of milliseconds)"<<endl;
cout<<" \t\t\tDs (decimal integer; units of seconds)"<<endl;
cout<<" \t\t\tSS (hex value 01 to ff; units of 256ms)"<<endl;
cout<<endl;
}

View File

@ -203,10 +203,17 @@ namespace utils {
return true;
}
bool parseSpeed(std::string val, uint8_t &speed) {
if (val.length() == 1) val = "0" + val;
if (val.length() != 2) return false;
speed = std::stoul("0x" + val, nullptr, 16);
bool parsePeriod(std::string val, std::chrono::duration<uint16_t, std::milli> &period) {
if (!val.empty() && val.back() == 's') {
if ((val.length() >= 2) && (val[val.length()-2] == 'm'))
period = std::chrono::milliseconds(std::stoul(val, nullptr));
else
period = std::chrono::seconds(std::stoul(val, nullptr));
} else {
if (val.length() == 1) val = "0" + val;
if (val.length() != 2) return false;
period = std::chrono::milliseconds(std::stoul("0x" + val, nullptr, 16) << 8);
}
return true;
}

View File

@ -1,6 +1,7 @@
#ifndef UTILS_HELPER
#define UTILS_HELPER
#include <chrono>
#include <iostream>
#include "../classes/Keyboard.h"
@ -14,7 +15,7 @@ namespace utils {
bool parseKey(std::string val, LedKeyboard::Key &key);
bool parseKeyGroup(std::string val, LedKeyboard::KeyGroup &keyGroup);
bool parseColor(std::string val, LedKeyboard::Color &color);
bool parseSpeed(std::string val, uint8_t &speed);
bool parsePeriod(std::string val, std::chrono::duration<uint16_t, std::milli> &period);
bool parseUInt8(std::string val, uint8_t &uint8);
bool parseUInt16(std::string val, uint16_t &uint16);

View File

@ -106,7 +106,7 @@ int setRegion(LedKeyboard &kbd, std::string arg2, std::string arg3) {
int setFX(LedKeyboard &kbd, std::string arg2, std::string arg3, std::string arg4, std::string arg5 = "") {
LedKeyboard::NativeEffect effect;
LedKeyboard::NativeEffectPart effectPart;
uint8_t speed = 0;
std::chrono::duration<uint16_t, std::milli> period(0);
LedKeyboard::Color color;
if (! utils::parseNativeEffect(arg2, effect)) return 1;
if (! utils::parseNativeEffectPart(arg3, effectPart)) return 1;
@ -118,20 +118,20 @@ int setFX(LedKeyboard &kbd, std::string arg2, std::string arg3, std::string arg4
case LedKeyboard::NativeEffect::breathing:
if (! utils::parseColor(arg4, color)) return 1;
if (arg5 == "") return 1;
if (! utils::parseSpeed(arg5, speed)) return 1;
if (! utils::parsePeriod(arg5, period)) return 1;
break;
case LedKeyboard::NativeEffect::cycle:
case LedKeyboard::NativeEffect::waves:
case LedKeyboard::NativeEffect::hwave:
case LedKeyboard::NativeEffect::vwave:
case LedKeyboard::NativeEffect::cwave:
if (! utils::parseSpeed(arg4, speed)) return 1;
if (! utils::parsePeriod(arg4, period)) return 1;
break;
}
if (! kbd.open()) return 1;
if (! kbd.setNativeEffect(effect, effectPart, speed, color)) return 1;
if (! kbd.setNativeEffect(effect, effectPart, period, color)) return 1;
return 0;
}