2
2
import usb .core
3
3
import usb .util
4
4
from usb .backend import libusb1
5
- from storage import RazerStorage , DEVICE
5
+ from storage import RebtStorage , DEVICE
6
6
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 :
8
10
9
11
def __init__ (self ) -> None :
10
- self .config = RazerStorage ()
12
+ self .config = RebtStorage ()
11
13
self .findAll = True
12
14
self .hasDefault = self .config .isExist ()
13
15
self .firstRun = not self .hasDefault
14
16
self .backend = libusb1 .get_backend ()
15
17
self .mouse = self .get_mouse ()
16
-
17
-
18
+
18
19
def map_mouse (self ):
19
20
mouses = usb .core .find (find_all = True ,
20
- idVendor = 0x1532 ,
21
- backend = self .backend )
21
+ idVendor = 0x1532 ,
22
+ backend = self .backend )
22
23
for mouse in mouses :
23
24
for name , device in DEVICE .items ():
24
25
knowProduct = int (device ["usbId" ], 16 )
25
26
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
28
30
return mouse
29
31
30
32
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
- """
36
33
mouse = None
37
34
if self .hasDefault :
38
35
idProduct = self .config .getDefault ("usbId" )
@@ -41,64 +38,32 @@ def get_mouse(self):
41
38
backend = self .backend )
42
39
if not mouse and self .findAll :
43
40
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
48
41
if not mouse :
49
42
raise RuntimeError (f"Mouse cannot be found." )
50
43
51
44
return mouse
52
45
53
46
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
61
47
msg = b"\x00 " + self .config .getDefault ("tranId" ) + b"\x00 \x00 \x00 \x02 \x07 \x80 "
62
48
crc = 0
63
49
for i in msg [2 :]:
64
50
crc ^= i
65
- # the next 80 bytes would be storing the data to be sent, but for getting the battery no data is sent
66
51
msg += bytes (80 )
67
- # the last 2 bytes would be the crc and a zero byte
68
52
msg += bytes ([crc , 0 ])
69
53
return msg
70
54
71
55
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
81
56
self .mouse = self .get_mouse ()
82
- self .mouse .set_configuration ()
83
57
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 (),
89
60
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 )
95
62
result = self .mouse .ctrl_transfer (bmRequestType = 0xa1 ,
96
63
bRequest = 0x01 ,
97
64
wValue = 0x300 ,
98
65
data_or_wLength = 90 ,
99
66
wIndex = 0x00 )
100
67
usb .util .dispose_resources (self .mouse )
101
68
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