Kiibohd Controller
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.

matrix_scan.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (C) 2014-2015 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #pragma once
  22. // ----- Includes -----
  23. // KLL Generated Defines
  24. #include <kll_defs.h>
  25. // ----- Defines -----
  26. #if ( DebounceDivThreshold_define < 0xFF + 1 )
  27. #define DebounceCounter uint8_t
  28. #elif ( DebounceDivThreshold_define < 0xFFFF + 1 )
  29. #define DebounceCounter uint16_t
  30. #elif ( DebounceDivThreshold_define < 0xFFFFFFFF + 1 )
  31. #define DebounceCounter uint32_t
  32. #else
  33. #error "Debounce threshold is too high... 32 bit max. Check .kll defines."
  34. #endif
  35. // ----- Enums -----
  36. // Freescale MK20s have GPIO ports A...E
  37. typedef enum Port {
  38. Port_A = 0,
  39. Port_B = 1,
  40. Port_C = 2,
  41. Port_D = 3,
  42. Port_E = 4,
  43. } Port;
  44. // Each port has a possible 32 pins
  45. typedef enum Pin {
  46. Pin_0 = 0,
  47. Pin_1 = 1,
  48. Pin_2 = 2,
  49. Pin_3 = 3,
  50. Pin_4 = 4,
  51. Pin_5 = 5,
  52. Pin_6 = 6,
  53. Pin_7 = 7,
  54. Pin_8 = 8,
  55. Pin_9 = 9,
  56. Pin_10 = 10,
  57. Pin_11 = 11,
  58. Pin_12 = 12,
  59. Pin_13 = 13,
  60. Pin_14 = 14,
  61. Pin_15 = 15,
  62. Pin_16 = 16,
  63. Pin_17 = 17,
  64. Pin_18 = 18,
  65. Pin_19 = 19,
  66. Pin_20 = 20,
  67. Pin_21 = 21,
  68. Pin_22 = 22,
  69. Pin_23 = 23,
  70. Pin_24 = 24,
  71. Pin_25 = 25,
  72. Pin_26 = 26,
  73. Pin_27 = 27,
  74. Pin_28 = 28,
  75. Pin_29 = 29,
  76. Pin_30 = 30,
  77. Pin_31 = 31,
  78. } Pin;
  79. // Type of pin
  80. typedef enum Type {
  81. Type_StrobeOn,
  82. Type_StrobeOff,
  83. Type_StrobeSetup,
  84. Type_Sense,
  85. Type_SenseSetup,
  86. } Type;
  87. // Sense/Strobe configuration
  88. typedef enum Config {
  89. Config_Pullup, // Internal pull-up
  90. Config_Pulldown, // Internal pull-down
  91. Config_Opendrain, // External pull resistor
  92. } Config;
  93. // Keypress States
  94. typedef enum KeyPosition {
  95. KeyState_Off = 0,
  96. KeyState_Press = 1,
  97. KeyState_Hold = 2,
  98. KeyState_Release = 3,
  99. KeyState_Invalid,
  100. } KeyPosition;
  101. // ----- Structs -----
  102. // Struct container for defining Rows (Sense) and Columns (Strobes)
  103. typedef struct GPIO_Pin {
  104. Port port;
  105. Pin pin;
  106. } GPIO_Pin;
  107. // Debounce Element
  108. typedef struct KeyState {
  109. KeyPosition prevState;
  110. KeyPosition curState;
  111. DebounceCounter activeCount;
  112. DebounceCounter inactiveCount;
  113. } KeyState;
  114. // ----- Functions -----
  115. void Matrix_setup();
  116. void Matrix_scan( uint16_t scanNum );