From 2755284777e58499ed4021a0da9b50a51ca8daa5 Mon Sep 17 00:00:00 2001 From: Sergio Sanchez Date: Tue, 21 Sep 2021 21:53:26 +0300 Subject: [PATCH] Added new display type - Added the DotDisplay type (dot matrix based) to distinguish it from the regular display (segments based). - Added initial implementation of the getIp() method. --- displays.cpp | 51 +++++++++++++++++++++++++++----- displays.hpp | 30 ++++++++++++++----- providers.cpp | 52 ++++++++++++++++++++++++++++++++- providers.hpp | 3 ++ smartdisplay.cpp | 76 ++++++++++++++++++++++++++++++------------------ 5 files changed, 167 insertions(+), 45 deletions(-) diff --git a/displays.cpp b/displays.cpp index 0ac4d4a..060ae91 100644 --- a/displays.cpp +++ b/displays.cpp @@ -1,6 +1,7 @@ #include #include #include + #include "displays.hpp" #ifdef WPI @@ -93,7 +94,7 @@ Display::~Display() void Display::demo() { - string txt = "quick demo"; + string txt = "Quick Demo"; string msg = alignCenter(txt); pause(1); @@ -125,11 +126,10 @@ void Display::demo() pause(1); } -void Display::print(string msg, int msgDelay, byte brightness) +void Display::print(string msg, int millis, byte brightness) { - // cout << "{" << (unsigned int)id << "} [" << msg << "] (" << msgDelay << "ms) (" << (unsigned int)brightness << ")" << endl; CONSOLE(consoleMutex.lock()); - CONSOLE(mvprintw((unsigned int)id, 0, "{%s} [%s] (delay %i ms) (brightness %i) ", name.c_str(), msg.c_str(), msgDelay, (unsigned int)brightness)); + CONSOLE(mvprintw((unsigned int)id, 0, "{%s} [%s] (delay %i ms) (brightness %i) ", name.c_str(), msg.c_str(), millis, (unsigned int)brightness)); CONSOLE(refresh()); CONSOLE(consoleMutex.unlock()); @@ -138,7 +138,7 @@ void Display::print(string msg, int msgDelay, byte brightness) { writeChar(msg[i]); } - delay(msgDelay); + delay(millis); } void Display::fadeIn(string msg) @@ -201,7 +201,7 @@ void Display::slideRight(string msg) } } -char randomChar() +char Display::randomChar() { char c = (rand() % 36) + 65; // A to Z + 10 digits if (c > 90) @@ -211,7 +211,7 @@ char randomChar() return c; } -string randomText(byte size) +string Display::randomText(byte size) { string txt = ""; for (int i = 0; i < size; i++) @@ -221,7 +221,7 @@ string randomText(byte size) return txt; } -string blankText(byte size) +string Display::blankText(byte size) { string txt = ""; txt.resize(size, ' '); @@ -397,6 +397,8 @@ void Display::init() writeByte(COUNTER_CONTROL); writeByte(POINTER_CONTROL); setBrightness(BRIGHTNESS_MAX); + + print(BLANK_DISPLAY, 10, BRIGHTNESS_MIN); } void Display::libSetup() @@ -460,4 +462,37 @@ string Display::alignJustify(string txt) } txt.replace(txt.find_first_of(BLANK_CHAR), 1, blankText(DISPLAY_SIZE - size + 1)); return txt; +} + +DotDisplay::DotDisplay(string name, byte sclk, byte data, byte rset) : Display(name, sclk, data, rset) +{ +} + +DotDisplay::~DotDisplay() +{ +} + +char DotDisplay::getChar(char c) +{ + // TODO - add a small cache + // direct ASCII mapping + if ((c >= 32) && (c <= 127)) + { + return c; + } + return 63; // '?' +} + +char DotDisplay::randomChar() +{ + char c = (rand() % 62) + 65; // A to Z + a to z + 10 digits + if ((c > 90) && (c < 97)) + { + c = (c - 91) + 48; // digits 0 to 5 + } + else if (c > 122) + { + c = (c - 123) + 54; // digits 6 to 9 + } + return c; } \ No newline at end of file diff --git a/displays.hpp b/displays.hpp index 7b42cb1..22521bf 100644 --- a/displays.hpp +++ b/displays.hpp @@ -57,7 +57,7 @@ class Display ~Display(); void demo(); - void print(string msg, int msgDelay, byte brightness); + void print(string msg, int millis, byte brightness); void fadeIn(string msg); void fadeOut(string msg); void fadeBlink(string msg); @@ -67,12 +67,6 @@ class Display void crack(string msg); void term(string msg); void pause(byte seconds); - - void setBrightness(byte brightness); - void writeChar(char aChar); - char getChar(char c); - void writeByte(byte aByte); - void writeBit(bool aBit); void reset(); void init(); @@ -83,4 +77,26 @@ class Display static string alignRight(string txt); static string alignLeft(string txt); static string alignJustify(string txt); + +protected: + void setBrightness(byte brightness); + void writeChar(char aChar); + virtual char getChar(char c); + void writeByte(byte aByte); + void writeBit(bool aBit); + + virtual char randomChar(); + string randomText(byte size); + static string blankText(byte size); }; + +class DotDisplay : public Display +{ +public: + DotDisplay(string name, byte sclk, byte data, byte rset); + ~DotDisplay(); + +protected: + char getChar(char c) override; + char randomChar() override; +}; \ No newline at end of file diff --git a/providers.cpp b/providers.cpp index e60df9e..67206b3 100644 --- a/providers.cpp +++ b/providers.cpp @@ -4,6 +4,10 @@ #include #include #include + +#include +#include + #include "providers.hpp" Provider::Provider() @@ -16,7 +20,53 @@ Provider::~Provider() string Provider::getIp() { - return "172.16.0.1"; + struct ifaddrs *ifaddr, *ifa; + int family, s, n; + char host[NI_MAXHOST]; + + string eth_addr; + string wlan_addr; + + if (getifaddrs(&ifaddr) == -1) + { + return "GET IP ERROR"; + } + + for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) + { + if (ifa->ifa_addr == NULL) + continue; + + family = ifa->ifa_addr->sa_family; + + if (family == AF_INET) + { + s = getnameinfo(ifa->ifa_addr, + (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), + host, NI_MAXHOST, + NULL, 0, NI_NUMERICHOST); + if (s != 0) + { + continue; + } + + string ifa_name(ifa->ifa_name); + string tmp_addr(host); + + if (ETH0.compare(ifa_name)) + { + eth_addr = tmp_addr; + } + else if (WLAN0.compare(ifa_name)) + { + wlan_addr = tmp_addr; + } + } + } + + freeifaddrs(ifaddr); + + return !wlan_addr.empty() ? wlan_addr : (!eth_addr.empty() ? eth_addr : "IP NOT FOUND"); } string Provider::getTimeInfo(string format) diff --git a/providers.hpp b/providers.hpp index a262a10..ef3e09c 100644 --- a/providers.hpp +++ b/providers.hpp @@ -12,6 +12,9 @@ const string DATE2 = "%d/%m/%Y"; // "25/12/2020" const string TIMEDATE1 = "%a %d %b %R"; // "Thu 24 Jun 17:30" const string TIMEDATE2 = TIME2 + " " + DATE2; // "17:30 24/06/2021" +const string ETH0 = "eth0"; +const string WLAN0 = "wlan0"; + const string EXCHANGE_URL = "https://api.cryptonator.com/api/ticker/"; const string USD = "btc-usd"; const string EUR = "btc-eur"; diff --git a/smartdisplay.cpp b/smartdisplay.cpp index af459d3..c0172a8 100644 --- a/smartdisplay.cpp +++ b/smartdisplay.cpp @@ -4,12 +4,12 @@ void topThread(Display d) { - string ip; - ip = Display::alignCenter(Provider::getIp()); + // string ip; + // ip = Display::alignCenter(Provider::getIp()); for (;;) { - d.fadeBlink(ip); - // d.demo(); + // d.fadeBlink(ip); + d.demo(); // d.fadeBlink(Display::alignCenter(Provider::getTime())); } } @@ -38,38 +38,56 @@ int main(int argc, char const *argv[]) Display::libSetup(); + // DotDisplay d1("top", 10, 9, 11); Display d1("top", 10, 9, 11); d1.init(); - Display d2("mid", 2, 3, 4); + Display d2("mid", 17, 27, 22); d2.init(); - Display d3("bot", 17, 27, 22); + Display d3("bot", 2, 3, 4); d3.init(); - // for (;;) - // { - // d1.slideLeft(Display::alignCenter(Provider::getTime())); - // d2.slideLeft(Display::alignCenter(Provider::getDate())); - // d3.slideLeft(Display::alignCenter(Provider::getDateTime())); - - // d1.print(Display::alignCenter(Provider::getTime()), 1000, BRIGHTNESS_MAX); - // d2.print(Display::alignCenter(Provider::getDate()), 1000, BRIGHTNESS_MAX); - // d3.print(Display::alignCenter(Provider::getDateTime()), 1000, BRIGHTNESS_MAX); - - // d1.print(Provider::getIp(), 1000, BRIGHTNESS_MAX); - // d1.demo(); - // d2.demo(); - // d3.demo(); - // } - - thread t1(topThread, d1); - thread t2(midThread, d2); - thread t3(botThread, d3); - - t1.join(); - t2.join(); - t3.join(); + // string ip; + // ip = Display::alignCenter(Provider::getIp()); + // ip = Provider::getIp(); + // ip = "192:168:1:123"; + + string txt = "Quick Demo"; + string msg = Display::alignCenter(txt); + + for (;;) + { + // d1.slideLeft(ip); + // d2.slideLeft(ip); + // d3.slideLeft(ip); + + // d1.print(Display::alignCenter(Provider::getTime()), 1000, BRIGHTNESS_MAX); + // d2.print(Display::alignCenter(Provider::getDate()), 1000, BRIGHTNESS_MAX); + // d3.print(Display::alignCenter(Provider::getDateTime()), 1000, BRIGHTNESS_MAX); + + d1.demo(); + d2.demo(); + d3.demo(); + + // d1.crack(msg); + // d1.print(msg, 2000, BRIGHTNESS_MAX); + // d1.pause(1); + // d2.crack(msg); + // d2.print(msg, 2000, BRIGHTNESS_MAX); + // d2.pause(1); + // d3.crack(msg); + // d3.print(msg, 2000, BRIGHTNESS_MAX); + // d3.pause(1); + } + + // thread t1(topThread, d1); + // thread t2(midThread, d2); + // thread t3(botThread, d3); + + // t1.join(); + // t2.join(); + // t3.join(); return 0; }