linux-st7735/demo/system_monitor.c
2020-08-02 21:32:02 +00:00

238 lines
7.1 KiB
C

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <linux/if_link.h>
#include <signal.h>
#include "spilcd_gfx.h"
#include "spilcd_font.h"
#include "base.h"
#define NI_MAXHOST 1025
#define NI_MAXSERV 32
volatile sig_atomic_t done = 0;
struct sigaction action;
/**
*
* @return
*/
float GetCPULoad() {
int FileHandler;
char FileBuffer[1024];
float load;
FileHandler = open("/proc/loadavg", O_RDONLY);
if(FileHandler < 0) {
return -1; }
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
sscanf(FileBuffer, "%f", &load);
close(FileHandler);
return load;
}
/**
*
* @return
*/
float GetMemUsage() {
int FileHandler;
char FileBuffer[1024];
int memTotal, memFree, memAvail, memBuf,memCached;
float result;
FileHandler = open("/proc/meminfo", O_RDONLY);
if(FileHandler < 0) {
return -1; }
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
sscanf(FileBuffer, "MemTotal: %d kB\n MemFree: %d kB\n MemAvailable: %d kB\n Buffers: %d kB\n Cached: %d kB",
&memTotal, &memFree, &memAvail, &memBuf, &memCached);
close(FileHandler);
result = 1.0 - (float)(memFree + memCached) / memTotal;
return result;
}
/**
*
* @return
*/
int GetCPUTemp() {
int FileHandler;
char FileBuffer[1024];
int CPU_temp;
FileHandler = open("/sys/devices/virtual/thermal/thermal_zone0/temp", O_RDONLY);
if(FileHandler < 0) {
return -1; }
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
sscanf(FileBuffer, "%d", &CPU_temp);
close(FileHandler);
return CPU_temp / 1000;
}
void sig_handler(int signo)
{
done = 1;
printf("received signo :%d \n", signo);
}
/*
*
*/
int main(int argc, char** argv) {
time_t mytime;
struct tm *tm;
uint8_t time_buffer[80];
uint8_t text_buffer[100];
struct ifaddrs *ifaddr, *ifa;
int family, s, n, row;
char host[NI_MAXHOST];
// Print pid, so that we can send signals from other shells
printf("My pid is: %d\n", getpid());
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = sig_handler;
// Intercept SIGHUP and SIGINT
if (sigaction(SIGINT, &action, NULL) == -1) {
perror("Error: cannot handle SIGINT"); // Should not happen
}
if (sigaction(SIGTERM, &action, NULL) == -1) {
perror("Error: cannot handle SIGTERM"); // Should not happen
}
lcd_t* lcd = demo_init();
int rowy = 10;
while (done == 0) {
row = 0;
// Date and Time
mytime = time(NULL);
tm = localtime (&mytime);
lcd_fillScreen(lcd, 0, 0, 0);
strftime(time_buffer, 80," %H:%M:%S", tm);
lcd_drawText(lcd, 0, row * rowy, time_buffer, 255, 255, 255);
row++;
strftime(time_buffer, 80," %x", tm);
lcd_drawText(lcd, 0, row * rowy, time_buffer, 200, 200, 200);
row++;
row++;
// CPU
float c = GetCPULoad() ;
lcd_drawText(lcd, 0, row * rowy, "CPU load:", 255, 255, 100);
row++;
snprintf(text_buffer, sizeof(text_buffer), " %0.2f", c);
printf("CPU load avg: %s\n", text_buffer);
if (c < 0.2) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 50, 255, 50);
} else if (c < 0.5) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 50);
} else {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 50, 50);
}
row++;
/* Memory usage */
float m = GetMemUsage();
snprintf(text_buffer, sizeof(text_buffer), " %3.0f%%", m*100);
printf("Mem used: %s\n", text_buffer);
lcd_drawText(lcd, 0, row * rowy, "Mem used:", 150, 150, 255);
row++;
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 255);
//ssd1306DrawRect(0, 0, 127, 13, INVERSE, LAYER0);
//ssd1306FillRect(2, 2, (int)(123 * m), 9, INVERSE, LAYER0);
row++;
/* CPU temperature */
int t = GetCPUTemp() ;
snprintf ( text_buffer, sizeof(text_buffer), "%3d C", t );
printf("CPU temp: %s\n", text_buffer);
lcd_drawText(lcd, 0, row * rowy, "CPU temp:", 255, 180, 170);
row++;
if (t < 20) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 150, 255);
} else if (t < 25) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 255, 150);
} else if (t < 30) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 100);
} else if (t < 35) {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 120, 100);
} else {
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 80, 20);
}
row++;
// Network devices and IPs
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(EXIT_FAILURE);
}
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 ((strncmp ("eth", ifa->ifa_name, 3 ) == 0) && family == AF_INET) {
s = getnameinfo(ifa->ifa_addr,
(ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("%-8s <%s>\n", ifa->ifa_name, host);
snprintf ( text_buffer, sizeof(text_buffer), "%s:", ifa->ifa_name);
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 200, 255);
row++;
snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
row++;
}
if ((strncmp ("wlan", ifa->ifa_name, 4 ) == 0) && family == AF_INET) {
s = getnameinfo(ifa->ifa_addr,
(ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("%-8s <%s>\n", ifa->ifa_name, host);
snprintf ( text_buffer, sizeof(text_buffer), "%s:",ifa->ifa_name);
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 200, 200);
row++;
snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
row++;
}
}
freeifaddrs(ifaddr);
row++;
lcd_redrawBuffer(lcd);
sleep(1);
}
demo_deinit(lcd);
}