2016-09-20 01:46:03 +00:00
/* keybrd_3b_layerLock.ino
2016-05-09 14:05:08 +00:00
This sketch :
2016-07-18 02:03:03 +00:00
is firmware for a simple 2 - layer keyboard
2016-05-09 14:05:08 +00:00
runs on the first two rows and columns of a breadboard keyboard
| Layout | * * 0 * * | * * 1 * * |
2016-09-20 01:46:03 +00:00
| : - - - - - - : | : - - - - - : | : - - - - - : |
| * * 0 * * | a [ | b ] |
| * * 1 * * | normal | sym |
normal layer keys are a b c
sym layer keys are brackets [ ] and num
num layer keys are 6 7
The num layer key is located on the sym layer
num layer is active while holding sym + num
2016-05-09 14:05:08 +00:00
Each cell in the table ' s body represents a key .
2016-09-17 05:45:12 +00:00
The layered keys in column 1 have two layers ; one character for each layer .
2016-07-18 02:26:00 +00:00
Letters ' a ' and ' b ' are on the normal layer . Numbers ' 1 ' and ' 2 ' are on the fn layer .
2016-05-09 14:05:08 +00:00
Holding the fn key down makes it the active layer . Releasing the fn key restores the normal layer .
*/
// ################## GLOBAL ###################
// ================= INCLUDES ==================
2016-07-18 02:03:03 +00:00
//Keys
2016-05-09 14:05:08 +00:00
# include <Code_Sc.h>
2016-09-20 01:46:03 +00:00
//#include <Code_ScS.h>
//#include <Code_Null.h>
2016-05-28 21:16:32 +00:00
# include <LayerState.h>
2016-09-20 01:46:03 +00:00
# include <Code_LayerLock.h>
2016-05-09 14:05:08 +00:00
# include <Code_LayerHold.h>
2016-09-18 06:42:21 +00:00
# include <Key_LayeredKeys.h>
2016-05-09 14:05:08 +00:00
//Matrix
2016-09-17 05:45:12 +00:00
# include <Row.h>
# include <Scanner_uC.h>
2016-07-18 02:03:03 +00:00
# include <ScanDelay.h>
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
// ============ SPEED CONFIGURATION ============
ScanDelay scanDelay ( 9000 ) ;
2016-05-09 14:05:08 +00:00
2016-09-17 05:45:12 +00:00
// ================== SCANNER ==================
2016-07-18 02:03:03 +00:00
uint8_t readPins [ ] = { 14 , 15 } ;
2016-09-17 05:45:12 +00:00
uint8_t readPinCount = sizeof ( readPins ) / sizeof ( * readPins ) ;
Scanner_uC scanner ( LOW , readPins , readPinCount ) ;
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
/* =================== CODES ===================
The CODES section instantiates six codes , one for each item in the layout .
2016-05-09 14:05:08 +00:00
*/
2016-07-18 02:03:03 +00:00
/* ---------------- LAYER CODE -----------------
2016-09-20 01:46:03 +00:00
enum assigns layerId numbers to the layers .
2016-05-09 14:05:08 +00:00
*/
2016-09-20 01:46:03 +00:00
enum layers { NORMAL , SYM } ;
2016-07-18 02:03:03 +00:00
/* layerState keeps track of the active layer.
2016-05-09 14:05:08 +00:00
*/
2016-05-28 21:16:32 +00:00
LayerState layerState ;
2016-07-18 02:03:03 +00:00
2016-05-09 14:05:08 +00:00
/*
2016-09-20 01:46:03 +00:00
NORMAL = 0 and FN = 1. LayerState ' s default layerId is 0.
2016-07-18 02:03:03 +00:00
The Code_LayerHold constructor has two parameters :
2016-09-20 01:46:03 +00:00
1 ) the layerId that will be active while the key is held down
2016-09-17 05:45:12 +00:00
2 ) a LayerState that will keep track of the active layer
2016-05-28 21:16:32 +00:00
When l_fn is pressed , it tells layerState to change the active layer to 1.
2016-07-18 02:03:03 +00:00
When l_fn is released , it tells layerState that layer 1 is released , and layerState restores the default layer .
2016-05-09 14:05:08 +00:00
*/
2016-09-20 01:46:03 +00:00
Code_LayerLock l_normal ( NORMAL , layerState ) ;
Code_LayerLock l_sym ( SYM , layerState ) ;
2016-05-09 14:05:08 +00:00
// ---------------- SCAN CODES -----------------
Code_Sc s_a ( KEY_A ) ;
Code_Sc s_b ( KEY_B ) ;
2016-09-20 01:46:03 +00:00
Code_Sc s_leftBracket ( KEY_LEFT_BRACE ) ; //[ ("brace" means curly bracket {})
Code_Sc s_rightBracket ( KEY_RIGHT_BRACE ) ; //]
//Code_Null code_null;
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
/* =================== KEYS ====================
Here we pack Codes into keys .
2016-09-18 06:42:21 +00:00
The Key_LayeredKeys constructor takes one array of Code pointers - one Code object per layer .
2016-05-09 14:05:08 +00:00
2016-07-18 02:26:00 +00:00
The Key object names in this sketch start with a " k_ " followed by row - column coordinates .
2016-05-09 14:05:08 +00:00
*/
2016-09-20 01:46:03 +00:00
Key * const ptrsCodes_00 [ ] = { & s_a , & s_leftBracket } ;
Key_LayeredKeys k_00 ( ptrsCodes_00 ) ;
2016-05-09 14:05:08 +00:00
2016-09-20 01:46:03 +00:00
Key * const ptrsCodes_01 [ ] = { & s_b , & s_rightBracket } ;
Key_LayeredKeys k_01 ( ptrsCodes_01 ) ;
2016-07-18 02:03:03 +00:00
2016-09-18 06:42:21 +00:00
/* Key_LayeredKeys has a reference to layerState.
2016-09-20 01:46:03 +00:00
Thus Key_LayeredKeys can call layerState to get the active layerId .
2016-05-09 14:05:08 +00:00
*/
2016-09-18 06:42:21 +00:00
LayerStateInterface & Key_LayeredKeys : : refLayerState = layerState ;
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
/* HOW LAYERED OBJECTS WORK
2016-09-18 06:42:21 +00:00
When a Key_LayeredKeys object is pressed , it gets the active layer id from layerState .
2016-09-17 05:45:12 +00:00
It then uses the layer id as an array index to call the Code of the active layer .
The Code object then sends its scancode over USB .
2016-05-09 14:05:08 +00:00
*/
2016-07-18 02:03:03 +00:00
/* =================== ROWS ====================
Here we pack Key pointers into row objects .
2016-05-09 14:05:08 +00:00
Codes are a kind of Key that only have one layer .
2016-07-18 02:26:00 +00:00
So rows can contain a mix of codes and multi - layered keys .
Arrays ptrsKeys_0 [ ] and ptrsKeys_1 [ ] contain both Code pointers and Key pointers .
2016-05-09 14:05:08 +00:00
*/
2016-09-20 01:46:03 +00:00
Key * const ptrsKeys_0 [ ] = { & k_00 , & k_01 } ;
2016-09-17 05:45:12 +00:00
uint8_t keyCount_0 = sizeof ( ptrsKeys_0 ) / sizeof ( * ptrsKeys_0 ) ;
Row row_0 ( scanner , 0 , ptrsKeys_0 , keyCount_0 ) ;
2016-05-09 14:05:08 +00:00
2016-09-20 01:46:03 +00:00
Key * const ptrsKeys_1 [ ] = { & l_normal , & l_sym } ;
2016-09-17 05:45:12 +00:00
uint8_t keyCount_1 = sizeof ( ptrsKeys_1 ) / sizeof ( * ptrsKeys_1 ) ;
Row row_1 ( scanner , 1 , ptrsKeys_1 , keyCount_1 ) ;
2016-05-09 14:05:08 +00:00
// ################### MAIN ####################
void setup ( )
{
Keyboard . begin ( ) ;
}
void loop ( )
{
2016-07-18 02:03:03 +00:00
row_0 . process ( ) ;
row_1 . process ( ) ;
scanDelay . delay ( ) ;
2016-05-09 14:05:08 +00:00
}