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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. | Layout | **0** | **1** |
  6. |:------:|-------|-------|
  7. | **0** | shift | a ! |
  8. | **1** | fn | b @ |
  9. The layered keys in row 0 have two layers; one character for each layer.
  10. Letters 'a' and 'b' are on the normal layer. Symbols '!' and '@' are one the fn layer.
  11. Holding the fn key down makes it the active layer. Releasing the fn key restores the normal layer.
  12. */
  13. // ################## GLOBAL ###################
  14. // ================= INCLUDES ==================
  15. //Keys
  16. #include <Code_Sc.h>
  17. #include <Code_ScS.h>
  18. #include <Code_Shift.h>
  19. #include <LayerState.h>
  20. #include <Code_LayerHold.h>
  21. #include <Key_LayeredKeysArray.h>
  22. //Matrix
  23. #include <Row_uC.h>
  24. #include <ScanDelay.h>
  25. // ============ SPEED CONFIGURATION ============
  26. ScanDelay scanDelay(9000);
  27. // ================ ACTIVE STATE ===============
  28. const bool Scanner_uC::STROBE_ON = LOW;
  29. const bool Scanner_uC::STROBE_OFF = HIGH;
  30. // =================== PINS ====================
  31. uint8_t readPins[] = {14, 15};
  32. uint8_t READ_PIN_COUNT = sizeof(readPins)/sizeof(*readPins);
  33. // =================== CODES ===================
  34. // ---------------- LAYER CODE -----------------
  35. enum layers { NORMAL, FN };
  36. LayerState layerState;
  37. Code_LayerHold l_fn(FN, layerState);
  38. /* ---------------- SCAN CODES -----------------
  39. The "Sc" in Code_Sc means "scancode".
  40. When a Code_Sc is pressed, it sends its scancode.
  41. */
  42. Code_Sc s_a(KEY_A);
  43. Code_Sc s_b(KEY_B);
  44. /* The "ScS" in Code_ScS means "scancode shifted".
  45. When Code_ScS is pressed, it calls Code_AutoShift before sending its scancode.
  46. */
  47. Code_ScS s_exclamation(KEY_1);
  48. Code_ScS s_at(KEY_2);
  49. // ----------------- SHIFT CODE ----------------
  50. /*
  51. The Code_Shift constructor takes one shift scancode.
  52. */
  53. Code_Shift s_shift(MODIFIERKEY_LEFT_SHIFT);
  54. /*
  55. Code_Shift pointers are placed in an array because most keyboards have a left and right shift.
  56. This sketch only has one shift code.
  57. */
  58. Code_Shift* const ptrsS[] = { &s_shift };
  59. Code_Shift* const* const Code_AutoShift::ptrsShifts = ptrsS;
  60. const uint8_t Code_AutoShift::shiftCount = sizeof(ptrsS)/sizeof(*ptrsS);
  61. /*
  62. HOW SHIFT WORKS
  63. When a shift key is pressed, a standard keyboard driver will temporarily modify the normal action of another key when pressed together.
  64. KEY_1 writes '1'
  65. MODIFIERKEY_LEFT_SHIFT + KEY_1 writes '!'
  66. KEY_2 writes '2'
  67. MODIFIERKEY_LEFT_SHIFT + KEY_2 writes '@'
  68. HOW AUTOSHIFT WORKS
  69. Code_ScS takes care of the MODIFIERKEY_LEFT_SHIFT automatically
  70. When the user presses '!' or '@' on the fn layer:
  71. Code_AutoShift checks the position of each shift key
  72. Code_ScS sends MODIFIERKEY_LEFT_SHIFT scancode if needed
  73. Code_ScS sends its scancode
  74. */
  75. // =================== KEYS ====================
  76. Key* const ptrsCodes_01[] = { &s_a, &s_exclamation };
  77. Key_LayeredKeysArray k_01(ptrsCodes_01);
  78. Key* const ptrsCodes_11[] = { &s_b, &s_at };
  79. Key_LayeredKeysArray k_11(ptrsCodes_11);
  80. LayerStateInterface& Key_LayeredKeysArray::refLayerState = layerState;
  81. // =================== ROWS ====================
  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. }