Skip to content

Commit 73464ae

Browse files
MatthewMArnoldsalkinium
authored andcommitted
[ui] Add support for a custom allocator to modm::ViewStack
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.
1 parent c57cab7 commit 73464ae

15 files changed

+303
-326
lines changed

src/modm/ui/menu/abstract_menu.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/modm/ui/menu/abstract_menu.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ namespace modm{
3030
* \author Thorsten Lajewski
3131
* \ingroup modm_ui_menu
3232
*/
33-
class AbstractMenu: public AbstractView
33+
template<typename Allocator = std::allocator<IAbstractView> >
34+
class AbstractMenu : public AbstractView<Allocator>
3435
{
3536
public:
3637

37-
AbstractMenu(modm::ViewStack* stack, uint8_t identifier);
38+
AbstractMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
39+
modm::AbstractView<Allocator>(stack, identifier)
40+
{
41+
}
3842

43+
virtual ~AbstractMenu() {}
3944

4045
virtual void
4146
shortButtonPress(modm::MenuButtons::Button button) = 0;

src/modm/ui/menu/abstract_view.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/modm/ui/menu/abstract_view.hpp

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (c) 2013, Kevin Läufer
66
* Copyright (c) 2013, Thorsten Lajewski
77
* Copyright (c) 2014, Sascha Schade
8+
* Copyright (c) 2020, Matthew Arnold
89
*
910
* This file is part of the modm project.
1011
*
@@ -17,13 +18,14 @@
1718
#ifndef MODM_ABSTRACT_VIEW_HPP
1819
#define MODM_ABSTRACT_VIEW_HPP
1920

20-
#include <modm/ui/display/color_graphic_display.hpp>
21+
#include <memory>
2122

22-
#include "menu_buttons.hpp"
23+
#include "iabstract_view.hpp"
2324

2425
namespace modm
2526
{
2627
// forward declaration
28+
template<typename T>
2729
class ViewStack;
2830

2931
/**
@@ -34,8 +36,10 @@ namespace modm
3436
*\ingroup modm_ui_menu
3537
*/
3638

37-
class AbstractView
39+
template<typename Allocator = std::allocator<IAbstractView> >
40+
class AbstractView : public IAbstractView
3841
{
42+
template<typename T>
3943
friend class ViewStack;
4044

4145
public:
@@ -44,83 +48,27 @@ namespace modm
4448
* @param identifier can be used to determine which screen is the currently
4549
* displayed on the graphicDisplay
4650
*/
47-
AbstractView(modm::ViewStack* stack, uint8_t identifier);
48-
49-
virtual ~AbstractView() = 0;
50-
51-
/**
52-
* @brief update The update function of the top most display gets called
53-
* as often as possible. Only the update of the top view in each
54-
* ViewStack gets called.
55-
*/
56-
virtual void
57-
update();
58-
59-
/**
60-
* @brief hasChanged indicates the current displayed view has changed.
61-
* This function prevents unnecessary drawing of the display
62-
* @return if true the display has to be redrawn.
63-
*/
64-
virtual bool
65-
hasChanged() = 0;
66-
67-
/**
68-
* @brief draw determine the output on the Graphic Display
69-
*/
70-
virtual void
71-
draw() = 0;
72-
73-
74-
/**
75-
* @brief shortButtonPress handle the action for the pressed button
76-
*/
77-
virtual void
78-
shortButtonPress(modm::MenuButtons::Button button);
79-
80-
/**
81-
* @brief isAlive tells the ViewStack if it should remove this screen.
82-
* @return
83-
*/
84-
bool
85-
isAlive() const;
86-
87-
/**
88-
* @brief remove the view from the screen. The viewStack handles the deletion.
89-
*/
90-
void
91-
remove();
92-
93-
/**
94-
* @brief getIdentifier of the view.
95-
*/
96-
inline uint8_t getIdentifier(){
97-
return this->identifier;
51+
AbstractView(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
52+
IAbstractView(identifier), stack(stack)
53+
{
9854
}
9955

100-
public:
101-
102-
modm::ColorGraphicDisplay&
103-
display();
56+
virtual ~AbstractView() = default;
10457

105-
/**
106-
* @brief onRemove will be called right before the view gets deleted,
107-
* can be reimplemented to reset external data.
108-
*/
109-
virtual void
110-
onRemove();
111-
112-
inline modm::ViewStack*
58+
inline modm::ViewStack<Allocator>*
11359
getViewStack()
11460
{
11561
return stack;
11662
}
11763

118-
private:
119-
modm::ViewStack* stack;
64+
modm::GraphicDisplay&
65+
display()
66+
{
67+
return stack->getDisplay();
68+
}
12069

121-
public:
122-
const uint8_t identifier;
123-
bool alive;
70+
private:
71+
modm::ViewStack<Allocator>* stack;
12472
};
12573
}
12674

src/modm/ui/menu/choice_menu.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2013, Kevin Läufer
33
* Copyright (c) 2013, Thorsten Lajewski
44
* Copyright (c) 2015, Niklas Hauser
5+
* Copyright (c) 2020, Matthew Arnold
56
*
67
* This file is part of the modm project.
78
*
@@ -37,13 +38,14 @@ namespace modm{
3738
* \ingroup modm_ui_menu
3839
*
3940
*/
40-
class ChoiceMenu: public AbstractMenu
41+
template<typename Allocator = std::allocator<IAbstractView> >
42+
class ChoiceMenu : public AbstractMenu<Allocator>
4143
{
4244
public:
4345

44-
ChoiceMenu(modm::ViewStack* stack, uint8_t identifier);
46+
ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier);
4547

46-
ChoiceMenu(modm::ViewStack* stack, uint8_t identifier, const char* title);
48+
ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier, const char* title);
4749

4850
/**
4951
* @brief addEntry a new entry to the ChoiceMenu
@@ -112,4 +114,6 @@ namespace modm{
112114
};
113115
}
114116

117+
#include "choice_menu_impl.hpp"
118+
115119
#endif /* CHOICE_MENU_HPP*/

src/modm/ui/menu/choice_menu.cpp renamed to src/modm/ui/menu/choice_menu_impl.hpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2013, Kevin Läufer
33
* Copyright (c) 2013, Thorsten Lajewski
44
* Copyright (c) 2015, Niklas Hauser
5+
* Copyright (c) 2020, Matthew Arnold
56
*
67
* This file is part of the modm project.
78
*
@@ -11,43 +12,48 @@
1112
*/
1213
// ----------------------------------------------------------------------------
1314

14-
#include "choice_menu.hpp"
15+
#ifndef CHOICE_MENU_HPP
16+
# error "Don't include this file directly, use choice_menu.hpp instead!"
17+
#endif
1518

19+
#include "abstract_view.hpp"
1620

17-
modm::ChoiceMenu::ChoiceMenu(modm::ViewStack* stack, uint8_t identifier) :
18-
modm::AbstractMenu(stack, identifier),
21+
template<typename Allocator>
22+
modm::ChoiceMenu<Allocator>::ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier) :
23+
modm::AbstractMenu<Allocator>(stack, identifier),
1924
display_update_time(500),
2025
timer(display_update_time),
2126
buttonAction(false),
2227
title(""),
2328
homePosition(0),
2429
position(0)
2530
{
26-
this->maximalDrawnEntrys = (getViewStack()->getDisplay().getHeight()- 16) / 8 ;
31+
this->maximalDrawnEntrys = (this->getViewStack()->getDisplay().getHeight()- 16) / 8 ;
2732
}
2833

29-
modm::ChoiceMenu::ChoiceMenu(modm::ViewStack* stack, uint8_t identifier, const char* title) :
30-
modm::AbstractMenu(stack, identifier),
34+
template<typename Allocator>
35+
modm::ChoiceMenu<Allocator>::ChoiceMenu(modm::ViewStack<Allocator>* stack, uint8_t identifier, const char* title) :
36+
modm::AbstractMenu<Allocator>(stack, identifier),
3137
display_update_time(500),
3238
timer(display_update_time),
3339
buttonAction(false),
3440
title(title),
3541
homePosition(0),
3642
position(0)
3743
{
38-
this->maximalDrawnEntrys = (getViewStack()->getDisplay().getHeight()- 16) / 8 ;
44+
this->maximalDrawnEntrys = (this->getViewStack()->getDisplay().getHeight()- 16) / 8 ;
3945
}
4046

41-
void
42-
modm::ChoiceMenu::addEntry(const char* text, bool *valuePtr, bool defaultValue)
47+
template<typename Allocator> void
48+
modm::ChoiceMenu<Allocator>::addEntry(const char* text, bool *valuePtr, bool defaultValue)
4349
{
44-
static uint16_t availableSpace = (getViewStack()->getDisplay().getWidth()-16)/6-6;
50+
static uint16_t availableSpace = (this->getViewStack()->getDisplay().getWidth()-16)/6-6;
4551
modm::ChoiceMenuEntry entry(text, availableSpace, valuePtr, defaultValue);
4652
this->entries.append(entry);
4753
}
4854

49-
void
50-
modm::ChoiceMenu::initialise()
55+
template<typename Allocator> void
56+
modm::ChoiceMenu<Allocator>::initialise()
5157
{
5258
EntryList::iterator iter = this->entries.begin();
5359
for(; iter!= this->entries.end(); ++iter){
@@ -62,17 +68,16 @@ modm::ChoiceMenu::initialise()
6268
}
6369
}
6470

65-
void
66-
modm::ChoiceMenu::setTitle(const char* text)
71+
template<typename Allocator> void
72+
modm::ChoiceMenu<Allocator>::setTitle(const char* text)
6773
{
6874
this->title = text;
6975
}
7076

71-
72-
void
73-
modm::ChoiceMenu::draw()
77+
template<typename Allocator> void
78+
modm::ChoiceMenu<Allocator>::draw()
7479
{
75-
modm::ColorGraphicDisplay* display = &getViewStack()->getDisplay();
80+
modm::GraphicDisplay* display = &this->getViewStack()->getDisplay();
7681
display->clear();
7782
display->setCursor(0,2);
7883
(*display) << this->title;
@@ -115,8 +120,8 @@ modm::ChoiceMenu::draw()
115120
// TODO wenn möglich pfeil nach oben und nach unten einfügen
116121
}
117122

118-
bool
119-
modm::ChoiceMenu::hasChanged()
123+
template<typename Allocator> bool
124+
modm::ChoiceMenu<Allocator>::hasChanged()
120125
{
121126
if (timer.execute() || this->buttonAction)
122127
{
@@ -130,9 +135,8 @@ modm::ChoiceMenu::hasChanged()
130135
}
131136
}
132137

133-
134-
void
135-
modm::ChoiceMenu::shortButtonPress(modm::MenuButtons::Button button)
138+
template<typename Allocator> void
139+
modm::ChoiceMenu<Allocator>::shortButtonPress(modm::MenuButtons::Button button)
136140
{
137141
switch(button)
138142
{

0 commit comments

Comments
 (0)