Skip to content

Commit

Permalink
Added providers interface and initial implementation
Browse files Browse the repository at this point in the history
Added providers interface and initial implementation that
retrieves date and time.
Added debug console based on ncurses.
  • Loading branch information
sergiosagu committed May 29, 2021
1 parent c78de9d commit ce43acc
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 78 deletions.
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
CC = g++

ifdef RPI
CFLAGS = -Wall -pthread -lwiringPi
CFLAGS = -Wall -pthread
WPIFLAG = -DWPI
LDFLAGS =
else
CFLAGS = -Wall -pthread
CFLAGS = -Wall -pthread -g
WPIFLAG =
LDFLAGS = -lncurses
endif

default: smartdisplay

smartdisplay: smartdisplay.o displays.o
$(CC) $(CFLAGS) -o smartdisplay smartdisplay.o displays.o
smartdisplay: smartdisplay.o displays.o providers.o
$(CC) $(CFLAGS) -o smartdisplay smartdisplay.o displays.o providers.o $(LDFLAGS)

smartdisplay.o: smartdisplay.cpp
$(CC) $(CFLAGS) -c smartdisplay.cpp

displays.o: displays.cpp displays.hpp
$(CC) $(CFLAGS) -c displays.cpp $(WPIFLAG)
$(CC) $(CFLAGS) -c displays.cpp $(WPIFLAG) $(LDFLAGS)

providers.o: providers.cpp providers.hpp
$(CC) $(CFLAGS) -c providers.cpp

clean:
$(RM) smartdisplay *.o *~ *.h*.gch
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ These define the interface and implementation related to the VFD modules. Essent
- Fade-in, fade-out and blink with fade effect a message.
- "Crack" a message, like deciphering it.
- Print a text like in a terminal.
### providers.hpp providers.cpp
These define the interface and implementation related to the providers of the data that will be displayed. Essentially, they provide the functionality to retrieve different data from different sources. Some examples are:
- Date and time
- Weather
- Quote of the day
- Stocks
- Push messages
### smartdisplay.cpp
Main application combining the previous functionality.
## Build & Run
Expand Down
162 changes: 103 additions & 59 deletions displays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,46 @@
#include "displays.hpp"

#ifdef WPI

#include <wiringPi.h>

#define CONSOLE(x) \
do \
{ \
} while (0)

#else

#include <chrono>
#include <thread>
#include <mutex>
#include <ncurses.h>

#define OUTPUT 1

#define LOW 0
#define HIGH 1

#define CONSOLE(x) (x)

std::mutex consoleMutex;

void consoleSetup()
{
initscr();
cbreak();
noecho();
curs_set(0);

clear();
refresh();
}

/*
* Mock methods from the wiringPi interface for the non-Raspberry Pi environments.
* See https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.h
*/

int piHiPri(const int pri)
{
return 0;
Expand Down Expand Up @@ -47,8 +76,12 @@ void delayMicroseconds(unsigned int howLong)

using namespace std;

Display::Display(byte sclk, byte data, byte rset)
byte displayCounter = 0;

Display::Display(string name, byte sclk, byte data, byte rset)
{
this->id = displayCounter++;
this->name = name;
this->sclk = sclk;
this->data = data;
this->rset = rset;
Expand Down Expand Up @@ -94,7 +127,12 @@ void Display::demo()

void Display::print(string msg, int msgDelay, byte brightness)
{
cout << "[" << msg << "] (" << msgDelay << "ms) (" << (unsigned int)brightness << ")\n";
// 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(refresh());
CONSOLE(consoleMutex.unlock());

setBrightness(brightness);
for (int i = 0; i < DISPLAY_SIZE; i++)
{
Expand Down Expand Up @@ -135,8 +173,9 @@ void Display::slideLeft(string msg)
{
msg = BLANK_DISPLAY + msg;
int size = msg.length();
for (int i = 0; i < size; i++)
for (int i = 0; i < (size + 1); i++)
{
// TODO - replace with substring
string txt = "";
for (int j = 0; j < DISPLAY_SIZE; j++)
{
Expand All @@ -150,8 +189,9 @@ void Display::slideRight(string msg)
{
msg = msg + BLANK_DISPLAY;
int size = msg.length();
for (int i = size - 1; i >= 0; i--)
for (int i = size - 1; i >= -1; i--)
{
// TODO - replace with substring
string txt = "";
for (int j = 0; j < DISPLAY_SIZE; j++)
{
Expand Down Expand Up @@ -249,6 +289,7 @@ void Display::term(string msg)
for (int i = 0; i < size; i++)
{
txt = "";
// TODO - replace with substring
for (int j = 0; j < min(DISPLAY_SIZE - 1, i); j++)
{
if (i < DISPLAY_SIZE)
Expand Down Expand Up @@ -279,61 +320,6 @@ void Display::pause(byte seconds)
print(BLANK_DISPLAY, seconds * 1000, BRIGHTNESS_MIN);
}

string Display::alignCenter(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
byte n = (DISPLAY_SIZE - size) / 2;
if ((DISPLAY_SIZE - size) % 2 == 0)
{
txt = blankText(n) + txt;
}
else
{
txt = blankText(n + 1) + txt;
}
return txt + blankText(n);
}

string Display::alignRight(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
return blankText(DISPLAY_SIZE - size) + txt;
}

string Display::alignLeft(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
return txt + blankText(DISPLAY_SIZE - size);
}

string Display::alignJustify(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
if (txt.find_first_of(BLANK_CHAR) != txt.find_last_of(BLANK_CHAR))
{
// justifies only two words separated by one single blank!
return txt;
}
txt.replace(txt.find_first_of(BLANK_CHAR), 1, blankText(DISPLAY_SIZE - size + 1));
return txt;
}

void Display::setBrightness(byte brightness)
{
if (this->currentBrightness != brightness)
Expand All @@ -350,6 +336,7 @@ void Display::writeChar(char aChar)

char Display::getChar(char c)
{
// TODO - add a small cache
// ASCII from space to '?'
if ((c >= 32) && (c <= 63))
{
Expand Down Expand Up @@ -416,4 +403,61 @@ void Display::libSetup()
{
piHiPri(99);
wiringPiSetupGpio();

CONSOLE(consoleSetup());
}

string Display::alignCenter(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
byte n = (DISPLAY_SIZE - size) / 2;
if ((DISPLAY_SIZE - size) % 2 == 0)
{
txt = blankText(n) + txt;
}
else
{
txt = blankText(n + 1) + txt;
}
return txt + blankText(n);
}

string Display::alignRight(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
return blankText(DISPLAY_SIZE - size) + txt;
}

string Display::alignLeft(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
return txt + blankText(DISPLAY_SIZE - size);
}

string Display::alignJustify(string txt)
{
int size = txt.length();
if (size >= DISPLAY_SIZE)
{
return txt;
}
if (txt.find_first_of(BLANK_CHAR) != txt.find_last_of(BLANK_CHAR))
{
// justifies only two words separated by one single blank!
return txt;
}
txt.replace(txt.find_first_of(BLANK_CHAR), 1, blankText(DISPLAY_SIZE - size + 1));
return txt;
}
14 changes: 8 additions & 6 deletions displays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ const byte TERM_BLINK = 4; // # of cursor blinks
class Display
{
private:
byte id;
string name;
byte sclk;
byte data;
byte rset;
volatile byte currentBrightness = -1;

public:
Display(byte sclk, byte data, byte rset);
Display(string name, byte sclk, byte data, byte rset);
~Display();

void demo();
Expand All @@ -66,11 +68,6 @@ class Display
void term(string msg);
void pause(byte seconds);

string alignCenter(string txt);
string alignRight(string txt);
string alignLeft(string txt);
string alignJustify(string txt);

void setBrightness(byte brightness);
void writeChar(char aChar);
char getChar(char c);
Expand All @@ -81,4 +78,9 @@ class Display
void init();

static void libSetup();

static string alignCenter(string txt);
static string alignRight(string txt);
static string alignLeft(string txt);
static string alignJustify(string txt);
};
55 changes: 55 additions & 0 deletions providers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "providers.hpp"

Provider::Provider()
{
}

Provider::~Provider()
{
}

string Provider::getIp()
{
return "172.16.0.1";
}

struct tm *getLocaltime()
{
time_t rawtime;
time(&rawtime);
return localtime(&rawtime);
}

string Provider::getTime()
{
tm *timeinfo;
timeinfo = getLocaltime();
char buffer[32];
strftime(buffer, sizeof(buffer), "%T", timeinfo);
string s(buffer);
return s;
}

string Provider::getDate()
{
tm *timeinfo;
timeinfo = getLocaltime();
char buffer[32];
strftime(buffer, sizeof(buffer), "%F", timeinfo);
string s(buffer);
return s;
}

string Provider::getDateTime()
{
tm *timeinfo;
timeinfo = getLocaltime();
char buffer[32];
strftime(buffer, sizeof(buffer), "%a %b %e %R", timeinfo);
string s(buffer);
return s;
}
15 changes: 15 additions & 0 deletions providers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <string>

using namespace std;

class Provider
{
public:
Provider();
~Provider();

static string getIp();
static string getTime();
static string getDate();
static string getDateTime();
};
Loading

0 comments on commit ce43acc

Please sign in to comment.