added support for the LDR based brightness dimming

This commit is contained in:
Jens Noack 2020-10-28 09:17:33 +01:00
parent 949942ac32
commit 568ddf6810
4 changed files with 123 additions and 20 deletions

@ -0,0 +1 @@
Subproject commit 76e457593e889885fd410fdbcdd659706a1eceb8

View file

@ -1,6 +1,8 @@
#ifndef LED_DISPLAY_CONTROLLER #ifndef LED_DISPLAY_CONTROLLER
#define LED_DISPLAY_CONTROLLER #define LED_DISPLAY_CONTROLLER
#define LDR_PIN 36
#include <Arduino.h> #include <Arduino.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h> #include <Adafruit_NeoMatrix.h>
@ -34,6 +36,8 @@ public:
textpixel = 0; textpixel = 0;
disp_show = false; disp_show = false;
this->brightness_init();
this->disp_init(); this->disp_init();
// create updater task // create updater task
@ -98,6 +102,13 @@ private:
TaskHandle_t displayUpdateTask; TaskHandle_t displayUpdateTask;
NeoPixelBrightnessBusGfx<NeoGrbFeature, Neo800KbpsMethod> *matrix; NeoPixelBrightnessBusGfx<NeoGrbFeature, Neo800KbpsMethod> *matrix;
// brigthness meassurement collection set
static const uint8_t meas_slots = 5;
uint8_t current_meas_slot;
uint16_t brightness_levels[meas_slots];
float brightness_adjust;
long last_brightness_meas;
// matrix variables // matrix variables
uint16_t text_curr_nr; uint16_t text_curr_nr;
uint32_t text_set_starttime; uint32_t text_set_starttime;
@ -117,9 +128,12 @@ private:
// matrix control // matrix control
//uint16_t remap(uint16_t x, uint16_t y); //uint16_t remap(uint16_t x, uint16_t y);
void disp_init(); void disp_init();
void brightness_init();
void disp_start_set(); void disp_start_set();
void disp_scroll_text(); void disp_scroll_text();
void show_matrix(const char *text, int pos, const char *color); void show_matrix(const char *text, int pos, const char *color);
void measureBrightnessEnv( void );
uint8_t adjustBrightnessToEnv( void );
uint16_t colorFromHex(String hex); uint16_t colorFromHex(String hex);
// storage structs // storage structs

View file

@ -9,6 +9,7 @@ void LedDisplayController::loop()
//Serial.printf("Ligth: %d \n" , analogRead(36)); //Serial.printf("Ligth: %d \n" , analogRead(36));
if (this->disp_show) if (this->disp_show)
{ {
measureBrightnessEnv();
disp_start_set(); disp_start_set();
if (true == text_sets.sets[text_curr_nr].active) if (true == text_sets.sets[text_curr_nr].active)
{ {
@ -60,7 +61,7 @@ bool LedDisplayController::setBrightness(int brightness)
this->text_sets.disp_brightness = brightness; this->text_sets.disp_brightness = brightness;
this->matrix->SetBrightness(pow (2, (this->text_sets.disp_brightness / RFade)) - 1); this->matrix->SetBrightness( adjustBrightnessToEnv() );
this->matrix->Show(); this->matrix->Show();
this->storeTextSets(); this->storeTextSets();
return true; return true;
@ -75,12 +76,24 @@ void LedDisplayController::setAutomaticBrightnessAdjustment(bool automaticBright
{ {
this->text_sets.disp_automatic_brightness_adjustment = automaticBrightnessAdjustment; this->text_sets.disp_automatic_brightness_adjustment = automaticBrightnessAdjustment;
this->storeTextSets(); this->storeTextSets();
brightness_init();
measureBrightnessEnv();
} }
// ------------------ // ------------------
// - Matrix control - // - Matrix control -
// ------------------ // ------------------
void LedDisplayController::brightness_init()
{
for(uint8_t i = 0; i < meas_slots; i++)
this->brightness_levels[i] = analogRead(LDR_PIN);
this->current_meas_slot = 0;
this->last_brightness_meas = millis();
measureBrightnessEnv();
}
void LedDisplayController::disp_init() void LedDisplayController::disp_init()
{ {
text_curr_nr = 0; text_curr_nr = 0;
@ -143,20 +156,27 @@ void LedDisplayController::disp_start_set()
textpixel = 6 * strlen(text_sets.sets[text_curr_nr].text); textpixel = 6 * strlen(text_sets.sets[text_curr_nr].text);
// scrolling text has always left or right allignment - depending on direction of scroll // scrolling text has always left or right allignment - depending on direction of scroll
if (true == text_sets.sets[text_curr_nr].scroll) if (true == text_sets.sets[text_curr_nr].scroll)
text_sets.sets[text_curr_nr].alignment = AlignRight;
switch (text_sets.sets[text_curr_nr].alignment)
{ {
case AlignLeft: if(0 == text_sets.sets[text_curr_nr].scrollDirection)
text_pos = 0; text_pos = this->matrix->width();
break; else
case AlignRight: text_pos = 0 - textpixel;
text_pos = this->matrix->width(); }
break; else
case AlignCenter: {
text_pos = this->matrix->width() - textpixel; switch (text_sets.sets[text_curr_nr].alignment)
text_pos = text_pos / 2; {
break; case AlignLeft:
text_pos = 0;
break;
case AlignRight:
text_pos = this->matrix->width() - textpixel;
break;
case AlignCenter:
text_pos = this->matrix->width() - textpixel;
text_pos = text_pos / 2;
break;
}
} }
show_matrix(text_sets.sets[text_curr_nr].text, text_pos, text_sets.sets[text_curr_nr].color); show_matrix(text_sets.sets[text_curr_nr].text, text_pos, text_sets.sets[text_curr_nr].color);
@ -180,12 +200,25 @@ void LedDisplayController::disp_scroll_text()
//Serial.printf("speed %d, waittime: %d' \n", text_sets.sets[text_curr_nr].scrollSpeed, display_show_wait_ms); //Serial.printf("speed %d, waittime: %d' \n", text_sets.sets[text_curr_nr].scrollSpeed, display_show_wait_ms);
this->matrix->fillScreen(0); this->matrix->fillScreen(0);
show_matrix(text_sets.sets[text_curr_nr].text, text_pos, text_sets.sets[text_curr_nr].color); show_matrix(text_sets.sets[text_curr_nr].text, text_pos, text_sets.sets[text_curr_nr].color);
(int)text_pos--; if(0==text_sets.sets[text_curr_nr].scrollDirection)
if (int(text_pos + textpixel) < 0)
{ {
text_pos = this->matrix->width(); (int)text_pos--;
text_pass++; if (int(text_pos + textpixel) < 0)
//Serial.printf("Pass[%d] - set nr %d, Text: '%s' \n", text_pass, text_curr_nr, text_sets.sets[text_curr_nr].text); {
text_pos = this->matrix->width();
text_pass++;
//Serial.printf("Pass[%d] - set nr %d, Text: '%s' \n", text_pass, text_curr_nr, text_sets.sets[text_curr_nr].text);
}
}
else
{
(int)text_pos++;
if (text_pos > this->matrix->width())
{
text_pos = 0 - textpixel;
text_pass++;
//Serial.printf("Pass[%d] - set nr %d, Text: '%s' \n", text_pass, text_curr_nr, text_sets.sets[text_curr_nr].text);
}
} }
} }
} }
@ -193,7 +226,7 @@ void LedDisplayController::disp_scroll_text()
void LedDisplayController::show_matrix(const char *text, int pos, const char *color) void LedDisplayController::show_matrix(const char *text, int pos, const char *color)
{ {
//Serial.printf("TEXT: %s (pos=%d, color=%d)\n", text, pos, this->colorFromHex(String(color))); //Serial.printf("TEXT: %s (pos=%d, color=%d)\n", text, pos, this->colorFromHex(String(color)));
this->matrix->SetBrightness(pow (2, (this->text_sets.disp_brightness / RFade)) - 1); this->matrix->SetBrightness( adjustBrightnessToEnv() );
this->matrix->setTextColor(this->colorFromHex(String(color))); this->matrix->setTextColor(this->colorFromHex(String(color)));
this->matrix->setCursor(pos, 0); this->matrix->setCursor(pos, 0);
this->matrix->print(text); this->matrix->print(text);
@ -202,6 +235,60 @@ void LedDisplayController::show_matrix(const char *text, int pos, const char *co
//portENABLE_INTERRUPTS(); //portENABLE_INTERRUPTS();
} }
uint8_t LedDisplayController::adjustBrightnessToEnv( void )
{
uint8_t brightness = ( pow (2, (this->text_sets.disp_brightness / RFade)) ) ;
uint8_t brightness_adj = brightness * this->brightness_adjust ;
//Serial.printf("brightness without adjust : %d . Adjust is: %f. Brightness with adjust : %d .\n", brightness, this->brightness_adjust, brightness_adj);
return brightness_adj;
}
void LedDisplayController::measureBrightnessEnv( void )
{
if(true == this->text_sets.disp_automatic_brightness_adjustment)
{
if( millis() - this->last_brightness_meas > 1000)
{
this->brightness_levels[this->current_meas_slot] = analogRead(LDR_PIN);
//Serial.printf("brightness last measured value = %d\n", brightness_levels[current_meas_slot]);
this->current_meas_slot = this->current_meas_slot+1>=this->meas_slots? 0 : this->current_meas_slot+1;
this->last_brightness_meas = millis();
uint16_t smallest = brightness_levels[0];
uint16_t biggest = brightness_levels[0];
uint64_t sum = brightness_levels[0];
for(uint8_t i = 1; i < this->meas_slots; i++)
{
sum = sum + this->brightness_levels[i] ;
if( this->brightness_levels[i] < smallest )
smallest = this->brightness_levels[i];
else
{
if( this->brightness_levels[i] > biggest )
biggest = this->brightness_levels[i];
}
}
sum = sum - smallest - biggest;
sum = sum / (this->meas_slots - 2);
//Serial.printf("brightness median value = %llu \n", sum);
float adjust = ( ( ( ( 100.0/ ( 4095.0 / 2.0 ) ) * (float)sum ) ) / 100.0 );
adjust = adjust > 1.0 ? 1.0 : adjust ;
adjust = adjust < 0.2 ? 0.1 : adjust ;
this->brightness_adjust = adjust;
Serial.printf("brightness_adjust = %f\n", this->brightness_adjust);
}
}
else
{
this->brightness_adjust = 1.0;
}
}
uint16_t LedDisplayController::colorFromHex(String hex) uint16_t LedDisplayController::colorFromHex(String hex)
{ {

View file

@ -36,6 +36,7 @@ void setup()
void loop() void loop()
{ {
// nothing to do in loop // nothing to do in loop
//Serial.printf("LDR value is: %d\n", analogRead(LDR_PIN));
display->loop(); display->loop();
delay(1); delay(1);
} }