Skip to content

Commit 0eddc06

Browse files
committed
auto brightness
add option to control brightness automatically from an LDR on pin 34. Note this adds a new value to the globalconf structure, so this breaks the layout of stored settings and will break them all.
1 parent 6a9b68e commit 0eddc06

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

html/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ <h1 id="centertext">Matrix Display</h1>
216216
<input type="date" id="datep" name="datep"><br>
217217
<label for="brightness">Brightness:</label>
218218
<input type="range" min="0" max="15" value="3" class="slider" name="brightness" id="brightness">
219+
<label for="autoBrightness">Auto Brightness:</label>
220+
<input type="checkbox" id="autoBrightness" name="autoBrightness" value="autoBrightness"><br><br>
219221
<input class="block" type="submit" value="Save">
220222
<br>
221223
<div style="overflow-y: scroll; height:400px;">
@@ -379,6 +381,7 @@ <h1 id="centertext">Matrix Display</h1>
379381
*/
380382

381383
document.getElementById("brightness").value = data.brightness;
384+
document.getElementById("autoBrightness").checked = data.autoBrightness;
382385
document.getElementById("cssid").value = data["cssid"];
383386
document.getElementById("cpassword").value = data["cpassword"];
384387
document.getElementById("datep").valueAsDate = new Date(data["datep"]);

src/index.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ const char index_html[] PROGMEM = R"rawliteral(
217217
<input type="date" id="datep" name="datep"><br>
218218
<label for="brightness">Brightness:</label>
219219
<input type="range" min="0" max="15" value="3" class="slider" name="brightness" id="brightness">
220+
<label for="autoBrightness">Auto Brightness:</label>
221+
<input type="checkbox" id="autoBrightness" name="autoBrightness" value="autoBrightness"><br><br>
220222
<input class="block" type="submit" value="Save">
221223
<br>
222224
<div style="overflow-y: scroll; height:400px;">
@@ -380,6 +382,7 @@ const char index_html[] PROGMEM = R"rawliteral(
380382
*/
381383

382384
document.getElementById("brightness").value = data.brightness;
385+
document.getElementById("autoBrightness").checked = data.autoBrightness;
383386
document.getElementById("cssid").value = data["cssid"];
384387
document.getElementById("cpassword").value = data["cpassword"];
385388
document.getElementById("datep").valueAsDate = new Date(data["datep"]);

src/matrixdisplay.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct
5858
{
5959
int pos;
6060
int brightness;
61+
bool autoBrightness;
62+
int autoBrightnessValue;
6163
char cssid[256];
6264
char cpassword[256];
6365
char date[32];
@@ -88,6 +90,8 @@ struct message messages[NUM_MESSAGES] = {0};
8890

8991
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
9092

93+
#define LIGHT_SENSOR_PIN GPIO_NUM_34
94+
9195
void handleroot()
9296
{
9397
server.send(200, "text/html", index_html);
@@ -118,10 +122,12 @@ void handlein()
118122
String body = server.arg("plain");
119123
deserializeJson(jsonDocument, body);
120124
int brightness = jsonDocument["brightness"];
125+
bool autoBrightness = jsonDocument["autoBrightness"];
121126
String cssid = jsonDocument["cssid"];
122127
String cpassword = jsonDocument["cpassword"];
123128
String date = jsonDocument["datep"];
124129
globalconf.brightness = brightness;
130+
globalconf.autoBrightness = autoBrightness;
125131
strncpy(globalconf.cssid, cssid.c_str(), 256);
126132
strncpy(globalconf.cpassword, cpassword.c_str(), 256);
127133
strncpy(globalconf.date, date.c_str(), 32);
@@ -196,6 +202,7 @@ void handleconfigout()
196202
{
197203
jsonDocument.clear();
198204
jsonDocument["brightness"] = globalconf.brightness;
205+
jsonDocument["autoBrightness"] = globalconf.autoBrightness;
199206
jsonDocument["cssid"] = globalconf.cssid;
200207
jsonDocument["cpassword"] = globalconf.cpassword;
201208
jsonDocument["datep"] = globalconf.date;
@@ -416,6 +423,8 @@ void setup()
416423
0); /* pin task to core 0 */
417424

418425
myDisplay.setTextAlignment(PA_LEFT);
426+
427+
pinMode(LIGHT_SENSOR_PIN, INPUT);
419428
}
420429
void saveconfig()
421430
{
@@ -442,6 +451,7 @@ void defaultdata()
442451
{
443452
globalconf.pos = 0;
444453
globalconf.brightness = 4;
454+
globalconf.autoBrightness = false;
445455
strncpy(globalconf.cssid, "Gensokyo", 256);
446456
strncpy(globalconf.cpassword, "passwordhere", 256);
447457
strncpy(globalconf.date, "0", 32);
@@ -575,7 +585,17 @@ void nextmessage()
575585
#ifdef VERTICAL
576586
strrev(msgbuff);
577587
#endif
578-
myDisplay.setIntensity(globalconf.brightness);
588+
if (globalconf.autoBrightness)
589+
{
590+
int lightValue = analogRead(LIGHT_SENSOR_PIN);
591+
int brightness = map(lightValue, 0, 950, 0, 15); // Map the light value to brightness range (0-15)
592+
printf("light value: %d\n", lightValue);
593+
printf("Brightness: %d\n", brightness);
594+
myDisplay.setIntensity(brightness);
595+
} else
596+
{
597+
myDisplay.setIntensity(globalconf.brightness);
598+
}
579599
myDisplay.setInvert(messages[globalconf.pos].invert);
580600
scrollAlign = PA_CENTER;
581601
myDisplay.displayText(msgbuff, scrollAlign, messages[globalconf.pos].speed, messages[globalconf.pos].scrollpause * 1000, messages[globalconf.pos].effect1, messages[globalconf.pos].effect2);
@@ -589,4 +609,4 @@ void loop()
589609
nextmessage();
590610
myDisplay.displayReset();
591611
}
592-
}
612+
}

src/networking.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)