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.

230 lines
7.0 KiB

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