-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx0keys.py
82 lines (62 loc) · 1.92 KB
/
x0keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##
## notes
##
## https://python-evdev.readthedocs.io/en/latest/tutorial.html
##
## Display IR codes visible
## sudo ir-keytable -c -p all -t
## Tell Linux to use the rs500.toml mappings
## sudo ir-keytable -c -w rs500.toml
## show the current keytable
## ir-keytable -r
## Start automatically
## add to /etc/rc.local
## /usr/bin/ir-keytable -c -w /home/pi/apex/rs500.toml &> /tmp/log.txt
##
## imports
##
import socket
import select
import json
try:
haveEvdev = True
from evdev import InputDevice, categorize, ecodes
except ImportError:
haveEvdev = False
##
## Code
##
class x0Keys:
"""Receive commands from IR Remotes"""
def __init__(self, useLog, inputDevice, inkeymap):
if not haveEvdev:
return
self.log = useLog
self.log.info(f'Starting up IR Keys... {inputDevice}')
self.device = InputDevice(inputDevice)
#self.log.debug(f'Using keyamp {inkeymap}')
mapping = {}
# work on the mappings
if inkeymap:
for key in inkeymap.keys():
try:
code = eval('ecodes.' + key)
mapping[code] = inkeymap[key]
except Exception as ex:
self.log.error(f'Unable ot map {key} {ex}')
self.mapping = mapping
self.log.debug(f'Key Mapping {self.mapping}')
def action(self):
if not haveEvdev:
return []
profiles = []
r, _w, _x = select.select([self.device.fd], [], [], 0)
if self.device.fd in r:
for event in self.device.read():
if event.type == ecodes.EV_KEY and event.value == 1:
# print(event.code)
# print(event.value)
self.log.debug(categorize(event))
if event.code in self.mapping:
profiles.append({'profile': self.mapping[event.code]['profile']})
return profiles