123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- #include <stdint.h>
- #include <stdbool.h>
- #include "cherry8x16.h"
- #include "nrf.h"
- #define CHERRY8x16_NUM_OF_COLUMNS 16
- #define CHERRY8x16_NUM_OF_ROWS 8
- #define MODIFIER_HID_START 0xE0
- #define MODIFIER_HID_END 0xE7
- static uint8_t m_currently_pressed_keys[CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS];
- static uint8_t m_transmitted_keys[CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS];
- static uint8_t m_num_of_currently_pressed_keys;
- static uint8_t m_number_of_transmitted_keys;
- static uint8_t m_key_packet[KEY_PACKET_SIZE];
- static const uint8_t volatile * m_row_port;
- static uint16_t volatile * m_column_port;
- static const uint8_t * matrix_lookup;
- static const uint8_t default_matrix_lookup[CHERRY8x16_NUM_OF_COLUMNS * CHERRY8x16_NUM_OF_ROWS] =
- {
- 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xE1, 0xE6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xE2, 0xE5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x29, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x3F, 0x40,
- 0x1E, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x24, 0x25,
- 0x4F, 0x43, 0x47, 0x53, 0x46, 0x48, 0x42, 0x41,
- 0x51, 0x2D, 0x2E, 0x2A, 0x00, 0x4A, 0x27, 0x26,
- 0x52, 0x13, 0x2F, 0x30, 0x00, 0x4B, 0x12, 0x0C,
- 0x50, 0x33, 0x34, 0x32, 0x28, 0x4E, 0x0F, 0x0E,
- 0x2C, 0x38, 0x4C, 0x49, 0x65, 0x4D, 0x37, 0x36,
- 0x35, 0x05, 0x19, 0x06, 0x1B, 0x1D, 0x11, 0x10,
- 0x39, 0x0A, 0x09, 0x07, 0x16, 0x04, 0x0B, 0x0D,
- 0x2B, 0x17, 0x15, 0x08, 0x1A, 0x14, 0x1C, 0x18
- };
- static bool cherry8x16_have_keys_changed(const uint8_t * state_now,
- uint8_t number_of_now_pressed_keys,
- const uint8_t * state_before,
- uint8_t number_of_before_pressed_keys);
- static bool cherry8x16_keymatrix_read(uint8_t * pressed_keys, uint8_t * number_of_pressed_keys);
- static void cherry8x16_keypacket_addkey(uint8_t key);
- static void cherry8x16_keypacket_create(uint8_t * key_packet, uint8_t key_packet_size);
- static void cherry8x16_remap_fn_keys(uint8_t * keys, uint8_t number_of_keys);
- static uint8_t cherry8x16_row_read(void);
- cherry8x16_status_t cherry8x16_init(const uint8_t volatile * row_port,
- uint16_t * column_port,
- const uint8_t * key_lookup_matrix)
- {
- cherry8x16_status_t status = CHERRY8x16_OK;
- if (row_port == 0 || column_port == 0)
- {
- status = CHERRY8x16_INVALID_PARAMETER;
- }
- else
- {
- m_row_port = row_port;
- m_column_port = column_port;
- *m_column_port = 0x0000;
- if (*m_row_port != 0x00)
- {
- status = CHERRY8x16_NOT_DETECTED;
- }
- else
- {
- m_num_of_currently_pressed_keys = 0;
- m_number_of_transmitted_keys = 0;
- for (uint_fast8_t i = CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS; i--;)
- {
- m_currently_pressed_keys[i] = 0;
- m_transmitted_keys[i] = 0;
- }
- }
- if (key_lookup_matrix == CHERRY8x16_DEFAULT_KEY_LOOKUP_MATRIX)
- {
- matrix_lookup = default_matrix_lookup;
- }
- else
- {
- matrix_lookup = key_lookup_matrix;
- }
- }
- return status;
- }
- bool cherry8x16_new_packet(const uint8_t ** p_key_packet, uint8_t * p_key_packet_size)
- {
- bool new_packet_prepared;
-
- for (uint_fast8_t i = CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS; i--; )
- {
- m_transmitted_keys[i] = m_currently_pressed_keys[i];
- }
- m_number_of_transmitted_keys = m_num_of_currently_pressed_keys;
-
- if (cherry8x16_keymatrix_read(m_currently_pressed_keys, &m_num_of_currently_pressed_keys))
- {
- if (cherry8x16_have_keys_changed(m_currently_pressed_keys, m_num_of_currently_pressed_keys,
- m_transmitted_keys, m_number_of_transmitted_keys))
- {
- cherry8x16_keypacket_create(&m_key_packet[0], KEY_PACKET_SIZE);
- *p_key_packet = &m_key_packet[0];
- *p_key_packet_size = KEY_PACKET_SIZE;
- new_packet_prepared = true;
- }
- else
- {
-
- new_packet_prepared = false;
- }
- }
- else
- {
-
- new_packet_prepared = false;
- }
- return new_packet_prepared;
- }
- static uint8_t cherry8x16_row_read(void)
- {
- return *m_row_port;
- }
- static bool cherry8x16_keymatrix_read(uint8_t * pressed_keys, uint8_t * number_of_pressed_keys)
- {
- uint_fast8_t row_state[CHERRY8x16_NUM_OF_COLUMNS];
- uint_fast8_t blocking_mask = 0;
- *number_of_pressed_keys = 0;
- for (uint_fast8_t column = CHERRY8x16_NUM_OF_COLUMNS; column--;)
- {
-
- *m_column_port = (uint16_t)(1UL << column);
- row_state[column] = cherry8x16_row_read();
-
- if (row_state[column] != 0)
- {
- uint_fast8_t detected_keypresses_on_column = 0;
-
- for (uint_fast8_t row = CHERRY8x16_NUM_OF_ROWS; row--;)
- {
- if (row_state[column] & (1U << row))
- {
- if (*number_of_pressed_keys < CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS)
- {
- *pressed_keys = matrix_lookup[column * CHERRY8x16_NUM_OF_ROWS + row];
- pressed_keys++;
- (*number_of_pressed_keys)++;
- }
- detected_keypresses_on_column++;
- }
- }
- if (detected_keypresses_on_column > 1)
- {
- if (blocking_mask & row_state[column])
- {
-
- return false;
- }
- }
- blocking_mask |= row_state[column];
- }
- }
- return true;
- }
- static void cherry8x16_remap_fn_keys(uint8_t * keys, uint8_t number_of_keys)
- {
-
-
- #define MODIFIER_LEFT_CONTROL_HID 0xE0
- #define MODIFER_RIGHT_CONTROL_HID 0xE4
-
-
- if ( keys[0] & (1UL << (MODIFIER_LEFT_CONTROL_HID - MODIFIER_HID_START)) )
- {
- keys[0] &= ~(1UL << (MODIFIER_LEFT_CONTROL_HID - MODIFIER_HID_START));
- keys[0] |= (1UL << (MODIFER_RIGHT_CONTROL_HID - MODIFIER_HID_START));
- }
-
-
- for (uint_fast8_t i = 2; i < number_of_keys; i++)
- {
- switch (keys[i])
- {
- case 0x10:
- keys[i] = 0x62;
- break;
- case 0x37:
- keys[i] = 0x63;
- break;
- case 0x38:
- keys[i] = 0x54;
- break;
- case 0x0D:
- keys[i] = 0x59;
- break;
- case 0x0E:
- keys[i] = 0x5A;
- break;
- case 0x0F:
- keys[i] = 0x5B;
- break;
- case 0x33:
- keys[i] = 0x57;
- break;
- case 0x28:
- keys[i] = 0x58;
- break;
- case 0x18:
- keys[i] = 0x5C;
- break;
- case 0x0C:
- keys[i] = 0x5D;
- break;
- case 0x12:
- keys[i] = 0x5E;
- break;
- case 0x13:
- keys[i] = 0x56;
- break;
- case 0x24:
- keys[i] = 0x5F;
- break;
- case 0x25:
- keys[i] = 0x60;
- break;
- case 0x26:
- keys[i] = 0x61;
- break;
- case 0x27:
- keys[i] = 0x55;
- break;
- case 0x3A:
- keys[i] = 0x44;
- break;
- case 0x3B:
- keys[i] = 0x45;
- break;
- default:
- break;
- }
- }
- }
- static bool cherry8x16_have_keys_changed(const uint8_t * state_now,
- uint8_t number_of_now_pressed_keys,
- const uint8_t * state_before,
- uint8_t number_of_before_pressed_keys)
- {
- if (number_of_now_pressed_keys != number_of_before_pressed_keys)
- {
- return true;
- }
- else
- {
- for (uint_fast8_t i = number_of_now_pressed_keys; i--;)
- {
- if (state_now[i] != state_before[i])
- {
- return true;
- }
- }
- }
- return false;
- }
- static void cherry8x16_keypacket_addkey(uint8_t key)
- {
- for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < KEY_PACKET_SIZE; i++)
- {
- if (m_key_packet[i] == key)
- {
- return;
- }
- }
- for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < KEY_PACKET_SIZE; i++)
- {
- if (m_key_packet[i] == KEY_PACKET_NO_KEY)
- {
- m_key_packet[i] = key;
- return;
- }
- }
- }
- static void cherry8x16_keypacket_create(uint8_t * key_packet, uint8_t key_packet_size)
- {
-
- for (uint_fast8_t i = KEY_PACKET_KEY_INDEX; i < key_packet_size; i++)
- {
- key_packet[i] = KEY_PACKET_NO_KEY;
- }
- key_packet[KEY_PACKET_MODIFIER_KEY_INDEX] = 0;
- key_packet[KEY_PACKET_RESERVED_INDEX] = 0;
-
- for (uint_fast8_t i = 0; i < m_number_of_transmitted_keys; i++)
- {
- for (uint_fast8_t j = 0; j < m_num_of_currently_pressed_keys; j++)
- {
- if (m_transmitted_keys[i] == m_currently_pressed_keys[j])
- {
- cherry8x16_keypacket_addkey(m_currently_pressed_keys[j]);
- break;
- }
- }
- }
- bool fn_key_is_set = false;
-
- for (uint_fast8_t i = 0; i < m_num_of_currently_pressed_keys; i++)
- {
- if (m_currently_pressed_keys[i] == 0xFF)
- {
- fn_key_is_set = true;
- }
-
- else if (m_currently_pressed_keys[i] >= MODIFIER_HID_START && m_currently_pressed_keys[i] <= MODIFIER_HID_END)
- {
- key_packet[KEY_PACKET_MODIFIER_KEY_INDEX] |= (uint8_t)(1U << (m_currently_pressed_keys[i] - MODIFIER_HID_START));
- }
- else if (m_currently_pressed_keys[i] != 0)
- {
- cherry8x16_keypacket_addkey(m_currently_pressed_keys[i]);
- }
- }
- if (fn_key_is_set)
- {
- cherry8x16_remap_fn_keys(&key_packet[0], KEY_PACKET_MAX_KEYS);
- }
- }
|