Archived
1
0

add Key_LayeredKeysBase and Key_LayeredKeys1

This commit is contained in:
wolfv6 2016-09-19 20:40:25 -06:00
parent f62d956918
commit 04e2752b0b
7 changed files with 82 additions and 21 deletions

View File

@ -53,7 +53,10 @@ Keybrd library class inheritance diagram
Key
|____
| \
| Key_LayeredKeys
| Key_LayeredKeysBase
| \____________________
| / \
| Key_LayeredKeys Key_LayeredKeys1
|
|___________________________
| \ \

View File

@ -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();
}

View File

@ -4,22 +4,16 @@
#include <inttypes.h>
#include <LayerStateInterface.h>
#include <Key.h>
#include <Key_LayeredKeysBase.h>
/* 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

6
src/Key_LayeredKeys1.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "Key_LayeredKeys1.h"
uint8_t Key_LayeredKeys1::getActiveLayer()
{
return refLayerState.getActiveLayer();
}

21
src/Key_LayeredKeys1.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef KEY_LAYEREDKEYS1_H
#define KEY_LAYEREDKEYS1_H
#include <Arduino.h>
#include <inttypes.h>
#include <LayerStateInterface.h>
#include <Key.h>
#include <Key_LayeredKeysBase.h>
/* 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

View File

@ -0,0 +1,13 @@
#include "Key_LayeredKeysBase.h"
void Key_LayeredKeysBase::press()
{
layerId = getActiveLayer();
ptrsKeys[layerId]->press();
}
void Key_LayeredKeysBase::release()
{
ptrsKeys[layerId]->release();
}

31
src/Key_LayeredKeysBase.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef KEY_LAYEREDKEYSBASE_H
#define KEY_LAYEREDKEYSBASE_H
#include <Arduino.h>
#include <inttypes.h>
#include <LayerStateInterface.h>
#include <Key.h>
/* 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