-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from MaWalla/threadloop
overhauled entire codebase to use threads properly
- Loading branch information
Showing
28 changed files
with
321 additions
and
1,426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[submodule "fxmodes/ScreenFX"] | ||
path = fxmodes/ScreenFX | ||
url = git@github.com:MaWalla/ScreenFX.git | ||
[submodule "fxmodes/PulseViz"] | ||
path = fxmodes/PulseViz | ||
url = git@github.com:MaWalla/PulseViz.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .serial import Serial | ||
from .dualshock import DualShock | ||
from .wled import WLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import numpy as np | ||
from math import sqrt | ||
|
||
__all__ = [ | ||
'Device', | ||
] | ||
|
||
|
||
class Device: | ||
""" | ||
Generic class for devices | ||
""" | ||
|
||
def __init__(self, device, *args, **kwargs): | ||
""" | ||
common attributes | ||
""" | ||
self.name = device.get('name') | ||
self.enabled = device.get('enabled') | ||
|
||
self.brightness = device.get('brightness') | ||
self.flip = device.get('flip') | ||
self.color_correction = device.get('color_correction') | ||
self.leds = device.get('leds', 1) | ||
|
||
def process_data(self, data): | ||
""" | ||
converts a list of rgb values into something device-compatible | ||
:param data: a list of rgb values | ||
""" | ||
|
||
if self.flip: | ||
data = np.flip(data, axis=0) | ||
|
||
flat_data = np.array([ | ||
self.color_correction(np.array(value) * self.brightness) | ||
for value in data | ||
]).astype(int).ravel() | ||
|
||
if self.color_correction: | ||
flat_data = self.color_correction(flat_data) | ||
|
||
return flat_data | ||
|
||
def loop(self, data): | ||
""" | ||
Function that will be repeatedly called by a threadloop. | ||
It should do the necessary work to get the device to display the sent data. | ||
:param data: 2d array containing a list of rgb values for the target device | ||
""" | ||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import glob | ||
|
||
from .device import Device | ||
|
||
|
||
__all__ = [ | ||
'DualShock', | ||
] | ||
|
||
|
||
class DualShock(Device): | ||
ds4_paths = { | ||
counter + 1: path | ||
for counter, path in enumerate(glob.glob('/sys/class/leds/0005:054C:05C4.*:global')) | ||
} | ||
|
||
def __init__(self, device, *args, **kwargs): | ||
super().__init__(device, *args, **kwargs) | ||
|
||
device_num = device.get('device_num') | ||
self.path = self.ds4_paths.get(device_num) | ||
|
||
if not self.path: | ||
print(f'WARNING: DualShock Controller with device_num {device_num} specified,') | ||
print(f'but there is no device path available for it. Disabling it.') | ||
self.enabled = False | ||
|
||
def set_dualshock_color(self, color): | ||
""" | ||
Sends an rgb color to a DualShock 4 controller for its lightbar | ||
:param color: rgb value that is sent to the lightbar | ||
""" | ||
red, green, blue = color | ||
|
||
# TODO improve the path handling. [:-6 ain't the best solution] | ||
red_path = f'{self.path[:-6]}red/brightness' | ||
green_path = f'{self.path[:-6]}green/brightness' | ||
blue_path = f'{self.path[:-6]}blue/brightness' | ||
|
||
r = open(red_path, 'w') | ||
r.write(str(red)) | ||
r.close() | ||
|
||
g = open(green_path, 'w') | ||
g.write(str(green)) | ||
g.close() | ||
|
||
b = open(blue_path, 'w') | ||
b.write(str(blue)) | ||
b.close() | ||
|
||
def loop(self, data): | ||
""" | ||
Function that will be repeatedly called by a threadloop. | ||
:param data: 2d array containing a list of rgb values for the target device | ||
""" | ||
|
||
# we get a list of rgb values which only contains one entry (since there is only one LED), so we grab that | ||
self.set_dualshock_color(data[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import serial | ||
import numpy as np | ||
|
||
from .device import Device | ||
|
||
|
||
__all__ = [ | ||
'Serial', | ||
] | ||
|
||
|
||
class Serial(Device): | ||
|
||
def __init__(self, device, *args, **kwargs): | ||
super().__init__(device, *args, **kwargs) | ||
|
||
path = device.get('path') | ||
|
||
try: | ||
self.serial_device = serial.Serial(path, device.get('baud')) | ||
except serial.serialutil.SerialException: | ||
print(f'WARNING: Serial device path "{path}" invalid. Disabling it.') | ||
self.enabled = False | ||
|
||
def set_serial_strip(self, data): | ||
""" | ||
Sends an array of colors to a Serial device | ||
:param data: the color value in rgb | ||
""" | ||
byte_data = bytes(np.array(data).ravel().tolist()) | ||
self.serial_device.write(byte_data) | ||
|
||
def loop(self, data): | ||
self.set_serial_strip(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import socket | ||
|
||
from .device import Device | ||
|
||
|
||
__all__ = [ | ||
'WLED', | ||
] | ||
|
||
|
||
class WLED(Device): | ||
|
||
def __init__(self, device, *args, **kwargs): | ||
super().__init__(device, *args, **kwargs) | ||
|
||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
self.ip = device.get('ip') | ||
self.port = device.get('port') | ||
|
||
def set_wled_strip(self, data): | ||
byte_data = bytes([2, 5, *data.ravel().astype(int).tolist()]) | ||
|
||
self.sock.sendto(byte_data, (self.ip, self.port)) | ||
|
||
def loop(self, data): | ||
self.set_wled_strip(data) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.