Skip to content

Commit

Permalink
Add some comments to the code (mostly the PS/2 -> USB scancode handli…
Browse files Browse the repository at this point in the history
…ng code)
  • Loading branch information
trilader committed Mar 6, 2018
1 parent 17ee741 commit 95d3d34
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions ps2handler.cpp
Expand Up @@ -12,6 +12,7 @@ void debug_print(Args... args)
printf(args...);*/
}

// Initialize receive ringbuffer
void ps2handler::init_recv_buffer()
{
for(uint8_t i=0; i<recv_buffer_size; ++i)
Expand All @@ -22,6 +23,7 @@ void ps2handler::init_recv_buffer()
recv_buffer_tail=0;
}

// Fetches the next scancode from the receive ring buffer
uint8_t ps2handler::get_scan_code()
{
uint8_t result,i;
Expand Down Expand Up @@ -110,6 +112,8 @@ static const uint8_t state_index_to_usb_scancode[]=
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, // 0x210
};

// Convert key event meta data and scancode into usb scancode translation LUT
// TODO: 2 bytes meta + 8 bytes scancode = 10 bits. Max index therefore is 0x3ff but the scancode translation table only goes up to 0x218. Why?
uint16_t ps2handler::map_to_state_index(uint8_t meta, uint8_t scancode) const
{
return ((meta&0b11)<<8) + scancode;
Expand Down Expand Up @@ -379,36 +383,43 @@ void ps2handler::decode_scancode()
{
if(usb_scancode!=0)
{
// This key sends something on the usb side. Try to find the slot we've reserved for it when it was pressed.
for(size_t index=0;index<usb_keys.size();++index)
{
if(usb_keys[index]==usb_scancode)
{
// If the key was sent to the host we can free the slot is is occupying in the tracking table.
if(usb_keys_state[index]&usb_key_state_is_sent)
{
usb_keys[index]=0;
}
// Also reset the down/sent tracking table slot.
usb_keys_state[index]=0;
break;
}
}
}
// Remove key from internal state tracking table
key_states[state_index]=false;
}
else
{
//debug_print("%#04x,\n",state_index);
if(usb_scancode!=0)
{
// This key sends something on the usb side. Try to find a free slot in the to send map.
for(size_t index=0;index<usb_keys.size();++index)
{
if(usb_keys[index]==0)
{
// We've found an empty slot. Put it in there and mark it as down in the state tracking table.
usb_keys_state[index]=usb_key_state_is_down;
usb_keys[index]=usb_scancode;
break;
}
}
}
// Mark key in internal state tracking table
key_states[state_index]=true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions ps2handler.h
Expand Up @@ -53,8 +53,8 @@ class ps2handler
const uint8_t usb_key_state_is_sent = 0x01;
const uint8_t usb_key_state_is_down = 0x02;

std::array<uint8_t,5> usb_keys_state = {};
std::array<uint8_t,5> usb_keys = {};
std::array<uint8_t,5> usb_keys_state = {}; // State of the keys to be sent to the host. States: Empty (0), Down but unsent, Sent
std::array<uint8_t,5> usb_keys = {}; // List of keys to be sent to the host. States: Empty (0), Scancode (not 0).
std::array<uint8_t,1024> key_states = {};
uint8_t usb_modifier_byte() const;
void update_leds(bool num, bool caps, bool scroll);
Expand Down
4 changes: 4 additions & 0 deletions usbhid.cpp
Expand Up @@ -277,16 +277,20 @@ void sys_tick_handler(void)

if(sent_bytes)
{
// After we've successfully sent the key state to the host we can clean up the key tracking table.
ps2keyboard.need_update=false;
for(size_t index=0; index<ps2keyboard.usb_keys.size();++index)
{
// Skip over unused slots
if(ps2keyboard.usb_keys[index]==0)
continue;

// If the key was presed before we sent the last package to the host, mark it as sent.
if(ps2keyboard.usb_keys_state[index]&ps2keyboard.usb_key_state_is_down)
{
ps2keyboard.usb_keys_state[index]|=ps2keyboard.usb_key_state_is_sent;
}
// If we've sent it before (state already was set to sent) we can clean up this slot (but we need to check again for changes the next time around)
else
{
ps2keyboard.usb_keys[index]=0;
Expand Down

0 comments on commit 95d3d34

Please sign in to comment.