From 144be797317d0bcfa43b8394dfd1a3e949f7c0ad Mon Sep 17 00:00:00 2001 From: Daniel Eble Date: Tue, 1 Jan 2019 12:34:43 -0500 Subject: [PATCH 1/2] Issue #156 (1/2): support setting effect period in s or ms --- src/classes/Keyboard.cpp | 69 +++++++++++++++------------------------- src/classes/Keyboard.h | 6 ++-- src/helpers/utils.cpp | 15 ++++++--- src/helpers/utils.h | 3 +- src/main.cpp | 8 ++--- 5 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index db90f9f..47ec2e0 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -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 period, Color color) { uint8_t protocolByte = 0; NativeEffectGroup effectGroup = static_cast(static_cast(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(static_cast(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(effectGroup), + // color of static-color and breathing effects + color.red, color.green, color.blue, + // period of breathing effect (ms) + static_cast(period.count() >> 8), static_cast(period.count() & 0xff), + // period of cycle effect (ms) + static_cast(period.count() >> 8), static_cast(period.count() & 0xff), + static_cast(static_cast(effect) & 0xff), // wave variation (e.g. horizontal) + 0x64, // unused? + // period of wave effect (ms) + static_cast(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); } diff --git a/src/classes/Keyboard.h b/src/classes/Keyboard.h index 37e9977..b7266d3 100644 --- a/src/classes/Keyboard.h +++ b/src/classes/Keyboard.h @@ -1,6 +1,7 @@ #ifndef KEYBOARD_CLASS #define KEYBOARD_CLASS +#include #include #include @@ -170,8 +171,9 @@ 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 period, Color color); private: diff --git a/src/helpers/utils.cpp b/src/helpers/utils.cpp index cdc7a06..5dd6dea 100644 --- a/src/helpers/utils.cpp +++ b/src/helpers/utils.cpp @@ -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 &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; } diff --git a/src/helpers/utils.h b/src/helpers/utils.h index 92de26b..863d0bf 100644 --- a/src/helpers/utils.h +++ b/src/helpers/utils.h @@ -1,6 +1,7 @@ #ifndef UTILS_HELPER #define UTILS_HELPER +#include #include #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 &period); bool parseUInt8(std::string val, uint8_t &uint8); bool parseUInt16(std::string val, uint16_t &uint16); diff --git a/src/main.cpp b/src/main.cpp index 6b9c8a0..e803ca3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 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; } From 886021d0338cf0ff4c4dde7a34692621cc730590 Mon Sep 17 00:00:00 2001 From: Daniel Eble Date: Tue, 1 Jan 2019 12:50:29 -0500 Subject: [PATCH 2/2] Issue #156 (2/2): update help --- src/helpers/help.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/helpers/help.cpp b/src/helpers/help.cpp index 7cbc919..e1e7b53 100644 --- a/src/helpers/help.cpp +++ b/src/helpers/help.cpp @@ -89,7 +89,9 @@ namespace help { cout<<" color formats :\t\t\tII (hex value for intensity)"<