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_MCP23017.ino 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* keybrd_MCP23017.ino
  2. This sketch:
  3. is a simple 1-layer keyboard
  4. runs on two matrices of a breadboard keyboard
  5. runs on both MCP23017 and MCP23018 IOEs (LED on/off will be reversed on MCP23017)
  6. Controller I/O expander
  7. | Left | **0** | **1** | | Right | **0** | **1** |
  8. |-------|-------|-------| |-------|-------|-------|
  9. | **1** | 1 | 2 | | **1** | 3 | 4 |
  10. | **0** | a | b | | **0** | fn | z 9 |
  11. MCP23017 pin assignments
  12. DESTINATION PIN PIN_NUMBER PIN DESTINATION
  13. row0 GPB0 1 28 GPA7
  14. row1 GPB1 2 27 GPA6
  15. GPB2 3 26 GPA5
  16. GPB3 4 25 GPA4
  17. GPB4 5 24 GPA3
  18. GPB5 6 23 GPA2
  19. GPB6 7 22 GPA1 col1
  20. GPB7 8 21 GPA0 col0
  21. LC 3.3V VDD 9 20 INTA
  22. GND VSS 10 19 INTB
  23. NC 11 18 /RESET VDD
  24. LC 19 SCL 12 17 A2 GND
  25. LC 18 SDA 13 16 A1 GND
  26. NC 14 15 A0 GND
  27. MCP23018 pin assignments
  28. DESTINATION PIN PIN_NUMBER PIN DESTINATION
  29. GND VSS 1 28 NC
  30. NC 2 27 GPA7
  31. row0 GPB0 3 26 GPA6
  32. row1 GPB1 4 25 GPA5
  33. GPB2 5 24 GPA4
  34. GPB4 7 22 GPA2
  35. GPB5 8 21 GPA1 col1
  36. GPB6 9 20 GPA0 col0
  37. GPB7 10 19 INTA
  38. LC 3.3V VCC 11 18 INTB
  39. LC 19 SCL 12 17 NC
  40. LC 18 SDA 13 16 /RESET VCC
  41. NC 14 15 ADDR GND
  42. */
  43. // ################## GLOBAL ###################
  44. // ================= INCLUDES ==================
  45. #include <ScanDelay.h>
  46. #include <Code_Sc.h>
  47. #include <Row.h>
  48. #include <Code_LayerHold.h>
  49. #include <Key_LayeredKeys.h>
  50. #include <LayerState_LED.h>
  51. #include <LED_PortOpenDrain.h>
  52. //left matrix
  53. #include <Scanner_uC.h>
  54. //right matrix
  55. #include <Port_MCP23018.h>
  56. #include <Scanner_IOE.h>
  57. // ============ SPEED CONFIGURATION ============
  58. ScanDelay scanDelay(9000);
  59. /* ================ LEFT SCANNER ===============
  60. Left matrix rows work the same as the ones in keybrd_2_single-layer.ino
  61. */
  62. uint8_t readPins[] = {14, 15};
  63. const uint8_t readPinCount = sizeof(readPins)/sizeof(*readPins);
  64. Scanner_uC scanner_L(LOW, readPins, readPinCount);
  65. // =============== RIGHT SCANNER ===============
  66. const uint8_t IOE_ADDR = 0x20; //MCP23018 ADDR pin grounded
  67. Port_MCP23018 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //read pins 0, 1
  68. Port_MCP23018 portB(IOE_ADDR, 1, 0);
  69. Scanner_IOE scanner_R(LOW, portB, portA);
  70. // ================= RIGHT LED =================
  71. LED_PortOpenDrain LED_normal(portA, 1<<2); //LED on/off will be reversed on MCP23017
  72. LED_PortOpenDrain LED_fn(portB, 1<<2); // because it's not open drain
  73. // =================== CODES ===================
  74. // ---------------- LAYER CODES ----------------
  75. enum layerIds { NORMAL, FN };
  76. LEDInterface* prtsLayerLEDs[] = { &LED_normal, &LED_fn }; //array index matches enum layerIds
  77. LayerState_LED layerState(prtsLayerLEDs);
  78. Code_LayerHold l_fn(FN, layerState);
  79. // ---------------- SCAN CODES -----------------
  80. Code_Sc s_a(KEY_A);
  81. Code_Sc s_b(KEY_B);
  82. Code_Sc s_z(KEY_Z);
  83. Code_Sc s_1(KEY_1);
  84. Code_Sc s_2(KEY_2);
  85. Code_Sc s_3(KEY_3);
  86. Code_Sc s_4(KEY_4);
  87. Code_Sc s_9(KEY_9);
  88. // =================== KEYS ====================
  89. Key* const ptrsKeys_z9[] = { &s_z, &s_9 };
  90. Key_LayeredKeys k_z9(ptrsKeys_z9);
  91. LayerStateInterface& Key_LayeredKeys::refLayerState = layerState;
  92. // =================== ROWS ====================
  93. // ---------------- LEFT ROWS ------------------
  94. Key* ptrsKeys_L0[] = { &s_1, &s_2 };
  95. const uint8_t KEY_COUNT_L0 = sizeof(ptrsKeys_L0)/sizeof(*ptrsKeys_L0);
  96. Row row_L0(scanner_L, 0, ptrsKeys_L0, KEY_COUNT_L0);
  97. Key* ptrsKeys_L1[] = { &s_a, &s_b };
  98. const uint8_t KEY_COUNT_L1 = sizeof(ptrsKeys_L1)/sizeof(*ptrsKeys_L1);
  99. Row row_L1(scanner_L, 1, ptrsKeys_L1, KEY_COUNT_L1);
  100. // ---------------- RIGHT ROWS -----------------
  101. Key* ptrsKeys_R0[] = { &s_3, &s_4 };
  102. const uint8_t KEY_COUNT_R0 = sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0);
  103. Row row_R0(scanner_R, 1<<0, ptrsKeys_R0, KEY_COUNT_R0);
  104. Key* ptrsKeys_R1[] = { &l_fn, &k_z9 };
  105. const uint8_t KEY_COUNT_R1 = sizeof(ptrsKeys_R1)/sizeof(*ptrsKeys_R1);
  106. Row row_R1(scanner_R, 1<<1, ptrsKeys_R1, KEY_COUNT_R1);
  107. // ################### MAIN ####################
  108. void setup()
  109. {
  110. delay(6000);
  111. Keyboard.print("keybrd_MCP23017.ino ");
  112. scanner_R.begin();
  113. layerState.begin();
  114. }
  115. void loop()
  116. {
  117. //left matrix
  118. row_L0.process();
  119. row_L1.process();
  120. //right matrix
  121. row_R0.process();
  122. row_R1.process();
  123. scanDelay.delay();
  124. //debug.printScansPerSecond();
  125. //debug.printMicrosecondsPerScan();
  126. }