Skip to content

Commit

Permalink
[ui] Add support for a custom allocator to modm::ViewStack
Browse files Browse the repository at this point in the history
 Allowing a custom allocator allows users to control how
 modm::AbstractView elements stored by the view stack are allocated.
 This, for example, allows the user to statically allocate all views and
 removes the memory and processing overhead of dynamically allocating
 views. For an example, see aruw-mcb's MainMenu class:
 https://github.com/uw-advanced-robotics/aruw-mcb/blob/develop/aruw-mcb-project/src/aruwsrc/display/main_menu.cpp
 By default, a dynamic allocator used, so default behavior remains unchanged.
  • Loading branch information
MatthewMArnold committed Jul 21, 2024
1 parent ee88ac7 commit cd4d223
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 326 deletions.
21 changes: 0 additions & 21 deletions src/modm/ui/menu/abstract_menu.cpp

This file was deleted.

9 changes: 7 additions & 2 deletions src/modm/ui/menu/abstract_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ namespace modm{
* \author Thorsten Lajewski
* \ingroup modm_ui_menu
*/
class AbstractMenu: public AbstractView
template<typename Allocator = std::allocator<IAbstractView> >
class AbstractMenu : public AbstractView<Allocator>
{
public:

AbstractMenu(modm::ViewStack* stack, uint8_t identifier);
AbstractMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
modm::AbstractView<Allocator>(stack, identifier)
{
}

virtual ~AbstractMenu() {}

virtual void
shortButtonPress(modm::MenuButtons::Button button) = 0;
Expand Down
68 changes: 0 additions & 68 deletions src/modm/ui/menu/abstract_view.cpp

This file was deleted.

90 changes: 19 additions & 71 deletions src/modm/ui/menu/abstract_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2013, Thorsten Lajewski
* Copyright (c) 2014, Sascha Schade
* Copyright (c) 2020, Matthew Arnold
*
* This file is part of the modm project.
*
Expand All @@ -17,13 +18,14 @@
#ifndef MODM_ABSTRACT_VIEW_HPP
#define MODM_ABSTRACT_VIEW_HPP

#include <modm/ui/display/color_graphic_display.hpp>
#include <memory>

#include "menu_buttons.hpp"
#include "iabstract_view.hpp"

namespace modm
{
// forward declaration
template<typename T>
class ViewStack;

/**
Expand All @@ -34,8 +36,10 @@ namespace modm
*\ingroup modm_ui_menu
*/

class AbstractView
template<typename Allocator = std::allocator<IAbstractView> >
class AbstractView : public IAbstractView
{
template<typename T>
friend class ViewStack;

public:
Expand All @@ -44,83 +48,27 @@ namespace modm
* @param identifier can be used to determine which screen is the currently
* displayed on the graphicDisplay
*/
AbstractView(modm::ViewStack* stack, uint8_t identifier);

virtual ~AbstractView() = 0;

/**
* @brief update The update function of the top most display gets called
* as often as possible. Only the update of the top view in each
* ViewStack gets called.
*/
virtual void
update();

/**
* @brief hasChanged indicates the current displayed view has changed.
* This function prevents unnecessary drawing of the display
* @return if true the display has to be redrawn.
*/
virtual bool
hasChanged() = 0;

/**
* @brief draw determine the output on the Graphic Display
*/
virtual void
draw() = 0;


/**
* @brief shortButtonPress handle the action for the pressed button
*/
virtual void
shortButtonPress(modm::MenuButtons::Button button);

/**
* @brief isAlive tells the ViewStack if it should remove this screen.
* @return
*/
bool
isAlive() const;

/**
* @brief remove the view from the screen. The viewStack handles the deletion.
*/
void
remove();

/**
* @brief getIdentifier of the view.
*/
inline uint8_t getIdentifier(){
return this->identifier;
AbstractView(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
IAbstractView(identifier), stack(stack)
{
}

public:

modm::ColorGraphicDisplay&
display();
virtual ~AbstractView() = default;

/**
* @brief onRemove will be called right before the view gets deleted,
* can be reimplemented to reset external data.
*/
virtual void
onRemove();

inline modm::ViewStack*
inline modm::ViewStack<Allocator>*
getViewStack()
{
return stack;
}

private:
modm::ViewStack* stack;
modm::GraphicDisplay&
display()
{
return stack->getDisplay();
}

public:
const uint8_t identifier;
bool alive;
private:
modm::ViewStack<Allocator>* stack;
};
}

Expand Down
10 changes: 7 additions & 3 deletions src/modm/ui/menu/choice_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2013, Thorsten Lajewski
* Copyright (c) 2015, Niklas Hauser
* Copyright (c) 2020, Matthew Arnold
*
* This file is part of the modm project.
*
Expand Down Expand Up @@ -37,13 +38,14 @@ namespace modm{
* \ingroup modm_ui_menu
*
*/
class ChoiceMenu: public AbstractMenu
template<typename Allocator = std::allocator<IAbstractView> >
class ChoiceMenu : public AbstractMenu<Allocator>
{
public:

ChoiceMenu(modm::ViewStack* stack, uint8_t identifier);
ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier);

ChoiceMenu(modm::ViewStack* stack, uint8_t identifier, const char* title);
ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier, const char* title);

/**
* @brief addEntry a new entry to the ChoiceMenu
Expand Down Expand Up @@ -112,4 +114,6 @@ namespace modm{
};
}

#include "choice_menu_impl.hpp"

#endif /* CHOICE_MENU_HPP*/
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2013, Thorsten Lajewski
* Copyright (c) 2015, Niklas Hauser
* Copyright (c) 2020, Matthew Arnold
*
* This file is part of the modm project.
*
Expand All @@ -11,43 +12,48 @@
*/
// ----------------------------------------------------------------------------

#include "choice_menu.hpp"
#ifndef CHOICE_MENU_HPP
# error "Don't include this file directly, use choice_menu.hpp instead!"
#endif

#include "abstract_view.hpp"

modm::ChoiceMenu::ChoiceMenu(modm::ViewStack* stack, uint8_t identifier) :
modm::AbstractMenu(stack, identifier),
template<typename Allocator>
modm::ChoiceMenu<Allocator>::ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
modm::AbstractMenu<Allocator>(stack, identifier),
display_update_time(500),
timer(display_update_time),
buttonAction(false),
title(""),
homePosition(0),
position(0)
{
this->maximalDrawnEntrys = (getViewStack()->getDisplay().getHeight()- 16) / 8 ;
this->maximalDrawnEntrys = (this->getViewStack()->getDisplay().getHeight()- 16) / 8 ;
}

modm::ChoiceMenu::ChoiceMenu(modm::ViewStack* stack, uint8_t identifier, const char* title) :
modm::AbstractMenu(stack, identifier),
template<typename Allocator>
modm::ChoiceMenu<Allocator>::ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier, const char* title) :
modm::AbstractMenu<Allocator>(stack, identifier),
display_update_time(500),
timer(display_update_time),
buttonAction(false),
title(title),
homePosition(0),
position(0)
{
this->maximalDrawnEntrys = (getViewStack()->getDisplay().getHeight()- 16) / 8 ;
this->maximalDrawnEntrys = (this->getViewStack()->getDisplay().getHeight()- 16) / 8 ;
}

void
modm::ChoiceMenu::addEntry(const char* text, bool *valuePtr, bool defaultValue)
template<typename Allocator> void
modm::ChoiceMenu<Allocator>::addEntry(const char* text, bool *valuePtr, bool defaultValue)
{
static uint16_t availableSpace = (getViewStack()->getDisplay().getWidth()-16)/6-6;
static uint16_t availableSpace = (this->getViewStack()->getDisplay().getWidth()-16)/6-6;
modm::ChoiceMenuEntry entry(text, availableSpace, valuePtr, defaultValue);
this->entries.append(entry);
}

void
modm::ChoiceMenu::initialise()
template<typename Allocator> void
modm::ChoiceMenu<Allocator>::initialise()
{
EntryList::iterator iter = this->entries.begin();
for(; iter!= this->entries.end(); ++iter){
Expand All @@ -62,17 +68,16 @@ modm::ChoiceMenu::initialise()
}
}

void
modm::ChoiceMenu::setTitle(const char* text)
template<typename Allocator> void
modm::ChoiceMenu<Allocator>::setTitle(const char* text)
{
this->title = text;
}


void
modm::ChoiceMenu::draw()
template<typename Allocator> void
modm::ChoiceMenu<Allocator>::draw()
{
modm::ColorGraphicDisplay* display = &getViewStack()->getDisplay();
modm::GraphicDisplay* display = &this->getViewStack()->getDisplay();
display->clear();
display->setCursor(0,2);
(*display) << this->title;
Expand Down Expand Up @@ -115,8 +120,8 @@ modm::ChoiceMenu::draw()
// TODO wenn möglich pfeil nach oben und nach unten einfügen
}

bool
modm::ChoiceMenu::hasChanged()
template<typename Allocator> bool
modm::ChoiceMenu<Allocator>::hasChanged()
{
if (timer.execute() || this->buttonAction)
{
Expand All @@ -130,9 +135,8 @@ modm::ChoiceMenu::hasChanged()
}
}


void
modm::ChoiceMenu::shortButtonPress(modm::MenuButtons::Button button)
template<typename Allocator> void
modm::ChoiceMenu<Allocator>::shortButtonPress(modm::MenuButtons::Button button)
{
switch(button)
{
Expand Down
Loading

0 comments on commit cd4d223

Please sign in to comment.