diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2021-10-10 20:33:48 +0000 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2021-10-10 20:33:48 +0000 |
commit | 72ad29efeb4709572e789a57aa94d00a0eaeb97d (patch) | |
tree | 7555a12d7302b59fe0b226a0fd0f18964bfd50fb /NeoRuntime/Runtime | |
parent | 5cf342cd4400d4f555732f80bfdf3639965c4f88 (diff) | |
download | Luxcena-Neo-72ad29efeb4709572e789a57aa94d00a0eaeb97d.tar.gz Luxcena-Neo-72ad29efeb4709572e789a57aa94d00a0eaeb97d.zip |
:construction: Make python 3.5 compatible and fix some weird bugs
Diffstat (limited to 'NeoRuntime/Runtime')
-rw-r--r-- | NeoRuntime/Runtime/luxcena_neo/color_utils.py | 7 | ||||
-rw-r--r-- | NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 20 | ||||
-rw-r--r-- | NeoRuntime/Runtime/luxcena_neo/strip.py | 36 | ||||
-rw-r--r-- | NeoRuntime/Runtime/neo_runtime.py | 35 |
4 files changed, 51 insertions, 47 deletions
diff --git a/NeoRuntime/Runtime/luxcena_neo/color_utils.py b/NeoRuntime/Runtime/luxcena_neo/color_utils.py index ab29092..3b7ece4 100644 --- a/NeoRuntime/Runtime/luxcena_neo/color_utils.py +++ b/NeoRuntime/Runtime/luxcena_neo/color_utils.py @@ -31,10 +31,9 @@ def twentyfour_bit_from_rgb(red, green, blue, white=0): def twentyfour_bit_from_hex(hex_color: str): """ Convert the provided hex code to a 24-bit color value. """ + print(hex_color) value = hex_color.lstrip('#') - lv = len(value) - rgb = tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3)) - return twentyfour_bit_from_rgb(red=rgb[1], green=rgb[0], blue=rgb[2]) + return (int(value[0:2], 16) << 16) | (int(value[2:4], 16) << 8) | (int(value[4:6], 16)) def detect_format_convert_color(*color) -> int: @@ -79,4 +78,4 @@ class Color: def __invert__(self): rgb_color = self.rgb - return Color((255-rgb_color[0], 255-rgb_color[1], 255-rgb_color[2]))
\ No newline at end of file + return Color((255-rgb_color[0], 255-rgb_color[1], 255-rgb_color[2])) diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index 5c89ca0..ce0fb62 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -61,7 +61,7 @@ class Variables: def __init__(self, package_path): self.__vars = {} - self.__vars_save_file = f"{package_path}/state.json" + self.__vars_save_file = "{}/state.json".format(package_path) self.__saved_variables = {} self.read_saved_variables() @@ -109,7 +109,7 @@ class Variables: def declare(self, variable): """ Declare a new variable. """ if variable.name in self.__vars: - raise Exception(f"Variable with name {variable.name} already defined.") + raise Exception("Variable with name {} already defined.".format(variable.name)) if variable.name in self.__saved_variables: variable.value = self.__saved_variables[variable.name] @@ -164,7 +164,7 @@ class Variable: return {"name": self.name, "value": self.value, "type": self.var_type} def __str__(self): - return f"{self.name}: {self.value}" + return "{}: {}".format(self.name, self.value) def set_save_func(self, save_func): self.__save_func = save_func @@ -173,13 +173,13 @@ class ColorVariable(Variable): def __init__(self, name: str, *color, **kwargs): if not self.verify_color(*color): - raise Exception(f"Invalid color {color}") + raise Exception("Invalid color {}".format(color)) super().__init__(name, self.extract_interesting(*color), VariableType.COLOR, **kwargs) @Variable.value.setter def value(self, *color): if not self.verify_color(*color): - print(f"Attempting to set {self.name} to invalid value {color}") + print("Attempting to set {} to invalid value {}".format(self.name, color)) return super(ColorVariable, type(self)).value.fset(self, self.extract_interesting(*color)) @@ -218,9 +218,9 @@ class IntegerVariable(Variable): if (self.__min <= value <= self.__max): super(ColorVariable, type(self)).value.fset(self, value) else: - print(f"Attempted to set {self.name} to {value} but range is [{self.__min},{self.__max}].") + print("Attempted to set {} to {} but range is [{},{}].".format(self.name, value, self.__min, self.__max)) except ValueError: - print(f"Attempted to set {self.name} to \"{value}\", which is not a valid integer...") + print("Attempted to set {} to \"{}\", which is not a valid integer...".format(self.name, value)) def to_dict(self): return {"name": self.name, "value": self.value, "type": self.var_type, "min": self.__min, "max": self.__max} @@ -240,9 +240,9 @@ class FloatVariable(Variable): if (self.__min <= value <= self.__max): super(ColorVariable, type(self)).value.fset(self, value) else: - print(f"Attempted to set {self.name} to {value} but range is [{self.__min},{self.__max}].") + print("Attempted to set {} to {} but range is [{},{}].".format(self.name, value, self.__min, self.__max)) except ValueError: - print(f"Attempted to set {self.name} to \"{value}\", which is not a valid float...") + print("Attempted to set {} to \"{}\", which is not a valid float...".format(self.name, self.value)) def __str__(self): return round(self.value, 2) @@ -262,4 +262,4 @@ class BooleanVariable(Variable): try: value = bool(value) except: - print(f"Attempted to set {self.name} to \"{value}\", which is not a valid bool...") + print("Attempted to set {} to \"{}\", which is not a valid bool...".format(self.name, value)) diff --git a/NeoRuntime/Runtime/luxcena_neo/strip.py b/NeoRuntime/Runtime/luxcena_neo/strip.py index bfe2bbc..32380da 100644 --- a/NeoRuntime/Runtime/luxcena_neo/strip.py +++ b/NeoRuntime/Runtime/luxcena_neo/strip.py @@ -1,6 +1,6 @@ import json from os import path -from .neopixel import * +import rpi_ws281x as ws from .matrix import Matrix, get_segment_range from .power_calc import calcCurrent @@ -10,11 +10,11 @@ class Strip: def __init__(self, strip_conf): self.SEGMENTS = strip_conf["segments"] - self.LED_FREQ_HZ = strip_conf["led_freq_hz"] # LED signal frequency in hertz (usually 800khz) - self.LED_CHANNEL = strip_conf["led_channel"] # Set to '1' for GPIOs 13, 19, 41, 45, 53 + self.LED_FREQ_HZ = int(strip_conf["led_freq_hz"]) # LED signal frequency in hertz (usually 800khz) + self.LED_CHANNEL = int(strip_conf["led_channel"]) # Set to '1' for GPIOs 13, 19, 41, 45, 53 self.LED_INVERT = strip_conf["led_invert"] # True to invert the signal, (when using NPN transistor level shift) - self.LED_PIN = strip_conf["led_pin"] # 18 uses PWM, 10 uses SPI /dev/spidev0.0 - self.LED_DMA = strip_conf["led_dma"] # DMA channel for generating the signal, on the newer ones, try 10 + self.LED_PIN = int(strip_conf["led_pin"]) # 18 uses PWM, 10 uses SPI /dev/spidev0.0 + self.LED_DMA = int(strip_conf["led_dma"]) # DMA channel for generating the signal, on the newer ones, try 10 self.LED_COUNT = sum(self.SEGMENTS) # Number of LEDs in strip if ("color_calibration" in strip_conf) and (strip_conf["color_calibration"] != ""): @@ -26,15 +26,16 @@ class Strip: self.COLORSTATE = [0 for x in range(self.LED_COUNT)] self.LED_BRIGHTNESS = 255 - - self.strip = Adafruit_NeoPixel( + + self.strip = ws.Adafruit_NeoPixel( self.LED_COUNT, self.LED_PIN, self.LED_FREQ_HZ, self.LED_DMA, self.LED_INVERT, self.LED_BRIGHTNESS, - self.LED_CHANNEL + self.LED_CHANNEL, + strip_type=ws.WS2812_STRIP ) self.strip.begin() @@ -86,15 +87,17 @@ class Strip: self.__power_on = value if (self.power_on): self.__actual_brightness = self.__brightness - # self.strip.setBrightness(self.__brightness) + self.strip.setBrightness(self.__brightness) + self.strip.show() else: self.__actual_brightness = 0 - # self.strip.setBrightness(0) + self.strip.setBrightness(0) + self.strip.show() self.save_globvars() @property def brightness(self): - # return self.strip.getBrightness() + #return self.strip.getBrightness() return self.__actual_brightness @brightness.setter @@ -103,10 +106,11 @@ class Strip: self.__brightness = value if (self.power_on): self.__actual_brightness = value - # self.strip.setBrightness(value) + self.strip.setBrightness(value) + self.strip.show() self.save_globvars() else: - raise Exception(f"Value ({value}) outside allowed range (0-255)") + raise Exception("Value ({}) outside allowed range (0-255)".format(value)) def show(self): """Update the display with the data from the LED buffer.""" @@ -118,7 +122,7 @@ class Strip: """ c = detect_format_convert_color(*color) self.TMPCOLORSTATE[n] = c - # self.strip.setPixelColor(n, ) + self.strip.setPixelColor(n, c) def set_pixel_color_XY(self, x, y, *color): """Set LED at position n to the provided 24-bit color value (in RGB order). @@ -161,7 +165,7 @@ def color_from_hex(hex_color: str): value = hex_color.lstrip('#') lv = len(value) rgb = tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3)) - return color_from_rgb(red=rgb[1], green=rgb[0], blue=rgb[2]) + return color_from_rgb(red=rgb[0], green=rgb[1], blue=rgb[2]) def detect_format_convert_color(*color) -> int: @@ -181,4 +185,4 @@ def detect_format_convert_color(*color) -> int: return color[0] if (len(color) == 3): return color_from_rgb(*color) - raise ValueError("Invalid parameters provided, check documentation.")
\ No newline at end of file + raise ValueError("Invalid parameters provided, check documentation.") diff --git a/NeoRuntime/Runtime/neo_runtime.py b/NeoRuntime/Runtime/neo_runtime.py index 6f84763..b028530 100644 --- a/NeoRuntime/Runtime/neo_runtime.py +++ b/NeoRuntime/Runtime/neo_runtime.py @@ -23,13 +23,13 @@ def init_strip(strip_config_file): strip_config_obj = configparser.ConfigParser() strip_config_obj.read(args.strip_config) strip_config = dict(strip_config_obj.items("DEFAULT")) - strip_config["matrix"] = json.loads(strip_config["matrix"].replace('"', "")) - strip_config["segments"] = [int(x) for x in strip_config["segments"].split(" ")] - strip_config["led_channel"] = int(strip_config["led_channel"]) - strip_config["led_dma"] = int(strip_config["led_dma"]) - strip_config["led_freq_hz"] = int(strip_config["led_freq_hz"]) - strip_config["led_invert"] = (strip_config["led_invert"] == "false") - strip_config["led_pin"] = int(strip_config["led_pin"]) + strip_config["matrix"] = json.loads(strip_config_obj.get("DEFAULT", "matrix").replace('"', "")) + strip_config["segments"] = [int(x) for x in strip_config_obj.get("DEFAULT", "segments").split(" ")] + strip_config["led_channel"] = strip_config_obj.getint("DEFAULT", "led_channel") + strip_config["led_dma"] = strip_config_obj.getint("DEFAULT", "led_dma") + strip_config["led_freq_hz"] = strip_config_obj.getint("DEFAULT", "led_freq_hz") + strip_config["led_invert"] = strip_config_obj.getboolean("DEFAULT", "led_invert") + strip_config["led_pin"] = strip_config_obj.getint("DEFAULT", "led_pin") strip = Strip(strip_config) return strip @@ -38,11 +38,12 @@ def init_package(package_path, entry_module, strip): print ("> Initializing package (mode)...") sys.path.append(package_path) module = importlib.import_module(entry_module) - module_entry_instance = module.Main(package_path) # Make the strip instance available in our modules setattr(module, "strip", strip) + module_entry_instance = module.Main(package_path) + return module_entry_instance def exec_module(module_executor_loop_func): @@ -164,14 +165,14 @@ class NeoRuntime: elif command[1] == 1: self.__strip.brightness = command[2] else: - print(f"Unknown globvar {command[1]}.") + print("Unknown globvar {}.".format(command[1])) elif command[0] == 1: name = command[3:3+command[1]].decode("ascii") value = command[3+command[1]:3+command[1]+command[2]].decode("ascii") if name in self.__module_entry_instance.var: self.__module_entry_instance.var[name] = value else: - print(f"Unknown variable {name}") + print("Unknown variable ".format(name)) elif command[0] == 2: self.__send_strip_buffer = (command[1] == 1) else: @@ -245,19 +246,19 @@ if __name__ == "__main__": args.mode_entry = args.mode_entry.replace("\"", "") args.socket_file = args.socket_file.replace("\"", "") if not path.exists(args.strip_config): - print(f"Strip config not found ({args.strip_config}).") + print("Strip config not found ({})".format(args.strip_config)) sys.exit(1) if not path.exists(args.mode_path): - print(f"Mode path not found ({args.mode_path}).") + print("Mode path not found ({})".format(args.mode_path)) sys.exit(1) - if not path.exists(f"{args.mode_path}/{args.mode_entry}.py"): - print(f"Mode entry not found in mode path ({args.mode_path}/{args.mode_entry}).") + if not path.exists("{}/{}.py".format(args.mode_path, args.mode_entry)): + print("Mode entry not found in mode path ({}/{})".format(args.mode_path, args.mode_entry)) sys.exit(1) - print(f"StripConfig: {args.strip_config}") - print(f"Module : {args.mode_path}/{args.mode_entry}") + print("StripConfig: ".format(args.strip_config)) + print("Module : ".format(args.mode_path, args.mode_entry)) - print(f"> Starting \"{args.mode_path}\" in NeoRuntime.") + print("> Starting \"{}\" in NeoRuntime.".format(args.mode_path)) runtime = NeoRuntime(args.mode_path, args.mode_entry, args.strip_config, args.socket_file) runtime.start() print ("> NeoRuntime exited...") |