2025-01-30 7:44 AM
In the following post the question is asked to add the same functionality to multiple screens: https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/how-to-display-the-same-content-in-different-screen-of-touchgfx/td-p/723711
I run into the same issue. Where I want to add the same custom containers to multiple screens. Since there is some interaction with these custom containers (updates for the clock and status bar, but also button interaction with a popup window) I now have to copy the code for this interaction to each corresponding view and presenter.
Would it be possible to make a sort of base class which contains this functionality so that all the screens which need to implement the functions can inherit from this base class?
This would greatly reduce maintenance efforts since the code would not have to be replicated for each screen.
I have made a project where I try this and it compiles however the software crashes when I try load a screen which adds the container to the view.
2025-02-06 2:32 AM
Hello @JG_UNI ,
There's no out of the box solution for this but you can draw inspiration from the post you mention .
In the baseView add the common container, and add a callback that callback you need . To know how to create callback from the code, you can create an interaction on a screen, generate the code and readapt it (here for a simple button):
// In BaseView.hpp
private:
/*
* Callback Declarations
*/
touchgfx::Callback<BaseView, const touchgfx::AbstractButton&> buttonCallback;
/*
* Callback Handler Declarations
*/
void buttonCallbackHandler(const touchgfx::AbstractButton& src);
//
BaseView::BaseView() :
buttonCallback(this, &BaseView::buttonCallbackHandler)
{
[...]
button1.setXY(211, 152);
button1.setBitmaps(touchgfx::Bitmap(BITMAP_ALTERNATE_THEME_IMAGES_WIDGETS_BUTTON_REGULAR_HEIGHT_50_MEDIUM_ROUNDED_NORMAL_ID), touchgfx::Bitmap(BITMAP_ALTERNATE_THEME_IMAGES_WIDGETS_BUTTON_REGULAR_HEIGHT_50_MEDIUM_ROUNDED_PRESSED_ID));
button1.setAction(buttonCallback);
add(button1);
}
void BaseView::buttonCallbackHandler(const touchgfx::AbstractButton& src)
{
if (&src== &button1)
{
//When button1 clicked call virtual function
//Call function1
function1();
}
}
Best regards,
2025-02-06 5:34 AM
"I have made a project where I try this and it compiles"
Please share that project @JG_UNI
2025-02-07 2:57 AM
2025-02-07 8:25 AM
The way I handled it is the following way. I created the following class which contains the common elements that are shared between the different screens:
#include <gui/containers/AlarmPopup.hpp>
#include <gui/containers/StatusBar.hpp>
#include <gui/containers/Clock.hpp>
class BaseScreenFunctionView
{
public:
BaseScreenFunctionView();
virtual ~BaseScreenFunctionView() {};
void setAlarm(int alarm);
void clearAlarm();
void setSCStatus(int status);
void updatePauseTimer(int16_t seconds);
void setDateTimeLabel(uint8_t day, uint8_t month, uint8_t year, uint8_t hour, uint8_t minute, uint8_t second);
void updateChargeState(uint8_t value);
protected:
AlarmPopup AlarmPopupWidget;
StatusBar StatusBarWidget;
Clock ClockWidget;
private:
};
#endif // BASESCREENFUNCTIONVIEW_HPP
In the constructor of this class I set the position and visibility of these containers:
BaseScreenFunctionView::BaseScreenFunctionView()
{
ClockWidget.setXY(2, 0);
ClockWidget.setVisible(true);
StatusBarWidget.setXY(409, 0);
StatusBarWidget.setVisible(true);
AlarmPopupWidget.setXY(0, 0);
AlarmPopupWidget.setVisible(false);
}
In the derived class I set the inheritance:
class ProcedureScreenView : public ProcedureScreenViewBase, public BaseScreenFunctionView
{
...
And add the elements in the constructor:
ProcedureScreenView::ProcedureScreenView()
{
add(ClockWidget);
add(StatusBarWidget);
add(AlarmPopupWidget);
}
Like I mentioned before this does compile. And after some tinkering and turning thing on and off I found that is actually works as intended. Since this is a refactoring of a project I still need to figure out how to transfer some of the functions, e.g. button interaction for the baseFunction class so I will check if the solution mentioned by @LouisB can help with this.
I mentioned in my first post that the software crashes. I narrowed this down a bit. I made four screens which inherit froi the baseFunction class. On three of them I actually add the StatusBarWidget. Of these three one functions as intended and two of them cause a hard fault when the page is loaded. Therefor I decided to turn different elements of to figure out which element causes the issue. In the statusbar I use a LineProgess element to show the battery status. When I remove this element the software works on all screen when I add this the software crashes with some, yet not all, pages.
Any suggestions what might cause this element to trigger a hardfault?