Skip to main content
Senior
December 29, 2024
Solved

Incorrect value prints to screen from my function.

  • December 29, 2024
  • 4 replies
  • 2287 views

Hi all. I'm having trouble with the following function. My TouchGFX gui
has two radio button groups, each with three radio buttons.

The first selects between three different wire gauges, awg42, awg43 and awg44.
The second set selects between the resistance values of each of the wire gauges.

For each wire gauge, I have three resistance values, min, nom and max to
account for variance in the actual wire resistance in a real-world situation.
Obviously, resistance is based on a variety of external factors.

I have a single TextArea on the screen to which I want to print the decimal
value of the selected resistance.

So for example, I want to select a wire gauge of awg42 with a nominal resistance value
or, select a wire gauge of awg43 with a minimal resistance value and have the screen
update with the result.

So far, this is not happening. The TextArea is updated with the format specifier, not
the resistance value. I have tried snprintf alternatives but without success.

I'm using a typedef struct to hold all the resistance values for reasons of clarity.

 

 

//	typedef struct: WireResPerKm (res)
WireResPerKm res = {
	0.00f,	// zero resistance
	9.06f,	// max awg44 max resistance (Km)
	8.43f,	// nom awg44 nom resistance (Km)
	7.80f,	// min awg44 min resistance (Km)
	7.71f,	// max awg43 max resistance (Km)
	7.01f,	// nom awg43 nom resistance (Km)
	6.30f,	// min awg43 min resistance (Km)
	5.91f,	// max awg42 max resistance (Km)
	5.42f,	// nom awg42 nom resistance (Km)
	4.93f	// min awg42 min resistance (Km)
}; 

//	Define WireResPerKm (res) struct
typedef struct {
	float zero_000;
	float awg44_max;
	float awg44_nom;
	float awg44_min;
	float awg43_max;
	float awg43_nom;
	float awg43_min;
	float awg42_max;
	float awg42_nom;
	float awg42_min;
} WireResPerKm;

 

 

Here is the function that I'm working with:

 

 

void MainView::awgParameters()
{
 float wire_res = res.zero_000;

 if (awg_btn_grp.getSelectedRadioButton() == &awg42_btn)
 {
 if (res_btn_grp.getSelectedRadioButton() == &resMin_btn)
 {
 	wire_res = res.awg42_min;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resNom_btn)
 {
 	wire_res = res.awg42_nom;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resMax_btn)
 {
 	wire_res = res.awg42_max;
 }
 }
 else if (awg_btn_grp.getSelectedRadioButton() == &awg43_btn)
 {
 if (res_btn_grp.getSelectedRadioButton() == &resMin_btn)
 {
 	wire_res = res.awg43_min;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resNom_btn)
 {
 	wire_res = res.awg43_nom;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resMax_btn)
 {
 	wire_res = res.awg43_max;
 }
 }
 else if (awg_btn_grp.getSelectedRadioButton() == &awg44_btn)
 {
 if (res_btn_grp.getSelectedRadioButton() == &resMin_btn)
 {
 	wire_res = res.awg44_min;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resNom_btn)
 {
 	wire_res = res.awg44_nom;
 }
 else if (res_btn_grp.getSelectedRadioButton() == &resMax_btn)
 {
 	wire_res = res.awg44_max;
 }
 }

 //Unicode::snprintfFloat(wire_res_valueBuffer, WIRE_RES_VALUE_SIZE, "%.3f", wire_res_val);
	//Unicode::snprintfFloat(wire_res_valueBuffer, sizeof(wire_res_valueBuffer), "%.3f", wire_res_val);
 Unicode::snprintf(wire_res_valueBuffer, 15, "%.3f", wire_res);
 wire_res_value.invalidate();
}

 

 

awgParameters() is called from the associated itemSelectHandler():

 

 

void MainView::awgItemSelectedHandler(const touchgfx::AbstractButton& src)
{

 if(&src== &awg42_btn) {
 	Unicode::snprintf(awg_selectionBuffer, 10, "AWG 42");
 }
 else if(&src== &awg43_btn) {
 	Unicode::snprintf(awg_selectionBuffer, 10, "AWG 43");
 }
 else if(&src== &awg44_btn) {
 	Unicode::snprintf(awg_selectionBuffer, 10, "AWG 44");
 }
 awg_selection.invalidate();
	awgParameters();
 }
 
 void MainView::resItemSelectedHandler(const touchgfx::AbstractButton& src)
{

	if(&src== &resMin_btn) {
 	Unicode::snprintf(res_selectionBuffer, 10, "Res Min");
 }
 else if(&src== &resNom_btn) {
 	Unicode::snprintf(res_selectionBuffer, 10, "Res Nom");
 }
 else if(&src== &resMax_btn) {
 	Unicode::snprintf(res_selectionBuffer, 10, "Res Max");
 }
 res_selection.invalidate();
	awgParameters();

 }

 

 

Callbacks are in place for both of the above in MainView:

 

 

 MainView::MainView() :
 scrollListItemSelectedCallback(this, &MainView::scrollListItemSelectedHandler),
 scrollWheelAnimateToCallback(this, &MainView::scrollWheelAnimateToHandler),
	awgItemSelectedCallback(this, &MainView::awgItemSelectedHandler),
	tplItemSelectedCallback(this, &MainView::tplItemSelectedHandler),
	resItemSelectedCallback(this, &MainView::tplItemSelectedHandler)
{
	//	...
}

 

 

I fail to understand exactly where my issue/issues lie. I have left myself wondering whether a nested if/else if statement is the correct way to be proceeding.

Screenshot 2024-12-29 084111.png

  Any help would be greatly appreciated. Thanks, and Happy New Year!

Best answer by Al-E-Bags

Thank you for all of your suggestions everybody.  Your input encouraged me to think in greater depth about what I was trying to achieve using radio button groups in preference to another widget type.  At some point earlier in the new year, frustration got the better of me and I ripped out all the button code, replacing it with a combination of scroll wheel and scroll list widgets (which, in my humble opinion appear to be much more suitable for my application).  I'm not where I want to be yet however, I am over the problem which prompted my post although I still do not know what was causing it.

Screenshot 2025-01-13 140730.png

  I'm much happier with the result    :)  

4 replies

Tesla DeLorean
Guru
December 29, 2024

Perhaps it's a limitation of the snprintf() here in this context.

Can you also print to a terminal/debug channel here to confirm what exactly is choking.

Perhaps you could stage in a string via ftoa() dtoa() type construct?

How is the Coil Length/Windings code constructed?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Al-E-BagsAuthor
Senior
December 29, 2024

@Tesla DeLorean Hi.  

I have both coil length and windings(turns) calculations working in a previous mock up
of this project. Much has changed in this version of the gui as I try to improve on my code
however, these are the brief, basic steps I'll be using to calculate the above:

/**
 * \defgroup CalculateTurns
 * \brief calculate_turns().
 *
 * calculates the number of coil wire turns based on several parameters
 *
 * turns_val formula steps:
 * 1: tpl = divide the bobbin type height (in mm) by
 * 2: awg = the selected wire diameter (in mm) multply by
 * 3: lay = the number of coil wire layers muliply by
 * 4: max = the selected fill factor (the max permissible layers before
 *	the coil exceeds the physical boundaries of the bobbin being wound
 *	or when the theoretical coil resistance is achieved (whichever comes first) 
 *
 * example turns value: 10.8 / 0.0663mm * 47 * 0.99
 * result is 7,938.7 turns @ fill factor 0.99
 *
**/

and:

/**
 * \defgroup CalculateCoilLength
 * \brief calculate_coil_length().
 *
 * calculates the total coil length in meters
 *
 * coil_len_val formula steps:
 * 1: bbd = bobbin type base dimension (in mm) for a single wire wrap
 * 2: tpl = bobbin type height (in mm) divided by the selected awg wire dia
 * 3: divide the resulting value by 1000 to achieve length in meters
 * 4: lay = multiply the result by the number of coil wire layers
 *
 * example coil length values: 117.631mm * 170.62mm / meters (to attain meters) * 47 (layers)
 *
 * results in a coil length of 943.3 meters (which is in the coil resistance ball-park 
 * for a Strat type pickup) 
**/

There may be anomalies in my notes as they are a work in progress but as yet, the functions based on these calculations can not be implemented until I get round to implementing the number of turns per layer of the coil (this is also working in the mock up).

I'm kind of engrossed in nailing the issue I'm having with the wire resistance as mentioned in my
post so the actual resistance of the wound coil will not materialize until the code required for
wire resistance, turns per layer, coil length and number of windings is properly implemented in this
version of the gui.

I'm familiar with neither of the methods (ftoa() and dtoa()) you mention but I see they are used for converting decimals and floating point number to strings.  I assume these methods are similar to itoa?  I'll be sure to look
into them as an alternative, thanks for the heads up.

I thank you for your interest and your help thus far.

Tesla DeLorean
Guru
December 29, 2024

I was wondering more about the buffering and formatting of those which were working better/properly.

Unicode::snprintf(wire_res_valueBuffer, 15, "%.3f", wire_res);
wire_res_value.invalidate();

How does wire_res_valueBuffer relate to the wire_res_value object? Is it initialized properly?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
GaetanGodart
Technical Moderator
December 30, 2024

Hello @Al-E-Bags ,

 

I am not sure which buffer store your value, is it awg_selectionBuffer, res_selectionBuffer or something else.
Also, it seems to print "..3." but what is the value expected.

Either way, could you try to use the snprintfFloat function if you are printing a float?
See this video .

 

Regards,

Flemming Gram CHRISTENSEN
ST Employee
January 3, 2025

Hi.

If you are unsure about the TouchGFX unicode sprintf functions, you can also use the normal C/C++ functions. These produce a char array that you can convert to unicode with Unicode::strcpy:
static uint16_t strncpy(UnicodeChar* RESTRICT dst, const char* RESTRICT src, uint16_t maxchars);

Question: In the original code, you did:

FlemmingGramCHRISTENSEN_0-1735901102883.png

so the resistance value is written to "wire_res", but it looks like you used "wire_res_val" as argument to snpintfFloat.

Probably a mistake. Try to check the argument with a debugger.



 

Al-E-BagsAuthorBest answer
Senior
January 13, 2025

Thank you for all of your suggestions everybody.  Your input encouraged me to think in greater depth about what I was trying to achieve using radio button groups in preference to another widget type.  At some point earlier in the new year, frustration got the better of me and I ripped out all the button code, replacing it with a combination of scroll wheel and scroll list widgets (which, in my humble opinion appear to be much more suitable for my application).  I'm not where I want to be yet however, I am over the problem which prompted my post although I still do not know what was causing it.

Screenshot 2025-01-13 140730.png

  I'm much happier with the result    :)