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-layer_annotated.ino 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* keybrd_3_multi-layer_annotated.ino
  2. This sketch:
  3. is a simple 2-layer keyboard
  4. runs on the first two rows and columns of a breadboard keyboard
  5. is annotated with a walk-through narrative
  6. This layout table shows how keys are arranged on the keyboard:
  7. | Layout | **0** | **1** |
  8. |:------:|-------|-------|
  9. | **0** | a 1 | b 2 |
  10. | **1** | fn | shift |
  11. The layout's row and column numbers are in the headers.
  12. Each cell in the table's body represents a key.
  13. The layered keys in row 0 have two layers; one character for each layer.
  14. Letters 'a' and 'b' are on the normal layer. Numbers '1' and '2' are one the fn layer.
  15. Holding the fn key down makes it the active layer. Releasing the fn key restores the normal layer.
  16. */
  17. // ################## GLOBAL ###################
  18. // ================= INCLUDES ==================
  19. //Ports
  20. #include <RowPort_AVR_Optic.h>
  21. #include <ColPort_AVR.h>
  22. //Codes
  23. #include <Code_Sc.h>
  24. #include <StateLayers.h>
  25. #include <Code_LayerHold.h>
  26. #include <Key_LayeredKeysArray.h>
  27. //Matrix
  28. #include <Row.h>
  29. #include <Matrix.h>
  30. // ============ SPEED CONFIGURATIONS ============
  31. const unsigned int Row::DELAY_MICROSECONDS = 1000;
  32. // =================== PORTS ===================
  33. RowPort_AVR_Optic rowPortF(DDRF, PORTF);
  34. ColPort_AVR colPortB(DDRB, PORTB, PINB, 1<<0 | 1<<1 );
  35. ColPort* const ptrsColPorts[] = { &colPortB };
  36. const uint8_t COL_PORT_COUNT = sizeof(ptrsColPorts)/sizeof(*ptrsColPorts);
  37. // =================== CODES ===================
  38. /*
  39. The CODES section instantiates six codes, one for each item in the layout:
  40. s_a s_1 s_b s_2
  41. l_fn s_shift
  42. */
  43. // ---------------- LAYER CODE -----------------
  44. /*
  45. enum assings layer numbers to the layers.
  46. */
  47. enum layers { NORMAL, FN };
  48. /*
  49. stateLayer keeps track of the active layer. The default layer number is 0.
  50. */
  51. StateLayers stateLayer;
  52. /*
  53. The Code_LayerHold constructor parameter specifies a layer number and the StateLayers it calls.
  54. When l_fn is pressed, it tells stateLayer to change the active layer to 1.
  55. When l_fn is released, it tells stateLayer to restore the normal layer.
  56. */
  57. Code_LayerHold l_fn(FN, stateLayer);
  58. // ---------------- SCAN CODES -----------------
  59. Code_Sc s_a(KEY_A);
  60. Code_Sc s_b(KEY_B);
  61. Code_Sc s_1(KEY_1);
  62. Code_Sc s_2(KEY_2);
  63. Code_Sc s_shift(MODIFIERKEY_LEFT_SHIFT);
  64. // ================== MATRIX ===================
  65. /*
  66. The MATRIX section instantiates the components of the matrix:
  67. Codes are grouped into keys.
  68. Keys are grouped into rows.
  69. Rows are grouped into a matrix.
  70. */
  71. // ------------------- KEYS --------------------
  72. /*
  73. Here we group Code pointers into keys.
  74. Array ptrsCodes_00[] contains two pointers to Code objects.
  75. Key_LayeredKeysArray constructor parameters are:
  76. one array of Code pointers
  77. Key_LayeredKeysArray objects are multi-layered - one Code object per layer.
  78. Layer numbers are array indexes for the Key_LayeredKeysArray.
  79. Defining layer numbers with enum insures that the layer numbers are a series starting at 0.
  80. The Key object names in this sketch start with a "k_" followed by matrix-row-column coordinates.
  81. */
  82. Key* const ptrsCodes_00[] = { &s_a, &s_1 };
  83. Key_LayeredKeysArray k_00(ptrsCodes_00);
  84. Key* const ptrsCodes_01[] = { &s_b, &s_2 };
  85. Key_LayeredKeysArray k_01(ptrsCodes_01);
  86. /*
  87. Key_LayeredKeysArray has a static variable refStateLayers defined here.
  88. It is a reference to stateLayer.
  89. */
  90. StateLayersInterface& Key_LayeredKeysArray::refStateLayers = stateLayer;
  91. /*
  92. HOW LAYERED OBJECTS WORK
  93. When a Key_LayeredKeysArray object is pressed,
  94. it gets the active layer from stateLayer and then sends the scancode for the active layer.
  95. */
  96. // ------------------- ROWS --------------------
  97. /*
  98. Here we group Key pointers into rows.
  99. Array ptrsKeys_0[] contains two pointers to Key_LayeredKeyArray objects.
  100. */
  101. Key* const ptrsKeys_0[] = { &k_00, &k_01 };
  102. Row row_0(rowPortF, 1<<0, ptrsColPorts, COL_PORT_COUNT, ptrsKeys_0);
  103. /*
  104. Codes are a kind of Key that only have one layer.
  105. So rows can contain multi-leyered a mix of keys and codes.
  106. Array ptrsKeys_1[] contains two Code pointers.
  107. */
  108. Key* const ptrsKeys_1[] = { &l_fn, &s_shift };
  109. Row row_1(rowPortF, 1<<1, ptrsColPorts, COL_PORT_COUNT, ptrsKeys_1);
  110. // ------------------ MATRIX -------------------
  111. /*
  112. Here we group Row pointers into a matrix.
  113. Array ptrsRows[] contains two pointers to Row objects.
  114. */
  115. Row* const ptrsRows[] = { &row_0, &row_1 };
  116. const uint8_t ROW_COUNT = sizeof(ptrsRows)/sizeof(*ptrsRows);
  117. Matrix matrix(ptrsRows, ROW_COUNT, 1);
  118. // ################### MAIN ####################
  119. void setup()
  120. {
  121. Keyboard.begin();
  122. }
  123. void loop()
  124. {
  125. matrix.scan();
  126. }