Skip to content

Commit

Permalink
Merge pull request #47 from andysheen/master
Browse files Browse the repository at this point in the history
added logarithmic scaling to remote led fading
  • Loading branch information
andysheen authored Oct 1, 2020
2 parents 840c4f8 + e255bd7 commit 3d0668e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
1 change: 1 addition & 0 deletions ESP32-SOCKETIO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ using namespace ace_button;
#define USERLED 0
#define REMOTELED 1
#define RGBLEDPWMSTART 120
#define FASTLONGFADE 120
#define LONGFADEMINUTESMAX 360
#define LONGFADECHECKMILLIS 60000
unsigned long prevLongFadeVal = 0;
Expand Down
24 changes: 12 additions & 12 deletions captivePortal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class CaptiveRequestHandler : public AsyncWebHandler {
void handleRequest(AsyncWebServerRequest *request) {
Serial.print("handleRequest: ");
Serial.println(request->url());
if(!isResetting) {

if (!isResetting) {
if (request->method() == HTTP_GET) {
if (request->url() == "/credentials") getCredentials(request);
else if(request->url() == "/reboot") {
request->send(200);
socket_server.textAll("RESTART");
softReset();
else if (request->url() == "/reboot") {
request->send(200);
socket_server.textAll("RESTART");
softReset();
}
else if (request->url() == "/scan") getScan(request);
else if (SPIFFS.exists(request->url())) sendFile(request, request->url());
Expand All @@ -27,7 +27,7 @@ class CaptiveRequestHandler : public AsyncWebHandler {
}
else if (request->url().endsWith("connecttest.txt") || request->url().endsWith("ncsi.txt")) {
request->send(200, "text/plain", "Microsoft NCSI");
} else if (strstr(request->url().c_str(), "generate_204_") != NULL) {
} else if (strstr(request->url().c_str(), "generate_204_") != NULL) {
Serial.println("you must be huawei!");
sendFile(request, "/index.html");
}
Expand All @@ -42,15 +42,15 @@ class CaptiveRequestHandler : public AsyncWebHandler {
Serial.print("handleBody: ");
Serial.println(request->url());

if(!isResetting) {
if (!isResetting) {
if (request->method() == HTTP_POST) {
if (request->url() == "/credentials") {
String json = "";
for (int i = 0; i < len; i++) json += char(data[i]);

StaticJsonDocument<1024> settingsJsonDoc;
if (!deserializeJson(settingsJsonDoc, json)) {
if(setCredentials(settingsJsonDoc.as<JsonObject>())) request->send(200);
if (setCredentials(settingsJsonDoc.as<JsonObject>())) request->send(200);
else request->send(400);
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ class CaptiveRequestHandler : public AsyncWebHandler {

bool setCredentials(JsonVariant json) {
bool result = false;

Serial.println("setCredentials");

String local_ssid = json["local_ssid"].as<String>();
Expand Down Expand Up @@ -141,7 +141,7 @@ class CaptiveRequestHandler : public AsyncWebHandler {
result = true;
}

return(result);
return (result);
}

void getScan(AsyncWebServerRequest * request) {
Expand Down
54 changes: 53 additions & 1 deletion rgbled.ino
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void longFadeHandler() {
if (currLongFadeVal != prevLongFadeVal) {
prevLongFadeVal = currLongFadeVal;
currLongFadeVal = currLongFadeVal - 1;
value[REMOTELED] = (byte)currLongFadeVal;
value[REMOTELED] = (byte)fscale(0,RGBLEDPWMSTART,0,RGBLEDPWMSTART,currLongFadeVal,-3);
ledChanged[REMOTELED] = true;
Serial.println(value[REMOTELED]);
}
Expand All @@ -126,3 +126,55 @@ void longFadeHandler() {
}
}
}


int fscale(float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}

// Zero Refference the values
OriginalRange = originalMax - originalMin;

if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}

if (invFlag == 0) {
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;

}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}

return (int)rangedValue;
}

0 comments on commit 3d0668e

Please sign in to comment.