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 Exponential Brightness #539

Merged
merged 2 commits into from
Dec 14, 2022
Merged
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
57 changes: 53 additions & 4 deletions src/cmnds/cmd_newLEDDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cmd_local.h"
#include "../mqtt/new_mqtt.h"
#include "../cJSON/cJSON.h"
#include <math.h>
#ifdef BK_LITTLEFS
#include "../littlefs/our_lfs.h"
#endif
Expand Down Expand Up @@ -274,6 +275,25 @@ void LED_RunQuickColorLerp(int deltaMS) {
}
#endif
}

int exponential_mode = 2;

/* exponential mode command handler, usage: led_expoMode <0|1|2|3|4>
exponential modes: 0 = Off
1 = 1% min brightness with moderate exponential
2 = 1% min brightness with full exponential
3 = 0.1% min brightness with moderate exponential
4 = 0.1% min brightness with full exponential
*/
static commandResult_t exponentialMode (const void *context, const char *cmd, const char *args, int cmdFlags) {
int mode = atoi (args);
if((mode >= 0) && (mode <= 4)) {
exponential_mode = mode;
apply_smart_light ();
}
return CMD_RES_OK;
}

void apply_smart_light() {
int i;
int firstChannelIndex;
Expand Down Expand Up @@ -329,11 +349,32 @@ void apply_smart_light() {
float raw, final;

raw = baseColors[i];
final = 0.0f;

if(g_lightEnableAll) {
final = raw * g_brightness;
} else {
final = 0;
// make brightness exponential:
if (exponential_mode == 0) {
final = raw * g_brightness;
} else if (g_brightness == 0.0f) {
final = 0.0f;
} else {
float expo_base;
float expo_factor;
float expo_offset = 0.0;
if ((exponential_mode == 1) || (exponential_mode == 2)) {
expo_offset = 0.009f;
}
if ((exponential_mode == 1) || (exponential_mode == 3)) {
expo_base = 1.2f; // moderate exponential
expo_factor = 15.32f;
expo_offset -= 0.06609f;
} else {
expo_base = 1.06f; // full exponential
expo_factor = 74.115f;
expo_offset -= 0.013f;
}
final = raw * (powf(expo_base, g_brightness * expo_factor) / expo_factor + expo_offset);
}
}
if(g_lightMode == Light_Temperature) {
// skip channels 0, 1, 2
Expand All @@ -354,13 +395,16 @@ void apply_smart_light() {
finalColors[i] = final;
finalRGBCW[i] = final;

int chVal = (int)(final * g_cfg_colorScaleToChannel);
if (chVal > 100)
chVal = 100;

channelToUse = firstChannelIndex + i;

// log printf with %f crashes N platform?
//ADDLOG_INFO(LOG_FEATURE_CMD, "apply_smart_light: ch %i raw is %f, bright %f, final %f, enableAll is %i",
// channelToUse,raw,g_brightness,final,g_lightEnableAll);

float chVal = final * g_cfg_colorScaleToChannel;
if(CFG_HasFlag(OBK_FLAG_LED_SMOOTH_TRANSITIONS) == false) {
if(isCWMode()) {
// in CW mode, we have only set two channels
Expand Down Expand Up @@ -1193,6 +1237,11 @@ void NewLED_InitCommands(){
//cmddetail:"fn":"lerpSpeed","file":"cmnds/cmd_newLEDDriver.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("led_lerpSpeed", "", lerpSpeed, NULL, NULL);
//cmddetail:{"name":"led_expoMode","args":"",
//cmddetail:"descr":"set brightness exponential mode 0..4",
//cmddetail:"fn":"exponentialMode","file":"cmnds/cmd_newLEDDriver.c","requires":"",
//cmddetail:"examples":"led_expoMode 4"}
CMD_RegisterCommand("led_expoMode", "", exponentialMode, NULL, NULL);

// HSBColor 360,100,100 - red
// HSBColor 90,100,100 - green
Expand Down