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

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