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
8991MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE , CS_PIN , MAX_DEVICES );
9092
93+ #define LIGHT_SENSOR_PIN GPIO_NUM_34
94+
9195void 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}
420429void 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+ }
0 commit comments