keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keybrd_3a_multi-layerHold.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* keybrd_3a_multi-layerHold.ino
  2. This sketch:
  3. is firmware for a simple 2-layer keyboard
  4. runs on the first two rows and columns of a breadboard keyboard
  5. | Layout | **0** | **1** |
  6. |:------:|-------|-------|
  7. | **0** | shift | a 1 |
  8. | **1** | fn | b 2 |
  9. Each cell in the table's body represents a key.
  10. The layered keys in column 1 have two layers; one character for each layer.
  11. Letters 'a' and 'b' are on the normal layer. Numbers '1' and '2' are on the fn layer.
  12. Holding the fn key down makes it the active layer. Releasing the fn key restores the normal layer.
  13. */
  14. // ################## GLOBAL ###################
  15. // ================= INCLUDES ==================
  16. //Keys
  17. #include <Code_Sc.h>
  18. #include <LayerState.h>
  19. #include <Code_LayerHold.h>
  20. #include <Key_LayeredKeys.h>
  21. //Matrix
  22. #include <Row.h>
  23. #include <Scanner_uC.h>
  24. #include <ScanDelay.h>
  25. // ============ SPEED CONFIGURATION ============
  26. ScanDelay scanDelay(9000);
  27. // ================== SCANNER ==================
  28. uint8_t readPins[] = {14, 15};
  29. uint8_t readPinCount = sizeof(readPins)/sizeof(*readPins);
  30. Scanner_uC scanner(LOW, readPins, readPinCount);
  31. /* =================== CODES ===================
  32. The CODES section instantiates six codes, one for each item in the layout.
  33. */
  34. /* ---------------- LAYER CODE -----------------
  35. enum assigns layerId numbers to the layers.
  36. */
  37. enum layers { NORMAL, FN };
  38. /* layerState keeps track of the active layer.
  39. */
  40. LayerState layerState;
  41. /*
  42. NORMAL=0 and FN=1. LayerState's default layerId is 0.
  43. The Code_LayerHold constructor has two parameters:
  44. 1) the layerId that will be active while the key is held down
  45. 2) a LayerState that will keep track of the active layer
  46. When l_fn is pressed, it tells layerState to change the active layer to 1.
  47. When l_fn is released, it tells layerState that layer 1 is released, and layerState restores the default layer.
  48. */
  49. Code_LayerHold l_fn(FN, layerState);
  50. // ---------------- SCAN CODES -----------------
  51. Code_Sc s_a(KEY_A);
  52. Code_Sc s_b(KEY_B);
  53. Code_Sc s_1(KEY_1);
  54. Code_Sc s_2(KEY_2);
  55. Code_Sc s_shift(MODIFIERKEY_LEFT_SHIFT);
  56. /* =================== KEYS ====================
  57. Here we pack Codes into keys.
  58. The Key_LayeredKeys constructor takes one array of Code pointers - one Code object per layer.
  59. The Key object names in this sketch start with a "k_" followed by row-column coordinates.
  60. */
  61. Key* const ptrsCodes_01[] = { &s_a, &s_1 };
  62. Key_LayeredKeys k_01(ptrsCodes_01);
  63. Key* const ptrsCodes_11[] = { &s_b, &s_2 };
  64. Key_LayeredKeys k_11(ptrsCodes_11);
  65. /* Key_LayeredKeys has a reference to layerState.
  66. Thus Key_LayeredKeys can call layerState to get the active layerId.
  67. */
  68. LayerStateInterface& Key_LayeredKeys::refLayerState = layerState;
  69. /* HOW LAYERED OBJECTS WORK
  70. When a Key_LayeredKeys object is pressed, it gets the active layer id from layerState.
  71. It then uses the layer id as an array index to call the Code of the active layer.
  72. The Code object then sends its scancode over USB.
  73. */
  74. /* =================== ROWS ====================
  75. Here we pack Key pointers into row objects.
  76. Codes are a kind of Key that only have one layer.
  77. So rows can contain a mix of codes and multi-layered keys.
  78. Arrays ptrsKeys_0[] and ptrsKeys_1[] contain both Code pointers and Key pointers.
  79. */
  80. Key* const ptrsKeys_0[] = { &s_shift, &k_01 };
  81. uint8_t keyCount_0 = sizeof(ptrsKeys_0)/sizeof(*ptrsKeys_0);
  82. Row row_0(scanner, 0, ptrsKeys_0, keyCount_0);
  83. Key* const ptrsKeys_1[] = { &l_fn, &k_11 };
  84. uint8_t keyCount_1 = sizeof(ptrsKeys_1)/sizeof(*ptrsKeys_1);
  85. Row row_1(scanner, 1, ptrsKeys_1, keyCount_1);
  86. // ################### MAIN ####################
  87. void setup()
  88. {
  89. Keyboard.begin();
  90. }
  91. void loop()
  92. {
  93. row_0.process();
  94. row_1.process();
  95. scanDelay.delay();
  96. }