-
-
Save haeshh/7717818a68116ce8bf70043d0b8736b2 to your computer and use it in GitHub Desktop.
TM1637 am ESP32: Datum und Zeit von NTP-Server holen und anzeigen // Text als Laufschrift anzeigen (siehe https://unsinnsbasis.de/tm1637/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Text auf TM1637 als Laufschrift anzeigen | |
* | |
* 2019-07-20 Heiko (unsinnsbasis.de) | |
*/ | |
#include <TM1637Display.h> | |
#define DISPLAY_SIZE 4 // vereinfacht Portierung auf größere Displays | |
#define CLK_PIN 33 | |
#define DIO_PIN 32 | |
TM1637Display display(CLK_PIN, DIO_PIN); // Datenstruktur für Display | |
// Codes für alle auf einer 7-Segment-Anzeige darstellbaren Zeichen | |
const uint8_t charToSegment[] = | |
// G F E D C B A | |
// HEX digits 0-9, A-F | |
{ 0x3f, // 0 1 1 1 1 1 1 - 0 (and uppercase o) | |
0x06, // 0 0 0 0 1 1 0 - 1 (and uppercase i, lowercase l) | |
0x5b, // 1 0 1 1 0 1 1 - 2 | |
0x4f, // 1 0 0 1 1 1 1 - 3 | |
0x66, // 1 1 0 0 1 1 0 - 4 | |
0x6d, // 1 1 0 1 1 0 1 - 5 (and uppercase s) | |
0x7d, // 1 1 1 1 1 0 1 - 6 | |
0x07, // 0 0 0 0 1 1 1 - 7 | |
0x7f, // 1 1 1 1 1 1 1 - 8 (and uppercase b) | |
0x6f, // 1 1 0 1 1 1 1 - 9 | |
0x77, // 1 1 1 0 1 1 1 - A | |
0x7c, // 1 1 1 1 1 0 0 - b | |
0x39, // 0 1 1 1 0 0 1 - C | |
0x5e, // 1 0 1 1 1 1 0 - d | |
0x79, // 1 1 1 1 0 0 1 - E | |
0x71, // 1 1 1 0 0 0 1 - F | |
// additional characters | |
// for words like: Lo, Hi, PUSH (PU5H), PULL, UP... | |
0x7f, // 1 1 1 1 1 1 1 - B (and digit 8) | |
0x58, // 1 0 1 1 0 0 0 - c | |
0x76, // 1 1 1 0 1 1 0 - H | |
0x74, // 1 1 1 0 1 0 0 - h | |
0x06, // 0 0 0 0 1 1 0 - I (and digit 1, lowercase l) | |
0x04, // 0 0 0 0 1 0 0 - i | |
0x0e, // 0 0 0 1 1 1 0 - J | |
0x38, // 0 1 1 1 0 0 0 - L | |
0x06, // 0 0 0 0 1 1 0 - l (and digit 1, uppercase I) | |
0x54, // 1 0 1 0 1 0 0 - n | |
0x3f, // 0 1 1 1 1 1 1 - O (and digit 0) | |
0x5c, // 1 0 1 1 1 0 0 - o | |
0x73, // 1 1 1 0 0 1 1 - P | |
0x67, // 1 1 0 0 1 1 1 - q | |
0x50, // 1 0 1 0 0 0 0 - r | |
0x6d, // 1 1 0 1 1 0 1 - S (and digit 5) | |
0x78, // 1 1 1 1 0 0 0 - t | |
0x3e, // 0 1 1 1 1 1 0 - U | |
0x1c, // 0 0 1 1 1 0 0 - u | |
0x6e, // 1 1 0 1 1 1 0 - y | |
0x40, // 1 0 0 0 0 0 0 - minus | |
0x08, // 0 0 0 1 0 0 0 - underscore | |
0x00 // 0 0 0 0 0 0 0 - space | |
}; | |
// alle darstellbaren Zeichen (in gleicher Reihenfolge wie oben) | |
const char *sevenSegChars = "0123456789AbCdEFBcHhIiJLlnOoPqrStUuy-_ "; | |
// Das Gradsymbol (°) muss extra codiert werden, weil es nicht Standard-ASCII | |
// ist, sondern UTF-codiert zwei Ausgabezeichen erzeugen würde | |
const uint8_t codeDegree = 0x63; // 1 1 0 0 0 1 1 | |
const uint8_t codeMinus = 0x40; // 1 0 0 0 0 0 0 | |
const uint8_t codeUnderscore = 0x08; // 0 0 0 1 0 0 0 | |
const uint8_t codeBlank = 0x00; // 0 0 0 0 0 0 0 | |
// encodeChar liefert zu einem Zeichen den 7-Segment-Code zurück. | |
// Nicht darstellbare Zeichen erzeugen ein Leerzeichen. | |
// | |
uint8_t encodeChar(char ch) { | |
char *pos; | |
pos = strchr(sevenSegChars, ch); | |
if (pos == NULL) { | |
return codeBlank; | |
} else { | |
return charToSegment[pos - sevenSegChars]; | |
} | |
} | |
// Eine Zeichenkette wird in 7-Segment-Codes übersetzt. | |
// Parameter: | |
// - Text | |
// - Referenz auf das Ziel zum Speichern der codierten Zeichen | |
// - Textlänge (Default: Displaygröße (4)) | |
// | |
void encodeString(char *src, uint8_t *tgt, int len=DISPLAY_SIZE) { | |
int i; | |
for (i=0; i<len && i<strlen(src); i++) { | |
tgt[i] = encodeChar(src[i]); | |
} | |
} | |
// scrollText erzeugt eine Laufschrift des Textes. Es werden immer so | |
// viele Zeichen dargestellt wie auf das Display passen. Nach einer | |
// einstellbaren Pause wandert die Anzeige eine Position weiter. | |
// Texte mit kürzerer Länge als die Displaygröße werden mit Leerzeichen | |
// aufgefüllt. | |
// Parameter: | |
// - Text | |
// - Pause in Millisekunden bis zum Anzeigen des nächsten Zeichens | |
// | |
void scrollText(char *text, int pause=300) { | |
uint8_t codes[DISPLAY_SIZE]; | |
int i; | |
if (strlen(text) < DISPLAY_SIZE) { | |
encodeString(text, codes, strlen(text)); | |
// überschüssige Segmente mit Leerzeichen auffüllen | |
for (i=strlen(text); i<DISPLAY_SIZE; i++) { | |
codes[i] = codeBlank; | |
} | |
display.setSegments(codes, DISPLAY_SIZE); | |
delay(pause); | |
} else { | |
for (i=0; i<strlen(text)-DISPLAY_SIZE+1; i++) { | |
encodeString(text+i, codes, DISPLAY_SIZE); | |
display.setSegments(codes, DISPLAY_SIZE); | |
delay(pause); | |
} | |
} | |
} | |
void setup() { | |
// Display auf mittl. Helligkeit einstellen | |
display.setBrightness(3); | |
} | |
void loop() { | |
uint8_t segments[DISPLAY_SIZE]; | |
scrollText("thiS codE iS Funny"); | |
display.clear(); | |
delay(1000); | |
// Text mit ein paar nicht darstellbaren Zeichen | |
scrollText("THIS CODE RunS FAStEr but hAS BLANKS", 200); | |
display.clear(); | |
delay(1000); | |
scrollText("thiS runS too FASt to rEAd", 80); | |
display.clear(); | |
delay(1000); | |
// kurzer Text | |
scrollText("Hi", 1000); | |
display.clear(); | |
delay(1000); | |
scrollText("0123456789AbCdEFBcHhIiJLlnOoPqrStUuy"); | |
display.clear(); | |
delay(1000); | |
// einzelne Segmente mit Zeichen belegen | |
segments[0] = codeDegree; | |
segments[1] = codeMinus; | |
segments[2] = codeBlank; | |
segments[3] = codeUnderscore; | |
display.setSegments(segments); | |
delay(1000); | |
display.clear(); | |
delay(1000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Zeit von einem NTP-Server holen und auf dem TM1637-LED-Modul anzeigen | |
* | |
* 2019-07-17 Heiko (unsinnsbasis.de) | |
*/ | |
#include <WiFi.h> | |
#include "time.h" | |
#include <TM1637Display.h> | |
const char* ssid = "Deine_WLAN_SSID"; | |
const char* password = "Dein_WLAN_Passwort"; | |
const char* ntp_server = "fritz.box"; // oder (de/at/ch).pool.ntp.org | |
// lokale Zeitzone definieren | |
// https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv | |
#define TIMEZONE "CET-1CEST,M3.5.0/02,M10.5.0/03" | |
#define CLK_PIN 33 | |
#define DIO_PIN 32 | |
TM1637Display display(CLK_PIN, DIO_PIN); // Datenstruktur für Display | |
void setup() { | |
struct tm tdata; | |
Serial.begin(115200); | |
// mit dem WLAN verbinden; nach Ermitteln der Zeit kann die | |
// Verbindung wieder getrennt werden | |
Serial.printf("Verbindung herstellen mit %s ", ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(" Verbunden!"); | |
// Zeit vom NTP-Server holen | |
configTime(0, 0, ntp_server); | |
// vor Trennung des WLANs einmal getLocalTime() | |
// aufrufen; sonst wird die Zeit nicht übernommen | |
getLocalTime(&tdata); | |
setenv("TZ", TIMEZONE, 1); // Zeitzone einstellen | |
// WLAN trennen | |
WiFi.disconnect(true); | |
WiFi.mode(WIFI_OFF); | |
// Display auf mittl. Helligkeit einstellen | |
display.setBrightness(3); | |
} | |
void loop() { | |
struct tm tdata; | |
if (!getLocalTime(&tdata)) { | |
Serial.println("Fehler beim Ermitteln der Zeit"); | |
delay (10000); | |
} else { | |
// Zeit als Zahl HHMM mit Doppelpunkt in der Mitte und führenden Nullen | |
// (sonst werden Zeiten in der ersten Stunde als xx statt 00:xx angezeigt) | |
display.showNumberDecEx((tdata.tm_hour)*100 + tdata.tm_min, 0b01000000, true); | |
delay(2000); | |
// Datum als Zahl TTMM mit führender Null | |
display.showNumberDec(tdata.tm_mday*100 + tdata.tm_mon+1, true); | |
delay(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment