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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (C) 2014-2016 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. #if ( MinDebounceTime_define > 0xFF )
  36. #error "MinDebounceTime is a maximum of 255 ms"
  37. #elif ( MinDebounceTime_define < 0x00 )
  38. #error "MinDebounceTime is a minimum 0 ms"
  39. #endif
  40. // ----- Enums -----
  41. // Freescale MK20s have GPIO ports A...E
  42. typedef enum Port {
  43. Port_A = 0,
  44. Port_B = 1,
  45. Port_C = 2,
  46. Port_D = 3,
  47. Port_E = 4,
  48. } Port;
  49. // Each port has a possible 32 pins
  50. typedef enum Pin {
  51. Pin_0 = 0,
  52. Pin_1 = 1,
  53. Pin_2 = 2,
  54. Pin_3 = 3,
  55. Pin_4 = 4,
  56. Pin_5 = 5,
  57. Pin_6 = 6,
  58. Pin_7 = 7,
  59. Pin_8 = 8,
  60. Pin_9 = 9,
  61. Pin_10 = 10,
  62. Pin_11 = 11,
  63. Pin_12 = 12,
  64. Pin_13 = 13,
  65. Pin_14 = 14,
  66. Pin_15 = 15,
  67. Pin_16 = 16,
  68. Pin_17 = 17,
  69. Pin_18 = 18,
  70. Pin_19 = 19,
  71. Pin_20 = 20,
  72. Pin_21 = 21,
  73. Pin_22 = 22,
  74. Pin_23 = 23,
  75. Pin_24 = 24,
  76. Pin_25 = 25,
  77. Pin_26 = 26,
  78. Pin_27 = 27,
  79. Pin_28 = 28,
  80. Pin_29 = 29,
  81. Pin_30 = 30,
  82. Pin_31 = 31,
  83. } Pin;
  84. // Type of pin
  85. typedef enum Type {
  86. Type_StrobeOn,
  87. Type_StrobeOff,
  88. Type_StrobeSetup,
  89. Type_Sense,
  90. Type_SenseSetup,
  91. } Type;
  92. // Sense/Strobe configuration
  93. typedef enum Config {
  94. Config_Pullup, // Internal pull-up
  95. Config_Pulldown, // Internal pull-down
  96. Config_Opendrain, // External pull resistor
  97. } Config;
  98. // Keypress States
  99. typedef enum KeyPosition {
  100. KeyState_Off = 0,
  101. KeyState_Press = 1,
  102. KeyState_Hold = 2,
  103. KeyState_Release = 3,
  104. KeyState_Invalid,
  105. } KeyPosition;
  106. // ----- Structs -----
  107. // Struct container for defining Rows (Sense) and Columns (Strobes)
  108. typedef struct GPIO_Pin {
  109. Port port;
  110. Pin pin;
  111. } GPIO_Pin;
  112. // Debounce Element
  113. typedef struct KeyState {
  114. DebounceCounter activeCount;
  115. DebounceCounter inactiveCount;
  116. KeyPosition prevState;
  117. KeyPosition curState;
  118. uint8_t prevDecisionTime;
  119. } __attribute__((packed)) KeyState;
  120. // Ghost Element, after ghost detection/cancelation
  121. typedef struct KeyGhost {
  122. KeyPosition prev;
  123. KeyPosition cur;
  124. KeyPosition saved; // state before ghosting
  125. } __attribute__((packed)) KeyGhost;
  126. // utility
  127. inline uint8_t keyOn(/*KeyPosition*/uint8_t st)
  128. {
  129. return (st == KeyState_Press || st == KeyState_Hold) ? 1 : 0;
  130. }
  131. // ----- Functions -----
  132. void Matrix_setup();
  133. void Matrix_scan( uint16_t scanNum );
  134. void Matrix_currentChange( unsigned int current );