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_2_single-layer.ino 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* keybrd_2_single-layer.ino
  2. This sketch:
  3. is firmware for a simple 1-layer keyboard
  4. runs on two rows and two columns of a breadboard keyboard
  5. This layout table shows how keys are arranged on the keyboard:
  6. | Layout | **0** | **1** |
  7. |:------:|-------|-------|
  8. | **0** | 1 | 2 |
  9. | **1** | a | b |
  10. The layout's row and column numbers are in the headers.
  11. Each cell in the table's body represents a key.
  12. The following sketch is annotated with a walk-through narrative enclosed in comment blocks.
  13. Each comment block explains one or two lines of code after the comnent.
  14. keybrd objects are instantiated under the "GLOBAL" heading.
  15. The keyboard runs at the end of the sketch, under the "MAIN" heading.
  16. */
  17. // ################## GLOBAL ###################
  18. /* ================= INCLUDES ==================
  19. All the includes in this sketch are to keybrd library classes.
  20. */
  21. #include <Code_Sc.h>
  22. #include <Row.h>
  23. #include <Scanner_uC.h>
  24. #include <ScanDelay.h>
  25. /* ============ SPEED CONFIGURATION ============
  26. Keyboard switches are made of moving contacts.
  27. When the contacts close, they bounce apart one or more times before making steady contact.
  28. ScanDelay gives the switches time to debounce.
  29. ScanDelay specifies microsecond between matrix scans.
  30. */
  31. ScanDelay scanDelay(9000);
  32. /* ================== SCANNER ==================
  33. Microcontroller pins 14 and 15 are connected to the matrix columns.
  34. sizeof() is used to compute the number of array elements.
  35. This eliminates the risk of a programmer forgetting to update a count
  36. after adding or removing an element from the array.
  37. */
  38. uint8_t readPins[] = {14, 15};
  39. uint8_t readPinCount = sizeof(readPins)/sizeof(*readPins);
  40. /*
  41. Scanner_uC constructor parameters are: strobeOn, readPins[], readPinCount.
  42. strobeOn defines the logic level for strobes, HIGH or LOW.
  43. "Active low" means that if a switch is pressed (active), the read pin is low.
  44. The scanner uses readPins and readPinCount to read the colums.
  45. */
  46. Scanner_uC scanner(LOW, readPins, readPinCount);
  47. /* HOW SCANNER OBJECTS WORK
  48. The Scanner object strobes a row.
  49. If a key is pressed, the LOW strobe pulls that readPin LOW.
  50. Then the scanner reads its readPins.
  51. */
  52. /* =================== CODES ===================
  53. Four Codes are instantiated, one for each key in the layout.
  54. The Code object names in this sketch start with a "s_" prefix.
  55. The Code_Sc constructor takes one scancode ("Sc" means "scancode").
  56. When Code_Sc is pressed, it sends the scancode.
  57. */
  58. Code_Sc s_a(KEY_A);
  59. Code_Sc s_b(KEY_B);
  60. Code_Sc s_1(KEY_1);
  61. Code_Sc s_2(KEY_2);
  62. /* =================== ROWS ====================
  63. Here we pack Code objects into Row objects.
  64. The Row objects names in this sketch start with a "row_" followed by a row number.
  65. Row constructor parameters are: scanner, strobePin, ptrsKeys[], keyCount.
  66. strobePin is the Arduino pin number connected to the row.
  67. ptrsKeys[] contains all the Code objects of the row, one Code object per key.
  68. */
  69. Key* ptrsKeys_0[] = { &s_1, &s_2 };
  70. uint8_t keyCount_0 = sizeof(ptrsKeys_0)/sizeof(*ptrsKeys_0);
  71. Row row_0(scanner, 0, ptrsKeys_0, keyCount_0);
  72. Key* ptrsKeys_1[] = { &s_a, &s_b };
  73. uint8_t keyCount_1 = sizeof(ptrsKeys_1)/sizeof(*ptrsKeys_1);
  74. Row row_1(scanner, 1, ptrsKeys_1, keyCount_1);
  75. /* ################### MAIN ####################
  76. setup() is used to initialize the keyboard firmware. Keyboard.begin() should be called once.
  77. */
  78. void setup()
  79. {
  80. Keyboard.begin();
  81. }
  82. /*
  83. loop() continually scans the matrix, one row at a time.
  84. Each Row object strobes its strobePin and reads the readPins.
  85. And when a key press is detected, the row sends the key's scancode.
  86. scanDelay creates time intervals between matrix scans.
  87. The delay is needed so that the debouncer is not overwelmed.
  88. */
  89. void loop()
  90. {
  91. row_0.process();
  92. row_1.process();
  93. scanDelay.delay();
  94. }