2018-08-28 17:13:14 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdsp_03_matrix.ino - Display 8x8 matrix support for Tasmota
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends and Adafruit
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_DISPLAY
|
|
|
|
#ifdef USE_DISPLAY_MATRIX
|
|
|
|
|
|
|
|
#define XDSP_03 3
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_05 5 // See I2CDEVICES.md
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
#define MTX_MAX_SCREEN_BUFFER 80
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_LEDBackpack.h> // 8x8 Matrix
|
|
|
|
|
|
|
|
Adafruit_8x8matrix *matrix[8];
|
|
|
|
uint8_t mtx_matrices = 0;
|
|
|
|
uint8_t mtx_state = 0;
|
|
|
|
uint8_t mtx_counter = 0;
|
|
|
|
int16_t mtx_x = 0;
|
|
|
|
int16_t mtx_y = 0;
|
|
|
|
|
2018-11-27 13:38:09 +00:00
|
|
|
//char mtx_buffer[MTX_MAX_SCREEN_BUFFER];
|
2019-03-26 17:26:50 +00:00
|
|
|
char *mtx_buffer = nullptr;
|
2018-11-27 13:38:09 +00:00
|
|
|
|
2018-08-28 17:13:14 +01:00
|
|
|
uint8_t mtx_mode = 0;
|
|
|
|
uint8_t mtx_loop = 0;
|
2018-08-31 11:17:09 +01:00
|
|
|
uint8_t mtx_done = 0;
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixWrite(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->writeDisplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixClear(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->clear();
|
|
|
|
}
|
|
|
|
MatrixWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixFixed(char* txt)
|
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->clear();
|
|
|
|
matrix[i]->setCursor(-i *8, 0);
|
|
|
|
matrix[i]->print(txt);
|
|
|
|
matrix[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
}
|
|
|
|
MatrixWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixCenter(char* txt)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
int len = strlen(txt);
|
|
|
|
offset = (len < 8) ? offset = ((mtx_matrices *8) - (len *6)) / 2 : 0;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->clear();
|
|
|
|
matrix[i]->setCursor(-(i *8)+offset, 0);
|
|
|
|
matrix[i]->print(txt);
|
|
|
|
matrix[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
}
|
|
|
|
MatrixWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixScrollLeft(char* txt, int loop)
|
|
|
|
{
|
|
|
|
switch (mtx_state) {
|
|
|
|
case 1:
|
|
|
|
mtx_state = 2;
|
|
|
|
// Horiz. position of text -- starts off right edge
|
|
|
|
mtx_x = 8 * mtx_matrices;
|
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "[%s]"), txt);
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
disp_refresh = Settings.display_refresh;
|
|
|
|
case 2:
|
|
|
|
disp_refresh--;
|
|
|
|
if (!disp_refresh) {
|
|
|
|
disp_refresh = Settings.display_refresh;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->clear();
|
|
|
|
matrix[i]->setCursor(mtx_x - i *8, 0);
|
|
|
|
matrix[i]->print(txt);
|
|
|
|
matrix[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
}
|
|
|
|
MatrixWrite();
|
|
|
|
// Move text position left by 1 pixel.
|
|
|
|
mtx_x--;
|
|
|
|
int16_t len = strlen(txt);
|
2018-08-31 11:17:09 +01:00
|
|
|
if (mtx_x < -(len *6)) { mtx_state = loop; }
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixScrollUp(char* txt, int loop)
|
|
|
|
{
|
|
|
|
int wordcounter = 0;
|
|
|
|
char tmpbuf[200];
|
|
|
|
char *words[100];
|
|
|
|
// char separators[] = " ,.;:!?";
|
|
|
|
// char separators[] = " ";
|
|
|
|
// char separators[] = " /|";
|
|
|
|
char separators[] = " /";
|
|
|
|
|
|
|
|
switch (mtx_state) {
|
|
|
|
case 1:
|
|
|
|
mtx_state = 2;
|
|
|
|
// Vertical position of text -- starts off left bottom edge
|
|
|
|
mtx_y = 8;
|
|
|
|
mtx_counter = 0;
|
|
|
|
disp_refresh = Settings.display_refresh;
|
|
|
|
case 2:
|
|
|
|
disp_refresh--;
|
|
|
|
if (!disp_refresh) {
|
|
|
|
disp_refresh = Settings.display_refresh;
|
|
|
|
strlcpy(tmpbuf, txt, sizeof(tmpbuf));
|
|
|
|
char *p = strtok(tmpbuf, separators);
|
2019-03-26 17:26:50 +00:00
|
|
|
while (p != nullptr && wordcounter < 40) {
|
2018-08-28 17:13:14 +01:00
|
|
|
words[wordcounter++] = p;
|
2019-03-26 17:26:50 +00:00
|
|
|
p = strtok(nullptr, separators);
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->clear();
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t j = 0; j < wordcounter; j++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->setCursor(-i *8, mtx_y + (j *8));
|
|
|
|
matrix[i]->println(words[j]);
|
|
|
|
}
|
|
|
|
matrix[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
}
|
|
|
|
MatrixWrite();
|
|
|
|
if (((mtx_y %8) == 0) && mtx_counter) {
|
|
|
|
mtx_counter--;
|
|
|
|
} else {
|
|
|
|
mtx_y--; // Move text position up by 1 pixel.
|
|
|
|
mtx_counter = STATES * 1; // Hold text for 1 seconds
|
|
|
|
}
|
2018-08-31 11:17:09 +01:00
|
|
|
if (mtx_y < -(wordcounter *8)) { mtx_state = loop; }
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixInitMode(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < mtx_matrices; i++) {
|
2018-08-28 17:13:14 +01:00
|
|
|
matrix[i]->setRotation(Settings.display_rotate); // 1
|
|
|
|
matrix[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
matrix[i]->blinkRate(0); // 0 - 3
|
|
|
|
matrix[i]->setTextWrap(false); // Allow text to run off edges
|
|
|
|
// matrix[i]->setTextSize(Settings.display_size);
|
|
|
|
// matrix[i]->setTextColor(LED_RED);
|
|
|
|
matrix[i]->cp437(true);
|
|
|
|
}
|
|
|
|
MatrixClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixInit(uint8_t mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case DISPLAY_INIT_MODE:
|
|
|
|
MatrixInitMode();
|
|
|
|
break;
|
|
|
|
case DISPLAY_INIT_PARTIAL:
|
|
|
|
case DISPLAY_INIT_FULL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixInitDriver(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2018-11-27 13:38:09 +00:00
|
|
|
mtx_buffer = (char*)(malloc(MTX_MAX_SCREEN_BUFFER));
|
2019-03-26 17:26:50 +00:00
|
|
|
if (mtx_buffer != nullptr) {
|
2018-11-27 13:38:09 +00:00
|
|
|
if (!Settings.display_model) {
|
2019-11-06 16:48:38 +00:00
|
|
|
if (I2cSetDevice(Settings.display_address[1])) {
|
2018-11-27 13:38:09 +00:00
|
|
|
Settings.display_model = XDSP_03;
|
|
|
|
}
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 13:38:09 +00:00
|
|
|
if (XDSP_03 == Settings.display_model) {
|
|
|
|
mtx_state = 1;
|
|
|
|
for (mtx_matrices = 0; mtx_matrices < 8; mtx_matrices++) {
|
|
|
|
if (Settings.display_address[mtx_matrices]) {
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(Settings.display_address[mtx_matrices], "8x8Matrix");
|
2018-11-27 13:38:09 +00:00
|
|
|
matrix[mtx_matrices] = new Adafruit_8x8matrix();
|
|
|
|
matrix[mtx_matrices]->begin(Settings.display_address[mtx_matrices]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:05:42 +01:00
|
|
|
Settings.display_width = mtx_matrices * 8;
|
|
|
|
Settings.display_height = 8;
|
|
|
|
|
2018-11-27 13:38:09 +00:00
|
|
|
MatrixInitMode();
|
|
|
|
}
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixOnOff(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!disp_power) { MatrixClear(); }
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixDrawStringAt(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t flag)
|
|
|
|
{
|
2019-03-08 18:24:02 +00:00
|
|
|
strlcpy(mtx_buffer, str, MTX_MAX_SCREEN_BUFFER);
|
2018-08-31 11:17:09 +01:00
|
|
|
mtx_mode = x &1; // Use x for selecting scroll up (0) or scroll left (1)
|
|
|
|
mtx_loop = y &1; // Use y for selecting no loop (0) or loop (1)
|
2018-08-28 17:13:14 +01:00
|
|
|
if (!mtx_state) { mtx_state = 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
void MatrixPrintLog(uint8_t direction)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2018-08-31 11:17:09 +01:00
|
|
|
char* txt = (!mtx_done) ? DisplayLogBuffer('\370') : mtx_buffer;
|
2019-03-26 17:26:50 +00:00
|
|
|
if (txt != nullptr) {
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!mtx_state) { mtx_state = 1; }
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!mtx_done) {
|
2018-08-28 17:13:14 +01:00
|
|
|
// Remove extra spaces
|
|
|
|
uint8_t space = 0;
|
|
|
|
uint8_t max_cols = (disp_log_buffer_cols < MTX_MAX_SCREEN_BUFFER) ? disp_log_buffer_cols : MTX_MAX_SCREEN_BUFFER;
|
|
|
|
mtx_buffer[0] = '\0';
|
2018-09-05 17:01:15 +01:00
|
|
|
uint8_t i = 0;
|
|
|
|
while ((txt[i] != '\0') && (i < max_cols)) {
|
2018-08-31 11:17:09 +01:00
|
|
|
if (txt[i] == ' ') {
|
2018-08-28 17:13:14 +01:00
|
|
|
space++;
|
|
|
|
} else {
|
|
|
|
space = 0;
|
|
|
|
}
|
|
|
|
if (space < 2) {
|
2018-11-27 13:38:09 +00:00
|
|
|
strncat(mtx_buffer, (const char*)txt +i, (strlen(mtx_buffer) < MTX_MAX_SCREEN_BUFFER -1) ? 1 : 0);
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2018-09-05 17:01:15 +01:00
|
|
|
i++;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "[%s]"), mtx_buffer);
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
mtx_done = 1;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2018-08-31 11:17:09 +01:00
|
|
|
|
|
|
|
if (direction) {
|
|
|
|
MatrixScrollUp(mtx_buffer, 0);
|
|
|
|
} else {
|
|
|
|
MatrixScrollLeft(mtx_buffer, 0);
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!mtx_state) { mtx_done = 0; }
|
2018-08-28 17:13:14 +01:00
|
|
|
} else {
|
|
|
|
char disp_time[9]; // 13:45:43
|
|
|
|
|
|
|
|
snprintf_P(disp_time, sizeof(disp_time), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
|
|
|
|
MatrixFixed(disp_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MatrixRefresh(void) // Every second
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
|
|
|
if (disp_power) {
|
|
|
|
switch (Settings.display_mode) {
|
|
|
|
case 0: {
|
|
|
|
switch (mtx_mode) {
|
|
|
|
case 0:
|
|
|
|
MatrixScrollLeft(mtx_buffer, mtx_loop);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
MatrixScrollUp(mtx_buffer, mtx_loop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
case 2: {
|
|
|
|
char disp_date[9]; // 24-04-17
|
|
|
|
snprintf_P(disp_date, sizeof(disp_date), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%02d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year -2000);
|
|
|
|
MatrixFixed(disp_date);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
char disp_day[10]; // Mon
|
|
|
|
snprintf_P(disp_day, sizeof(disp_day), PSTR("%d %s"), RtcTime.day_of_month, RtcTime.name_of_month);
|
|
|
|
MatrixCenter(disp_day);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4:
|
2018-08-31 11:17:09 +01:00
|
|
|
MatrixPrintLog(0);
|
2018-08-28 17:13:14 +01:00
|
|
|
break;
|
|
|
|
case 1: // Time and user text
|
|
|
|
case 5: // Time, user text and MQTT
|
2018-08-31 11:17:09 +01:00
|
|
|
MatrixPrintLog(1);
|
2018-08-28 17:13:14 +01:00
|
|
|
break;
|
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xdsp03(uint8_t function)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_05)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2019-11-04 09:38:05 +00:00
|
|
|
if (FUNC_DISPLAY_INIT_DRIVER == function) {
|
|
|
|
MatrixInitDriver();
|
|
|
|
}
|
|
|
|
else if (XDSP_03 == Settings.display_model) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_DISPLAY_MODEL:
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_INIT:
|
|
|
|
MatrixInit(dsp_init);
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_EVERY_50_MSECOND:
|
|
|
|
MatrixRefresh();
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_POWER:
|
|
|
|
MatrixOnOff();
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_DRAW_STRING:
|
|
|
|
MatrixDrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
|
|
|
|
break;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DISPLAY_MATRIX
|
|
|
|
#endif // USE_DISPLAY
|
|
|
|
#endif // USE_I2C
|