Merge pull request #446 from iprak/kelvin

Kelvin temperature
This commit is contained in:
openshwprojects
2022-11-11 14:08:32 +01:00
committed by GitHub
4 changed files with 37 additions and 23 deletions

View File

@@ -3,7 +3,7 @@
#include "../new_common.h"
typedef int (*commandHandler_t)(const void *context, const char *cmd, const char *args, int flags);
typedef int (*commandHandler_t)(const void* context, const char* cmd, const char* args, int flags);
// command was entered in console (web app etc)
#define COMMAND_FLAG_SOURCE_CONSOLE 1
@@ -21,9 +21,9 @@ typedef int (*commandHandler_t)(const void *context, const char *cmd, const char
//
void CMD_Init();
void CMD_RegisterCommand(const char *name, const char *args, commandHandler_t handler, const char *userDesc, void *context);
int CMD_ExecuteCommand(const char *s, int cmdFlags);
int CMD_ExecuteCommandArgs(const char *cmd, const char *args, int cmdFlags);
void CMD_RegisterCommand(const char* name, const char* args, commandHandler_t handler, const char* userDesc, void* context);
int CMD_ExecuteCommand(const char* s, int cmdFlags);
int CMD_ExecuteCommandArgs(const char* cmd, const char* args, int cmdFlags);
enum EventCode {
CMD_EVENT_NONE,
@@ -43,12 +43,12 @@ enum EventCode {
CMD_EVENT_CHANGE_VOLTAGE, // must match order in drv_bl0942.c
CMD_EVENT_CHANGE_CURRENT,
CMD_EVENT_CHANGE_POWER,
CMD_EVENT_CHANGE_CONSUMPTION_TOTAL,
CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR,
CMD_EVENT_CHANGE_CONSUMPTION_TOTAL,
CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR,
// this is for ToggleChannelOnToggle
CMD_EVENT_PIN_ONTOGGLE,
// Argument is a string
// You can fire an event when TuyaMCU or something receives given string
CMD_EVENT_ON_UART,
@@ -87,6 +87,8 @@ enum EventCode {
#define HASS_TEMPERATURE_MIN 154
#define HASS_TEMPERATURE_MAX 500
#define KELVIN_TEMPERATURE_MIN 2000
#define KELVIN_TEMPERATURE_MAX 6500
// In general, LED can be in two modes:
// - Temperature (Cool and Warm LEDs are on)
@@ -104,13 +106,13 @@ enum LightMode {
// cmd_tokenizer.c
int Tokenizer_GetArgsCount();
const char *Tokenizer_GetArg(int i);
const char *Tokenizer_GetArgFrom(int i);
const char* Tokenizer_GetArg(int i);
const char* Tokenizer_GetArgFrom(int i);
int Tokenizer_GetArgInteger(int i);
bool Tokenizer_IsArgInteger(int i);
float Tokenizer_GetArgFloat(int i);
int Tokenizer_GetArgIntegerRange(int i, int rangeMax, int rangeMin);
void Tokenizer_TokenizeString(const char *s, int flags);
void Tokenizer_TokenizeString(const char* s, int flags);
// cmd_repeatingEvents.c
void RepeatingEvents_Init();
void RepeatingEvents_OnEverySecond();
@@ -118,7 +120,7 @@ void RepeatingEvents_OnEverySecond();
void EventHandlers_Init();
// This is useful to fire an event when a certain UART string command is received.
// For example, you can fire an event while getting 55 AA 01 02 00 03 FF 01 01 06 on UART..
void EventHandlers_FireEvent_String(byte eventCode, const char *argument);
void EventHandlers_FireEvent_String(byte eventCode, const char* argument);
// This is useful to fire an event when, for example, a button is pressed.
// Then eventCode is a BUTTON_PRESS and argument is a button index.
void EventHandlers_FireEvent(byte eventCode, int argument);
@@ -140,17 +142,17 @@ void LED_SetTemperature(int tmpInteger, bool bApply);
float LED_GetTemperature0to1Range();
void LED_SetTemperature0to1Range(float f);
void LED_SetDimmer(int iVal);
int LED_SetBaseColor(const void *context, const char *cmd, const char *args, int bAll);
int LED_SetBaseColor(const void* context, const char* cmd, const char* args, int bAll);
void LED_SetFinalCW(byte c, byte w);
void LED_SetFinalRGB(byte r, byte g, byte b);
void LED_SetFinalRGBCW(byte *rgbcw);
void LED_SetFinalRGBCW(byte* rgbcw);
void LED_NextColor();
void LED_ToggleEnabled();
bool LED_IsLedDriverChipRunning();
bool LED_IsLEDRunning();
void LED_SetEnableAll(int bEnable);
int LED_GetEnableAll();
void LED_GetBaseColorString(char * s);
void LED_GetBaseColorString(char* s);
int LED_GetMode();
float LED_GetHue();
float LED_GetSaturation();
@@ -169,7 +171,7 @@ void CMD_StartTCPCommandLine();
void SVM_RunThreads(int deltaMS);
void CMD_InitScripting();
byte *LFS_ReadFile(const char *fname);
byte* LFS_ReadFile(const char* fname);
#endif // __CMD_PUBLIC_H__

View File

@@ -571,15 +571,20 @@ int http_fn_index(http_request_t* request) {
inputName = "pwm";
pwmValue = LED_GetTemperature();
long pwmKelvin = 1000000 / pwmValue;
poststr(request, "<tr><td>");
hprintf255(request, "<h5>LED Temperature Slider %s (cur=%i, min=%i, max=%i) Mired (Cool <--- ---> Warm)</h5>", activeStr, pwmValue, HASS_TEMPERATURE_MIN, HASS_TEMPERATURE_MAX);
hprintf255(request, "<form class='r' style='background: linear-gradient(to right, rgb(166, 209, 255), rgb(255, 160, 0));' action=\"index\" id=\"form%i\">", SPECIAL_CHANNEL_TEMPERATURE);
hprintf255(request, "<input type=\"range\" min=\"%i\" max=\"%i\"", HASS_TEMPERATURE_MIN, HASS_TEMPERATURE_MAX);
hprintf255(request, "name=\"%s\" id=\"slider%i\" value=\"%i\" onchange=\"this.form.submit()\">", inputName, SPECIAL_CHANNEL_TEMPERATURE, pwmValue);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\">", inputName, SPECIAL_CHANNEL_TEMPERATURE);
hprintf255(request, "<input type=\"submit\" style=\"display:none;\" value=\"Toggle %i\"/></form>", SPECIAL_CHANNEL_TEMPERATURE);
poststr(request, "</td></tr>");
hprintf255(request, "<h5>LED Temperature Slider %s (%ld K) (Warm <--- ---> Cool)</h5>", activeStr, pwmKelvin);
hprintf255(request, "<form class='r' style='background: linear-gradient(to right, rgb(255, 160, 0), rgb(166, 209, 255));' action=\"index\" id=\"form%i\">", SPECIAL_CHANNEL_TEMPERATURE);
//(KELVIN_TEMPERATURE_MAX - KELVIN_TEMPERATURE_MIN) / (HASS_TEMPERATURE_MAX - HASS_TEMPERATURE_MIN) = 13
hprintf255(request, "<input type=\"range\" step='13' min=\"%ld\" max=\"%ld\" ", KELVIN_TEMPERATURE_MIN, KELVIN_TEMPERATURE_MAX);
hprintf255(request, "value=\"%ld\" onchange=\"submitTemperature(this);\"/>", pwmKelvin);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\"/>", inputName, SPECIAL_CHANNEL_TEMPERATURE);
hprintf255(request, "<input id=\"kelvin%i\" type=\"hidden\" name=\"%s\" />", SPECIAL_CHANNEL_TEMPERATURE, inputName);
poststr(request, "</form></td></tr>");
}
}

View File

@@ -686,7 +686,7 @@ const char htmlHeadStyle[] = "<style>div,fieldset,input,select{padding:5px;font-
//region_end htmlHeadStyle
//region_start pageScript
const char pageScript[] = "<script type='text/javascript'>var firstTime,lastTime,onlineFor,req=null,onlineForEl=null,getElement=e=>document.getElementById(e);function showState(){clearTimeout(firstTime),clearTimeout(lastTime),null!=req&&req.abort(),(req=new XMLHttpRequest).onreadystatechange=()=>{var e;4==req.readyState&&200==req.status&&((\"INPUT\"!=document.activeElement.tagName||\"number\"!=document.activeElement.type&&\"color\"!=document.activeElement.type)&&(e=getElement(\"state\"))&&(e.innerHTML=req.responseText),clearTimeout(firstTime),clearTimeout(lastTime),lastTime=setTimeout(showState,3e3))},req.open(\"GET\",\"index?state=1\",!0),req.send(),firstTime=setTimeout(showState,3e3)}function fmtUpTime(e){var t,n,o=Math.floor(e/86400);return e%=86400,t=Math.floor(e/3600),e%=3600,n=Math.floor(e/60),e=e%60,0<o?o+` days, ${t} hours, ${n} minutes and ${e} seconds`:0<t?t+` hours, ${n} minutes and ${e} seconds`:0<n?n+` minutes and ${e} seconds`:`just ${e} seconds`}function updateOnlineFor(){onlineForEl.textContent=fmtUpTime(++onlineFor)}function onLoad(){(onlineForEl=getElement(\"onlineFor\"))&&(onlineFor=parseInt(onlineForEl.dataset.initial,10))&&setInterval(updateOnlineFor,1e3),showState()}window.addEventListener(\"load\",onLoad),history.pushState(null,\"\",\"index\"),setTimeout(()=>{var e=getElement(\"changed\");e&&(e.innerHTML=\"\")},5e3);</script>";
const char pageScript[] = "<script type='text/javascript'>var firstTime,lastTime,onlineFor,req=null,onlineForEl=null,getElement=e=>document.getElementById(e);function showState(){clearTimeout(firstTime),clearTimeout(lastTime),null!=req&&req.abort(),(req=new XMLHttpRequest).onreadystatechange=()=>{var e;4==req.readyState&&200==req.status&&((\"INPUT\"!=document.activeElement.tagName||\"number\"!=document.activeElement.type&&\"color\"!=document.activeElement.type)&&(e=getElement(\"state\"))&&(e.innerHTML=req.responseText),clearTimeout(firstTime),clearTimeout(lastTime),lastTime=setTimeout(showState,3e3))},req.open(\"GET\",\"index?state=1\",!0),req.send(),firstTime=setTimeout(showState,3e3)}function fmtUpTime(e){var t,n,o=Math.floor(e/86400);return e%=86400,t=Math.floor(e/3600),e%=3600,n=Math.floor(e/60),e=e%60,0<o?o+` days, ${t} hours, ${n} minutes and ${e} seconds`:0<t?t+` hours, ${n} minutes and ${e} seconds`:0<n?n+` minutes and ${e} seconds`:`just ${e} seconds`}function updateOnlineFor(){onlineForEl.textContent=fmtUpTime(++onlineFor)}function onLoad(){(onlineForEl=getElement(\"onlineFor\"))&&(onlineFor=parseInt(onlineForEl.dataset.initial,10))&&setInterval(updateOnlineFor,1e3),showState()}function submitTemperature(e){var t=getElement(\"form132\");getElement(\"kelvin132\").value=Math.round(1e6/parseInt(e.value)),t.submit()}window.addEventListener(\"load\",onLoad),history.pushState(null,\"\",\"index\"),setTimeout(()=>{var e=getElement(\"changed\");e&&(e.innerHTML=\"\")},5e3);</script>";
//region_end pageScript
//region_start ha_discovery_script

View File

@@ -77,6 +77,13 @@ function onLoad() {
showState();
}
function submitTemperature(slider) {
var form = getElement("form132");
var kelvinField = getElement("kelvin132");
kelvinField.value = Math.round(1000000 / parseInt(slider.value));
form.submit();
}
window.addEventListener("load", onLoad);
history.pushState(null, "", "index"); // drop actions like 'toggle' from URL