Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Illumination Light control interface for HID++ 2.0 #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libhidpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(LIBHIDPP_SOURCES
hidpp20/ITouchpadRawXY.cpp
hidpp20/ILEDControl.cpp
hidpp20/IBatteryLevelStatus.cpp
hidpp20/IIllumination.cpp
hidpp20/ProfileDirectoryFormat.cpp
hidpp20/ProfileFormat.cpp
hidpp20/MemoryMapping.cpp
Expand Down
156 changes: 156 additions & 0 deletions src/libhidpp/hidpp20/IIllumination.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2024 Bastien Nocera
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include <hidpp20/IIllumination.h>
#include <hidpp20/Error.h>

#include <misc/Endian.h>

#include <cassert>

using namespace HIDPP20;

constexpr uint16_t IIllumination::ID;

IIllumination::IIllumination (Device *dev):
FeatureInterface (dev, ID, "Illumination")
{
}

bool IIllumination::getIllumination(void)
{
std::vector<uint8_t> params (16), results;
results = call (GetIllumination, params);
return readLE<uint8_t> (results, 0) != 0;
}

void IIllumination::setIllumination(bool state)
{
std::vector<uint8_t> params (16);
writeLE<uint8_t> (params, 0, state);
call (SetIllumination, params);
}

IIllumination::Info IIllumination::getBrightnessInfo(void)
{
std::vector<uint8_t> params (16), results;
results = call (GetBrightnessInfo, params);
return Info {
(uint8_t)(readLE<uint8_t> (results, 0) & 0x0f), // capabilities
readBE<uint16_t> (results, 1), // min
readBE<uint16_t> (results, 3), // max
readBE<uint16_t> (results, 5), // res
(uint8_t)(readLE<uint8_t> (results, 7) & 0x0f), // maxLevels
};
}

uint16_t IIllumination::getBrightness(void)
{
uint16_t value;
std::vector<uint8_t> params (16), results;
results = call (GetBrightness, params);
value = readBE<uint16_t> (results, 0);
return value;
}

uint16_t IIllumination::getBrightnessEffectiveMax(void)
{
uint16_t value;
std::vector<uint8_t> params (16), results;
try {
results = call (GetBrightnessEffectiveMax, params);
value = readBE<uint16_t> (results, 0);
}
catch (HIDPP20::Error &e) {
if (e.errorCode () == HIDPP20::Error::InvalidFunctionID)
value = 0;
else throw e;
}
return value;
}

void IIllumination::setBrightness(uint16_t value)
{
std::vector<uint8_t> params (16);
writeBE<uint16_t> (params, 0, value);
call (SetBrightness, params);
}

IIllumination::Info IIllumination::getColorTemperatureInfo(void)
{
std::vector<uint8_t> params (16), results;
results = call (GetColorTemperatureInfo, params);
return Info {
(uint8_t)(readLE<uint8_t> (results, 0) & 0x0f), // capabilities
readBE<uint16_t> (results, 1), // min
readBE<uint16_t> (results, 3), // max
readBE<uint16_t> (results, 5), // res
(uint8_t)(readLE<uint8_t> (results, 7) & 0x0f), // maxLevels
};
}

uint16_t IIllumination::getColorTemperature(void)
{
uint16_t value;
std::vector<uint8_t> params (16), results;
results = call (GetColorTemperature, params);
value = readBE<uint16_t> (results, 0);
return value;
}

void IIllumination::setColorTemperature(uint16_t value)
{
std::vector<uint8_t> params (16);
writeBE<uint16_t> (params, 0, value);
call (SetColorTemperature, params);
}

bool IIllumination::illuminationChangeEvent (const HIDPP::Report &event)
{
assert (event.function () == IIllumination::IlluminationChangeEvent);
auto params = event.parameterBegin ();
return readLE<uint8_t> (params) != 0;
}

uint16_t IIllumination::brightnessChangeEvent (const HIDPP::Report &event)
{
assert (event.function () == IIllumination::BrightnessChangeEvent);
auto params = event.parameterBegin ();
return readBE<uint16_t> (params);
}

uint16_t IIllumination::colorTemperatureChangeEvent (const HIDPP::Report &event)
{
assert (event.function () == IIllumination::ColorTemperatureChangeEvent);
auto params = event.parameterBegin ();
return readBE<uint16_t> (params);
}

uint16_t IIllumination::brightnessEffectiveMaxChangeEvent (const HIDPP::Report &event)
{
assert (event.function () == IIllumination::BrightnessEffectiveMaxChangeEvent);
auto params = event.parameterBegin ();
return readBE<uint16_t> (params);
}

uint16_t IIllumination::brightnessClampedEvent (const HIDPP::Report &event)
{
assert (event.function () == IIllumination::BrightnessClampedEvent);
auto params = event.parameterBegin ();
return readBE<uint16_t> (params);
}
153 changes: 153 additions & 0 deletions src/libhidpp/hidpp20/IIllumination.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright 2024 Bastien Nocera
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef LIBHIDPP_HIDPP20_IILLUMINATION_H
#define LIBHIDPP_HIDPP20_IILLUMINATION_H

#include <hidpp20/FeatureInterface.h>

namespace HIDPP20
{

/**
* Control non-RGB LED features.
*/
class IIllumination: public FeatureInterface
{
public:
static constexpr uint16_t ID = 0x1990;

enum Function {
GetIllumination = 0,
SetIllumination = 1,
GetBrightnessInfo = 2,
GetBrightness = 3,
SetBrightness = 4,
GetBrightnessLevels = 5,
SetBrightnessLevels = 6,
GetColorTemperatureInfo = 7,
GetColorTemperature = 8,
SetColorTemperature = 9,
GetColorTemperatureLevels = 10,
SetColorTemperatureLevels = 11,
GetBrightnessEffectiveMax = 12,
};

enum Event {
IlluminationChangeEvent = 0,
BrightnessChangeEvent = 1,
ColorTemperatureChangeEvent = 2,
BrightnessEffectiveMaxChangeEvent = 3,
BrightnessClampedEvent = 4,
};

enum Flags {
hasEvents = 1 << 0,
hasLinearLevels = 1 << 1,
hasNonLinearLevels = 1 << 2,
hasDynamicMaximum = 1 << 3,
};

struct Info {
uint8_t flags;
uint16_t min;
uint16_t max;
uint16_t res;
unsigned int maxLevels : 4;
};

IIllumination (Device *dev);

/**
* Get the current Illumination state.
*/
bool getIllumination(void);

/**
* Set the current Illumination state.
*/
void setIllumination(bool state);

/**
* Get information about brightness.
*/
Info getBrightnessInfo(void);

/**
* Get the current Illumination brightness.
*/
uint16_t getBrightness(void);

/**
* Get the maximum brightness based on hardware limits,
* 0 means the max value from getBrightnessInfo().
*/
uint16_t getBrightnessEffectiveMax(void);

/**
* Set the Illumination brightness.
*/
void setBrightness(uint16_t value);

/**
* Get information about color temperature.
*/
Info getColorTemperatureInfo(void);

/**
* Get the current Illumination color temperature.
*/
uint16_t getColorTemperature(void);

/**
* Set the Illumination color temperature.
*/
void setColorTemperature(uint16_t value);

/**
* Parse an illumination change event.
*/
static bool illuminationChangeEvent (const HIDPP::Report &event);

/**
* Parse a brightness change event.
*/
static uint16_t brightnessChangeEvent (const HIDPP::Report &event);

/**
* Parse a color temperature change event.
*/
static uint16_t colorTemperatureChangeEvent (const HIDPP::Report &event);

/**
* Parse a change in the effective maximum brightness.
*/
static uint16_t brightnessEffectiveMaxChangeEvent (const HIDPP::Report &event);

/**
* Parse a notification of a recent request to set the
* brightness to a value larger than the current effective
* maximum brightness.
*/
static uint16_t brightnessClampedEvent (const HIDPP::Report &event);
};

}

#endif

1 change: 1 addition & 0 deletions src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(TOOLS
hidpp20-onboard-profiles-get-description
hidpp20-reprog-controls
hidpp20-led-control
hidpp20-illumination-light-control
hidpp20-dump-page
hidpp20-write-page
hidpp20-write-data
Expand Down
1 change: 1 addition & 0 deletions src/tools/hidpp-list-features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static const std::map<uint16_t, const char *> HIDPP20Features = {
{ 0x1981, "Backlight" },
{ 0x1982, "Backlight" },
{ 0x1983, "Backlight" },
{ 0x1990, "Illumination Light control" },
{ 0x1a00, "Presenter control" },
{ 0x1a01, "3D sensor" },
{ 0x1b00, "Reprog controls" },
Expand Down
Loading