Skip to content

Commit 0f89d79

Browse files
authored
Conform to coding standards (#63)
* Use underscore_case for variable and function names * Use _prefix for class attributes * Braces on same line as block/statement/expression * Doxygen comments * Indent 4 spaces
1 parent e6a84ed commit 0f89d79

File tree

13 files changed

+405
-556
lines changed

13 files changed

+405
-556
lines changed

MenuSystem.cpp

Lines changed: 84 additions & 155 deletions
Large diffs are not rendered by default.

MenuSystem.h

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class MenuSystem;
2929
//!
3030
//! \see Menu
3131
//! \see MenuItem
32-
class MenuComponent
33-
{
32+
class MenuComponent {
3433
friend class MenuSystem;
3534
friend class Menu;
3635
public:
@@ -186,8 +185,7 @@ class MenuComponent
186185
//!
187186
//! \see MenuComponent
188187
//! \see Menu
189-
class MenuItem : public MenuComponent
190-
{
188+
class MenuItem : public MenuComponent {
191189
public:
192190
//! \brief Construct a MenuItem
193191
//! \param[in] name The name of the menu component that is displayed in
@@ -222,8 +220,7 @@ class MenuItem : public MenuComponent
222220

223221
//! \brief A MenuItem that calls MenuSystem::back() when selected.
224222
//! \see MenuItem
225-
class BackMenuItem : public MenuItem
226-
{
223+
class BackMenuItem : public MenuItem {
227224
public:
228225
BackMenuItem(const char* name, SelectFnPtr select_fn, MenuSystem* ms);
229226

@@ -233,12 +230,11 @@ class BackMenuItem : public MenuItem
233230
virtual Menu* select();
234231

235232
protected:
236-
MenuSystem* menu_system;
233+
MenuSystem* _menu_system;
237234
};
238235

239236

240-
class NumericMenuItem : public MenuItem
241-
{
237+
class NumericMenuItem : public MenuItem {
242238
public:
243239
//! \brief Callback for formatting the numeric value into a String.
244240
//!
@@ -247,39 +243,39 @@ class NumericMenuItem : public MenuItem
247243
using FormatValueFnPtr = const String (*)(const float value);
248244

249245
public:
250-
/// Constructor
251-
///
252-
/// @param name The name of the menu item.
253-
/// @param select_fn The function to call when this MenuItem is selected.
254-
/// @param value Default value.
255-
/// @param minValue The minimum value.
256-
/// @param maxValue The maximum value.
257-
/// @param increment How much the value should be incremented by.
258-
/// @param valueFormatter The custom formatter. If nullptr the String float
259-
/// formatter will be used.
246+
//! Constructor
247+
//!
248+
//! @param name The name of the menu item.
249+
//! @param select_fn The function to call when this MenuItem is selected.
250+
//! @param value Default value.
251+
//! @param min_value The minimum value.
252+
//! @param max_value The maximum value.
253+
//! @param increment How much the value should be incremented by.
254+
//! @param format_value_fn The custom formatter. If nullptr the String
255+
//! float formatter will be used.
260256
NumericMenuItem(const char* name, SelectFnPtr select_fn,
261-
float value, float minValue, float maxValue,
257+
float value, float min_value, float max_value,
262258
float increment=1.0,
263259
FormatValueFnPtr format_value_fn=nullptr);
264260

265-
/**
266-
* Sets the custom number formatter.
267-
*
268-
* @param numberFormat the custom formatter. If nullptr the String float
269-
* formatter will be used (2 decimals)
270-
*/
261+
//!
262+
//! \brief Sets the custom number formatter.
263+
//!
264+
//! \param numberFormat the custom formatter. If nullptr the String float
265+
//! formatter will be used (2 decimals)
266+
//!
271267
void set_number_formatter(FormatValueFnPtr format_value_fn);
272268

273269
float get_value() const;
274-
float get_minValue() const;
275-
float get_maxValue() const;
270+
float get_min_value() const;
271+
float get_max_value() const;
276272

277-
// TODO: get_value_string is a poor name. get_formatted_value maybe?
278-
String get_value_string() const;
279273
void set_value(float value);
280274
void set_min_value(float value);
281275
void set_max_value(float value);
282276

277+
String get_formatted_value() const;
278+
283279
virtual void render(MenuComponentRenderer const& renderer) const;
284280

285281
protected:
@@ -290,20 +286,30 @@ class NumericMenuItem : public MenuItem
290286

291287
protected:
292288
float _value;
293-
float _minValue;
294-
float _maxValue;
289+
float _min_value;
290+
float _max_value;
295291
float _increment;
296292
FormatValueFnPtr _format_value_fn;
297293
};
298294

299295

300-
class Menu : public MenuComponent
301-
{
296+
//! \brief A MenuComponent that can contain other MenuComponents.
297+
//!
298+
//! Menu represents the branch in the composite design pattern (see:
299+
//! https://en.wikipedia.org/wiki/Composite_pattern). When a Menu is
300+
//! selected, the user-defined Menu::_select_fn callback is called.
301+
//!
302+
//! \see MenuComponent
303+
//! \see MenuItem
304+
class Menu : public MenuComponent {
302305
friend class MenuSystem;
303306
public:
304307
Menu(const char* name, SelectFnPtr select_fn=nullptr);
305308

309+
//! \brief Adds a MenuItem to the Menu
306310
void add_item(MenuItem* p_item);
311+
312+
//! \brief Adds a Menu to the Menu
307313
void add_menu(Menu* p_menu);
308314

309315
MenuComponent const* get_current_component() const;
@@ -313,16 +319,29 @@ class Menu : public MenuComponent
313319
uint8_t get_current_component_num() const;
314320
uint8_t get_previous_component_num() const;
315321

322+
//! \copydoc MenuComponent::render
316323
void render(MenuComponentRenderer const& renderer) const;
317324

318325
protected:
319-
void set_parent(Menu* pParent);
326+
void set_parent(Menu* p_parent);
320327
Menu const* get_parent() const;
321328

329+
//! \brief Activates the current selection
330+
//!
331+
//! When a client makes a selection, activate is called on the current menu
332+
//! which in turn calls the menu's current item's callback.
322333
Menu* activate();
334+
335+
//! \copydoc MenuComponent::next
323336
virtual bool next(bool loop=false);
337+
338+
//! \copydoc MenuComponent::prev
324339
virtual bool prev(bool loop=false);
340+
341+
//! \copydoc MenuComponent::select
325342
virtual Menu* select();
343+
344+
//! \copydoc MenuComponent::reset
326345
virtual void reset();
327346

328347
void add_component(MenuComponent* p_component);
@@ -337,8 +356,7 @@ class Menu : public MenuComponent
337356
};
338357

339358

340-
class MenuSystem
341-
{
359+
class MenuSystem {
342360
public:
343361
MenuSystem(MenuComponentRenderer const& renderer);
344362

@@ -359,8 +377,7 @@ class MenuSystem
359377
};
360378

361379

362-
class MenuComponentRenderer
363-
{
380+
class MenuComponentRenderer {
364381
public:
365382
virtual void render(Menu const& menu) const = 0;
366383

examples/current_item/current_item.ino

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,25 @@
1111

1212
// renderer
1313

14-
class MyRenderer : public MenuComponentRenderer
15-
{
14+
class MyRenderer : public MenuComponentRenderer {
1615
public:
17-
virtual void render(Menu const& menu) const
18-
{
16+
void render(Menu const& menu) const {
1917
menu.get_current_component()->render(*this);
2018
}
2119

22-
virtual void render_menu_item(MenuItem const& menu_item) const
23-
{
20+
void render_menu_item(MenuItem const& menu_item) const {
2421
Serial.println(menu_item.get_name());
2522
}
2623

27-
virtual void render_back_menu_item(BackMenuItem const& menu_item) const
28-
{
24+
void render_back_menu_item(BackMenuItem const& menu_item) const {
2925
Serial.println(menu_item.get_name());
3026
}
3127

32-
virtual void render_numeric_menu_item(NumericMenuItem const& menu_item) const
33-
{
28+
void render_numeric_menu_item(NumericMenuItem const& menu_item) const {
3429
Serial.println(menu_item.get_name());
3530
}
3631

37-
virtual void render_menu(Menu const& menu) const
38-
{
32+
void render_menu(Menu const& menu) const {
3933
Serial.println(menu.get_name());
4034
}
4135
};
@@ -62,52 +56,45 @@ MenuItem mu1_mi2("Level 2 - Item 2 (Item)", &on_item4_selected);
6256

6357
bool bRanCallback = false;
6458

65-
void on_item1_selected(MenuComponent* p_menu_component)
66-
{
67-
Serial.println("Item1 Selected");
68-
bRanCallback = true;
59+
void on_item1_selected(MenuComponent* p_menu_component) {
60+
Serial.println("Item1 Selected");
61+
bRanCallback = true;
6962
}
7063

71-
void on_item2_selected(MenuComponent* p_menu_component)
72-
{
73-
Serial.println("Item2 Selected");
74-
bRanCallback = true;
64+
void on_item2_selected(MenuComponent* p_menu_component) {
65+
Serial.println("Item2 Selected");
66+
bRanCallback = true;
7567
}
7668

77-
void on_item3_selected(MenuComponent* p_menu_component)
78-
{
79-
Serial.println("Item3 Selected");
80-
bRanCallback = true;
69+
void on_item3_selected(MenuComponent* p_menu_component) {
70+
Serial.println("Item3 Selected");
71+
bRanCallback = true;
8172
}
8273

83-
void on_item4_selected(MenuComponent* p_menu_component)
84-
{
85-
Serial.println("Item4 Selected");
86-
bRanCallback = false;
87-
ms.reset();
74+
void on_item4_selected(MenuComponent* p_menu_component) {
75+
Serial.println("Item4 Selected");
76+
bRanCallback = false;
77+
ms.reset();
8878
}
8979

9080
// Standard arduino functions
9181

92-
void setup()
93-
{
94-
Serial.begin(9600);
82+
void setup() {
83+
Serial.begin(9600);
9584

96-
ms.get_root_menu().add_item(&mm_mi1);
97-
ms.get_root_menu().add_item(&mm_mi2);
98-
ms.get_root_menu().add_menu(&mu1);
99-
mu1.add_item(&mu1_mi1);
100-
mu1.add_item(&mu1_mi2);
85+
ms.get_root_menu().add_item(&mm_mi1);
86+
ms.get_root_menu().add_item(&mm_mi2);
87+
ms.get_root_menu().add_menu(&mu1);
88+
mu1.add_item(&mu1_mi1);
89+
mu1.add_item(&mu1_mi2);
10190
}
10291

103-
void loop()
104-
{
105-
ms.display();
106-
ms.select();
107-
if (bRanCallback)
108-
{
109-
ms.next();
110-
bRanCallback = false;
111-
}
112-
delay(2000);
92+
void loop() {
93+
ms.display();
94+
ms.select();
95+
if (bRanCallback) {
96+
ms.next();
97+
bRanCallback = false;
98+
}
99+
delay(2000);
113100
}

0 commit comments

Comments
 (0)