You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
7.1 KiB

  1. #include <errno.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <ifaddrs.h>
  14. #include <arpa/inet.h>
  15. #include <sys/socket.h>
  16. #include <netdb.h>
  17. #include <linux/if_link.h>
  18. #include <signal.h>
  19. #include "spilcd_gfx.h"
  20. #include "spilcd_font.h"
  21. #include "base.h"
  22. #define NI_MAXHOST 1025
  23. #define NI_MAXSERV 32
  24. volatile sig_atomic_t done = 0;
  25. struct sigaction action;
  26. /**
  27. *
  28. * @return
  29. */
  30. float GetCPULoad() {
  31. int FileHandler;
  32. char FileBuffer[1024];
  33. float load;
  34. FileHandler = open("/proc/loadavg", O_RDONLY);
  35. if(FileHandler < 0) {
  36. return -1; }
  37. read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
  38. sscanf(FileBuffer, "%f", &load);
  39. close(FileHandler);
  40. return load;
  41. }
  42. /**
  43. *
  44. * @return
  45. */
  46. float GetMemUsage() {
  47. int FileHandler;
  48. char FileBuffer[1024];
  49. int memTotal, memFree, memAvail, memBuf,memCached;
  50. float result;
  51. FileHandler = open("/proc/meminfo", O_RDONLY);
  52. if(FileHandler < 0) {
  53. return -1; }
  54. read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
  55. sscanf(FileBuffer, "MemTotal: %d kB\n MemFree: %d kB\n MemAvailable: %d kB\n Buffers: %d kB\n Cached: %d kB",
  56. &memTotal, &memFree, &memAvail, &memBuf, &memCached);
  57. close(FileHandler);
  58. result = 1.0 - (float)(memFree + memCached) / memTotal;
  59. return result;
  60. }
  61. /**
  62. *
  63. * @return
  64. */
  65. int GetCPUTemp() {
  66. int FileHandler;
  67. char FileBuffer[1024];
  68. int CPU_temp;
  69. FileHandler = open("/sys/devices/virtual/thermal/thermal_zone0/temp", O_RDONLY);
  70. if(FileHandler < 0) {
  71. return -1; }
  72. read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
  73. sscanf(FileBuffer, "%d", &CPU_temp);
  74. close(FileHandler);
  75. return CPU_temp / 1000;
  76. }
  77. void sig_handler(int signo)
  78. {
  79. done = 1;
  80. printf("received signo :%d \n", signo);
  81. }
  82. /*
  83. *
  84. */
  85. int main(int argc, char** argv) {
  86. time_t mytime;
  87. struct tm *tm;
  88. uint8_t time_buffer[80];
  89. uint8_t text_buffer[100];
  90. struct ifaddrs *ifaddr, *ifa;
  91. int family, s, n, row;
  92. char host[NI_MAXHOST];
  93. // Print pid, so that we can send signals from other shells
  94. printf("My pid is: %d\n", getpid());
  95. memset(&action, 0, sizeof(struct sigaction));
  96. action.sa_handler = sig_handler;
  97. // Intercept SIGHUP and SIGINT
  98. if (sigaction(SIGINT, &action, NULL) == -1) {
  99. perror("Error: cannot handle SIGINT"); // Should not happen
  100. }
  101. if (sigaction(SIGTERM, &action, NULL) == -1) {
  102. perror("Error: cannot handle SIGTERM"); // Should not happen
  103. }
  104. lcd_t* lcd = demo_init();
  105. int rowy = 10;
  106. while (done == 0) {
  107. row = 0;
  108. // Date and Time
  109. mytime = time(NULL);
  110. tm = localtime (&mytime);
  111. lcd_fillScreen(lcd, 0, 0, 0);
  112. strftime(time_buffer, 80," %H:%M:%S", tm);
  113. lcd_drawText(lcd, 0, row * rowy, time_buffer, 255, 255, 255);
  114. row++;
  115. strftime(time_buffer, 80," %x", tm);
  116. lcd_drawText(lcd, 0, row * rowy, time_buffer, 200, 200, 200);
  117. row++;
  118. row++;
  119. // CPU
  120. float c = GetCPULoad() ;
  121. lcd_drawText(lcd, 0, row * rowy, "CPU load:", 255, 255, 100);
  122. row++;
  123. snprintf(text_buffer, sizeof(text_buffer), " %0.2f", c);
  124. printf("CPU load avg: %s\n", text_buffer);
  125. if (c < 0.2) {
  126. lcd_drawText(lcd, 0, row * rowy, text_buffer, 50, 255, 50);
  127. } else if (c < 0.5) {
  128. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 50);
  129. } else {
  130. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 50, 50);
  131. }
  132. row++;
  133. /* Memory usage */
  134. float m = GetMemUsage();
  135. snprintf(text_buffer, sizeof(text_buffer), " %3.0f%%", m*100);
  136. printf("Mem used: %s\n", text_buffer);
  137. lcd_drawText(lcd, 0, row * rowy, "Mem used:", 150, 150, 255);
  138. row++;
  139. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 255);
  140. //ssd1306DrawRect(0, 0, 127, 13, INVERSE, LAYER0);
  141. //ssd1306FillRect(2, 2, (int)(123 * m), 9, INVERSE, LAYER0);
  142. row++;
  143. /* CPU temperature */
  144. int t = GetCPUTemp() ;
  145. snprintf ( text_buffer, sizeof(text_buffer), "%3d C", t );
  146. printf("CPU temp: %s\n", text_buffer);
  147. lcd_drawText(lcd, 0, row * rowy, "CPU temp:", 255, 180, 170);
  148. row++;
  149. if (t < 20) {
  150. lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 150, 255);
  151. } else if (t < 25) {
  152. lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 255, 150);
  153. } else if (t < 30) {
  154. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 100);
  155. } else if (t < 35) {
  156. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 120, 100);
  157. } else {
  158. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 80, 20);
  159. }
  160. row++;
  161. // Network devices and IPs
  162. if (getifaddrs(&ifaddr) == -1)
  163. {
  164. perror("getifaddrs");
  165. exit(EXIT_FAILURE);
  166. }
  167. for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
  168. if (ifa->ifa_addr == NULL)
  169. continue;
  170. family = ifa->ifa_addr->sa_family;
  171. if ((strncmp ("eth", ifa->ifa_name, 3 ) == 0) && family == AF_INET) {
  172. s = getnameinfo(ifa->ifa_addr,
  173. (ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
  174. sizeof(struct sockaddr_in6),
  175. host, NI_MAXHOST,
  176. NULL, 0, NI_NUMERICHOST);
  177. if (s != 0) {
  178. printf("getnameinfo() failed: %s\n", gai_strerror(s));
  179. exit(EXIT_FAILURE);
  180. }
  181. printf("%-8s <%s>\n", ifa->ifa_name, host);
  182. snprintf ( text_buffer, sizeof(text_buffer), "%s:", ifa->ifa_name);
  183. lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 200, 255);
  184. row++;
  185. snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
  186. lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
  187. row++;
  188. }
  189. if ((strncmp ("wlan", ifa->ifa_name, 4 ) == 0) && family == AF_INET) {
  190. s = getnameinfo(ifa->ifa_addr,
  191. (ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
  192. sizeof(struct sockaddr_in6),
  193. host, NI_MAXHOST,
  194. NULL, 0, NI_NUMERICHOST);
  195. if (s != 0) {
  196. printf("getnameinfo() failed: %s\n", gai_strerror(s));
  197. exit(EXIT_FAILURE);
  198. }
  199. printf("%-8s <%s>\n", ifa->ifa_name, host);
  200. snprintf ( text_buffer, sizeof(text_buffer), "%s:",ifa->ifa_name);
  201. lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 200, 200);
  202. row++;
  203. snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
  204. lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
  205. row++;
  206. }
  207. }
  208. freeifaddrs(ifaddr);
  209. row++;
  210. lcd_redrawBuffer(lcd);
  211. sleep(1);
  212. }
  213. demo_deinit(lcd);
  214. }