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.7KB

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