cherry8x16.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include "cherry8x16.h"
  15. #include "nrf.h"
  16. #define CHERRY8x16_NUM_OF_COLUMNS 16 // !< Number of columns in the keyboard matrix
  17. #define CHERRY8x16_NUM_OF_ROWS 8 // !< Number of rows in the keyboard matrix
  18. #define MODIFIER_HID_START 0xE0
  19. #define MODIFIER_HID_END 0xE7
  20. static uint8_t m_currently_pressed_keys[CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS]; //!< Array holding currently pressed keys. Filled up from index 0. Values are
  21. static uint8_t m_transmitted_keys[CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS]; //!< Array holding the keys that have already been transmitted.
  22. static uint8_t m_num_of_currently_pressed_keys; //!< Number of keys in m_currently_pressed_keys
  23. static uint8_t m_number_of_transmitted_keys; //!< Number of keys in m_transmitted_keys
  24. static uint8_t m_key_packet[KEY_PACKET_SIZE]; //!< Stores last created key packet. One byte is used for modifier keys, one for OEMs. Key values are USB HID keycodes.
  25. static const uint8_t volatile * m_row_port; //!< Pointer to location where row IO can be read
  26. static uint16_t volatile * m_column_port; //!< Pointer to location where column IO can be written
  27. static const uint8_t * matrix_lookup; //!< Pointer to the key lookup matrix in use
  28. /** Table containing the mapping between the key matrix and the HID Usage codes for each key. */
  29. static const uint8_t default_matrix_lookup[CHERRY8x16_NUM_OF_COLUMNS * CHERRY8x16_NUM_OF_ROWS] =
  30. {
  31. 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0xE1, 0xE6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0xE2, 0xE5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x29, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x3F, 0x40,
  38. 0x1E, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x24, 0x25,
  39. 0x4F, 0x43, 0x47, 0x53, 0x46, 0x48, 0x42, 0x41,
  40. 0x51, 0x2D, 0x2E, 0x2A, 0x00, 0x4A, 0x27, 0x26,
  41. 0x52, 0x13, 0x2F, 0x30, 0x00, 0x4B, 0x12, 0x0C,
  42. 0x50, 0x33, 0x34, 0x32, 0x28, 0x4E, 0x0F, 0x0E,
  43. 0x2C, 0x38, 0x4C, 0x49, 0x65, 0x4D, 0x37, 0x36,
  44. 0x35, 0x05, 0x19, 0x06, 0x1B, 0x1D, 0x11, 0x10,
  45. 0x39, 0x0A, 0x09, 0x07, 0x16, 0x04, 0x0B, 0x0D,
  46. 0x2B, 0x17, 0x15, 0x08, 0x1A, 0x14, 0x1C, 0x18
  47. };
  48. static bool cherry8x16_have_keys_changed(const uint8_t * state_now,
  49. uint8_t number_of_now_pressed_keys,
  50. const uint8_t * state_before,
  51. uint8_t number_of_before_pressed_keys);
  52. static bool cherry8x16_keymatrix_read(uint8_t * pressed_keys, uint8_t * number_of_pressed_keys);
  53. static void cherry8x16_keypacket_addkey(uint8_t key);
  54. static void cherry8x16_keypacket_create(uint8_t * key_packet, uint8_t key_packet_size);
  55. static void cherry8x16_remap_fn_keys(uint8_t * keys, uint8_t number_of_keys);
  56. static uint8_t cherry8x16_row_read(void);
  57. cherry8x16_status_t cherry8x16_init(const uint8_t volatile * row_port,
  58. uint16_t * column_port,
  59. const uint8_t * key_lookup_matrix)
  60. {
  61. cherry8x16_status_t status = CHERRY8x16_OK;
  62. if (row_port == 0 || column_port == 0)
  63. {
  64. status = CHERRY8x16_INVALID_PARAMETER;
  65. }
  66. else
  67. {
  68. m_row_port = row_port;
  69. m_column_port = column_port;
  70. *m_column_port = 0x0000;
  71. if (*m_row_port != 0x00)
  72. {
  73. status = CHERRY8x16_NOT_DETECTED;
  74. }
  75. else
  76. {
  77. m_num_of_currently_pressed_keys = 0;
  78. m_number_of_transmitted_keys = 0;
  79. for (uint_fast8_t i = CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS; i--;)
  80. {
  81. m_currently_pressed_keys[i] = 0;
  82. m_transmitted_keys[i] = 0;
  83. }
  84. }
  85. if (key_lookup_matrix == CHERRY8x16_DEFAULT_KEY_LOOKUP_MATRIX)
  86. {
  87. matrix_lookup = default_matrix_lookup;
  88. }
  89. else
  90. {
  91. matrix_lookup = key_lookup_matrix;
  92. }
  93. }
  94. return status;
  95. }
  96. bool cherry8x16_new_packet(const uint8_t ** p_key_packet, uint8_t * p_key_packet_size)
  97. {
  98. bool new_packet_prepared;
  99. // Save currently pressed keys
  100. for (uint_fast8_t i = CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS; i--; )
  101. {
  102. m_transmitted_keys[i] = m_currently_pressed_keys[i];
  103. }
  104. m_number_of_transmitted_keys = m_num_of_currently_pressed_keys;
  105. // Create a new packet if key states have changed and there are no keys blocking each other (ghosting/phantom keys)
  106. if (cherry8x16_keymatrix_read(m_currently_pressed_keys, &m_num_of_currently_pressed_keys))
  107. {
  108. if (cherry8x16_have_keys_changed(m_currently_pressed_keys, m_num_of_currently_pressed_keys,
  109. m_transmitted_keys, m_number_of_transmitted_keys))
  110. {
  111. cherry8x16_keypacket_create(&m_key_packet[0], KEY_PACKET_SIZE);
  112. *p_key_packet = &m_key_packet[0];
  113. *p_key_packet_size = KEY_PACKET_SIZE;
  114. new_packet_prepared = true;
  115. }
  116. else
  117. {
  118. // The same keys are still pressed, no need to create a new packet
  119. new_packet_prepared = false;
  120. }
  121. }
  122. else
  123. {
  124. // Ghosting detected. Don't create a packet.
  125. new_packet_prepared = false;
  126. }
  127. return new_packet_prepared;
  128. }
  129. /**
  130. * @brief Function for reading and returning keyboard matrix row state.
  131. *
  132. * @return uint8_t Row state
  133. */
  134. static uint8_t cherry8x16_row_read(void)
  135. {
  136. return *m_row_port;
  137. }
  138. /**
  139. * @brief Function for reading the keyboard matrix state and stores the pressed keys to an array.
  140. *
  141. * This function resolves keys from the matrix and finds their corresponding HID usage codes
  142. * If there are any ghost key conditions the packet will be discarded
  143. * @param pressed_keys Array holding pressed keys. Must be at least CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS in size.
  144. * @param number_of_pressed_keys Pointer to variable where number of pressed keys will be stored.
  145. * @return
  146. * @retval true If no keys were blocking each other.
  147. * @retval false If some keys were blocking each other o rno key is pressed.
  148. */
  149. static bool cherry8x16_keymatrix_read(uint8_t * pressed_keys, uint8_t * number_of_pressed_keys)
  150. {
  151. uint_fast8_t row_state[CHERRY8x16_NUM_OF_COLUMNS];
  152. uint_fast8_t blocking_mask = 0;
  153. *number_of_pressed_keys = 0;
  154. for (uint_fast8_t column = CHERRY8x16_NUM_OF_COLUMNS; column--;)
  155. {
  156. // drive column under test
  157. *m_column_port = (uint16_t)(1UL << column);
  158. row_state[column] = cherry8x16_row_read();
  159. // Check if any keys are pressed
  160. if (row_state[column] != 0)
  161. {
  162. uint_fast8_t detected_keypresses_on_column = 0;
  163. // Loop through rows, check for active rows and add pressed keys to the array
  164. for (uint_fast8_t row = CHERRY8x16_NUM_OF_ROWS; row--;)
  165. {
  166. if (row_state[column] & (1U << row))
  167. {
  168. if (*number_of_pressed_keys < CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS)
  169. {
  170. *pressed_keys = matrix_lookup[column * CHERRY8x16_NUM_OF_ROWS + row];
  171. pressed_keys++;
  172. (*number_of_pressed_keys)++;
  173. }
  174. detected_keypresses_on_column++;
  175. }
  176. }
  177. if (detected_keypresses_on_column > 1)
  178. {
  179. if (blocking_mask & row_state[column])
  180. {
  181. // Cannot determine reliably all pressed keys, two or more keys are blocking each other.
  182. return false;
  183. }
  184. }
  185. blocking_mask |= row_state[column];
  186. }
  187. }
  188. return true;
  189. }
  190. /**
  191. * @brief Function for remapping the keypad, F11 and F12 keys in case when Fn key is pressed.
  192. *
  193. * @param keys Array holding pressed keys.
  194. * @param number_of_keys Number of elements if 'keys' array.
  195. */
  196. static void cherry8x16_remap_fn_keys(uint8_t * keys, uint8_t number_of_keys)
  197. {
  198. /*lint -e845 -save // A zero has been given as right argument to operator '<<'" */
  199. /*lint -e778 -save // Constant expression evaluates to zero */
  200. #define MODIFIER_LEFT_CONTROL_HID 0xE0
  201. #define MODIFER_RIGHT_CONTROL_HID 0xE4
  202. // Check if Fn key is pressed along with any other modifier key (only usage now is Fn+Left_Ctrl = Right Ctrl)
  203. // So we modify the modifier byte if Fn+Left_Ctrl is pressed, HID for left_Ctrl = 0xE0
  204. if ( keys[0] & (1UL << (MODIFIER_LEFT_CONTROL_HID - MODIFIER_HID_START)) )
  205. {
  206. keys[0] &= ~(1UL << (MODIFIER_LEFT_CONTROL_HID - MODIFIER_HID_START));
  207. keys[0] |= (1UL << (MODIFER_RIGHT_CONTROL_HID - MODIFIER_HID_START));
  208. }
  209. /*lint -restore */
  210. /*lint -restore */
  211. for (uint_fast8_t i = 2; i < number_of_keys; i++)
  212. {
  213. switch (keys[i])
  214. {
  215. case 0x10: // 'M'
  216. keys[i] = 0x62; // Keypad 0
  217. break;
  218. case 0x37: // '>'
  219. keys[i] = 0x63; // Keypad .
  220. break;
  221. case 0x38: // '/'
  222. keys[i] = 0x54; // Keypad /
  223. break;
  224. case 0x0D: // 'J'
  225. keys[i] = 0x59; // Keypad 1
  226. break;
  227. case 0x0E: // 'K'
  228. keys[i] = 0x5A; // Keypad 2
  229. break;
  230. case 0x0F: // 'L'
  231. keys[i] = 0x5B; // Keypad 3
  232. break;
  233. case 0x33: // ''
  234. keys[i] = 0x57; // Keypad +
  235. break;
  236. case 0x28: // 'Enter'
  237. keys[i] = 0x58; // Keypad enter
  238. break;
  239. case 0x18: // 'U'
  240. keys[i] = 0x5C; // Keypad 4
  241. break;
  242. case 0x0C: // 'I'
  243. keys[i] = 0x5D; // Keypad 5
  244. break;
  245. case 0x12: // 'O'
  246. keys[i] = 0x5E; // Keypad 6
  247. break;
  248. case 0x13: // 'P'
  249. keys[i] = 0x56; // Keypad -
  250. break;
  251. case 0x24: // '7'
  252. keys[i] = 0x5F; // Keypad 7
  253. break;
  254. case 0x25: // '8'
  255. keys[i] = 0x60; // Keypad 8
  256. break;
  257. case 0x26: // '9'
  258. keys[i] = 0x61; // Keypad 9
  259. break;
  260. case 0x27: // '0'
  261. keys[i] = 0x55; // Keypad *
  262. break;
  263. case 0x3A: // 'F1'
  264. keys[i] = 0x44; // 'F11'
  265. break;
  266. case 0x3B: // 'F2'
  267. keys[i] = 0x45; // 'F12'
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. }
  274. /**
  275. * @brief Function for determining whether the keyboard matrix state has changed compared to the state before.
  276. *
  277. * @param state_now List of pressed keys in current state
  278. * @param number_of_now_pressed_keys Number of pressed keys in current state
  279. * @param state_before List of pressed keys in previous state
  280. * @param number_of_before_pressed_keys Number of pressed keys in previous state
  281. * @return
  282. * @retval true If keyboard matrix is different compared to state before.
  283. * @retval false If keyboard matrix is the same compared to state before.
  284. */
  285. static bool cherry8x16_have_keys_changed(const uint8_t * state_now,
  286. uint8_t number_of_now_pressed_keys,
  287. const uint8_t * state_before,
  288. uint8_t number_of_before_pressed_keys)
  289. {
  290. if (number_of_now_pressed_keys != number_of_before_pressed_keys)
  291. {
  292. return true;
  293. }
  294. else
  295. {
  296. for (uint_fast8_t i = number_of_now_pressed_keys; i--;)
  297. {
  298. if (state_now[i] != state_before[i])
  299. {
  300. return true;
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. /**
  307. * @brief Function for adding a key to the key packet.
  308. *
  309. * If key is found to be in the packet, it will not be added twice.
  310. * Attempts to add more keys than the buffer capacity allows will be silently ignored.
  311. *
  312. * @param key Key to add
  313. */
  314. static void cherry8x16_keypacket_addkey(uint8_t key)
  315. {
  316. for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < KEY_PACKET_SIZE; i++)
  317. {
  318. if (m_key_packet[i] == key)
  319. {
  320. return;
  321. }
  322. }
  323. for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < KEY_PACKET_SIZE; i++)
  324. {
  325. if (m_key_packet[i] == KEY_PACKET_NO_KEY)
  326. {
  327. m_key_packet[i] = key;
  328. return;
  329. }
  330. }
  331. }
  332. /**
  333. * @brief Function for creating a new key packet.
  334. *
  335. * This function uses @ref m_currently_pressed_keys to determine pressed keys.
  336. * Priority is given to those keys that were found in the previous packet.
  337. * All modifier keys can be found in all packets.
  338. * If Fn key is detected to be pressed, some keys are remapped to different functions.
  339. *
  340. * @param key_packet Pointer to location where packet contents will be put
  341. * @param key_packet_size Key packet size in bytes
  342. */
  343. static void cherry8x16_keypacket_create(uint8_t * key_packet, uint8_t key_packet_size)
  344. {
  345. // Clear key_packet contents
  346. for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < key_packet_size; i++)
  347. {
  348. key_packet[i] = KEY_PACKET_NO_KEY;
  349. }
  350. key_packet[KEY_PACKET_MODIFIER_KEY_INDEX] = 0;
  351. key_packet[KEY_PACKET_RESERVED_INDEX] = 0;
  352. // Give priority to keys that were already pressed when we transmitted them the last time.
  353. for (uint_fast8_t i = 0; i < m_number_of_transmitted_keys; i++)
  354. {
  355. for (uint_fast8_t j = 0; j < m_num_of_currently_pressed_keys; j++)
  356. {
  357. if (m_transmitted_keys[i] == m_currently_pressed_keys[j])
  358. {
  359. cherry8x16_keypacket_addkey(m_currently_pressed_keys[j]);
  360. break;
  361. }
  362. }
  363. }
  364. bool fn_key_is_set = false;
  365. // Detect if Fn is pressed, detect modifier keys, and add rest of the keys to the packet
  366. for (uint_fast8_t i = 0; i < m_num_of_currently_pressed_keys; i++)
  367. {
  368. if (m_currently_pressed_keys[i] == 0xFF) // Pressing Fn key changes function of certain keys and it must handled by the firmware
  369. {
  370. fn_key_is_set = true;
  371. }
  372. // Modifier HID usage codes are from 0xE0 to 0xE7
  373. else if (m_currently_pressed_keys[i] >= MODIFIER_HID_START && m_currently_pressed_keys[i] <= MODIFIER_HID_END) // Detect and set modifier keys
  374. {
  375. key_packet[KEY_PACKET_MODIFIER_KEY_INDEX] |= (uint8_t)(1U << (m_currently_pressed_keys[i] - MODIFIER_HID_START));
  376. }
  377. else if (m_currently_pressed_keys[i] != 0)
  378. {
  379. cherry8x16_keypacket_addkey(m_currently_pressed_keys[i]);
  380. }
  381. }
  382. if (fn_key_is_set)
  383. {
  384. cherry8x16_remap_fn_keys(&key_packet[0], KEY_PACKET_MAX_KEYS);
  385. }
  386. }