-
Notifications
You must be signed in to change notification settings - Fork 1
/
physical_keyboard.py
65 lines (48 loc) · 1.77 KB
/
physical_keyboard.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
import json
import sys
import os
import time
from collections import OrderedDict
from importlib import import_module
from abstract_keyboard import KeyData, Colours
basepath = os.path.dirname(os.path.abspath(__file__))
basepath = os.path.join(basepath, os.pardir)
logi_dir = os.path.join(basepath, "logiPy")
sys.path.append(logi_dir)
from logipy import logi_led
class PhysicalKeyboard(object):
def __init__(self, model="g810"):
layout_dir = os.path.join(basepath, "layouts")
layout_file = os.path.join(layout_dir, model + ".json")
with open(layout_file, 'r') as fh:
layout = json.load(fh, object_pairs_hook=OrderedDict)
self.keys = {}
self.layout = {}
self.layout["rows"] = {}
for key, data in layout["keys"].iteritems():
# Load keycodes from logiPy
self.keys[key] = {
"data" : KeyData(),
"keycode" : int(getattr(logi_led, key)),
"rows" : data["rows"]
}
for row in data["rows"]:
if row not in self.layout["rows"].keys():
self.layout["rows"][row] = []
self.layout["rows"][row].append(key)
logi_led.logi_led_init()
time.sleep(1)
def __del__(self):
logi_led.logi_led_shutdown()
def stop_effects(self):
return logi_led.logi_led_stop_effects()
def set_all_colour(self, colour):
return logi_led.logi_led_set_lighting(
colour.r, colour.g, colour.b
)
def set_key_colour(self, key, colour):
res = logi_led.logi_led_set_lighting_for_key_with_scan_code(
self.keys[key]["keycode"], colour.r, colour.g, colour.b
)
self.keys[key]["data"].colours = colour
return res