-
Notifications
You must be signed in to change notification settings - Fork 0
/
rivaloled.py
134 lines (105 loc) · 3.36 KB
/
rivaloled.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from gi.repository import GLib as glib
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import xprintidle
import threading
import rivalcfg
import numpy
import time
import sys
# Set config for Rival 700
VENDOR_ID = 0x1038
PRODUCT_ID = 0x1700
mouse = rivalcfg.get_mouse(VENDOR_ID, PRODUCT_ID)
# Set Oled Screen Dims
MAXX, MAXY = 36, 128
blankframe = numpy.zeros((MAXX, MAXY), dtype=int, order='C')
oldframes = numpy.zeros((MAXX, MAXY, 2), dtype=int, order='C')
oldframe = []
screendisabled = False
timeout = 200000
shuttingdown = False
hibernating = False
# flatten array before sending to rivalcfg
def _bitstobytes_(array):
array = numpy.packbits(array, axis=-1)
array = array.flatten("A")
return array
def writeframe(frame):
if hibernating == False:
mouse.send_oled_frame(frame)
def sendblankframe():
writeframe(_bitstobytes_(blankframe))
def screensaver(delay):
global screendisabled, timeout, shuttingdown, oldframe, hibernating, mouse
timeout = delay
while True:
if xprintidle.idle_time() < timeout:
if screendisabled:
screendisabled = False
writeframe(oldframe)
screendisabled = False
else:
sendblankframe()
screendisabled = True
if shuttingdown:
sendblankframe()
break
time.sleep(2)
def hibernate_callback(sleeping):
global hibernating, mouse
hibernating = sleeping
if sleeping:
# System going to hibernate or sleep
mouse.send_oled_frame(_bitstobytes_(blankframe))
else:
# System just resumed from hibernate or suspend
mouse = None
mouse = rivalcfg.get_mouse(VENDOR_ID, PRODUCT_ID)
def hibernate():
glib.MainLoop().run()
# Check dbus for hibernate calls
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(
hibernate_callback,
'PrepareForSleep',
'org.freedesktop.login1.Manager',
'org.freedesktop.login1'
)
# Start screensaver thread
screensaver = threading.Thread(target=screensaver, args=(timeout,))
screensaver.start()
hibernate = threading.Thread(target=hibernate, args=())
hibernate.start()
# convert pil image to a binary numpy array
def imagetobits(im):
frame = numpy.asarray(im, dtype=int)
return frame
# sendframe(numpy array of (36x128), delay for screensaver in milliseconds)
def sendframe(frame, delay):
global oldframe, timeout
timeout = delay
if not numpy.array_equal(frame, oldframe):
if not screendisabled:
frame = _bitstobytes_(frame)
writeframe(frame)
if not numpy.array_equal(frame, _bitstobytes_(blankframe)):
oldframe = frame
# sendsquenece(numpy array (36x128, frames in squenece), delay for screensaver in milliseconds)
def sendsquenece(frames, delay):
global oldframes, timeout, shuttingdown
while not numpy.array_equal(frames, oldframes):
depth = frames.shape[2]
for d in range(depth):
try:
frame = frames[:, :, d]
sendframe(frame, delay)
time.sleep(.1) # 1th sec ergo 10 fps
except (KeyboardInterrupt, SystemExit):
print(" ---- Ctrl+c Detected Closing Down Please Wait.")
shuttingdown = True
sendblankframe()
sys.exit(1)
def main(frame):
pass