2024-09-23 03:32 AM
Hi,
I'm looking to print value as "32.45 degC" or "90.41 degF"(based on other function output to print degC or deg F) in textArea2 but not able to print using Unicode::snprintfFloat. Can you please help me in printing float value and its units in textarea2.
If the value is in decimal, I'm able to print using below logic and print the output as 32 degC or 90 degF:
Unicode::snprintf(textArea2Buffer,TEXTAREA2_SIZE,"%d %s",tempValue, tempUnit);
Thanks ,
Bhavya
2024-09-23 08:05 AM
Hi,
what about?
Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, tempUnit);
float tempValue = 32.45f; // Example temperature value as a float
const char* tempUnit; // Pointer to the temperature unit (Celsius or Fahrenheit)
tempUnit = "degC"; or tempUnit = "degF";
2024-09-23 08:09 AM
Hi,
and you have enabled print-float ?
-> use float with printf...
2024-09-24 01:59 AM
Thanks for the response,
I tried it but its throwing error as ''no matching function for call to snprintfFloat" (argument types are: (uint16_t [11], uint16_t const, char const [8], float, char const *))
FYI, I'm using TouchGfx 4.21.3 for designing LCD screens for my project with ST Microcontroller and updating part of code in Container with two textAreas.
My code:
float tempValue = 32.45f;
tempUnitsTitleType tempUnitsDescTable[] = {
{1, "degC"},
{2, "degF"},
};
const char* GetTempUnitsString(int8_t enumValue)
{ // Iterate through the table to find the matching enum value
for (size_t i = 0; i < sizeof(tempUnitsDescTable) / sizeof(tempUnitsDescTable[0]); i++)
{
if (tempUnitsDescTable[i].enumValue == enumValue)
{
return tempUnitsDescTable[i].unitString;
}
} // Return NULL if no matching enum value is found return NULL;
}
Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, GetTempUnitsString(1);
2024-09-24 02:00 AM
Thanks for the response.
I'm not sure where to set this option? can you please provide more details.
2024-09-24 02:12 AM
As all other settings...
in IDE, select your project as "active now" , by having the main.c open or click in left window on the project name;
then menu->project->properties
select ... (as in pic) C/C++ build -> settings -> MCUsettings -> X use float with printf
(then float lib is included in build for this project )
2024-09-24 04:38 AM
Please insert code samples with the "insert/Edit Code sample" button:
This is how I print temperatures:
textArea1.invalidateContent();
int32_t tempInt = int32_t(roundf(m_temperature*10));
Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%3d.%d%c%c", tempInt/10, abs(tempInt)%10, 0x00B0, m_unitFahrenheit ? 'F' : 'C'); // 0x00B0 is degree symbol
//textArea1.resizeToCurrentText(); // in case of right align use resizeToCurrentTextWithAlignment()
textArea1.resizeToCurrentTextWithAlignment();
textArea1.invalidateContent();
snprintfFloat only supports floats: https://support.touchgfx.com/docs/api/classes/classtouchgfx_1_1_unicode#function-snprintffloat
You can probably print just the number to a temporary string and then print again using snprintf and %s. But I just converted it to an integer instead.
Good luck!
2024-09-24 06:19 AM
Hello @BhavyaSri ,
I am able to print a float by doing :
float myVal = 37.05;
Unicode::snprintfFloat(textArea1Buffer, TEXTAREA1_SIZE, "%.2f", myVal);
textArea1.invalidate();
Please find attached the whole project.
I hope this helps! :smiling_face_with_smiling_eyes:
If this comment or another one answers your question, I invite you to select it as "best answer".
Regards,
2024-09-24 07:11 AM
Thanks for the reply, I wanted to print not only a float but also a string value also along with the float value like "32.45 degC" or "90.41 degF"(based on other function output to print degC or deg F).
My Current Code:
float tempValue = 32.45f;
tempUnitsTitleType tempUnitsDescTable[] = {
{1, "degC"},
{2, "degF"},
};
const char* GetTempUnitsString(int8_t enumValue)
{ // Iterate through the table to find the matching enum value
for (size_t i = 0; i < sizeof(tempUnitsDescTable) /sizeof(tempUnitsDescTable[0]); i++)
{
if (tempUnitsDescTable[i].enumValue == enumValue)
{
return tempUnitsDescTable[i].unitString;
}
} // Return NULL if no matching enum value is found return NULL;
}
Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, GetTempUnitsString(1);
This is not printing units but it throws error 'no matching function for call to snprintfFloat"
2024-09-24 07:16 AM
Read my comment.