AceUtils  0.6.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  mCurrModeIterator{rootModeGroup, 0} {
24  updateCurrentModeId();
25  }
26 
28  uint8_t modeId() const { return mModeId; }
29 
31  void changeMode() {
32  ace_common::incrementMod(
33  mCurrModeIterator.recordIndex,
34  mCurrModeIterator.group->numModes);
35  updateCurrentModeId();
36  }
37 
46  void changeGroup() {
47  const ModeGroup* parentGroup = mCurrModeIterator.group->parentGroup;
48 
49  if (parentGroup) {
50  // Go back to the parent.
51  mCurrModeIterator = mPrevModeIterator;
52  } else {
53  // Go down to the child group.
54  const ModeRecord* children = mCurrModeIterator.group->children;
55  const ModeRecord* childRecord = children
56  ? &children[mCurrModeIterator.recordIndex]
57  : nullptr;
58  if (childRecord && childRecord->childGroup) {
59  mPrevModeIterator = mCurrModeIterator;
60  mCurrModeIterator.group = childRecord->childGroup;
61  mCurrModeIterator.recordIndex = 0;
62  }
63  }
64 
65  updateCurrentModeId();
66  }
67 
68  private:
69  void updateCurrentModeId() {
70  mModeId = mCurrModeIterator.group->children
71  ? mCurrModeIterator.group->children[
72  mCurrModeIterator.recordIndex].modeId
73  : 0;
74  }
75 
76  ModeIterator mCurrModeIterator;
77  ModeIterator mPrevModeIterator;
78  uint8_t mModeId = kModeUnknown;
79 };
80 
81 } // mode_group
82 } // ace_utils
83 
84 #endif
A class that helps navigate the hierarchical ModeGroup tree defined by the rootModeGroup.
Definition: ModeNavigator.h:19
void changeMode()
Move to the next sibling mode and wrap to 0 if the end is reached.
Definition: ModeNavigator.h:31
uint8_t modeId() const
Return the current mode identifier.
Definition: ModeNavigator.h:28
ModeNavigator(ModeGroup const *rootModeGroup)
Constructor.
Definition: ModeNavigator.h:22
void changeGroup()
Alternate between a root group and a child group, going to the first mode of the group.
Definition: ModeNavigator.h:46
A data structure that captures the group of sibliing clock modes which can be cycled through using th...
Definition: ModeGroup.h:31
const ModeRecord *const children
Array of children mode groups.
Definition: ModeGroup.h:39
uint8_t const numModes
Number of modes.
Definition: ModeGroup.h:36
const ModeGroup *const parentGroup
Pointer to the parent ModeGroup.
Definition: ModeGroup.h:33
A record of a child of a ModeGroup.
Definition: ModeGroup.h:13
uint8_t const modeId
Unique integer identifier of the mode.
Definition: ModeGroup.h:15
const ModeGroup *const childGroup
ModeGroup containing children ModeRecords.
Definition: ModeGroup.h:18