Solved Sensor readings on GUI
-
I have a temperature and humidity sensor whose readings are to be displayed on QLCDNumber, but I'm not able to do the same.
Note that the humidity or the temperature readings are stored in float data types 'humidity' and 'temperature' variable respectively. -
Hi
Its very unclear what part is covered by
"but I'm not able to do the same."Show your code,
how do you try to set to value to LCDNumber
and what is not working for you. -
while(1) { float humidity; float temperature; /*pinconf1[OE_ADDR/4] &= (0xFFFFFFFF ^ (1 << 28)); pinconf1[GPIO_DATAOUT/4] |= (1 << 28); usleep(500000); pinconf1[GPIO_DATAOUT/4] ^= (1 << 28); usleep(20000); pinconf1[GPIO_DATAOUT/4] |= (1 << 28); usleep(20); pinconf1[OE_ADDR/4] |= ( 1 << 28); */ temperature = 0.0f; humidity = 0.0f; int pulseCounts[PULSES*2] = {0}; pinconf1[OE_ADDR/4] &= (0xFFFFFFFF ^ (1 << 28)); // Set pin high for ~500 milliseconds. pinconf1[GPIO_DATAOUT/4] |= (1 << 28); //sleep_milliseconds(500); usleep(500000); // Set pin low for ~20 milliseconds. pinconf1[GPIO_DATAOUT/4] ^= (1 << 28); usleep(20000); // busy_wait_milliseconds(20); // Set pin as input. pinconf1[OE_ADDR/4] |= ( 1 << 28); uint32_t count = 0; while(pinconf1[GPIO_DATAIN/4] & (1 << 28)) { if (++count >= MAXCOUNT) { printf("0"); return 0; } } // Record pulse widths for the expected result bits. for (int i=0; i < PULSES*2; i+=2) { // Count how long pin is low and store in pulseCounts[i] while(!(pinconf1[GPIO_DATAIN/4] & (1 << 28))){ if (++pulseCounts[i] >= MAXCOUNT) { // printf("1"); return 1; } } // Count how long pin is high and store in pulseCounts[i+1] while((pinconf1[GPIO_DATAIN/4] & (1 << 28))) { if (++pulseCounts[i+1] >= MAXCOUNT) { //printf("2"); break; } } } uint32_t threshold = 0; for (int i=2; i < PULSES*2; i+=2) { threshold += pulseCounts[i]; } threshold /= PULSES-1; // Interpret each high pulse as a 0 or 1 by comparing it to the 50us reference. // If the count is less than 50us it must be a ~28us 0 pulse, and if it's higher // then it must be a ~70us 1 pulse. uint8_t data[5] = {0}; for (int i=3; i < PULSES*2; i+=2) { int index = (i-3)/16; data[index] <<= 1; if (pulseCounts[i] >= threshold) { // One bit for long pulse. data[index] |= 1; } // Else zero bit for short pulse. } // Useful debug info: //printf("Data: 0x%x 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3], data[4]); // Verify checksum of received data. if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) { { humidity = (float)data[0]; temperature = (float)data[2]; printf("humidity=%f\n",humidity); printf("temperature=%f\n",temperature); } // return SUCCESS; }
Here is the sensor code,
I did something like:
ui->lcdnumber->display(humidity);
ui->lcdnumber->display(temperature);But i am not getting the readings on the GUI after compiling it.
-
@sush said:
while(1)
This might/will loop forever and kill the GUI.
does
printf("humidity=%f\n",humidity);
printf("temperature=%f\n",temperature);print what u expect?
-
@mrjj it would print the real time atmospheric temperature and humidity values in decimals such as 25.00 and 30.00 respectively.
-
@sush
so if u set
LCDNumber there
and then call
QCoreApplication::processEvents();You should see some values.
Note that while(1) in event driven programs are
very evil and should not be used in production code. :)a better way:
You can use a timer and a slot to have your function at fixed interval
or use a thread. -
@mrjj so you mean to say that I have to make a function containing my sensor code and LCDNumber code and call this function in processEvents()??
-
@sush
no
in the while(1), you should call
processEvents() to allow the GUI to function.
else it never get a chance to get (Paint)Events and
the GUI will be empty.Using while(1)
and usleep(500000) in the normal GUI thread will
lag/kill/dont work very well.
processEvents() might help but might not be enough... -
@mrjj okay it worked quite well, although i had one more question, presently my sensor code is executed when i click a pushbutton, what do i have to do in order to display the sensor data on the gui without clicking any button on the gui?
-
Hi
You can use a QTimer
it can call the same function over and over.
Sort of the same
https://forum.qt.io/topic/66417/what-s-the-easiest-way-to-have-an-animation-run-a-particular-function-undefinitely/2