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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* keybrd_2_single-layer.ino
  2. This sketch:
  3. is firmware for a simple 1-layer keyboard
  4. runs on the first two rows and columns of a breadboard keyboard
  5. This layout table shows how keys are arranged on the keyboard:
  6. | Layout | **0** | **1** |
  7. |:------:|-------|-------|
  8. | **0** | shift | a |
  9. | **1** | b | c |
  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 the next one or two lines of code.
  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 <ScanDelay.h>
  22. #include <Code_Sc.h>
  23. #include <Row_uC.h>
  24. /* ============ SPEED CONFIGURATION ============
  25. ScanDelay specifies microsecond between matrix scans.
  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. */
  30. ScanDelay scanDelay(9000);
  31. /* ================ ACTIVE STATE ===============
  32. The read pins detect which keys are pressed while a row is strobed.
  33. STROBE_ON and STROBE_OFF define the logic levels for the strobe.
  34. "Active low" means that if a switch is pressed (active), the read pin is low.
  35. To make this sketch active low, STROBE_ON should be LOW (tutorial 6 coveres this in more detail).
  36. */
  37. const bool Scanner_uC::STROBE_ON = LOW; //set scanner for active low
  38. const bool Scanner_uC::STROBE_OFF = HIGH;
  39. /* ================= PINS =================
  40. Microcontroller 14 and 15 are connected to the matrix columns.
  41. These readPins detect which keys are pressed while a row is strobed.
  42. sizeof() is used to compute the number of array elements.
  43. This eliminates the risk of forgetting to update the count
  44. after adding or removing an element from the array.
  45. */
  46. uint8_t readPins[] = {14, 15};
  47. uint8_t READ_PIN_COUNT = sizeof(readPins)/sizeof(*readPins);
  48. /* =================== CODES ===================
  49. Four Codes are instantiated, one for each key in the layout.
  50. The Code object names in this sketch start with a "s_" prefix.
  51. The Code_Sc constructor takes one scancode ("Sc" means "scancode").
  52. When Code_Sc is pressed, it sends the scancode.
  53. */
  54. Code_Sc s_a(KEY_A);
  55. Code_Sc s_b(KEY_B);
  56. Code_Sc s_c(KEY_C);
  57. Code_Sc s_shift(MODIFIERKEY_LEFT_SHIFT);
  58. /* =================== ROWS ====================
  59. Here we pack Code objects into Row objects.
  60. The Row objects names in this sketch start with a "row_" followed by a row number.
  61. Row_uC constructor has four parameters:
  62. 1) strobePin connected to the row.
  63. 2) readPins[] connected to the colums.
  64. 3) the number of readPins.
  65. 4) ptrsKeys[] containing all the Code objects of the row, one Code object per key.
  66. */
  67. Key* ptrsKeys_0[] = { &s_shift, &s_a };
  68. Row_uC row_0(0, readPins, READ_PIN_COUNT, ptrsKeys_0);
  69. Key* ptrsKeys_1[] = { &s_b, &s_c };
  70. Row_uC row_1(1, readPins, READ_PIN_COUNT, ptrsKeys_1);
  71. /* ################### MAIN ####################
  72. setup() is used to initialize the keyboard firmware. Keyboard.begin() should be called once.
  73. */
  74. void setup()
  75. {
  76. Keyboard.begin();
  77. }
  78. /*
  79. loop() continually scans the matrix, one row at a time.
  80. Each row object strobes its strobePin and reads the readPins.
  81. And when a key press is detected, the row sends the key's scancode.
  82. scanDelay creates time intervals between matrix scans.
  83. A debouncer uses this time interval to debounce key presses and releases.
  84. */
  85. void loop()
  86. {
  87. row_0.process();
  88. row_1.process();
  89. scanDelay.delay();
  90. }