From 69e07867db60be4b065f1134aa2ada95c5c33f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vuchener?= Date: Wed, 16 Dec 2015 15:14:49 +0100 Subject: [PATCH] Add lift threshold and color for G500 profile --- src/libhidpp/hidpp10/Profile.cpp | 33 +++++++++++++++++++++++++++++--- src/libhidpp/hidpp10/Profile.h | 17 ++++++++++++---- src/tools/profile/ProfileXML.cpp | 23 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/libhidpp/hidpp10/Profile.cpp b/src/libhidpp/hidpp10/Profile.cpp index de24179..f5518b2 100644 --- a/src/libhidpp/hidpp10/Profile.cpp +++ b/src/libhidpp/hidpp10/Profile.cpp @@ -24,6 +24,12 @@ using namespace HIDPP10; +template +static T clamp (T value, T min, T max) +{ + return std::min (std::max (value, min), max); +} + std::string Profile::Button::specialFunctionToString (SpecialFunction special) { switch (special) { @@ -269,7 +275,8 @@ G500Profile::G500Profile (const Sensor *sensor): Profile (13), _sensor (sensor), _color ({ 255, 0, 0 }), _angle (0x80), - _lift (0x10), _unk (0x10), + _lift (0), + _unk (0x10), _poll_interval (8) { } @@ -315,7 +322,7 @@ void G500Profile::read (ByteArray::const_iterator begin) } _angle_snap = *(begin+34) == 0x02; _default_mode = *(begin+35); - _lift = *(begin+36); + _lift = static_cast (*(begin+36) & 0x1f) - 0x10; _unk = *(begin+37); _poll_interval = *(begin+38); readButtons (begin+39); @@ -343,7 +350,7 @@ void G500Profile::write (ByteArray::iterator begin) const } *(begin+34) = (_angle_snap ? 0x02 : 0x01); *(begin+35) = _default_mode; - *(begin+36) = _lift; + *(begin+36) = 0x10 + clamp (_lift, -15, 15); *(begin+37) = _unk; *(begin+38) = _poll_interval; writeButtons (begin+39); @@ -382,6 +389,16 @@ void G500Profile::setDefaultMode (unsigned int index) _default_mode = index; } +Profile::Color G500Profile::color () const +{ + return _color; +} + +void G500Profile::setColor (Color color) +{ + _color = color; +} + bool G500Profile::angleSnap () const { return _angle_snap; @@ -392,6 +409,16 @@ void G500Profile::setAngleSnap (bool enabled) _angle_snap = enabled; } +int G500Profile::liftThreshold () const +{ + return _lift; +} + +void G500Profile::setLiftThreshold (int lift) +{ + _lift = lift; +} + unsigned int G500Profile::pollInterval () const { return _poll_interval; diff --git a/src/libhidpp/hidpp10/Profile.h b/src/libhidpp/hidpp10/Profile.h index ecc81ed..66c2e66 100644 --- a/src/libhidpp/hidpp10/Profile.h +++ b/src/libhidpp/hidpp10/Profile.h @@ -39,6 +39,10 @@ enum ProfileType { class Profile { public: + struct Color { + uint8_t r, g, b; + }; + class Button { public: enum Type: uint8_t { @@ -148,22 +152,27 @@ class G500Profile: public Profile unsigned int defaultMode () const; void setDefaultMode (unsigned int index); + Color color () const; + void setColor (Color color); + bool angleSnap () const; void setAngleSnap (bool enabled); + int liftThreshold () const; + void setLiftThreshold (int lift); + unsigned int pollInterval () const; void setPollInterval (unsigned int interval); private: const Sensor *_sensor; - struct { - uint8_t r, g, b; - } _color; + Color _color; uint8_t _angle; std::vector _modes; bool _angle_snap; unsigned int _default_mode; - uint8_t _lift, _unk; + int _lift; + uint8_t _unk; unsigned int _poll_interval; }; diff --git a/src/tools/profile/ProfileXML.cpp b/src/tools/profile/ProfileXML.cpp index 5d70de8..b771cb8 100644 --- a/src/tools/profile/ProfileXML.cpp +++ b/src/tools/profile/ProfileXML.cpp @@ -155,6 +155,17 @@ void G500ProfileToXML (const Profile *p, const std::vector ¯os, XMLNo angle_snap->SetText (profile->angleSnap ()); node->InsertEndChild (angle_snap); + XMLElement *lift = doc->NewElement ("lift"); + lift->SetText (profile->liftThreshold ()); + node->InsertEndChild (lift); + + XMLElement *color = doc->NewElement ("color"); + Profile::Color c = profile->color (); + char color_str[7]; + sprintf (color_str, "%02hhx%02hhx%02hhx", c.r, c.g, c.b); + color->SetText (color_str); + node->InsertEndChild (color); + XMLElement *buttons = doc->NewElement ("buttons"); ButtonsToXML (profile, macros, buttons); node->InsertEndChild (buttons); @@ -304,6 +315,18 @@ void XMLToG500Profile (const XMLNode *node, Profile *p, std::vector ¯ Log::error () << "Invalid angle snap." << std::endl; profile->setAngleSnap (angle_snap); } + else if (name == "lift") { + int lift; + if (XML_NO_ERROR != element->QueryIntText (&lift)) + Log::error () << "Invalid lift threshold." << std::endl; + profile->setLiftThreshold (lift); + } + else if (name == "color") { + Profile::Color color; + if (3 != sscanf (element->GetText (), "%02hhx%02hhx%02hhx", &color.r, &color.g, &color.b)) + Log::error () << "Invalid color value." << std::endl; + profile->setColor (color); + } else if (name == "buttons") { XMLToButtons (element, profile, macros); }