LedDisplay/vscode/OmobiLEDdisplayBluetooth/src/LedDisplayController.cpp

258 lines
8.8 KiB
C++
Raw Normal View History

#include "LedDisplayController.h"
LedDisplayController *ledDisplayControllerGlobal = nullptr;
LedDisplayController::LedDisplayController(const byte pin)
{
ledDisplayControllerGlobal = this;
this->matrix = new Adafruit_NeoMatrix(8, 8, 1, 1, pin,
NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE +
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_TILE_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
this->matrix->begin();
this->matrix->setTextWrap(false);
this->matrix->setBrightness(40);
text_curr_nr = 0;
text_set_starttime = 0;
text_pass = 0;
textpixel = 0;
disp_show = false;
this->disp_init();
// create updater task
xTaskCreatePinnedToCore(updateDisplayGlobal, "DisplayUpdateTask", 10000, NULL, 1, &displayUpdateTask, 0);
}
void LedDisplayController::disp_update()
{
if (this->disp_show)
{
disp_start_set();
if (true == text_sets.sets[text_curr_nr].active)
{
if (text_sets.sets[text_curr_nr].text != '\0')
{
if (text_sets.sets[text_curr_nr].text_scroll)
{
disp_scroll_text();
}
}
}
}
}
void LedDisplayController::disp_init()
{
text_curr_nr = 0;
text_set_starttime = 0;
text_pass = 0;
text_pos = this->matrix->width();
disp_show = true;
}
void LedDisplayController::disp_scroll_text()
{
this->matrix->fillScreen(0);
show_matrix(text_sets.sets[text_curr_nr].text, text_pos, text_sets.sets[text_curr_nr].color);
(int)text_pos--;
if (int(text_pos + textpixel) < 0)
{
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);
}
delay(100);
}
void LedDisplayController::disp_start_set()
{
if ((0 == text_set_starttime) ||
(text_sets.sets[text_curr_nr].text == '\0') ||
text_sets.sets[text_curr_nr].active == false ||
((text_sets.sets[text_curr_nr].text_scroll == false) && (text_sets.sets[text_curr_nr].time_ms > 0) && ((millis() - text_set_starttime) >= text_sets.sets[text_curr_nr].time_ms)) ||
((text_sets.sets[text_curr_nr].text_scroll == true) && (text_sets.sets[text_curr_nr].text_scroll_pass > 0) && (text_pass >= text_sets.sets[text_curr_nr].text_scroll_pass)) ||
(((text_sets.sets[text_curr_nr].text_scroll_pass == 0) || text_sets.sets[text_curr_nr].text_scroll == false) && (text_sets.sets[text_curr_nr].time_ms == 0) && ((millis() - text_set_starttime) >= 10000)))
{
//Serial.printf("[%lu] Meet start set condition. Curr set is %d. \n", millis(), text_curr_nr);
if (0 < text_set_starttime || text_sets.sets[text_curr_nr].text == '\0' || text_sets.sets[text_curr_nr].active == false)
text_curr_nr++;
if (text_curr_nr == text_nr_sets)
text_curr_nr = 0;
text_pass = 0;
if (text_sets.sets[text_curr_nr].text != '\0' && text_sets.sets[text_curr_nr].active == true)
{
Serial.printf("[%lu] Set %d. Runtime %d. Text:'%s'\n", millis(), text_curr_nr, text_sets.sets[text_curr_nr].time_ms, text_sets.sets[text_curr_nr].text);
this->matrix->fillScreen(0);
textpixel = 6 * strlen(text_sets.sets[text_curr_nr].text);
switch (text_sets.sets[text_curr_nr].align)
{
case TEXTLEFT:
text_pos = 0;
break;
case TEXTRIGHT:
text_pos = this->matrix->width();
break;
case TEXTCENTER:
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);
text_set_starttime = millis();
}
}
else {
//Serial.printf("[%lu] Don't meet start set condition. Text is: %s. Active is: %d\n", millis(), text_sets.sets[text_curr_nr].text, text_sets.sets[text_curr_nr].active);
}
}
void LedDisplayController::show_matrix(const char *text, int pos, uint16_t color)
{
Serial.printf("TEXT: %s (pos=%d, color=%d)\n", text, pos, color);
this->matrix->setTextColor(color);
this->matrix->setCursor(pos, 0);
this->matrix->print(text);
portDISABLE_INTERRUPTS();
this->matrix->show();
portENABLE_INTERRUPTS();
}
void LedDisplayController::storeDisplaySet()
{
// write conf to EEPROM
EEPROM.begin(DISP_STRUCT_SIZE);
//for (size_t i = 0 ; i < DISP_STRUCT_SIZE ; i++)
//{
// EEPROM.write(i, 0);
//}
strncpy(text_sets.valid, "OK", sizeof(text_sets.valid));
EEPROM.put(0, text_sets);
EEPROM.commit();
EEPROM.end();
}
bool LedDisplayController::loadDisplaySet()
{
bool rc = false;
sets_t buf = {};
// read conf from EEPROM
EEPROM.begin(DISP_STRUCT_SIZE);
EEPROM.get(0, buf);
EEPROM.end();
if (strcmp(buf.valid, "OK") == 0)
{
rc = true;
memcpy(&text_sets, &buf, sizeof(text_sets));
}
else
{
memset(&text_sets, 0, sizeof(text_sets));
}
return rc;
}
String LedDisplayController::get_paramstring_from_struct(String name)
{
return (getset_param_at_struct(name, "", false));
}
void LedDisplayController::set_param_to_struct(String name, String value)
{
getset_param_at_struct(name, value, true);
}
String LedDisplayController::getset_param_at_struct(String name, String value, bool set)
{
String name_value = "";
int name_setnr_index = name.lastIndexOf("_");
int name_setnr = -1;
int name_index = -1;
String name_name = "unknown";
if (name_setnr_index > 0)
{
name_setnr = name.substring(name_setnr_index + 1).toInt();
name_name = name.substring(0, name_setnr_index + 1);
for (int pnr = 0; pnr < nr_param_names; pnr++)
{
if (name_name == set_param_name[pnr])
{
name_index = pnr;
break;
}
}
//Serial.printf("Param: (name=%s,set=%d, index=%d) %s = %s \n", name_name.c_str(), name_setnr, name_index, name.c_str(), value.c_str());
if (name_index != -1 && name_setnr != -1 && name_name != "unknown")
{
name_value += name_name + String(name_setnr) + "=";
switch (name_index)
{
case PCOLOR:
//if (true == set)
//text_sets.sets[name_setnr].color = (unint16_t)value.toInt();
name_value += String(text_sets.sets[name_setnr].color);
break;
case PTEXT:
if (true == set)
snprintf((char *)text_sets.sets[name_setnr].text, MAX_TXT_LENGTH, value.c_str());
name_value += String(text_sets.sets[name_setnr].text);
break;
case PTIME:
if (true == set)
text_sets.sets[name_setnr].time_ms = value.toInt() * 1000;
name_value += String(text_sets.sets[name_setnr].time_ms / 1000);
break;
case PSCROLL:
if (true == set)
text_sets.sets[name_setnr].text_scroll = (value == "true") ? true : false;
name_value += String(text_sets.sets[name_setnr].text_scroll);
break;
case PACTIVE:
if (true == set)
text_sets.sets[name_setnr].active = (value == "true") ? true : false;
name_value += String(text_sets.sets[name_setnr].active);
break;
case PALIGN:
if (true == set)
text_sets.sets[name_setnr].align = (text_align_t)value.toInt();
name_value += String(text_sets.sets[name_setnr].align);
break;
case PSCROLL_RUNS:
if (true == set)
text_sets.sets[name_setnr].text_scroll_pass = value.toInt();
name_value += String(text_sets.sets[name_setnr].text_scroll_pass);
break;
}
//Serial.printf("get/set %s\n", name_value.c_str());
}
}
return (name_value);
}
void LedDisplayController::setTexts(sets_t texts) {
this->text_sets = texts;
this->storeDisplaySet();
}
LedDisplayController::sets_t LedDisplayController::getTexts() {
return this->text_sets;
}
uint16_t LedDisplayController::Color(uint8_t r, uint8_t g, uint8_t b) {
return this->matrix->Color(r,g,b);
}
void updateDisplayGlobal(void *)
{
for(;;) {
esp_task_wdt_reset();
delay(1);
ledDisplayControllerGlobal->disp_update();
}
}