From 280725e4ed499ab5166ba938d36ed7e736accc78 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Sun, 2 Aug 2020 15:58:11 +0000 Subject: [PATCH] Add status monitor example --- example.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++++++++ meson.build | 6 +- 2 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 example.c diff --git a/example.c b/example.c new file mode 100644 index 0000000..da02b6b --- /dev/null +++ b/example.c @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//#include "ssd1306.h" +#include "spilcd_gfx.h" +#include "spilcd_font.h" + + +#define NI_MAXHOST 1025 +#define NI_MAXSERV 32 + +volatile sig_atomic_t done = 0; +struct sigaction action; + +//extern FONT_INFO *_font; + +/** + * + * @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 + } + + wiringPiSetup(); + + lcd_t* lcd = lcd_init(40000000, 1, 10, 7, 8); + + while (done == 0) { + row = 2; + //_font = (FONT_INFO*)&ubuntuMono_8ptFontInfo; + /* Display time */ + mytime = time(NULL); + tm = localtime (&mytime); + //ssd1306ClearScreen(LAYER0 | LAYER1) ; + lcd_fillScreen(lcd, 0, 0, 0); + strftime(time_buffer, 80," %H:%M:%S %x", tm); + //ssd1306DrawString(0, row * 8, time_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 0, row * 8, time_buffer, 255, 255, 255); + row++; + + /* Display IP */ + /* Get network information */ + if (getifaddrs(&ifaddr) == -1) + { + perror("getifaddrs"); + exit(EXIT_FAILURE); + } + /* Walk through linked list, maintaining head pointer so we + can free list later */ + for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { + if (ifa->ifa_addr == NULL) + continue; + + family = ifa->ifa_addr->sa_family; + /* Display interface name and family (including symbolic + form of the latter for the common families) */ + 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: %s",ifa->ifa_name, host ); + //ssd1306DrawString(0, row * 8, text_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 0, row * 8, text_buffer, 255, 255, 255); + 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: %s",ifa->ifa_name, host ); + //ssd1306DrawString(0, row * 8, text_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 0, row * 8, text_buffer, 255, 255, 255); + row++; + } + } + freeifaddrs(ifaddr); + + /* CPU usage + * + */ + float c = GetCPULoad() ; + snprintf ( text_buffer, sizeof(text_buffer), "CPU loadavg: %0.2f", c ); + printf("%s\n", text_buffer); + //ssd1306DrawString(0, row * 8, text_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 0, row * 8, text_buffer, 255, 255, 255); + row++; + + /* Memory usage */ + float m = GetMemUsage(); + snprintf ( text_buffer, sizeof(text_buffer), "Mem used: %3.0f%%", m*100 ); + printf("%s\n", text_buffer); + //ssd1306DrawString(4, 2, text_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 4, 2, text_buffer, 255, 255, 255); + //ssd1306DrawRect(0, 0, 127, 13, INVERSE, LAYER0); + //ssd1306FillRect(2, 2, (int)(123 * m), 9, INVERSE, LAYER0); + + /* CPU temperature */ + int t = GetCPUTemp() ; + snprintf ( text_buffer, sizeof(text_buffer), "CPU temp: %3d C", t ); + printf("%s\n", text_buffer); + //ssd1306DrawString(0, row * 8, text_buffer, 1, WHITE, LAYER0); + lcd_drawText(lcd, 0, row * 8, text_buffer, 255, 255, 255); + row++; + + /* Refresh screen */ + //ssd1306Refresh(); + //SSD1306MSDELAY(1000); + sleep(1); + } + + + //_font = (FONT_INFO*)&ubuntuMono_24ptFontInfo; + //ssd1306ClearScreen(LAYER0 | LAYER1) ; + //ssd1306Refresh(); + printf("All done!\r\n"); + return (EXIT_SUCCESS); +} + diff --git a/meson.build b/meson.build index 3ee7d16..dbc7530 100644 --- a/meson.build +++ b/meson.build @@ -1,3 +1,5 @@ project('dejvino-ST7735', 'c') -src = ['main.c', 'spilcd_gfx.c', 'spilcd_font.c', 'st7735.c'] -executable('st7735', src, link_args: '-lwiringPi') +src1 = ['main.c', 'spilcd_gfx.c', 'spilcd_font.c', 'st7735.c'] +src2 = ['example.c', 'spilcd_gfx.c', 'spilcd_font.c', 'st7735.c'] +executable('st7735', src1, link_args: '-lwiringPi') +executable('example', src2, link_args: '-lwiringPi')