Non-Arduino keyMatrix library with Arduino support.

keymatrix.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
  3. * Date: 04/2016
  4. * License: GPLv3
  5. */
  6. /******************************************************************************/
  7. #include "keymatrix.h"
  8. /******************************************************************************/
  9. // Declare a array with 4x4 key state values
  10. // yes other dimensions are possible, but you have to change the preprocessor defines
  11. uint8_t km_state[] = { KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP,
  12. KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP,
  13. KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP,
  14. KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP,
  15. KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP,
  16. KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, KEY_STATE_UP, };
  17. // columns
  18. // Arduino Nano Pins: 17 16 15 14
  19. uint8_t km_out_pins[] = { PC3, PC2, PC1, PC0 }; // output pin declaration
  20. volatile uint8_t *km_out_ports[] = { &PORTC, &PORTC, &PORTC, &PORTC }; // output pin port declaration
  21. volatile uint8_t *km_out_ddr[] = { &DDRC, &DDRC, &DDRC, &DDRC }; // output pin data direction register
  22. // rows - Arduino Nano Pins: 7 8 9 10 11 12
  23. uint8_t km_in_pins[] = { PIND7, PINB0, PINB1, PINB2, PINB3, PINB4 }; // input pin declaration
  24. volatile uint8_t *km_in_ports[] = { &PORTD, &PORTB, &PORTB, &PORTB, &PORTB, &PORTB }; // input pin port register
  25. volatile uint8_t *km_in_pinregs[] = { &PIND, &PINB, &PINB, &PINB, &PINB, &PINB }; // input pin pinregister
  26. volatile uint8_t *km_in_ddr[] = { &DDRD, &DDRB, &DDRB, &DDRB, &DDRB, &DDRB }; // input pin data direction register
  27. uint8_t km_lastChangedKey = 255;
  28. /******************************************************************************/
  29. /**
  30. * Call initially before using any of the other functions in this module.
  31. */
  32. void km_init()
  33. {
  34. // all rows as input
  35. for (uint8_t i = 0; i < KEYMATRIX_ROWS; i++)
  36. {
  37. // pin should be configured as input (tristate)
  38. *(km_in_ddr[i]) &= ~(1 << km_in_pins[i]); // configure input pins
  39. *(km_in_ports[i]) &= ~(1 << km_in_pins[i]); // configure input pins without pullup
  40. }
  41. // all columns as output
  42. for (uint8_t i = 0; i < KEYMATRIX_COLUMNS; i++)
  43. {
  44. *(km_out_ddr[i]) |= (1 << km_out_pins[i]); // configure output pins
  45. *(km_out_ports[i]) &= ~(1 << km_out_pins[i]); // set pins to low
  46. }
  47. }
  48. /******************************************************************************/
  49. /**
  50. * Could be used to receive the key state array.
  51. */
  52. void km_getKeyStates(uint8_t *tmpArray)
  53. {
  54. // copy the array data to the destination temporary array
  55. // should be optimized because this code is ugly
  56. for (uint8_t i = 0; i < KEYMATRIX_COLUMNS*KEYMATRIX_ROWS; i++)
  57. {
  58. tmpArray[i] = km_state[i];
  59. }
  60. }
  61. /**
  62. * Get the internally saved key state value.
  63. */
  64. uint8_t km_getKeyState(uint8_t key)
  65. {
  66. return km_state[key];
  67. }
  68. /**
  69. * Could be used to figure out which key has been changed last time
  70. */
  71. uint8_t km_getLastChangedKey()
  72. {
  73. return km_lastChangedKey;
  74. }
  75. /******************************************************************************/
  76. /**
  77. * Internally used to configure the column pins.
  78. */
  79. void km_activateCol(uint8_t c)
  80. {
  81. if (c == DISABLE_ALL_COLUMNS)
  82. {
  83. // set all column pins to low
  84. for (uint8_t i = 0; i < KEYMATRIX_COLUMNS; i++)
  85. {
  86. *(km_out_ports[i]) &= ~(1 << km_out_pins[i]); // low
  87. }
  88. return;
  89. }
  90. for (uint8_t i = 0; i < KEYMATRIX_COLUMNS; i++)
  91. {
  92. if (i == c)
  93. // pin should be set to HIGH-level
  94. *(km_out_ports[i]) |= (1 << km_out_pins[i]); // high
  95. else
  96. // pin should be set to LOW-level
  97. *(km_out_ports[i]) &= ~(1 << km_out_pins[i]); // low
  98. }
  99. }
  100. /******************************************************************************/
  101. /**
  102. * Call this function in a loop.
  103. */
  104. void km_updateKeyStates()
  105. {
  106. // reset the last changed key information because there could be an newer one
  107. km_lastChangedKey = 255;
  108. for (uint8_t row = 0; row < KEYMATRIX_ROWS; row++)
  109. {
  110. for (uint8_t col = 0; col < KEYMATRIX_COLUMNS; col++)
  111. {
  112. km_activateCol(col);
  113. km_updateKeyState(row, col);
  114. }
  115. // disable all rows until next read
  116. km_activateCol(DISABLE_ALL_COLUMNS);
  117. }
  118. }
  119. /**
  120. * Internally used to get the state of the keypad matrix.
  121. */
  122. uint8_t km_updateKeyState(uint8_t row, uint8_t col)
  123. {
  124. uint8_t idx = (row * KEYMATRIX_COLUMNS) + col; // calculate the index of the key in the km_state array
  125. uint8_t km_state_old = km_state[idx];
  126. // get the pin state of the specified row
  127. uint8_t ks = (*(km_in_pinregs[row]) & (1 << km_in_pins[row]));
  128. if (km_state[idx] == KEY_STATE_UP && ks)
  129. { // button is going to be pressed
  130. km_state[idx] = KEY_STATE_GO_DOWN;
  131. } else if (km_state[idx] == KEY_STATE_GO_DOWN && ks)
  132. { // button is pressed
  133. km_state[idx] = KEY_STATE_DOWN;
  134. } else if (km_state[idx] == KEY_STATE_DOWN && !ks)
  135. { // button is going to be released
  136. km_state[idx] = KEY_STATE_GO_UP;
  137. } else if (km_state[idx] == KEY_STATE_GO_UP && !ks)
  138. { // button is released
  139. km_state[idx] = KEY_STATE_UP;
  140. }
  141. if (km_state[idx] != km_state_old)
  142. // the key state has changed. the last changed key state has to be updated
  143. km_lastChangedKey = idx;
  144. return km_state[idx]; // return the key state
  145. }
  146. /******************************************************************************/
  147. /** /
  148. // arduino style
  149. void setup()
  150. {
  151. Serial.begin(9600);
  152. km_init();
  153. }
  154. void loop()
  155. {
  156. // call this set of functionality every xxxx ms
  157. km_updateKeyStates();
  158. uint8_t key = km_getLastChangedKey();
  159. Serial.print("key=%d ", key);
  160. if (key < KEYMATRIX_COLUMNS*KEYMATRIX_ROWS)
  161. {
  162. // get key status
  163. uint8_t state = km_getKeyState(key);
  164. // process key state
  165. Serial.println("state=%d", state);
  166. }
  167. }
  168. / **/
  169. /** /
  170. // example usage - this could be part of your main function - non Arduino
  171. #import "keymatrix.h"
  172. int main(void)
  173. {
  174. // call at start of the program
  175. km_init();
  176. // loop forever
  177. for (;;)
  178. {
  179. // call as often as you think that you need updated key states
  180. // for example every 50 ms are quite ok for me
  181. km_updateKeyStates();
  182. uint8_t key = km_getLastChangedKey();
  183. if (key >= KEYMATRIX_COLUMNS*KEYMATRIX_ROWS)
  184. {
  185. uint8_t state = km_getKeyState(key);
  186. switch(key)
  187. {
  188. case 0:;
  189. if (state == KEY_STATE_DOWN)
  190. {
  191. // do the key 0 action
  192. }
  193. break;
  194. case 1:;
  195. if (state == KEY_STATE_GO_DOWN)
  196. {
  197. // do the key 1 action
  198. }
  199. break;
  200. // ...
  201. case 23:;
  202. // do the key 23 action
  203. break;
  204. default:;
  205. // do other things
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. / **/
  212. /** /
  213. // there is also an alternative way to check every key like modification keys or
  214. // a key combination behaviour
  215. #import "keymatrix.h"
  216. int main(void)
  217. {
  218. // call at start of the program
  219. km_init();
  220. // loop forever
  221. for (;;)
  222. {
  223. uint8_t tmpArray[KEYMATRIX_COLUMNS*KEYMATRIX_ROWS];
  224. km_getKeyStates(tmpArray);
  225. for (uint8_t i = 0; i < KEYMATRIX_COLUMNS*KEYMATRIX_ROWS; i++)
  226. {
  227. if (tmpArray[i] == KEY_STATE_GO_DOWN)
  228. {
  229. if (i == 0)
  230. {
  231. // do key i specific things
  232. }
  233. }
  234. }
  235. }
  236. return 0;
  237. }
  238. / **/
  239. /******************************************************************************/