| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
Contains the functions and classes to perform ripple effects |
| 5 |
""" |
| 6 |
import datetime |
| 7 |
import logging |
| 8 |
import math |
| 9 |
import threading |
| 10 |
import time |
| 11 |
|
| 12 |
# pylint: disable=import-error |
| 13 |
from openrazer_daemon.keyboard import KeyboardColour |
| 14 |
|
| 15 |
|
| 16 |
class RippleEffectThread(threading.Thread): |
| 17 |
""" |
| 18 |
Ripple thread. |
| 19 |
|
| 20 |
This thread contains the run loop which performs all the circle calculations and generating of the binary payload |
| 21 |
""" |
| 22 |
|
| 23 |
def __init__(self, parent, device_number): |
| 24 |
super().__init__() |
| 25 |
|
| 26 |
self._logger = logging.getLogger('razer.device{0}.ripplethread'.format(device_number)) |
| 27 |
self._parent = parent |
| 28 |
|
| 29 |
self._colour = (0, 255, 0) |
| 30 |
self._refresh_rate = 0.040 |
| 31 |
|
| 32 |
self._shutdown = False |
| 33 |
self._active = False |
| 34 |
|
| 35 |
self._rows, self._cols = self._parent._parent.MATRIX_DIMS |
| 36 |
|
| 37 |
self._keyboard_grid = KeyboardColour(self._rows, self._cols) |
| 38 |
|
| 39 |
@property |
| 40 |
def shutdown(self): |
| 41 |
""" |
| 42 |
Get the shutdown flag |
| 43 |
""" |
| 44 |
return self._shutdown |
| 45 |
|
| 46 |
@shutdown.setter |
| 47 |
def shutdown(self, value): |
| 48 |
""" |
| 49 |
Set the shutdown flag |
| 50 |
|
| 51 |
:param value: Shutdown |
| 52 |
:type value: bool |
| 53 |
""" |
| 54 |
self._shutdown = value |
| 55 |
|
| 56 |
@property |
| 57 |
def active(self): |
| 58 |
""" |
| 59 |
Get if the thread is active |
| 60 |
|
| 61 |
:return: Active |
| 62 |
:rtype: bool |
| 63 |
""" |
| 64 |
return self._active |
| 65 |
|
| 66 |
@property |
| 67 |
def key_list(self): |
| 68 |
""" |
| 69 |
Get key list |
| 70 |
|
| 71 |
:return: Key list |
| 72 |
:rtype: list |
| 73 |
""" |
| 74 |
return self._parent.key_list |
| 75 |
|
| 76 |
def enable(self, colour, refresh_rate): |
| 77 |
""" |
| 78 |
Enable the ripple effect |
| 79 |
|
| 80 |
If the colour tuple contains None then it will set the ripple to random colours |
| 81 |
:param colour: Colour tuple like (0, 255, 255) |
| 82 |
:type colour: tuple |
| 83 |
|
| 84 |
:param refresh_rate: Refresh rate in seconds |
| 85 |
:type refresh_rate: float |
| 86 |
""" |
| 87 |
if colour[0] is None: |
| 88 |
self._colour = None |
| 89 |
else: |
| 90 |
self._colour = colour |
| 91 |
self._refresh_rate = refresh_rate |
| 92 |
self._active = True |
| 93 |
|
| 94 |
def disable(self): |
| 95 |
""" |
| 96 |
Disable the ripple effect |
| 97 |
""" |
| 98 |
self._active = False |
| 99 |
|
| 100 |
def run(self): |
| 101 |
""" |
| 102 |
Event loop |
| 103 |
""" |
| 104 |
# pylint: disable=too-many-nested-blocks,too-many-branches |
| 105 |
expire_diff = datetime.timedelta(seconds=2) |
| 106 |
|
| 107 |
# self._parent: RippleManager |
| 108 |
# self._parent._parent: The device class (e.g. RazerBlackWidowUltimate2013) |
| 109 |
if self._rows == 6 and self._cols == 22: |
| 110 |
needslogohandling = True |
| 111 |
# a virtual 7th row for logo handling |
| 112 |
self._rows += 1 |
| 113 |
else: |
| 114 |
needslogohandling = False |
| 115 |
|
| 116 |
# TODO time execution and then sleep for _refresh_rate - time_taken |
| 117 |
while not self._shutdown: |
| 118 |
if self._active: |
| 119 |
# Clear keyboard |
| 120 |
self._keyboard_grid.reset_rows() |
| 121 |
|
| 122 |
now = datetime.datetime.now() |
| 123 |
|
| 124 |
radiuses = [] |
| 125 |
|
| 126 |
for expire_time, (key_row, key_col), colour in self.key_list: |
| 127 |
event_time = expire_time - expire_diff |
| 128 |
|
| 129 |
now_diff = now - event_time |
| 130 |
|
| 131 |
# Current radius is based off a time metric |
| 132 |
if self._colour is not None: |
| 133 |
colour = self._colour |
| 134 |
radiuses.append((key_row, key_col, now_diff.total_seconds() * 24, colour)) |
| 135 |
|
| 136 |
# Iterate through the rows |
| 137 |
for row in range(0, self._rows): |
| 138 |
# Iterate through the columns |
| 139 |
for col in range(0, self._cols): |
| 140 |
# The logo location is physically at (6, 11), logically at (0, 20) |
| 141 |
# Skip when we come across the logo location, as the ripple would look wrong |
| 142 |
if needslogohandling and row == 0 and col == 20: |
| 143 |
continue |
| 144 |
|
| 145 |
if needslogohandling and row == 6: |
| 146 |
if col != 11: |
| 147 |
continue |
| 148 |
|
| 149 |
# To account for logo placement |
| 150 |
for cirlce_centre_row, circle_centre_col, rad, colour in radiuses: |
| 151 |
radius = math.sqrt(math.pow(cirlce_centre_row - row, 2) + math.pow(circle_centre_col - col, 2)) |
| 152 |
if rad >= radius >= rad - 2: |
| 153 |
# Again, (0, 20) is the logical location of the logo led |
| 154 |
self._keyboard_grid.set_key_colour(0, 20, colour) |
| 155 |
break |
| 156 |
else: |
| 157 |
for cirlce_centre_row, circle_centre_col, rad, colour in radiuses: |
| 158 |
radius = math.sqrt(math.pow(cirlce_centre_row - row, 2) + math.pow(circle_centre_col - col, 2)) |
| 159 |
if rad >= radius >= rad - 2: |
| 160 |
self._keyboard_grid.set_key_colour(row, col, colour) |
| 161 |
break |
| 162 |
|
| 163 |
# Set the colors on the device |
| 164 |
payload = self._keyboard_grid.get_total_binary() |
| 165 |
|
| 166 |
self._parent.set_rgb_matrix(payload) |
| 167 |
self._parent.refresh_keyboard() |
| 168 |
|
| 169 |
# Sleep until the next ripple refresh |
| 170 |
time.sleep(self._refresh_rate) |
| 171 |
|
| 172 |
|
| 173 |
class RippleManager(object): |
| 174 |
""" |
| 175 |
Class which manages the overall process of performing a ripple effect |
| 176 |
""" |
| 177 |
|
| 178 |
def __init__(self, parent, device_number): |
| 179 |
self._logger = logging.getLogger('razer.device{0}.ripplemanager'.format(device_number)) |
| 180 |
self._parent = parent |
| 181 |
self._parent.register_observer(self) |
| 182 |
|
| 183 |
self._is_closed = False |
| 184 |
|
| 185 |
self._ripple_thread = RippleEffectThread(self, device_number) |
| 186 |
self._ripple_thread.start() |
| 187 |
|
| 188 |
@property |
| 189 |
def key_list(self): |
| 190 |
""" |
| 191 |
Get the list of keys from the key manager |
| 192 |
|
| 193 |
:return: List of tuples (expire_time, (key_row, key_col), random_colour) |
| 194 |
:rtype: list of tuple |
| 195 |
""" |
| 196 |
result = [] |
| 197 |
if hasattr(self._parent, 'key_manager'): |
| 198 |
result = self._parent.key_manager.temp_key_store |
| 199 |
|
| 200 |
return result |
| 201 |
|
| 202 |
def set_rgb_matrix(self, payload): |
| 203 |
""" |
| 204 |
Set the LED matrix on the keyboard |
| 205 |
|
| 206 |
:param payload: Binary payload |
| 207 |
:type payload: bytes |
| 208 |
""" |
| 209 |
self._parent._set_key_row(payload) |
| 210 |
|
| 211 |
def refresh_keyboard(self): |
| 212 |
""" |
| 213 |
Refresh the keyboard |
| 214 |
""" |
| 215 |
self._parent._set_custom_effect() |
| 216 |
|
| 217 |
def notify(self, msg): |
| 218 |
""" |
| 219 |
Receive notificatons from the device (we only care about effects) |
| 220 |
|
| 221 |
:param msg: Notification |
| 222 |
:type msg: tuple |
| 223 |
""" |
| 224 |
if not isinstance(msg, tuple): |
| 225 |
self._logger.warning("Got msg that was not a tuple") |
| 226 |
elif msg[0] == 'effect': |
| 227 |
# We have a message directed at us |
| 228 |
# MSG format |
| 229 |
# 0 1 2 3 |
| 230 |
# ('effect', Device, 'effectName', 'effectparams'...) |
| 231 |
# Device is the device the msg originated from (could be parent device) |
| 232 |
if msg[2] == 'setRipple': |
| 233 |
# Get (red, green, blue) tuple (args 3:6), and refreshrate arg 6 |
| 234 |
self._parent.key_manager.temp_key_store_state = True |
| 235 |
self._ripple_thread.enable(msg[3:6], msg[6]) |
| 236 |
else: |
| 237 |
# Effect other than ripple so stop |
| 238 |
self._ripple_thread.disable() |
| 239 |
|
| 240 |
self._parent.key_manager.temp_key_store_state = False |
| 241 |
|
| 242 |
def close(self): |
| 243 |
""" |
| 244 |
Close the manager, stop ripple thread |
| 245 |
""" |
| 246 |
if not self._is_closed: |
| 247 |
self._logger.debug("Closing Ripple Manager") |
| 248 |
self._is_closed = True |
| 249 |
|
| 250 |
self._ripple_thread.shutdown = True |
| 251 |
self._ripple_thread.join(timeout=2) |
| 252 |
if self._ripple_thread.is_alive(): |
| 253 |
self._logger.error("Could not stop RippleEffect thread") |
| 254 |
|
| 255 |
def __del__(self): |
| 256 |
self.close() |