From 92accba34251672ddbdeb6684faf2a68eb1097ec Mon Sep 17 00:00:00 2001 From: Underdevelopment Date: Mon, 15 May 2023 21:24:49 -0400 Subject: [PATCH] Simplify configuring which device to get IP address from. --- README.md | 19 ++++----- hardware/rpiInfo/rpiInfo.c | 85 +++++++++++++------------------------- hardware/rpiInfo/rpiInfo.h | 4 +- 3 files changed, 37 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 8b26df6..601a6b9 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,19 @@ sudo reboot ``` **Wait for the system to restart** -## Clone SKU_RM0004 library +## Clone SKU_RM0004 library ```bash git clone https://github.com/UCTRONICS/SKU_RM0004.git ``` -## Compile +## Configure +Modify `rpiInfo.h` and set `IPADDRESS_INTERFACE` to the interface to use to display the IP address from. + +## Compile ```bash cd SKU_RM0004 make ``` -## Run +## Run ``` ./display ``` @@ -44,14 +47,8 @@ sudo nano /etc/rc.local **Add command to the rc.local file** ```bash cd /home/pi/SKU_RM0004 -make clean -make +make clean +make ./display & ``` **reboot your system** - - - - - - diff --git a/hardware/rpiInfo/rpiInfo.c b/hardware/rpiInfo/rpiInfo.c index c1cd831..39a5794 100644 --- a/hardware/rpiInfo/rpiInfo.c +++ b/hardware/rpiInfo/rpiInfo.c @@ -18,57 +18,28 @@ #include /* -* Get the IP address of wlan0 or eth0 -*/ + * Get the IP address of the interface defined in IPADDRESS_INTERFACE + */ char* get_ip_address(void) { - int fd; - struct ifreq ifr; - int symbol=0; - if (IPADDRESS_TYPE == ETH0_ADDRESS) - { - fd = socket(AF_INET, SOCK_DGRAM, 0); - /* I want to get an IPv4 IP address */ - ifr.ifr_addr.sa_family = AF_INET; - /* I want IP address attached to "eth0" */ - strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); - symbol=ioctl(fd, SIOCGIFADDR, &ifr); - close(fd); - if(symbol==0) - { - return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); - } - else - { - char* buffer="xxx.xxx.xxx.xxx"; - return buffer; - } - } - else if (IPADDRESS_TYPE == WLAN0_ADDRESS) - { - fd = socket(AF_INET, SOCK_DGRAM, 0); - /* I want to get an IPv4 IP address */ - ifr.ifr_addr.sa_family = AF_INET; - /* I want IP address attached to "wlan0" */ - strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ-1); - symbol=ioctl(fd, SIOCGIFADDR, &ifr); - close(fd); - if(symbol==0) - { - return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); - } - else - { - char* buffer="xxx.xxx.xxx.xxx"; - return buffer; - } - } - else - { - char* buffer="xxx.xxx.xxx.xxx"; - return buffer; - } + int fd; + struct ifreq ifr; + int symbol = 0; + char *buffer = "xxx.xxx.xxx.xxx"; + fd = socket(AF_INET, SOCK_DGRAM, 0); + /* I want to get an IPv4 IP address */ + ifr.ifr_addr.sa_family = AF_INET; + /* I want IP address attached to "wlan0" */ + strncpy(ifr.ifr_name, IPADDRESS_INTERFACE, IFNAMSIZ - 1); + symbol = ioctl(fd, SIOCGIFADDR, &ifr); + close(fd); + if (symbol == 0) + { + buffer = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); + } + + return buffer; } /* @@ -103,8 +74,8 @@ void get_cpu_memory(float *Totalram,float *freeram) *freeram=value/1000.0/1000.0; } } - fclose(fp); - } + fclose(fp); + } } /* @@ -115,7 +86,7 @@ void get_sd_memory(uint32_t *MemSize, uint32_t *freesize) struct statfs diskInfo; statfs("/",&diskInfo); unsigned long long blocksize = diskInfo.f_bsize;// The number of bytes per block - unsigned long long totalsize = blocksize*diskInfo.f_blocks;//Total number of bytes + unsigned long long totalsize = blocksize*diskInfo.f_blocks;//Total number of bytes *MemSize=(unsigned int)(totalsize>>30); @@ -135,11 +106,11 @@ uint8_t get_hard_disk_memory(uint16_t *diskMemSize, uint16_t *useMemSize) uint8_t diskMembuff[10] = {0}; uint8_t useMembuff[10] = {0}; FILE *fd = NULL; - fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(2)}'","r"); + fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(2)}'","r"); fgets(diskMembuff,sizeof(diskMembuff),fd); fclose(fd); - fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(3)}'","r"); + fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(3)}'","r"); fgets(useMembuff,sizeof(useMembuff),fd); fclose(fd); @@ -160,7 +131,7 @@ uint8_t get_temperature(void) fgets(buff,sizeof(buff),fd); sscanf(buff, "%d", &temp); fclose(fd); - return TEMPERATURE_TYPE == FAHRENHEIT ? temp/1000*1.8+32 : temp/1000; + return TEMPERATURE_TYPE == FAHRENHEIT ? temp/1000*1.8+32 : temp/1000; } /* @@ -176,13 +147,13 @@ uint8_t get_cpu_message(void) fp=popen("top -bn1 | grep %Cpu | awk '{printf \"%.2f\", $(2)}'","r"); //Gets the load on the CPU fgets(usCpuBuff, sizeof(usCpuBuff),fp); //Read the user CPU load - pclose(fp); + pclose(fp); fp=popen("top -bn1 | grep %Cpu | awk '{printf \"%.2f\", $(4)}'","r"); //Gets the load on the CPU fgets(syCpubuff, sizeof(syCpubuff),fp); //Read the system CPU load - pclose(fp); + pclose(fp); usCpu = atoi(usCpuBuff); syCpu = atoi(syCpubuff); return usCpu+syCpu; - + } \ No newline at end of file diff --git a/hardware/rpiInfo/rpiInfo.h b/hardware/rpiInfo/rpiInfo.h index 6b93605..30042da 100644 --- a/hardware/rpiInfo/rpiInfo.h +++ b/hardware/rpiInfo/rpiInfo.h @@ -9,9 +9,7 @@ /**********Select display temperature type**************/ /**********Select display network IP type**************/ -#define ETH0_ADDRESS 0 -#define WLAN0_ADDRESS 1 -#define IPADDRESS_TYPE ETH0_ADDRESS // or WLAN0_ADDRESS for WiFi +#define IPADDRESS_INTERFACE "eth0" // or WLAN0_ADDRESS for WiFi /**********Select display network IP type**************/