2024-12-29 12:59 AM
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.
Any help would be greatly appreciated. Thanks, and Happy New Year!
2025-01-03 02:46 AM
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:
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.