Skip to content

Commit 34b3227

Browse files
committed
update: simplify
1 parent 2baa8a5 commit 34b3227

File tree

1 file changed

+15
-50
lines changed

1 file changed

+15
-50
lines changed

controller.py

+15-50
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@
22
import usb.core
33
import usb.util
44
from usb.backend import libusb1
5-
from storage import RazerStorage,DEVICE
5+
from storage import RebtStorage, DEVICE
66

7-
class Razer():
7+
8+
# You can learn more info from https://github.com/hsutungyu/razer-mouse-battery-windows/blob/main/mamba.pyw
9+
class Rebt:
810

911
def __init__(self) -> None:
10-
self.config = RazerStorage()
12+
self.config = RebtStorage()
1113
self.findAll = True
1214
self.hasDefault = self.config.isExist()
1315
self.firstRun = not self.hasDefault
1416
self.backend = libusb1.get_backend()
1517
self.mouse = self.get_mouse()
16-
17-
18+
1819
def map_mouse(self):
1920
mouses = usb.core.find(find_all=True,
20-
idVendor=0x1532,
21-
backend=self.backend)
21+
idVendor=0x1532,
22+
backend=self.backend)
2223
for mouse in mouses:
2324
for name, device in DEVICE.items():
2425
knowProduct = int(device["usbId"], 16)
2526
if knowProduct == mouse.idProduct:
26-
self.config.generate(name, device["usbId"],device["tranId"])
27-
self.hasDefault=True
27+
self.config.generate(name, device["usbId"],
28+
device["tranId"])
29+
self.hasDefault = True
2830
return mouse
2931

3032
def get_mouse(self):
31-
"""
32-
Function that checks whether the mouse is plugged in or not
33-
:return: [mouse, wireless]: a list that stores (1) a Device object that represents the mouse; and
34-
(2) a boolean for stating if the mouse is in wireless state (True) or wired state (False)
35-
"""
3633
mouse = None
3734
if self.hasDefault:
3835
idProduct = self.config.getDefault("usbId")
@@ -41,64 +38,32 @@ def get_mouse(self):
4138
backend=self.backend)
4239
if not mouse and self.findAll:
4340
mouse = self.map_mouse()
44-
45-
# if the receiver is not found, mouse would be None
46-
47-
# still not found, then the mouse is not plugged in, raise error
4841
if not mouse:
4942
raise RuntimeError(f"Mouse cannot be found.")
5043

5144
return mouse
5245

5346
def battery_msg(self):
54-
"""
55-
Function that creates and returns the message to be sent to the device
56-
:return: meg: the message to be sent to the mouse for getting the battery level
57-
"""
58-
# adapted from https://github.com/rsmith-nl/scripts/blob/main/set-ornata-chroma-rgb.py
59-
# the first 8 bytes in order from left to right
60-
# status + transaction_id.id + remaining packets (\x00\x00) + protocol_type + command_class + command_id + data_size
6147
msg = b"\x00" + self.config.getDefault("tranId") + b"\x00\x00\x00\x02\x07\x80"
6248
crc = 0
6349
for i in msg[2:]:
6450
crc ^= i
65-
# the next 80 bytes would be storing the data to be sent, but for getting the battery no data is sent
6651
msg += bytes(80)
67-
# the last 2 bytes would be the crc and a zero byte
6852
msg += bytes([crc, 0])
6953
return msg
7054

7155
def get_battery(self):
72-
"""
73-
Function for getting the battery level of a Razer Mamba Wireless, or other device if adapted
74-
:return: a string with the battery level as a percentage (0 - 100)
75-
"""
76-
# find the mouse and the state, see get_mouse() for detail
77-
# the message to be sent to the mouse, see battery_msg() for detail
78-
# logging.info(f"Message sent to the mouse: {list(msg)}")
79-
# needed by PyUSB
80-
# if Linux, need to detach kernel driver
8156
self.mouse = self.get_mouse()
82-
self.mouse.set_configuration()
8357
usb.util.claim_interface(self.mouse, 0)
84-
# send request (battery), see razer_send_control_msg in razercommon.c in OpenRazer driver for detail
85-
self.mouse.ctrl_transfer(bmRequestType=0x21,
86-
bRequest=0x09,
87-
wValue=0x300,
88-
data_or_wLength=self.battery_msg(),
58+
self.mouse.set_configuration()
59+
self.mouse.ctrl_transfer(bmRequestType=0x21, bRequest=0x09, wValue=0x300, data_or_wLength=self.battery_msg(),
8960
wIndex=0x00)
90-
# needed by PyUSB
91-
usb.util.dispose_resources(self.mouse)
92-
# if the mouse is wireless, need to wait before getting response
93-
time.sleep(0.3305)
94-
# receive response
61+
time.sleep(0.1)
9562
result = self.mouse.ctrl_transfer(bmRequestType=0xa1,
9663
bRequest=0x01,
9764
wValue=0x300,
9865
data_or_wLength=90,
9966
wIndex=0x00)
10067
usb.util.dispose_resources(self.mouse)
10168
usb.util.release_interface(self.mouse, 0)
102-
# logging.info(f"Message received from the mouse: {list(result)}")
103-
# the raw battery level is in 0 - 255, scale it to 100 for human, correct to 2 decimal places
104-
return int(result[9] / 255 * 100)
69+
return int(result[9] / 255 * 100)

0 commit comments

Comments
 (0)