hello guys, i am c programming user. i have a project using raspberry Pi. i have a temperature sensor ds18b20 and its code, i able show its reading value in uxterm or Terminal. i recently make a simple gtk (gui) that do not have any button or any toolbar. i only able make a label that does not connect to the sensor reading value. how do i combine these code in order to make a gtk (gui) that able display the temperature sensor reading value and able update its reading all the time in any second when temperature change? the project is only want C programming not other language. the below code are
#include <wiringPi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 128
typedef unsigned char uchar;
typedef unsigned int uint;
float tempRead(void)
{
float temp;
int i, j;
int fd;
int ret;
char buf[BUFSIZE];
char tempBuf[5];
fd = open("/sys/bus/w1/devices/28-0314979400b5/w1_slave", O_RDONLY);
if(-1 == fd){
perror("open device file error");
return 1;
}
while(1){
ret = read(fd, buf, BUFSIZE);
if(0 == ret){
break;
}
if(-1 == ret){
if(errno == EINTR){
continue;
}
perror("read()");
close(fd);
return 1;
}
}
for(i=0;i<sizeof(buf);i++){
if(buf[i] == 't'){
for(j=0;j<sizeof(tempBuf);j++){
tempBuf[j] = buf[i+2+j];
}
}
}
temp = (float)atoi(tempBuf) / 1000;
close(fd);
return temp;
}
int main(void)
{
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
float temp;
while(1){
temp = tempRead();
printf("Current temperature : %0.3f\n", temp);
}
return 0;
}
#include <gtk/gtk.h>
void main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(win), 250, 150);
GtkWidget *lbl = gtk_label_new ("100 C");
gtk_container_add (GTK_CONTAINER (win), lbl);
gtk_widget_show_all (win);
gtk_main();
}
i am trying to replaced label "100 C" with live sensor reading. my gtk able shows reading value of sensors. can someone help me?
What I have tried:
i still starter learning c-programming, current i had no idea how to combine these 2 code.