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_3b_autoShift_annotated.ino 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* keybrd_3_autoShift_annotated.ino
  2. This sketch:
  3. is a simple 2-layer keyboard with AutoShift
  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 ! | b @ |
  10. | **1** | fn | shift |
  11. The layered keys in row 0 have two layers; one character for each layer.
  12. Letters 'a' and 'b' are on the normal layer. Symbols '!' and '@' are one the fn layer.
  13. Holding the fn key down makes it the active layer. Releasing the fn key restores the normal layer.
  14. */
  15. // ################## GLOBAL ###################
  16. // ================= INCLUDES ==================
  17. //Ports
  18. #include <RowPort_AVR_Optic.h>
  19. #include <ColPort_AVR.h>
  20. //Codes
  21. #include <Code_Sc.h>
  22. #include <Code_ScS.h>
  23. #include <Code_Shift.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_exclamation s_b s_at
  41. l_fn s_shift
  42. */
  43. // ---------------- LAYER CODE -----------------
  44. enum layers { NORMAL, FN };
  45. StateLayers stateLayer;
  46. Code_LayerHold l_fn(FN, stateLayer);
  47. // ---------------- SCAN CODES -----------------
  48. /*
  49. The Code_Sc constructor takes one scancode ("Sc" means "scancode").
  50. When Code_Sc is pressed, it sends its scancode.
  51. */
  52. Code_Sc s_a(KEY_A);
  53. Code_Sc s_b(KEY_B);
  54. /*
  55. The Code_ScS constructor takes one scancode to be shifted ("ScS" means "scancode shifted").
  56. When Code_ScS is pressed, it calls Code_AutoShift before sending its scancode.
  57. */
  58. Code_ScS s_exclamation(KEY_1);
  59. Code_ScS s_at(KEY_2);
  60. // ----------------- SHIFT CODE ----------------
  61. /*
  62. The Code_Shift constructor takes one scancode.
  63. */
  64. Code_Shift s_shift(MODIFIERKEY_LEFT_SHIFT);
  65. /*
  66. Code_Shift pointers are placed in an array because most keyboards have a left and right shift.
  67. This sketch only has one shift code.
  68. */
  69. Code_Shift* const ptrsS[] = { &s_shift };
  70. /*
  71. Code_AutoShift is the base class of Codes_ScS (Codes_ScS is explained in the preceding section).
  72. It has two static variables, ptrsShifts and shiftCount, which are defined here.
  73. ptrsShifts is the array of Code_Shift pointers; one pointer for each shift key.
  74. */
  75. Code_Shift* const* const Code_AutoShift::ptrsShifts = ptrsS;
  76. const uint8_t Code_AutoShift::shiftCount = sizeof(ptrsShifts)/sizeof(*ptrsShifts);
  77. /*
  78. HOW AUTOSHIFT WORKS
  79. When a modifier key is pressed, a standard keyboard driver will temporarily modify the normal action of another key when pressed together.
  80. KEY_1 writes '1'
  81. MODIFIERKEY_LEFT_SHIFT + KEY_1 writes '!'
  82. KEY_2 writes '2'
  83. MODIFIERKEY_LEFT_SHIFT + KEY_2 writes '@'
  84. Code_ScS takes care of the MODIFIERKEY_LEFT_SHIFT automatically
  85. When the user presses '!' or '@' on the fn layer:
  86. Code_AutoShift checks the position of each shift key
  87. Code_ScS sends MODIFIERKEY_LEFT_SHIFT scancode if needed
  88. Code_ScS sends its scancode
  89. */
  90. // ================== MATRIX ===================
  91. // ------------------- KEYS --------------------
  92. Key* const ptrsCodes_00[] = { &s_a, &s_exclamation };
  93. Key_LayeredKeysArray k_00(ptrsCodes_00);
  94. Key* const ptrsCodes_01[] = { &s_b, &s_at };
  95. Key_LayeredKeysArray k_01(ptrsCodes_01);
  96. StateLayersInterface& Key_LayeredKeysArray::refStateLayers = stateLayer;
  97. // ------------------- ROWS --------------------
  98. Key* const ptrsKeys_0[] = { &k_00, &k_01 };
  99. Row row_0(rowPortF, 1<<0, ptrsColPorts, COL_PORT_COUNT, ptrsKeys_0);
  100. Key* const ptrsKeys_1[] = { &l_fn, &s_shift };
  101. Row row_1(rowPortF, 1<<1, ptrsColPorts, COL_PORT_COUNT, ptrsKeys_1);
  102. // ------------------ MATRIX -------------------
  103. Row* const ptrsRows[] = { &row_0, &row_1 };
  104. const uint8_t ROW_COUNT = sizeof(ptrsRows)/sizeof(*ptrsRows);
  105. Matrix matrix(ptrsRows, ROW_COUNT, 1);
  106. // ################### MAIN ####################
  107. void setup()
  108. {
  109. Keyboard.begin();
  110. }
  111. void loop()
  112. {
  113. matrix.scan();
  114. }