Last active
June 24, 2019 10:57
-
-
Save StefanoBelli/652625bf773fd93a9441a3e30189d816 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//gcc showstats.c -lwiringPi -lwiringPiDev | |
#define ROWS 2 | |
#define COLS 16 | |
#define RS 1 | |
#define EN 4 | |
#define D7 7 | |
#define D6 0 | |
#define D5 2 | |
#define D4 3 | |
#define TIMER_TRIGGER_SECONDS_AFTER_EXPIRATION 3 | |
#define TIMER_EXPIRATION_SECONDS_AFTER_TRIGGER 1 | |
#define MINUTE 60 | |
#define HOUR (MINUTE * 60) | |
#define DAY (HOUR * 24) | |
#define LCD_4BIT_INIT() (lcdInit(ROWS, COLS, 4, RS, EN, D7, D6, D5, D4, 0, 0, 0, 0)) | |
#include <wiringPi.h> | |
#include <lcd.h> | |
#include <signal.h> | |
#include <sys/time.h> | |
#include <sys/sysinfo.h> | |
#include <string.h> | |
#include <time.h> | |
#include <unistd.h> | |
typedef struct { | |
int days; | |
int hours; | |
int mins; | |
int secs; | |
} uptime; | |
typedef void(*stat_fp)(int); | |
void datetime_stat(int); | |
void uptime_stat(int); | |
void hostname_stat(int); | |
static stat_fp stats[] = { | |
datetime_stat, | |
uptime_stat, | |
hostname_stat, | |
NULL | |
}; | |
static volatile _Bool setinfo; | |
static volatile _Bool run; | |
static uptime get_uptime() { | |
struct sysinfo si; | |
sysinfo(&si); | |
uptime upt; | |
upt.days = si.uptime / DAY; | |
upt.hours = (si.uptime % DAY) / HOUR; | |
upt.mins = (si.uptime % HOUR) / MINUTE; | |
upt.secs = (si.uptime % MINUTE); | |
return upt; | |
} | |
static void display_set() { | |
setinfo = TRUE; | |
} | |
static void stopproc() { | |
run = FALSE; | |
} | |
static int initialize() { | |
setinfo = FALSE; | |
run = TRUE; | |
signal(SIGALRM, display_set); | |
signal(SIGTERM, stopproc); | |
signal(SIGINT, stopproc); | |
struct itimerval timer = { 0 }; | |
timer.it_value.tv_sec = TIMER_EXPIRATION_SECONDS_AFTER_TRIGGER; | |
timer.it_interval.tv_sec = TIMER_TRIGGER_SECONDS_AFTER_EXPIRATION; | |
setitimer(ITIMER_REAL, &timer, NULL); | |
wiringPiSetup(); | |
return LCD_4BIT_INIT(); | |
} | |
int main() { | |
int lcdHandle = initialize(); | |
lcdClear(lcdHandle); | |
lcdHome(lcdHandle); | |
stat_fp* current_ptr = stats; | |
while(run) { | |
lcdHome(lcdHandle); | |
lcdClear(lcdHandle); | |
if(setinfo) { | |
setinfo = FALSE; | |
current_ptr++; | |
if(!(*current_ptr)) | |
current_ptr = stats; | |
} | |
(*current_ptr)(lcdHandle); | |
usleep(500000); | |
} | |
lcdClear(lcdHandle); | |
lcdHome(lcdHandle); | |
lcdPrintf(lcdHandle,"(%d) STOP REQ", getpid()); | |
lcdPosition(lcdHandle,0,1); | |
lcdPuts(lcdHandle,"!BYE!"); | |
return 0; | |
} | |
void datetime_stat(int lcd) { | |
time_t now; | |
struct tm* info; | |
time(&now); | |
info = localtime( &now ); | |
char date[16] = { 0 }; | |
strftime(date,16,"%d-%m-%Y", info); | |
lcdPuts(lcd,date); | |
lcdPosition(lcd, 0,1); | |
char time[16] = { 0 }; | |
strftime(time,16,"%H:%M:%S", info); | |
lcdPuts(lcd, time); | |
} | |
void uptime_stat(int lcd) { | |
uptime u = get_uptime(); | |
lcdPrintf(lcd, "%dd %dh %dm %ds",u.days, u.hours, u.mins, u.secs); | |
} | |
void hostname_stat(int lcd) { | |
char hostname[16] = { 0 }; | |
gethostname(hostname, 16); | |
lcdPuts(lcd, hostname); | |
lcdPosition(lcd, 0, 1); | |
lcdPuts(lcd,"----------------"); | |
} |
This file contains hidden or 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
[Unit] | |
Description=Show stats on LCD screen | |
After=network.target | |
[Service] | |
Type=simple | |
Restart=always | |
ExecStart=/usr/bin/showstats | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment