2020-04-10 09:02:23 +01:00
|
|
|
/*
|
2020-04-10 14:31:20 +01:00
|
|
|
xdsp_11_sevenseg.ino - Display seven segment support for Tasmota
|
2020-04-10 09:02:23 +01:00
|
|
|
|
|
|
|
Copyright (C) 2020 Theo Arends and Adafruit
|
|
|
|
|
|
|
|
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_SEVENSEG
|
|
|
|
|
|
|
|
#define XDSP_11 11
|
2020-04-10 14:31:20 +01:00
|
|
|
#define XI2C_47 47 // See I2CDEVICES.md
|
2020-04-10 09:02:23 +01:00
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_LEDBackpack.h> // Seven segment LED
|
|
|
|
|
2020-07-12 07:16:29 +01:00
|
|
|
Adafruit_7segment *sevenseg[8];
|
|
|
|
uint8_t sevensegs = 0;
|
2020-04-10 09:02:23 +01:00
|
|
|
uint8_t sevenseg_state = 0;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
void SevensegWrite(void)
|
|
|
|
{
|
2020-07-12 07:16:29 +01:00
|
|
|
for (uint32_t i = 0; i < sevensegs; i++) {
|
|
|
|
sevenseg[i]->writeDisplay();
|
|
|
|
}
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegClear(void)
|
|
|
|
{
|
2020-07-12 07:16:29 +01:00
|
|
|
for (uint32_t i = 0; i < sevensegs; i++) {
|
|
|
|
sevenseg[i]->clear();
|
|
|
|
}
|
2020-04-10 09:02:23 +01:00
|
|
|
SevensegWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
void SevensegInitMode(void)
|
|
|
|
{
|
2020-07-12 07:16:29 +01:00
|
|
|
for (uint32_t i = 0; i < sevensegs; i++) {
|
|
|
|
sevenseg[i]->setBrightness(Settings.display_dimmer);
|
|
|
|
sevenseg[i]->blinkRate(0);
|
|
|
|
}
|
2020-04-10 09:02:23 +01:00
|
|
|
SevensegClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegInit(uint8_t mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case DISPLAY_INIT_MODE:
|
|
|
|
case DISPLAY_INIT_PARTIAL:
|
|
|
|
case DISPLAY_INIT_FULL:
|
|
|
|
SevensegInitMode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegInitDriver(void)
|
|
|
|
{
|
|
|
|
if (!Settings.display_model) {
|
2020-07-12 07:16:29 +01:00
|
|
|
if (I2cSetDevice(Settings.display_address[0])) {
|
2020-04-10 09:02:23 +01:00
|
|
|
Settings.display_model = XDSP_11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (XDSP_11 == Settings.display_model) {
|
|
|
|
sevenseg_state = 1;
|
2020-07-12 07:16:29 +01:00
|
|
|
for (sevensegs = 0; sevensegs < 8; sevensegs++) {
|
|
|
|
if (Settings.display_address[sevensegs]) {
|
|
|
|
I2cSetActiveFound(Settings.display_address[sevensegs], "SevenSeg");
|
|
|
|
sevenseg[sevensegs] = new Adafruit_7segment();
|
|
|
|
sevenseg[sevensegs]->begin(Settings.display_address[sevensegs]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 09:02:23 +01:00
|
|
|
Settings.display_width = 4;
|
2020-07-12 07:16:29 +01:00
|
|
|
Settings.display_height = sevensegs;
|
2020-04-10 09:02:23 +01:00
|
|
|
|
|
|
|
SevensegInitMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegOnOff(void)
|
|
|
|
{
|
|
|
|
if (!disp_power) { SevensegClear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegDrawStringAt(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t flag)
|
|
|
|
{
|
2020-07-14 09:42:41 +01:00
|
|
|
enum OutNumType {DECIMAL, HEXADECIMAL, FLOAT, SEGMENTS};
|
|
|
|
int16_t number = 0;
|
|
|
|
double numberf = 0;
|
2020-04-10 09:02:23 +01:00
|
|
|
boolean hasnumber= false;
|
|
|
|
uint8_t dots= 0;
|
2020-07-14 09:42:41 +01:00
|
|
|
OutNumType outnumtype= DECIMAL;
|
|
|
|
uint8 fds = 0; // number of fractional digits for fp number
|
|
|
|
boolean done= false;
|
|
|
|
boolean s= false;
|
|
|
|
uint8_t unit= y;
|
|
|
|
char *buf;
|
|
|
|
|
2020-07-12 07:16:29 +01:00
|
|
|
if ((unit>=sevensegs) || (unit<0)) {
|
|
|
|
unit=0;
|
|
|
|
}
|
|
|
|
|
2020-04-10 09:02:23 +01:00
|
|
|
for (int i=0; (str[i]!='\0') && (!done); i++) {
|
2020-07-12 07:16:29 +01:00
|
|
|
// [optional prefix(es) chars]digits
|
2020-04-10 09:02:23 +01:00
|
|
|
// Some combinations won't make sense.
|
2020-07-14 09:42:41 +01:00
|
|
|
// Reference: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-led-backpack.pdf
|
|
|
|
// This code has been tested on 1.2" and 0.56" 7-Segment LED displays, but should mostly work for others.
|
2020-07-12 07:16:29 +01:00
|
|
|
//
|
|
|
|
// Prefixes:
|
2020-07-14 09:42:41 +01:00
|
|
|
// x upcoming decimal integer number displayed as hex
|
2020-07-12 07:16:29 +01:00
|
|
|
// : turn on middle colon
|
2020-07-12 08:02:39 +01:00
|
|
|
// ^ turn on top left dot
|
2020-07-12 07:16:29 +01:00
|
|
|
// v turn on bottom left dot
|
|
|
|
// . turn on AM/PM/Degree dot
|
|
|
|
// s upcoming number is seconds, print as HH:MM or MM:SS
|
|
|
|
// z clear this display
|
2020-07-14 09:42:41 +01:00
|
|
|
// f upcoming number is floating point
|
|
|
|
// r raw segment based on bitmap of upcoming integer number (see reference document above)
|
2020-07-12 07:16:29 +01:00
|
|
|
//
|
2020-04-10 09:56:23 +01:00
|
|
|
// Some sample valid combinations:
|
2020-07-14 09:42:41 +01:00
|
|
|
// 787 -> 787
|
|
|
|
// x47 -> 2F
|
|
|
|
// s:241 -> 04:01
|
|
|
|
// s241 -> 4 01
|
|
|
|
// s1241 -> 20:41
|
|
|
|
// z ->
|
|
|
|
// x88 -> 58
|
|
|
|
// f8.5 -> 8.5
|
|
|
|
// f-9.34 -> -9.34
|
2020-07-14 10:35:49 +01:00
|
|
|
// f:-9.34 -> -9.:34
|
2020-07-14 09:42:41 +01:00
|
|
|
// r255 -> 8. (all 8 segments on)
|
2020-07-12 07:16:29 +01:00
|
|
|
|
2020-04-10 09:02:23 +01:00
|
|
|
switch (str[i]) {
|
|
|
|
case 'x': // print given dec value as hex
|
2020-07-14 09:42:41 +01:00
|
|
|
// hex = true;
|
|
|
|
outnumtype = HEXADECIMAL;
|
|
|
|
break;
|
|
|
|
case 'f': // given number is floating point number
|
|
|
|
// fp = true;
|
|
|
|
outnumtype = FLOAT;
|
2020-04-10 09:02:23 +01:00
|
|
|
break;
|
|
|
|
case ':': // print colon
|
|
|
|
dots |= 0x02;
|
|
|
|
break;
|
|
|
|
case '^': // print top_left_dot
|
|
|
|
dots |= 0x08;
|
|
|
|
break;
|
|
|
|
case 'v': // print bottom_left_dot
|
|
|
|
dots |= 0x04;
|
|
|
|
break;
|
|
|
|
case '.': // print ampm
|
|
|
|
dots |= 0x10;
|
|
|
|
break;
|
|
|
|
case 's': // duration in seconds
|
|
|
|
s = true;
|
|
|
|
break;
|
2020-07-14 09:42:41 +01:00
|
|
|
case '-':
|
2020-04-10 09:02:23 +01:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
hasnumber= true;
|
2020-07-14 09:42:41 +01:00
|
|
|
if (outnumtype == FLOAT) {
|
|
|
|
// Floating point number is given
|
|
|
|
numberf = atof(str+i);
|
|
|
|
// Find number of fractional digits
|
|
|
|
buf= str+i;
|
|
|
|
char *cp= strchr(buf, '.');
|
|
|
|
if (cp == NULL) {
|
|
|
|
fds= 0;
|
|
|
|
} else {
|
|
|
|
fds= buf+strlen(buf) - 1 - cp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Integer is given
|
|
|
|
number = atoi(str+i);
|
|
|
|
}
|
2020-04-10 09:02:23 +01:00
|
|
|
done = true;
|
|
|
|
break;
|
2020-07-12 07:16:29 +01:00
|
|
|
case 'z': // Clear this display
|
|
|
|
hasnumber=false;
|
|
|
|
dots=0;
|
|
|
|
s=false;
|
|
|
|
sevenseg[unit]->clear();
|
|
|
|
break;
|
2020-07-14 09:42:41 +01:00
|
|
|
case 'r': // Raw segment
|
|
|
|
outnumtype= SEGMENTS;
|
|
|
|
break;
|
2020-04-10 09:02:23 +01:00
|
|
|
default: // unknown format, ignore
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-14 09:42:41 +01:00
|
|
|
if (hasnumber) {
|
|
|
|
if (s) {
|
|
|
|
// number is duration in seconds
|
|
|
|
int hour = number/60/60;
|
|
|
|
int minute = (number/60)%60;
|
|
|
|
|
|
|
|
if (hour) {
|
|
|
|
// HH:MM
|
|
|
|
number = hour*100 + minute;
|
|
|
|
} else {
|
|
|
|
// MM:SS
|
|
|
|
number = minute*100 + number%60;
|
|
|
|
}
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 09:42:41 +01:00
|
|
|
if (outnumtype == HEXADECIMAL) {
|
|
|
|
// Hex
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[unit]->print(number, HEX);
|
2020-07-14 09:42:41 +01:00
|
|
|
} else if (outnumtype == FLOAT) {
|
|
|
|
// Floating point
|
|
|
|
sevenseg[unit]->printFloat(numberf, fds, 10);
|
|
|
|
} else if (outnumtype == SEGMENTS) {
|
|
|
|
// Raw segments
|
|
|
|
sevenseg[unit]->writeDigitRaw(x, number);
|
2020-04-10 09:02:23 +01:00
|
|
|
} else {
|
2020-07-14 09:42:41 +01:00
|
|
|
// Decimal
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[unit]->print(number, DEC);
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dots) {
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[unit]->writeDigitRaw(2, dots);
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[unit]->writeDisplay();
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
void SevensegTime(boolean time_24)
|
|
|
|
{
|
2020-04-10 15:05:43 +01:00
|
|
|
|
2020-04-10 09:02:23 +01:00
|
|
|
uint hours = RtcTime.hour;
|
|
|
|
uint minutes = RtcTime.minute;
|
|
|
|
uint second = RtcTime.second;
|
|
|
|
uint16_t displayValue = hours * 100 + minutes;
|
|
|
|
uint16_t dots = 0;
|
|
|
|
|
|
|
|
// Do 24 hour to 12 hour format conversion when required.
|
|
|
|
if (!time_24) {
|
|
|
|
// Handle when hours are past 12 by subtracting 12 hours (1200 value).
|
|
|
|
if (hours > 12) {
|
|
|
|
displayValue -= 1200;
|
|
|
|
}
|
|
|
|
// Handle hour 0 (midnight) being shown as 12.
|
|
|
|
else if (hours == 0) {
|
|
|
|
displayValue += 1200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now print the time value to the display.
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[0]->print(displayValue, DEC);
|
2020-04-10 09:02:23 +01:00
|
|
|
|
|
|
|
// Add zero padding when in 24 hour mode and it's midnight.
|
|
|
|
// In this case the print function above won't have leading 0's
|
|
|
|
// which can look confusing. Go in and explicitly add these zeros.
|
|
|
|
if (time_24) {
|
|
|
|
if (hours == 0) {
|
|
|
|
// Pad hour 0.
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[0]->writeDigitNum(1, 0);
|
2020-04-10 09:02:23 +01:00
|
|
|
// Also pad when the 10's minute is 0 and should be padded.
|
|
|
|
if (minutes < 10) {
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[0]->writeDigitNum(3, 0);
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hours < 10) {
|
|
|
|
// Always have 4 digits time
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[0]->writeDigitNum(0, 0);
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Identify and display AM/PM
|
|
|
|
if (hours >= 12) {
|
|
|
|
dots |= 0x10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 07:16:29 +01:00
|
|
|
sevenseg[0]->writeDigitRaw(2, dots |= ((second%2) << 1));
|
|
|
|
sevenseg[0]->writeDisplay();
|
2020-04-10 09:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SevensegRefresh(void) // Every second
|
|
|
|
{
|
|
|
|
if (disp_power) {
|
|
|
|
if (Settings.display_mode) { // Mode 0 is User text
|
|
|
|
switch (Settings.display_mode) {
|
|
|
|
case 1: // Time 12
|
|
|
|
SevensegTime(false);
|
|
|
|
break;
|
|
|
|
case 2: // Time 24
|
|
|
|
SevensegTime(true);
|
|
|
|
break;
|
|
|
|
case 4: // Mqtt
|
|
|
|
case 3: // Local
|
|
|
|
case 5: { // Mqtt
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 09:21:36 +01:00
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
|
2020-04-10 09:02:23 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xdsp11(uint8_t function)
|
|
|
|
{
|
2020-04-10 14:31:20 +01:00
|
|
|
if (!I2cEnabled(XI2C_47)) { return false; }
|
2020-04-10 09:02:23 +01:00
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_DISPLAY_INIT_DRIVER == function) {
|
|
|
|
SevensegInitDriver();
|
|
|
|
}
|
|
|
|
else if (XDSP_11 == Settings.display_model) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_DISPLAY_MODEL:
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_INIT:
|
|
|
|
SevensegInit(dsp_init);
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_CLEAR:
|
|
|
|
SevensegClear();
|
|
|
|
break;
|
2020-05-25 09:21:36 +01:00
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
2020-04-10 09:02:23 +01:00
|
|
|
case FUNC_DISPLAY_EVERY_SECOND:
|
|
|
|
SevensegRefresh();
|
|
|
|
break;
|
2020-05-25 09:21:36 +01:00
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
2020-04-10 09:02:23 +01:00
|
|
|
case FUNC_DISPLAY_ONOFF:
|
|
|
|
case FUNC_DISPLAY_POWER:
|
|
|
|
SevensegOnOff();
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_DRAW_STRING:
|
|
|
|
SevensegDrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:05:43 +01:00
|
|
|
#endif // USE_DISPLAY_SEVENSEG
|
2020-04-10 09:02:23 +01:00
|
|
|
#endif // USE_DISPLAY
|
|
|
|
#endif // USE_I2C
|