keybrd library is an open source library for creating custom-keyboard firmware.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keybrd_3a_multi-layer.ino 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* keybrd_3_multi-layer_annotated.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 row 0 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_LayeredKeysArray.h>
  21. //Matrix
  22. #include <Row_uC.h>
  23. #include <ScanDelay.h>
  24. // ============ SPEED CONFIGURATION ============
  25. ScanDelay scanDelay(9000);
  26. // ================ ACTIVE STATE ===============
  27. const bool Scanner_uC::STROBE_ON = LOW;
  28. const bool Scanner_uC::STROBE_OFF = HIGH;
  29. // =================== PINS ====================
  30. uint8_t readPins[] = {14, 15};
  31. uint8_t READ_PIN_COUNT = sizeof(readPins)/sizeof(*readPins);
  32. /* =================== CODES ===================
  33. The CODES section instantiates six codes, one for each item in the layout.
  34. */
  35. /* ---------------- LAYER CODE -----------------
  36. enum assigns id numbers to the layers.
  37. */
  38. enum layers { NORMAL, FN };
  39. /* layerState keeps track of the active layer.
  40. */
  41. LayerState layerState;
  42. /*
  43. NORMAL=0 and FN=1. LayerState's default layer id is 0.
  44. The Code_LayerHold constructor has two parameters:
  45. 1) the layer that will be active while the key is held down.
  46. 2) a LayerState
  47. When l_fn is pressed, it tells layerState to change the active layer to 1.
  48. When l_fn is released, it tells layerState that layer 1 is released, and layerState restores the default layer.
  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_1(KEY_1);
  55. Code_Sc s_2(KEY_2);
  56. Code_Sc s_shift(MODIFIERKEY_LEFT_SHIFT);
  57. /* =================== KEYS ====================
  58. Here we pack Codes into keys.
  59. The Key_LayeredKeysArray constructor takes one array of Code pointers - one Code object per layer.
  60. Key_LayeredKeysArray uses layer id numbers as array indexes.
  61. Thus Key_LayeredKeysArray calls the Code corresponding to the active layer id.
  62. The Key object names in this sketch start with a "k_" followed by row-column coordinates.
  63. */
  64. Key* const ptrsCodes_01[] = { &s_a, &s_1 };
  65. Key_LayeredKeysArray k_01(ptrsCodes_01);
  66. Key* const ptrsCodes_11[] = { &s_b, &s_2 };
  67. Key_LayeredKeysArray k_11(ptrsCodes_11);
  68. /* Key_LayeredKeysArray has a reference to layerState.
  69. Thus Key_LayeredKeysArray can call layerState to get the active layer id.
  70. */
  71. LayerStateInterface& Key_LayeredKeysArray::refLayerState = layerState;
  72. /* HOW LAYERED OBJECTS WORK
  73. When a Key_LayeredKeysArray object is pressed, it gets the active layer id from layerState
  74. It then uses the layer id as an array index to send the scancode for the active layer.
  75. */
  76. /* =================== ROWS ====================
  77. Here we pack Key pointers into row objects.
  78. Codes are a kind of Key that only have one layer.
  79. So rows can contain a mix of codes and multi-layered keys.
  80. Arrays ptrsKeys_0[] and ptrsKeys_1[] contain both Code pointers and Key pointers.
  81. */
  82. Key* const ptrsKeys_0[] = { &s_shift, &k_01 };
  83. Row_uC row_0(0, readPins, READ_PIN_COUNT, ptrsKeys_0);
  84. Key* const ptrsKeys_1[] = { &l_fn, &k_11 };
  85. Row_uC row_1(1, readPins, READ_PIN_COUNT, ptrsKeys_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. }