StuBS
PS2Controller Namespace Reference

PS/2 Controller. More...

Enumerations

enum  Status {
  HAS_OUTPUT = 1 << 0 ,
  INPUT_PENDING = 1 << 1 ,
  SYSTEM_FLAG = 1 << 2 ,
  IS_COMMAND = 1 << 3 ,
  IS_MOUSE = 1 << 5 ,
  TIMEOUT_ERROR = 1 << 6 ,
  PARITY_ERROR = 1 << 7
}
 Flags in the PS/2 controller status register. More...
 
enum  KeyboardCommand : uint8_t {
  KEYBOARD_SET_LED = 0xed ,
  KEYBOARD_SEND_ECHO = 0xee ,
  KEYBOARD_SET_SPEED = 0xf3 ,
  KEYBOARD_ENABLE = 0xf4 ,
  KEYBOARD_DISABLE = 0xf5 ,
  KEYBOARD_SET_DEFAULT = 0xf6
}
 Commands to be send to the Keyboard. More...
 
enum  Reply {
  ACK = 0xfa ,
  RESEND = 0xfe ,
  ECHO = 0xee
}
 Replies. More...
 
enum  ControllerCommand {
  CONTROLLER_GET_COMMAND_BYTE = 0x20 ,
  CONTROLLER_SET_COMMAND_BYTE = 0x60 ,
  CONTROLLER_MOUSE_DISABLE = 0xa7 ,
  CONTROLLER_MOUSE_ENABLE = 0xa8 ,
  CONTROLLER_KEYBOARD_DISABLE = 0xad ,
  CONTROLLER_KEYBOARD_ENABLE = 0xae ,
  CONTROLLER_SEND_TO_MOUSE = 0xd4
}
 Commands for the PS/2 Controller. More...
 
enum  MouseCommand {
  MOUSE_RESET = 0xff ,
  MOUSE_DEFAULTS = 0xf6 ,
  MOUSE_STREAMING_DISABLE = 0xf5 ,
  MOUSE_STREAMING_ENABLE = 0xf4 ,
  MOUSE_SAMPLE_RATE = 0xf3 ,
  MOUSE_ID = 0xf2 ,
  MOUSE_REQUEST_PACKET = 0xeb ,
  MOUSE_REQUEST_STATUS = 0xe9 ,
  MOUSE_RESOLUTION = 0xe8 ,
  MOUSE_SCALING_2_1 = 0xe7 ,
  MOUSE_SCALING_1_1 = 0xe6
}
 Commands to be send to the mouse. More...
 
enum  Delay {
  DELAY_250MS = 0 ,
  DELAY_500MS = 1 ,
  DELAY_750MS = 2 ,
  DELAY_1000MS = 3
}
 Delay before the keyboard starts repeating sending a pressed key. More...
 
enum  Speed {
  SPEED_30_0CPS = 0x00 ,
  SPEED_26_7CPS = 0x01 ,
  SPEED_24_0CPS = 0x02 ,
  SPEED_21_8CPS = 0x03 ,
  SPEED_20_7CPS = 0x04 ,
  SPEED_18_5CPS = 0x05 ,
  SPEED_17_1CPS = 0x06 ,
  SPEED_16_0CPS = 0x07 ,
  SPEED_15_0CPS = 0x08 ,
  SPEED_13_3CPS = 0x09 ,
  SPEED_12_0CPS = 0x0a ,
  SPEED_10_9CPS = 0x0b ,
  SPEED_10_0CPS = 0x0c ,
  SPEED_09_2CPS = 0x0d ,
  SPEED_08_6CPS = 0x0e ,
  SPEED_08_0CPS = 0x0f ,
  SPEED_07_5CPS = 0x10 ,
  SPEED_06_7CPS = 0x11 ,
  SPEED_06_0CPS = 0x12 ,
  SPEED_05_5CPS = 0x13 ,
  SPEED_05_0CPS = 0x14 ,
  SPEED_04_6CPS = 0x15 ,
  SPEED_04_3CPS = 0x16 ,
  SPEED_04_0CPS = 0x17 ,
  SPEED_03_7CPS = 0x18 ,
  SPEED_03_3CPS = 0x19 ,
  SPEED_03_0CPS = 0x1a ,
  SPEED_02_7CPS = 0x1b ,
  SPEED_02_5CPS = 0x1c ,
  SPEED_02_3CPS = 0x1d ,
  SPEED_02_1CPS = 0x1e ,
  SPEED_02_0CPS = 0x1f
}
 Repeat Rate of Characters. More...
 
enum  LED {
  LED_SCROLL_LOCK = 1 << 0 ,
  LED_NUM_LOCK = 1 << 1 ,
  LED_CAPS_LOCK = 1 << 2
}
 Keyboard LEDs. More...
 

Functions

void init ()
 Initialization of connected devices. More...
 
bool fetch (Key &pressed)
 Retrieve the keyboard event. More...
 
bool fetch (Pointer &state)
 Retrieve the mouse event. More...
 
void setRepeatRate (Speed speed, Delay delay)
 Configure the repeat rate of the keyboard. More...
 
void setLed (enum LED led, bool on)
 Enable or disable a keyboard LED. More...
 
void drainBuffer ()
 Empties the keyboard buffer. More...
 
bool isPressed (Key::Scancode key)
 Check if a key is currently pressed. More...
 

Detailed Description

PS/2 Controller.

Initializes the PS/2 devices (Keyboard and optional Mouse), and determines both the scan code and ASCII character of a pressed key from the transmitted make and break codes using the KeyDecoder.

Note
This controller is also known as Intel 8042 (nowadays integrated in the mainboard) or Keyboard Controller. But to avoid confusion with the actual Keyboard and since we use the PS/2-compatible mode to support the Mouse as well, the name PS/2 Controller was chosen for the sake of simplicity.
Since modern PCs sometimes don't have an PS/2 connector, USB keyboards and mice are emulated as PS/2 device with USB Legacy Support.

Enumeration Type Documentation

◆ Status

Flags in the PS/2 controller status register.

Enumerator
HAS_OUTPUT 

Output buffer non-empty?

INPUT_PENDING 

Is input buffer full?

SYSTEM_FLAG 

set on soft reset, cleared on power up

IS_COMMAND 

Is command Byte? (otherwise data)

IS_MOUSE 

Mouse output has data.

TIMEOUT_ERROR 

Timeout error.

PARITY_ERROR 

Parity error.

◆ KeyboardCommand

Commands to be send to the Keyboard.

Enumerator
KEYBOARD_SET_LED 

Set the LED (according to the following parameter byte)

KEYBOARD_SEND_ECHO 

Send an echo packet.

KEYBOARD_SET_SPEED 

Set the repeat rate (according to the following parameter byte)

KEYBOARD_ENABLE 

Enable Keyboard.

KEYBOARD_DISABLE 

Disable Keyboard.

KEYBOARD_SET_DEFAULT 

Load defaults.

◆ Reply

Replies.

Enumerator
ACK 

Acknowledgement.

RESEND 

Request to resend (not required to implement)

ECHO 

Echo answer.

◆ ControllerCommand

Commands for the PS/2 Controller.

These commands are processed by the controller and not send to keyboard/mouse. They have to be written into the command register.

Enumerator
CONTROLLER_GET_COMMAND_BYTE 

Read Command Byte of PS/2 Controller.

CONTROLLER_SET_COMMAND_BYTE 

Write Command Byte of PS/2 Controller.

CONTROLLER_MOUSE_DISABLE 

Disable mouse interface.

CONTROLLER_MOUSE_ENABLE 

Enable mouse interface.

CONTROLLER_KEYBOARD_DISABLE 

Disable keyboard interface.

CONTROLLER_KEYBOARD_ENABLE 

Enable keyboard interface.

CONTROLLER_SEND_TO_MOUSE 

Send parameter to mouse device.

◆ MouseCommand

Commands to be send to the mouse.

See also
ps2mouse
Enumerator
MOUSE_RESET 

Reset and perform self-test.

MOUSE_DEFAULTS 

Reset settings to default.

MOUSE_STREAMING_DISABLE 

Don't report mouse movements.

MOUSE_STREAMING_ENABLE 

Send data on mouse movement/button events.

MOUSE_SAMPLE_RATE 

Set sample rate.

MOUSE_ID 

Get device ID.

MOUSE_REQUEST_PACKET 

Get movement data.

MOUSE_REQUEST_STATUS 

Get status packet (responses with 3 bytes)

MOUSE_RESOLUTION 

Set resolution (requires parameter)

MOUSE_SCALING_2_1 

Set scaling 2:1.

MOUSE_SCALING_1_1 

Set scaling 1:1.

◆ Delay

Delay before the keyboard starts repeating sending a pressed key.

Enumerator
DELAY_250MS 

Delay of 0.25s.

DELAY_500MS 

Delay of 0.5s.

DELAY_750MS 

Delay of 0.75s.

DELAY_1000MS 

Delay of 1s.

◆ Speed

Repeat Rate of Characters.

See also
The PS/2 Keyboard Interface
Enumerator
SPEED_30_0CPS 

30 characters per second

SPEED_26_7CPS 

26.7 characters per second

SPEED_24_0CPS 

24 characters per second

SPEED_21_8CPS 

12.8 characters per second

SPEED_20_7CPS 

20.7 characters per second

SPEED_18_5CPS 

18.5 characters per second

SPEED_17_1CPS 

17.1 characters per second

SPEED_16_0CPS 

16 characters per second

SPEED_15_0CPS 

15 characters per second

SPEED_13_3CPS 

13.3 characters per second

SPEED_12_0CPS 

12 characters per second

SPEED_10_9CPS 

10.9 characters per second

SPEED_10_0CPS 

10 characters per second

SPEED_09_2CPS 

9.2 characters per second

SPEED_08_6CPS 

8.6 characters per second

SPEED_08_0CPS 

8 characters per second

SPEED_07_5CPS 

7.5 characters per second

SPEED_06_7CPS 

6.7 characters per second

SPEED_06_0CPS 

6 characters per second

SPEED_05_5CPS 

5.5 characters per second

SPEED_05_0CPS 

5 characters per second

SPEED_04_6CPS 

4.6 characters per second

SPEED_04_3CPS 

4.3 characters per second

SPEED_04_0CPS 

4 characters per second

SPEED_03_7CPS 

3.7 characters per second

SPEED_03_3CPS 

3.3 characters per second

SPEED_03_0CPS 

3 characters per second

SPEED_02_7CPS 

2.7 characters per second

SPEED_02_5CPS 

2.5 characters per second

SPEED_02_3CPS 

2.3 characters per second

SPEED_02_1CPS 

2.1 characters per second

SPEED_02_0CPS 

2 characters per second

◆ LED

Keyboard LEDs.

Enumerator
LED_SCROLL_LOCK 

Scroll Lock.

LED_NUM_LOCK 

Num Lock.

LED_CAPS_LOCK 

Caps Lock.

Function Documentation

◆ init()

void PS2Controller::init ( )

Initialization of connected devices.

All status LEDs of the keyboard are switched off and the repetition rate is set to maximum speed.

◆ fetch() [1/2]

bool PS2Controller::fetch ( Key pressed)

Retrieve the keyboard event.

Retrieves make and brake events from the keyboard. If a valid (non special) key was pressed, the scan code is determined using KeyDecoder::decode into a Key object. Events on special keys like Shift, Alt, CapsLock etc. are stored (in KeyDecoder) and applied on subsequent keystrokes, while no valid key is retrieved.

Mouse events are ignored.

Parameters
pressedReference to an object which will contain the pressed Key on success
Returns
true if a valid key was decoded

◆ fetch() [2/2]

bool PS2Controller::fetch ( Pointer state)

Retrieve the mouse event.

If a full mouse state was retrieved (all 3 packets have been successfully processed by the MouseDecoder) the new state is retrieved. Keyboard events are not processed.

Note
fetch(Key&) has to be adjusted to not process (instead of ignore) mouse packets, or events might be lost.
Parameters
stateReference to an object which will contain the Mouse Pointer state on success
Returns
true if a valid mouse pointer state was decoded

◆ setRepeatRate()

void PS2Controller::setRepeatRate ( Speed  speed,
Delay  delay 
)

Configure the repeat rate of the keyboard.

Parameters
delayconfigures how long a key must be pressed before the repetition begins.
speeddetermines how fast the key codes should follow each other. Valid values are between 0 (30 characters per second) and 31 (2 characters per second).

◆ setLed()

void PS2Controller::setLed ( enum LED  led,
bool  on 
)

Enable or disable a keyboard LED.

Parameters
ledLED to enable or disable
ontrue will enable the specified LED, false disable

◆ drainBuffer()

void PS2Controller::drainBuffer ( )

Empties the keyboard buffer.

The keyboard may not send any interrupts if the buffer is not empty. To prevent unhandled keystrokes (for example during boot) the buffer should be emptied once right before allowing keyboard interrupts (even if keystrokes might be lost).

◆ isPressed()

bool PS2Controller::isPressed ( Key::Scancode  key)

Check if a key is currently pressed.

Parameters
keyScancode of the key
Returns
true if pressed