AceUtils  0.5.0
Useful Arduino utilties which are too small as separate libraries, but complex enough to be shared among multiple projects, and often have external dependencies to other libraries.
ModeNavigator.h
1 #ifndef ACE_UTILS_MODE_GROUP_MODE_NAVIGATOR_H
2 #define ACE_UTILS_MODE_GROUP_MODE_NAVIGATOR_H
3 
4 #include <AceCommon.h> // incrementMod()
5 #include "ModeGroup.h"
6 
7 namespace ace_utils {
8 namespace mode_group {
9 
20  public:
22  ModeNavigator(ModeGroup const* rootModeGroup) :
23  mCurrentModeGroup(rootModeGroup) {
24  }
25 
27  uint8_t mode() const { return mMode; }
28 
33  void setup() {
34  mMode = getCurrentMode(mCurrentModeIndex);
35  }
36 
38  void changeMode() {
39  ace_common::incrementMod(mCurrentModeIndex, mCurrentModeGroup->numModes);
40  mMode = getCurrentMode(mCurrentModeIndex);
41  }
42 
47  void changeGroup() {
48  const ModeGroup* parentGroup = mCurrentModeGroup->parentGroup;
49 
50  if (parentGroup) {
51  mCurrentModeGroup = parentGroup;
52  mCurrentModeIndex = mTopLevelIndexSave;
53  } else {
54  const ModeGroup* const* childGroups = mCurrentModeGroup->childGroups;
55  const ModeGroup* childGroup = childGroups
56  ? childGroups[mCurrentModeIndex]
57  : nullptr;
58  if (childGroup) {
59  mCurrentModeGroup = childGroup;
60  // Save the current top level index so that we can go back to it.
61  mTopLevelIndexSave = mCurrentModeIndex;
62  mCurrentModeIndex = 0;
63  }
64  }
65 
66  mMode = getCurrentMode(mCurrentModeIndex);
67  }
68 
69  private:
70  uint8_t getCurrentMode(uint8_t index) const {
71  return mCurrentModeGroup->modes[index];
72  }
73 
74  ModeGroup const* mCurrentModeGroup;
75  uint8_t mTopLevelIndexSave = 0;
76  uint8_t mCurrentModeIndex = 0;
77  uint8_t mMode = kModeUnknown;
78 };
79 
80 } // mode_group
81 } // ace_utils
82 
83 #endif
ace_utils::mode_group::ModeNavigator::mode
uint8_t mode() const
Return the current mode identifier.
Definition: ModeNavigator.h:27
ace_utils::mode_group::ModeGroup
A data structure that captures the group of sibliing clock modes which can be cycled through using th...
Definition: ModeGroup.h:17
ace_utils::mode_group::ModeNavigator::changeGroup
void changeGroup()
Alternate between a root group and a child group.
Definition: ModeNavigator.h:47
ace_utils::mode_group::ModeGroup::childGroups
const ModeGroup *const *const childGroups
List of child ModeGroup corresponding to each element in 'modes'.
Definition: ModeGroup.h:34
ace_utils::mode_group::ModeGroup::numModes
const uint8_t numModes
Number of modes.
Definition: ModeGroup.h:22
ace_utils::mode_group::ModeNavigator
A class that helps navigate the hierarchical ModeGroup tree defined by the rootModeGroup.
Definition: ModeNavigator.h:19
ace_utils::mode_group::ModeGroup::parentGroup
const ModeGroup *const parentGroup
Pointer to the parent ModeGroup.
Definition: ModeGroup.h:19
ace_utils::mode_group::ModeGroup::modes
const uint8_t *const modes
Array of mode identifiers of size numModes.
Definition: ModeGroup.h:25
ace_utils::mode_group::ModeNavigator::ModeNavigator
ModeNavigator(ModeGroup const *rootModeGroup)
Constructor.
Definition: ModeNavigator.h:22
ace_utils::mode_group::ModeNavigator::changeMode
void changeMode()
Move to the next sibling mode and wrap to 0 if the end is reached.
Definition: ModeNavigator.h:38
ace_utils::mode_group::ModeNavigator::setup
void setup()
Activate the navigator by setting the mode to be the first mode in the ModeGroup hierarchy.
Definition: ModeNavigator.h:33