Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1dfa32b

Browse files
committedJul 31, 2019
applet.sensor.keyboard_ps2: new applet (WIP)
1 parent 34d7f03 commit 1dfa32b

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
 

‎software/glasgow/applet/all.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from .control.tps6598x import ControlTPS6598xApplet
2929

3030
from .sensor.bmp280 import SensorBMP280Applet
31+
from .sensor.keyboard_ps2 import SensorKeyboardPS2Applet
3132
from .sensor.mouse_ps2 import SensorMousePS2Applet
3233
from .sensor.scd30 import SensorSCD30Applet
3334

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Ref: IBM PS/2 Hardware Technical Reference ­- Keyboards (101- and 102-Key)
2+
# Accession: G00037
3+
4+
import logging
5+
import asyncio
6+
7+
from ... import *
8+
from ...interface.ps2_host import PS2HostApplet
9+
10+
11+
CMD_RESET = 0xff
12+
CMD_RESEND = 0xfe
13+
CMD_SET_KEY_MAKE_ONLY = 0xfd
14+
CMD_SET_KEY_MAKE_BREAK = 0xfc
15+
CMD_SET_KEY_TYPEMATIC = 0xfb
16+
CMD_SET_ALL_MAKE_ONLY = 0xf9
17+
CMD_SET_ALL_MAKE_BREAK = 0xf8
18+
CMD_SET_ALL_TYPEMATIC = 0xf7
19+
CMD_SET_DEFAULTS = 0xf6
20+
CMD_DISABLE_REPORTING = 0xf5
21+
CMD_ENABLE_REPORTING = 0xf4 # default
22+
CMD_SET_TYPEMATIC_OPTIONS = 0xf3
23+
CMD_GET_DEVICE_ID = 0xf2
24+
CMD_GET_SET_SCANCODE_SET = 0xf0
25+
CMD_ECHO = 0xee
26+
CMD_SET_INDICATORS = 0xed
27+
28+
29+
class SensorKeyboardPS2Error(GlasgowAppletError):
30+
pass
31+
32+
33+
class SensorKeyboardPS2Interface:
34+
def __init__(self, interface, logger):
35+
self.lower = interface
36+
self._logger = logger
37+
self._level = logging.DEBUG if self._logger.name == __name__ else logging.TRACE
38+
39+
def _log(self, message, *args):
40+
self._logger.log(self._level, "PS/2 Keyboard: " + message, *args)
41+
42+
async def reset(self):
43+
bat_result, = await self.lower.send_command(CMD_RESET, ret=1)
44+
self._log("reset bat-result=%02x", bat_result)
45+
if bat_result == 0xaa:
46+
pass # passed
47+
elif bat_result == 0xfc:
48+
raise SensorKeyboardPS2Error("Basic Assurance Test failed")
49+
else:
50+
raise SensorKeyboardPS2Error("invalid Basic Assurance Test response {:#04x}"
51+
.format(bat_result))
52+
53+
async def identify(self):
54+
ident, = await self.lower.send_command(CMD_GET_DEVICE_ID, ret=1)
55+
self._log("ident=%02x", ident)
56+
return ident
57+
58+
59+
class SensorKeyboardPS2Applet(PS2HostApplet, name="sensor-keyboard-ps2"):
60+
logger = logging.getLogger(__name__)
61+
help = "receive key press/release information from PS/2 keyboards"
62+
description = """
63+
Identify PS/2 keyboard, and receive key press/release updates. The updates may be logged or
64+
forwarded to the desktop on Linux.
65+
66+
This applet has additional Python dependencies:
67+
* uinput (optional, required for Linux desktop forwarding)
68+
"""
69+
70+
async def run(self, device, args):
71+
ps2_iface = await self.run_lower(SensorKeyboardPS2Applet, device, args)
72+
kbd_iface = SensorKeyboardPS2Interface(ps2_iface, self.logger)
73+
return kbd_iface
74+
75+
@classmethod
76+
def add_interact_arguments(cls, parser):
77+
parser.add_argument(
78+
"--no-reset", dest="reset", default=True, action="store_false",
79+
help="do not send the reset command before initialization (does not affect reset pin)")
80+
81+
p_operation = parser.add_subparsers(dest="operation", metavar="OPERATION")
82+
83+
p_stream_log = p_operation.add_parser("stream-log",
84+
help="stream events and log them")
85+
86+
p_stream_uinput = p_operation.add_parser("stream-uinput",
87+
help="stream events and forward them to desktop via uinput (Linux only)")
88+
89+
async def interact(self, device, args, kbd_iface):
90+
async def initialize():
91+
if args.reset:
92+
await kbd_iface.reset()
93+
return await kbd_iface.identify()
94+
95+
try:
96+
ident = await asyncio.wait_for(initialize(), timeout=1)
97+
except asyncio.TimeoutError:
98+
raise SensorKeyboardPS2Error("initialization timeout; connection problem?")

0 commit comments

Comments
 (0)
Please sign in to comment.