chore(color): Replace dynamic_cast with virtual functions, disable RTTI to reduce FLASH usage (#3885)

* Replace dynamic_cast with virtual functions and disable RTTI.

* Cleanup TopBar class hierarchy.

* Fix compiler warning.
This commit is contained in:
philmoz
2023-08-28 12:55:15 +10:00
committed by GitHub
parent 96d94eeeab
commit dcc2118d19
16 changed files with 49 additions and 43 deletions
+1 -1
View File
@@ -508,7 +508,7 @@ set(COMMON_FLAGS "${COMMON_FLAGS} -O${OPT} ${FPU_FLAGS}")
set(COMMON_FLAGS "${COMMON_FLAGS} ${WARNING_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS} -Wimplicit")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS} -fno-rtti")
# these are in addition to CMAKE_CXX_FLAGS
set(CMAKE_EXE_LINKER_FLAGS "-lm -T${RADIO_SRC_DIR}/${LINKER_SCRIPT}")
+1
View File
@@ -21,6 +21,7 @@
#include "opentx.h"
#include "view_main.h"
#include "topbar_impl.h"
WidgetsContainer * customScreens[MAX_CUSTOM_SCREENS] = {};
@@ -113,6 +113,8 @@ class Layout: public LayoutBase
// Updates settings for trims, sliders, pots, etc...
void adjustLayout() override;
bool isLayout() override { return true; }
protected:
const LayoutFactory * factory = nullptr;
std::unique_ptr<ViewMainDecoration> decoration;
@@ -24,17 +24,17 @@
constexpr uint32_t TOPBAR_REFRESH = 1000 / 10; // 10 Hz
TopbarImpl::TopbarImpl(Window * parent) :
TopbarImplBase(parent, {0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData)
TopBar::TopBar(Window * parent) :
TopBarBase(parent, {0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData)
{
}
unsigned int TopbarImpl::getZonesCount() const
unsigned int TopBar::getZonesCount() const
{
return MAX_TOPBAR_ZONES;
}
rect_t TopbarImpl::getZone(unsigned int index) const
rect_t TopBar::getZone(unsigned int index) const
{
if (index == MAX_TOPBAR_ZONES - 1) {
coord_t size = LCD_W - 48 - (MAX_TOPBAR_ZONES - 1) * (TOPBAR_ZONE_WIDTH + TOPBAR_ZONE_HMARGIN);
@@ -49,7 +49,7 @@ rect_t TopbarImpl::getZone(unsigned int index) const
};
}
void TopbarImpl::setVisible(float visible) // 0.0 -> 1.0
void TopBar::setVisible(float visible) // 0.0 -> 1.0
{
if (visible == 0.0) {
setTop(-(int)MENU_HEADER_HEIGHT);
@@ -63,7 +63,7 @@ void TopbarImpl::setVisible(float visible) // 0.0 -> 1.0
}
}
coord_t TopbarImpl::getVisibleHeight(float visible) const // 0.0 -> 1.0
coord_t TopBar::getVisibleHeight(float visible) const // 0.0 -> 1.0
{
if (visible == 0.0) {
return 0;
@@ -76,13 +76,13 @@ coord_t TopbarImpl::getVisibleHeight(float visible) const // 0.0 -> 1.0
return (coord_t)h;
}
void TopbarImpl::paint(BitmapBuffer * dc)
void TopBar::paint(BitmapBuffer * dc)
{
dc->drawSolidFilledRect(0, 0, width(), height(), COLOR_THEME_SECONDARY1);
EdgeTxTheme::instance()->drawHeaderIcon(dc, ICON_EDGETX);
}
void TopbarImpl::checkEvents()
void TopBar::checkEvents()
{
uint32_t now = RTOS_GET_MS();
if (now - lastRefresh >= TOPBAR_REFRESH) {
@@ -91,7 +91,7 @@ void TopbarImpl::checkEvents()
}
}
void TopbarImpl::removeWidget(unsigned int index)
void TopBar::removeWidget(unsigned int index)
{
bool mark = false;
+6 -6
View File
@@ -24,14 +24,12 @@
#include "topbar.h"
#include "layouts/layout_factory_impl.h"
typedef WidgetsContainerImpl<MAX_TOPBAR_ZONES, MAX_TOPBAR_OPTIONS> TopbarImplBase;
typedef WidgetsContainerImpl<MAX_TOPBAR_ZONES, MAX_TOPBAR_OPTIONS> TopBarBase;
class TopbarImpl:
public TopbarImplBase,
public Topbar
class TopBar: public TopBarBase
{
public:
explicit TopbarImpl(Window * parent);
explicit TopBar(Window * parent);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
@@ -42,7 +40,7 @@ class TopbarImpl:
void load() override
{
TopbarImplBase::load();
TopBarBase::load();
}
unsigned int getZonesCount() const override;
@@ -56,6 +54,8 @@ class TopbarImpl:
void checkEvents() override;
bool isTopBar() override { return true; }
void removeWidget(unsigned int index) override;
protected:
+6 -6
View File
@@ -238,9 +238,9 @@ void ScreenSetupPage::build(FormWindow * window)
auto idx = customScreenIndex;
LayoutChoice::LayoutFactoryGetter getFactory = [idx] () -> const LayoutFactory * {
auto layout = dynamic_cast<Layout*>(customScreens[idx]);
if (!layout) return nullptr;
return layout->getFactory();
auto layout = customScreens[idx];
if (!layout->isLayout()) return nullptr;
return ((Layout*)layout)->getFactory();
};
LayoutChoice::LayoutFactorySetter setLayout =
@@ -298,10 +298,10 @@ void ScreenSetupPage::buildLayoutOptions()
layoutOptions->setFlexLayout();
// Layout options...
auto layout = dynamic_cast<Layout*>(customScreens[customScreenIndex]);
if (!layout) return;
auto layout = customScreens[customScreenIndex];
if (!layout->isLayout()) return;
auto factory = layout->getFactory();
auto factory = ((Layout*)layout)->getFactory();
if (!factory) return;
int index = 0;
+3 -3
View File
@@ -29,9 +29,9 @@
#include "widgets_setup.h"
Topbar * TopbarFactory::create(Window * parent)
TopBar * TopbarFactory::create(Window * parent)
{
return new TopbarImpl(parent);
return new TopBar(parent);
}
SetupTopBarWidgetsPage::SetupTopBarWidgetsPage():
@@ -48,7 +48,7 @@ SetupTopBarWidgetsPage::SetupTopBarWidgetsPage():
// adopt the dimensions of the main view
setRect(viewMain->getRect());
auto topbar = dynamic_cast<TopbarImpl*>(viewMain->getTopbar());
auto topbar = viewMain->getTopbar();
for (unsigned i = 0; i < topbar->getZonesCount(); i++) {
auto rect = topbar->getZone(i);
new SetupWidgetsPageSlot(this, rect, topbar, i);
+2 -8
View File
@@ -29,18 +29,12 @@ constexpr coord_t TOPBAR_ZONE_HMARGIN = 3;
constexpr coord_t TOPBAR_ZONE_HEIGHT = MENU_HEADER_HEIGHT - 2 * TOPBAR_ZONE_VMARGIN;
class ScreenMenu;
class TopbarImpl;
class Topbar
{
public:
virtual void load() = 0;
};
class TopBar;
class TopbarFactory
{
public:
static Topbar* create(Window* parent);
static TopBar* create(Window* parent);
};
class SetupTopBarWidgetsPage : public FormWindow
+2 -2
View File
@@ -84,7 +84,7 @@ ViewMain::ViewMain():
lv_obj_add_event_cb(lvobj, ViewMain::long_pressed, LV_EVENT_LONG_PRESSED, nullptr);
// create last to be on top
topbar = dynamic_cast<TopbarImpl*>(TopbarFactory::create(this));
topbar = TopbarFactory::create(this);
}
ViewMain::~ViewMain()
@@ -167,7 +167,7 @@ void ViewMain::previousMainView()
saveViewId(view);
}
Topbar* ViewMain::getTopbar()
TopBar* ViewMain::getTopbar()
{
return topbar;
}
+3 -3
View File
@@ -77,8 +77,8 @@ class ViewMain: public Window
void nextMainView();
void previousMainView();
Topbar* getTopbar();
TopBar* getTopbar();
void onEvent(event_t event) override;
void onClicked() override;
void onCancel() override;
@@ -87,7 +87,7 @@ class ViewMain: public Window
static ViewMain * _instance;
lv_obj_t* tile_view = nullptr;
TopbarImpl* topbar = nullptr;
TopBar* topbar = nullptr;
bool widget_select = false;
lv_timer_t* widget_select_timer = nullptr;
ViewMainMenu* viewMainMenu = nullptr;
+3 -4
View File
@@ -39,7 +39,7 @@ Widget::Widget(const WidgetFactory* factory, Window* parent,
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
if (dynamic_cast<Topbar*>(parent))
if (parent->isTopBar())
fsAllowed = false;
setPressHandler([&]() -> uint8_t {
@@ -112,9 +112,8 @@ void Widget::setFullscreen(bool enable)
if (!enable) {
// Reset all zones in container
auto container = dynamic_cast<WidgetsContainer*>(parent);
if (container)
container->updateZones();
if (parent->isWidgetsContainer())
((WidgetsContainer*)parent)->updateZones();
setWindowFlags(getWindowFlags() & ~OPAQUE);
lv_obj_set_style_bg_opa(lvobj, LV_OPA_0, LV_PART_MAIN);
+2
View File
@@ -180,6 +180,8 @@ class WidgetFactory
virtual Widget * create(Window* parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const = 0;
virtual bool isLuaWidgetFactory() const { return false; }
protected:
const char * name;
const char * displayName;
@@ -89,6 +89,9 @@ class WidgetsContainer: public Window
virtual void adjustLayout() = 0;
virtual void updateZones() = 0;
virtual void updateFromTheme() = 0;
virtual bool isLayout() { return false; }
bool isWidgetsContainer() override { return true; }
};
+2
View File
@@ -37,6 +37,8 @@ class LuaWidgetFactory : public WidgetFactory
Widget::PersistentData* persistentData,
bool init = true) const override;
bool isLuaWidgetFactory() const override { return true; }
protected:
void translateOptions(ZoneOption * options);
+1 -1
View File
@@ -376,7 +376,7 @@ void luaUnregisterWidgets()
{
std::list<const WidgetFactory *> regWidgets(getRegisteredWidgets());
for (auto w : regWidgets) {
if (dynamic_cast<const LuaWidgetFactory*>(w)) {
if (w->isLuaWidgetFactory()) {
delete w;
}
}
+3
View File
@@ -201,6 +201,9 @@ class Window
inline lv_obj_t *getLvObj() { return lvobj; }
virtual bool isTopBar() { return false; }
virtual bool isWidgetsContainer() { return false; }
protected:
static std::list<Window*> trash;