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_MCP23018.ino 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* keybrd_MCP23018.ino
  2. This sketch:
  3. is a simple 1-layer keyboard
  4. runs on two matrices of a breadboard keyboard
  5. Controller I/O expander
  6. | Left | **0** | **1** | | Right | **0** | **1** |
  7. |-------|-------|-------| |-------|-------|-------|
  8. | **1** | 1 | 2 | | **1** | 3 | 4 |
  9. | **0** | a | b | | **0** | c | d |
  10. */
  11. // ################## GLOBAL ###################
  12. // ================= INCLUDES ==================
  13. #include <ScanDelay.h>
  14. #include <Code_Sc.h>
  15. #include <Code_LEDLock.h>
  16. #include <Row.h>
  17. //left matrix
  18. #include <Scanner_uC.h>
  19. //right matrix
  20. #include <Port_MCP23018.h>
  21. #include <Scanner_IOE.h>
  22. #include <LED_Port.h>
  23. // ============ SPEED CONFIGURATION ============
  24. ScanDelay scanDelay(9000);
  25. /* ================ LEFT SCANNER ===============
  26. Left matrix rows work the same as the ones in keybrd_2_single-layer.ino
  27. */
  28. uint8_t readPins[] = {14, 15};
  29. const uint8_t readPinCount = sizeof(readPins)/sizeof(*readPins);
  30. Scanner_uC scanner_L(LOW, readPins, readPinCount);
  31. /* =============== RIGHT SCANNER ===============
  32. The right matrix is scanned by an I/O expander.
  33. The MCP23018 address is set by grounding or powering pins.
  34. */
  35. const uint8_t IOE_ADDR = 0x20; //MCP23018 address pin grounded
  36. /*
  37. Normally all strobe pins are on one port, and all the read pins are on the other port.
  38. In this example, portB stobes the row while portA reads the colums.
  39. Port_MCP23018 constructor parameters are: deviceAddr, portNum, readPins.
  40. readPins is a bit pattern, where 0=output, 1=input.
  41. In portA, the first two pins are set to input for reading.
  42. "<<" (bit shift left) and "|" (OR) are bitwise operators.
  43. Pin numbers to be read are delimited by "|" and to the right of "1<<".
  44. */
  45. Port_MCP23018 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //read pins 0, 1
  46. Port_MCP23018 portB(IOE_ADDR, 1, 0);
  47. Scanner_IOE scanner_R(LOW, portB, portA);
  48. /* ================= RIGHT LED =================
  49. The LED_Port constructor parameters are a port and pin number that is connected to an LED.
  50. */
  51. LED_Port LED_capsLck(portA, 1<<7);
  52. // =================== CODES ===================
  53. Code_Sc s_a(KEY_A);
  54. Code_Sc s_b(KEY_B);
  55. Code_Sc s_c(KEY_C);
  56. Code_Sc s_d(KEY_D);
  57. Code_Sc s_1(KEY_1);
  58. Code_Sc s_2(KEY_2);
  59. Code_Sc s_3(KEY_3);
  60. Code_Sc s_4(KEY_4);
  61. Code_LEDLock o_capsLock(KEY_CAPS_LOCK, LED_capsLck);//was testing LED, restore s_4 when done
  62. /* =================== ROWS ====================
  63. Left row names contain the letter 'L', while right row names conatain the letter 'R'.
  64. Row constructor parameters are: scanner, strobePin, ptrsKeys[], keyCount.
  65. strobePin has one of two formats:
  66. * if refScanner a Scanner_uC, then strobePin is an Arduino pin number connected to this row
  67. * otherwise strobePin is a bit pattern, 1 indicating an IC pin connected to the row
  68. */
  69. /* ---------------- LEFT ROWS ------------------
  70. The left rows have a Scanner_uC and Arduino pin numbers to strobe.
  71. */
  72. Key* ptrsKeys_L0[] = { &s_1, &s_2 };
  73. const uint8_t KEY_COUNT_L0 = sizeof(ptrsKeys_L0)/sizeof(*ptrsKeys_L0);
  74. Row row_L0(scanner_L, 0, ptrsKeys_L0, KEY_COUNT_L0);
  75. Key* ptrsKeys_L1[] = { &s_a, &s_b };
  76. const uint8_t KEY_COUNT_L1 = sizeof(ptrsKeys_L1)/sizeof(*ptrsKeys_L1);
  77. Row row_L1(scanner_L, 1, ptrsKeys_L1, KEY_COUNT_L1);
  78. /* ---------------- RIGHT ROWS -----------------
  79. The right rows have a Scanner_IOE and pin bits to strobe.
  80. */
  81. Key* ptrsKeys_R0[] = { &s_3, &o_capsLock };
  82. const uint8_t KEY_COUNT_R0 = sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0);
  83. Row row_R0(scanner_R, 1<<0, ptrsKeys_R0, KEY_COUNT_R0);
  84. Key* ptrsKeys_R1[] = { &s_c, &s_d };
  85. const uint8_t KEY_COUNT_R1 = sizeof(ptrsKeys_R1)/sizeof(*ptrsKeys_R1);
  86. Row row_R1(scanner_R, 1<<1, ptrsKeys_R1, KEY_COUNT_R1);
  87. // ################### MAIN ####################
  88. void setup()
  89. {
  90. Keyboard.begin();
  91. scanner_R.begin();
  92. }
  93. void loop()
  94. {
  95. //left matrix
  96. row_L0.process();
  97. row_L1.process();
  98. //right matrix
  99. row_R0.process();
  100. row_R1.process();
  101. scanDelay.delay();
  102. //debug.printScansPerSecond();
  103. //debug.printMicrosecondsPerScan();
  104. }