Skip to content

Commit

Permalink
Added new display type
Browse files Browse the repository at this point in the history
- Added the DotDisplay type (dot matrix based) to distinguish it
from the regular display (segments based).
- Added initial implementation of the getIp() method.
  • Loading branch information
sergiosagu committed Sep 21, 2021
1 parent bae7e66 commit 2755284
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 45 deletions.
51 changes: 43 additions & 8 deletions displays.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <string>
#include <cstdlib>

#include "displays.hpp"

#ifdef WPI
Expand Down Expand Up @@ -93,7 +94,7 @@ Display::~Display()

void Display::demo()
{
string txt = "quick demo";
string txt = "Quick Demo";
string msg = alignCenter(txt);

pause(1);
Expand Down Expand Up @@ -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());

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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++)
Expand All @@ -221,7 +221,7 @@ string randomText(byte size)
return txt;
}

string blankText(byte size)
string Display::blankText(byte size)
{
string txt = "";
txt.resize(size, ' ');
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
30 changes: 23 additions & 7 deletions displays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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;
};
52 changes: 51 additions & 1 deletion providers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <ctime>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>

#include <netdb.h>
#include <ifaddrs.h>

#include "providers.hpp"

Provider::Provider()
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions providers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
76 changes: 47 additions & 29 deletions smartdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 2755284

Please sign in to comment.