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 support for a per channel correction factor for color adjustment #1155

Open
wants to merge 3 commits into
base: dev
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
19 changes: 16 additions & 3 deletions code/espurna/light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct {
unsigned char inputValue; // value that has been inputted
unsigned char value; // normalized value including brightness
unsigned char shadow; // represented value
unsigned char correction; // correction factor
double current; // transition value
} channel_t;
std::vector<channel_t> _light_channel;
Expand Down Expand Up @@ -392,18 +393,19 @@ void _toCSV(char * buffer, size_t len, bool applyBrightness) {
// PROVIDER
// -----------------------------------------------------------------------------

unsigned int _toPWM(unsigned long value, bool gamma, bool reverse) {
unsigned int _toPWM(unsigned long value, bool gamma, bool reverse, unsigned char correction) {
value = constrain(value, 0, LIGHT_MAX_VALUE);
if (gamma) value = _light_gamma_table[value];
if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
if (reverse) value = LIGHT_LIMIT_PWM - value;
if (correction<255) value = (value * correction) / 255;
return value;
}

// Returns a PWM value for the given channel ID
unsigned int _toPWM(unsigned char id) {
bool useGamma = _light_use_gamma && _light_has_color && (id < 3);
return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].reverse);
return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].reverse, _light_channel[id].correction);
}

void _shadow() {
Expand Down Expand Up @@ -463,6 +465,7 @@ void _lightProviderUpdate() {
void _lightColorSave() {
for (unsigned int i=0; i < _light_channel.size(); i++) {
setSetting("ch", i, _light_channel[i].inputValue);
setSetting("chCorrection", i, _light_channel[i].correction);
}
setSetting("brightness", _light_brightness);
setSetting("mireds", _light_mireds);
Expand All @@ -472,6 +475,7 @@ void _lightColorSave() {
void _lightColorRestore() {
for (unsigned int i=0; i < _light_channel.size(); i++) {
_light_channel[i].inputValue = getSetting("ch", i, i==0 ? 255 : 0).toInt();
_light_channel[i].correction = getSetting("chCorrection", i, 255).toInt();
}
_light_brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
_light_mireds = getSetting("mireds", _light_mireds).toInt();
Expand Down Expand Up @@ -790,7 +794,9 @@ void _lightWebSocketOnSend(JsonObject& root) {
}
JsonArray& channels = root.createNestedArray("channels");
for (unsigned char id=0; id < _light_channel.size(); id++) {
channels.add(lightChannel(id));
JsonObject& channel = channels.createNestedObject();
channel["value"] = lightChannel(id);
channel["correction"] = _light_channel[id].correction;
}
}

Expand Down Expand Up @@ -824,6 +830,13 @@ void _lightWebSocketOnAction(uint32_t client_id, const char * action, JsonObject
lightUpdate(true, true);
}
}

if (strcmp(action, "correction") == 0) {
if (data.containsKey("id") && data.containsKey("value")) {
_light_channel[data["id"]].correction = data["value"];
lightUpdate(true, true);
}
}
}

#endif
Expand Down
37 changes: 27 additions & 10 deletions code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ function initColorHSV() {
function initChannels(num) {

// check if already initialized
var done = $("#channels > div").length > 0;
var done = $("#channelcorrections > div").length > 0;
if (done) { return; }

// does it have color channels?
Expand All @@ -986,24 +986,39 @@ function initChannels(num) {

var onChannelSliderChange = function() {
var id = $(this).attr("data");
var type = $(this).attr("datatype");
var value = $(this).val();
var parent = $(this).parents(".pure-g");
$("span", parent).html(value);
sendAction("channel", {id: id, value: value});
if (type == "correction") {
sendAction("correction", {id: id, value: value});
} else {
sendAction("channel", {id: id, value: value});
}
};

// add templates
var i = 0;
var template = $("#channelTemplate").children();
for (i=0; i<max; i++) {

var channel_id = start + i;
for (i=0; i<num; i++) {

var channel_id = i;
var line = $(template).clone();
$("span.slider", line).attr("data", channel_id);
$("input.slider", line).attr("data", channel_id).on("change", onChannelSliderChange);
$("label", line).html("Channel #" + channel_id);

line.appendTo("#channels");
if (channel_id >= start) {
var line = $(template).clone();
$("span.slider", line).attr("data", channel_id).attr("datatype", "value");
$("input.slider", line).attr("data", channel_id).attr("datatype", "value").on("change", onChannelSliderChange);
$("label", line).html("Channel #" + channel_id);
line.appendTo("#channels");
}

var line = $(template).clone();
$("span.slider", line).attr("data", channel_id).attr("datatype", "correction");
$("input.slider", line).attr("data", channel_id).attr("datatype", "correction").on("change", onChannelSliderChange);
$("label", line).html("Correction Channel #" + channel_id);
line.appendTo("#channelcorrections");

}

Expand Down Expand Up @@ -1208,8 +1223,10 @@ function processData(data) {
initChannels(len);
for (i in value) {
var ch = value[i];
$("input.slider[data=" + i + "]").val(ch);
$("span.slider[data=" + i + "]").html(ch);
$("input.slider[data=" + i + "][datatype=value]").val(ch.value);
$("span.slider[data=" + i + "][datatype=value]").html(ch.value);
$("input.slider[data=" + i + "][datatype=correction]").val(ch.correction);
$("span.slider[data=" + i + "][datatype=correction]").html(ch.correction);
}
return;
}
Expand Down
4 changes: 3 additions & 1 deletion code/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ <h2>Lights configuration</h2>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">Sync color between different lights.</div>
</div>

<div id="channelcorrections"></div>

</fieldset>
</div>
Expand Down Expand Up @@ -1541,7 +1543,7 @@ <h2>
<div id="channelTemplate" class="template">
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Channel #</label>
<input type="range" min="0" max="255" class="slider channels pure-u-lg-1-4" data="99">
<input type="range" min="0" max="255" class="slider channels pure-u-lg-1-4" data="99" datatype="xyz">
<span class="slider pure-u-lg-1-4"></span>
</div>
</div>
Expand Down