cancel
Showing results for 
Search instead for 
Did you mean: 

How to write decimal figures to TouchGFX Text Box

DElli.1
Associate III

Hello!

Sorry I am new to stm32 and TouchGfx.

I am currently reading from SPI and want to relay that data to the screen using:

Unicode::snprintf(textCounterBuffer, 10, "%d", finalData);

textCounter.invalidate();

my problem is that the finalData is a decimal figure and the decimal gets cut off.

When I try to display the raw data number from my spi it displays fine and is a uint8_t variable. I need to adjust this number using:

finalData = (spiRxBuf/4096)*100;

but as I said before the screen does not display the number properly.

10 REPLIES 10

%d is integer, for a floating point double use %lf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Wow, thanks, I feel stupid for missing that.

Thank you! ​

sorry, I changed it to %lf and when I click the button on the screen the program locks up.

Any thoughts? Maybe its the way I have my wildcard?

Make sure the buffer is deep enough to hold the string and terminate it.

Sorry not using TouchGFX / CubeIDE

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi, it is because you are using snprintf() and not snprintfFloat() (or floats).

%f is not supported by the "normal" snprintf() because floats are converted to doubles when given as parameters in a variable argument list (va_list).

/Romain

I apologize I should have closed this thread, I found a page from TouchGfx website that helped with the snprintfFloat().

I am now having a strange crash though, the data that I receive through SPI needs to run through a formula before it is useful, but when I use the SPI data that is saved as int result:

int result = ((highByte & 0x1f) << 7) | ((lowByte >> 1));

and send the finalData from that to:

Unicode::snprintfFloat(textCounterBuffer, 50, "%.4f", finalData);

I get a crash.

BUT when I send a regular int, lets say, int x = 2000. To the formula:

float finalData = (float)result*3.3;

finalData /= 4096;

then put it into:

Unicode::snprintfFloat(textCounterBuffer, 50, "%.4f", finalData);

it runs just fine and I get the number I expect to the screen.

Hi,

First are you sure of what you are receiving? If your process works with a set int then the issue comes from what you are receiving or how you are handling it.

Could you share your formula or code as well, it is a bit confusing.

/Romain

Sorry, yes here is my code:

//put CS low

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

//Transmit Data

spiTxBuf[0] = 0x05;

HAL_SPI_Transmit(&hspi2, spiTxBuf, 1, 10);

//read the data

HAL_SPI_Receive(&hspi2, &spiRxBuf, 1, 10);

highByte = spiRxBuf;

HAL_SPI_Receive(&hspi2, &spiRxBuf, 1, 10);

lowByte = spiRxBuf;

int result = ((highByte & 0x1f) << 7) | ((lowByte >> 1));// & 0x7f);

float finalData = (float)result*3.3;

finalData /= 4096;

//bring CS high

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

Unicode::snprintfFloat(textCounterBuffer, 50, "%.4f", finalData);

While is was using the snprintf for %d I was able to print the int result perfectly fine, which led me to believe this was an int value.

So what do you get when you do:

int result = ((highByte & 0x1f) << 7) | ((lowByte >> 1));// & 0x7f);
Unicode::snprintf(textCounterBuffer, 50, "%d", result);
 
 
//float finalData = (float)result*3.3;
 
//finalData /= 4096;
 
//bring CS high
 
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
 
//Unicode::snprintfFloat(textCounterBuffer, 50, "%.4f", finalData);

if the data is what you expected, uncomment/debug step by step to see if it is a calculation problem or something else.