diff --git a/doc/keybrd_library_developer_guide.md b/doc/keybrd_library_developer_guide.md index ef9e6be..2b8b518 100644 --- a/doc/keybrd_library_developer_guide.md +++ b/doc/keybrd_library_developer_guide.md @@ -53,7 +53,10 @@ Keybrd library class inheritance diagram Key |____ | \ - | Key_LayeredKeys + | Key_LayeredKeysBase + | \____________________ + | / \ + | Key_LayeredKeys Key_LayeredKeys1 | |___________________________ | \ \ diff --git a/src/Key_LayeredKeys.cpp b/src/Key_LayeredKeys.cpp index 0a63a14..b72eaac 100644 --- a/src/Key_LayeredKeys.cpp +++ b/src/Key_LayeredKeys.cpp @@ -1,13 +1,6 @@ #include "Key_LayeredKeys.h" -void Key_LayeredKeys::press() +uint8_t Key_LayeredKeys::getActiveLayer() { - layerId = refLayerState.getActiveLayer(); - - ptrsKeys[layerId]->press(); -} - -void Key_LayeredKeys::release() -{ - ptrsKeys[layerId]->release(); + return refLayerState.getActiveLayer(); } diff --git a/src/Key_LayeredKeys.h b/src/Key_LayeredKeys.h index ed55b0e..588445c 100644 --- a/src/Key_LayeredKeys.h +++ b/src/Key_LayeredKeys.h @@ -4,22 +4,16 @@ #include #include #include +#include -/* Class Key_LayeredKeys contains an array of Key pointers, one pointer per layer. -Codes are a kind of Key, so the Key pointers can point to Codes or Keys. - -When the key is pressed, active layerId is retreived from refLayerState and -the Key object of the active layerId is called. +/* Most of the Key_LayeredKeys functionality is in Key_LayeredKeysBase. */ -class Key_LayeredKeys : public Key +class Key_LayeredKeys : public Key_LayeredKeysBase { private: - Key*const *const ptrsKeys; //array of Key pointers, one Key per layer - uint8_t layerId; //active layer when key was pressed static LayerStateInterface& refLayerState; public: - Key_LayeredKeys(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} - virtual void press(); - virtual void release(); + Key_LayeredKeys(Key* const ptrsKeys[]) : Key_LayeredKeysBase(ptrsKeys) {} + virtual uint8_t getActiveLayer(); //get active layer from refLayerState }; #endif diff --git a/src/Key_LayeredKeys1.cpp b/src/Key_LayeredKeys1.cpp new file mode 100644 index 0000000..a7fcb4a --- /dev/null +++ b/src/Key_LayeredKeys1.cpp @@ -0,0 +1,6 @@ +#include "Key_LayeredKeys1.h" + +uint8_t Key_LayeredKeys1::getActiveLayer() +{ + return refLayerState.getActiveLayer(); +} diff --git a/src/Key_LayeredKeys1.h b/src/Key_LayeredKeys1.h new file mode 100644 index 0000000..dfa6fe3 --- /dev/null +++ b/src/Key_LayeredKeys1.h @@ -0,0 +1,21 @@ +#ifndef KEY_LAYEREDKEYS1_H +#define KEY_LAYEREDKEYS1_H +#include +#include +#include +#include +#include + +/* Most of the Key_LayeredKeys1 functionality is in Key_LayeredKeysBase. +Key_LayeredKeys1 is just like Key_LayeredKeys, but with a distinct refLayerState. +Key_LayeredKeys1 and Key_LayeredKeys can have different size ptrsKeys[]. +*/ +class Key_LayeredKeys1 : public Key_LayeredKeysBase +{ + private: + static LayerStateInterface& refLayerState; + public: + Key_LayeredKeys1(Key* const ptrsKeys[]) : Key_LayeredKeysBase(ptrsKeys) {} + virtual uint8_t getActiveLayer(); //get active layer from refLayerState +}; +#endif diff --git a/src/Key_LayeredKeysBase.cpp b/src/Key_LayeredKeysBase.cpp new file mode 100644 index 0000000..c83c1e7 --- /dev/null +++ b/src/Key_LayeredKeysBase.cpp @@ -0,0 +1,13 @@ +#include "Key_LayeredKeysBase.h" + +void Key_LayeredKeysBase::press() +{ + layerId = getActiveLayer(); + + ptrsKeys[layerId]->press(); +} + +void Key_LayeredKeysBase::release() +{ + ptrsKeys[layerId]->release(); +} diff --git a/src/Key_LayeredKeysBase.h b/src/Key_LayeredKeysBase.h new file mode 100644 index 0000000..822d05b --- /dev/null +++ b/src/Key_LayeredKeysBase.h @@ -0,0 +1,31 @@ +#ifndef KEY_LAYEREDKEYSBASE_H +#define KEY_LAYEREDKEYSBASE_H +#include +#include +#include +#include + +/* Class Key_LayeredKeysBase contains an array of Key pointers, one pointer per layer. +Codes are a kind of Key, so the Key pointers can point to Codes or Keys. + +When the key is pressed, active layerId is retreived and the Key object of that layer is called. + +Design note: +Child classes (Key_LayeredKeys and Key_LayeredKeys1) have distinct static refLayerState +and different size ptrsKeys[]. +That way Key_LayeredKeys instantiations don't need a LayerState argument, +which is significant for a sketch with 100 keys. +*/ +class Key_LayeredKeysBase : public Key +{ + private: + Key*const *const ptrsKeys; //array of Key pointers, one Key per layer + uint8_t layerId; //active layer when key was pressed + static LayerStateInterface& refLayerState; + public: + Key_LayeredKeysBase(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} + virtual uint8_t getActiveLayer()=0; + virtual void press(); + virtual void release(); +}; +#endif