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

Issue #156 (1/2): support setting effect period in s or ms

This commit is contained in:
Daniel Eble 2019-01-01 12:34:43 -05:00
parent b8964c3708
commit 144be79731
5 changed files with 46 additions and 55 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

@ -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;
}