From 7bdce37fd3f18e2712e18c4e2c64cac69af0aca1 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 19 Sep 2021 19:43:11 +0200 Subject: :boom: Introduce new UI based on svelte, and rewrite a lot of the node app and the NeoRuntime --- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 149 ++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py new file mode 100644 index 0000000..9920ca4 --- /dev/null +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -0,0 +1,149 @@ +import json +from os import path +from enum import Enum + +class NeoBehaviour: + """ + This is the base-class "main" should inherit from! + All methods are blocking :) This means that you could potentially loose a "tick" + For example, if "eachSecond" is taking up the thread, and the clock goes from 11:58 to 12:02, "eachHour", will not be called. + """ + + def __init__(self, package_path): + """ + THIS METHOD SHOULD NOT BE OVERIDDEN! Use onStart if you want a setup-method!!! + Contains basic setup + """ + self.vars = Variables(package_path) + + def declare_variables(self): + """ This should be overridden, and ALL variables should be declared here. """ + return + + def on_start(self): + """ This method will be run right after __init__ """ + return + + def each_tick(self): + """ This method will be run every tick, that means every time the program has time """ + return + + def each_second(self): + """ This method is called every second (on the clock), given that the thread is open """ + return + + def each_minute(self): + """ This method is called every mintue (on the clock), given that the thread is open """ + return + + def each_hour(self): + """ This method is called every whole hour (on the clock), given that the thread is open """ + return + + def each_day(self): + """ This method is called every day at noon, given that the thread is open """ + return + +class VariableType(Enum): + TEXT = 1 + INT = 2 + RANGE = 3 + COLOR = 4 + +class Variables: + + def __init__(self, package_path): + self.__vars = {} + self.__vars_save_file = f"{package_path}/variables.json" + self.__saved_variables = {} + self.read_saved_variables() + + def __getattr__(self, name): + if name in ["_Variables__vars", "_Variables__vars_save_file", "_Variables__saved_variables"]: + return super(Variables, self).__getattr__(name) + return self.__vars[name].value + + def __setattr__(self, name, value): + if name in ["_Variables__vars", "_Variables__vars_save_file", "_Variables__saved_variables"]: + super(Variables, self).__setattr__(name, value) + elif type(value) == Variable: + self.__vars[name] = value + else: + self.__vars[name].value = value + + def __delattr__(self, name): + if name in ["_Variables__vars", "_Variables__vars_save_file", "_Variables__saved_variables"]: + super(Variables, self).__delattr__(name) + else: + del self.__vars[name] + + def __getitem__(self, name): + return self.__vars[name] + + def __setitem__(self, name, value): + self.__vars[name].value = value + + def __contains__(self, name): + return name in self.__vars + + def __repr__(self): + return json.dumps({name: var.value for name, var in self.__vars.items()}) + + def __str__(self): + return repr(self) + + def __dir__(self): + return super(Variables, self).__dir__() + [name for name in self.__vars.keys()] + + def __len__(self): + return len(self.__vars) + + def __iter__(self): + return iter(self.__vars.items()) + + def declare(self, name: str, default: any, var_type: VariableType, on_change = None, **kwargs): + """ Declare a new variable. """ + if name in self.__saved_variables: + default = self.__saved_variables[name] + var = Variable(self.save_variables, name, default, var_type, on_change, **kwargs) + self.__setattr__(name, var) + + def read_saved_variables(self): + """ Read saved variable values from file. """ + if not path.exists(self.__vars_save_file): return + try: + with open(self.__vars_save_file, "r") as f: + self.__saved_variables = json.load(f) + except: + print("Could not load saved variables") + + def save_variables(self): + """ Save variable values to file. """ + self.__saved_variables = {name: var.value for name, var in self.__vars.items()} + with open(self.__vars_save_file, "w") as f: + f.write(json.dumps(self.__saved_variables)) + +class Variable: + + def __init__(self, save_func, key, default, var_type, on_change): + self.__save_func = save_func + self.__key = key + self.__value = default + self.__var_type = var_type + self.__on_change = on_change + + @property + def key(self): return self.__key + + @property + def value(self): return self.__value + + @value.setter + def value(self, value): + self.__value = value + self.__save_func() + if self.__on_change is not None: + self.__on_change(self.value) + + @property + def var_type(self): return self.__var_type.name -- cgit v1.2.3 From 076c967a8aaac929735694f295ade5adaf8c9ff3 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 3 Oct 2021 16:41:40 +0200 Subject: :sparkles: Add actual different variable types, this also changes slightly how they are defined --- NeoRuntime/Runtime/luxcena_neo/__init__.py | 2 +- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 131 +++++++++++++++++++++--- 2 files changed, 117 insertions(+), 16 deletions(-) (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') diff --git a/NeoRuntime/Runtime/luxcena_neo/__init__.py b/NeoRuntime/Runtime/luxcena_neo/__init__.py index dfec639..fbbc670 100644 --- a/NeoRuntime/Runtime/luxcena_neo/__init__.py +++ b/NeoRuntime/Runtime/luxcena_neo/__init__.py @@ -1,2 +1,2 @@ -from .neo_behaviour import NeoBehaviour, VariableType +from .neo_behaviour import NeoBehaviour, VariableType, ColorVariable, FloatVariable, IntegerVariable import luxcena_neo.color_utils as utils \ No newline at end of file diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index 9920ca4..66678c4 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -1,6 +1,8 @@ import json from os import path from enum import Enum +from .strip import detect_format_convert_color +from .color_utils import rgb_from_24bit, hex_from_24bit class NeoBehaviour: """ @@ -14,7 +16,11 @@ class NeoBehaviour: THIS METHOD SHOULD NOT BE OVERIDDEN! Use onStart if you want a setup-method!!! Contains basic setup """ - self.vars = Variables(package_path) + self.var = Variables(package_path) + + self.declare = self.var.declare + self.declare_variables() + del self.declare def declare_variables(self): """ This should be overridden, and ALL variables should be declared here. """ @@ -47,14 +53,14 @@ class NeoBehaviour: class VariableType(Enum): TEXT = 1 INT = 2 - RANGE = 3 + FLOAT = 3 COLOR = 4 class Variables: def __init__(self, package_path): self.__vars = {} - self.__vars_save_file = f"{package_path}/variables.json" + self.__vars_save_file = f"{package_path}/state.json" self.__saved_variables = {} self.read_saved_variables() @@ -66,8 +72,6 @@ class Variables: def __setattr__(self, name, value): if name in ["_Variables__vars", "_Variables__vars_save_file", "_Variables__saved_variables"]: super(Variables, self).__setattr__(name, value) - elif type(value) == Variable: - self.__vars[name] = value else: self.__vars[name].value = value @@ -101,12 +105,15 @@ class Variables: def __iter__(self): return iter(self.__vars.items()) - def declare(self, name: str, default: any, var_type: VariableType, on_change = None, **kwargs): + def declare(self, variable): """ Declare a new variable. """ - if name in self.__saved_variables: - default = self.__saved_variables[name] - var = Variable(self.save_variables, name, default, var_type, on_change, **kwargs) - self.__setattr__(name, var) + if variable.name in self.__vars: + raise Exception(f"Variable with name {variable.name} already defined.") + if variable.name in self.__saved_variables: + variable.value = self.__saved_variables[variable.name] + + variable.set_save_func(self.save_variables) + self.__vars[variable.name] = variable def read_saved_variables(self): """ Read saved variable values from file. """ @@ -123,17 +130,20 @@ class Variables: with open(self.__vars_save_file, "w") as f: f.write(json.dumps(self.__saved_variables)) + def to_dict(self): + return {x.name: x.to_dict() for x in self.__vars.values()} + class Variable: - def __init__(self, save_func, key, default, var_type, on_change): - self.__save_func = save_func - self.__key = key + def __init__(self, name, default, var_type: VariableType, on_change = None): + self.__name = name self.__value = default self.__var_type = var_type self.__on_change = on_change + self.__save_func = None @property - def key(self): return self.__key + def name(self): return self.__name @property def value(self): return self.__value @@ -141,9 +151,100 @@ class Variable: @value.setter def value(self, value): self.__value = value - self.__save_func() + if self.__save_func is not None: + self.__save_func() if self.__on_change is not None: self.__on_change(self.value) @property def var_type(self): return self.__var_type.name + + def to_dict(self): + return {"name": self.name, "value": self.value, "type": self.var_type} + + def __str__(self): + return f"{self.name}: {self.value}" + + def set_save_func(self, save_func): + self.__save_func = save_func + +class ColorVariable(Variable): + + def __init__(self, name: str, *color, **kwargs): + if not self.verify_color(*color): + raise Exception(f"Invalid color {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}") + return + super(ColorVariable, type(self)).value.fset(self, self.extract_interesting(*color)) + + def verify_color(self, *color): + if (len(color) == 1) and (isinstance(color[0], str)): + return True + if (len(color) == 1) and (isinstance(color[0], tuple)): + if len(color[0]) != 3: return False + for c in color[0]: + if not (0 <= c <= 255): return False + return True + if (len(color) == 1) and (isinstance(color[0], int)): + return True + if (len(color) == 3): + for c in color: + if not isinstance(c, int): return False + if not (0 <= c <= 255): return False + return True + return False + + def extract_interesting(self, *color): + if (len(color) == 1): return color[0] + return color + +class IntegerVariable(Variable): + + def __init__(self, name: str, default: int = 0, min_val: int = 0, max_val: int = 255, **kwargs): + self.__min = min_val + self.__max = max_val + super().__init__(name, default, VariableType.INT) + + @Variable.value.setter + def value(self, value): + try: + value = int(value) + 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}].") + except ValueError: + print(f"Attempted to set {self.name} to \"{value}\", which is not a valid integer...") + + def to_dict(self): + return {"name": self.name, "value": self.value, "type": self.var_type, "min": self.__min, "max": self.__max} + + +class FloatVariable(Variable): + + def __init__(self, name: str, default: float = 0.0, min_val: float = 0.0, max_val: float = 255.0, **kwargs): + self.__min = min_val + self.__max = max_val + super().__init__(name, default, VariableType.FLOAT) + + @Variable.value.setter + def value(self, value): + try: + value = float(value) + 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}].") + except ValueError: + print(f"Attempted to set {self.name} to \"{value}\", which is not a valid float...") + + def __str__(self): + return round(self.value, 2) + + def to_dict(self): + return {"name": self.name, "value": self.value, "type": self.var_type, "min": self.__min, "max": self.__max} -- cgit v1.2.3 From 4850c11d87df287beacf5a5bd9012f7b54f13566 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 3 Oct 2021 20:01:31 +0200 Subject: :sprakles: Add BooleanVariable --- NeoRuntime/Runtime/luxcena_neo/__init__.py | 4 ++-- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 15 +++++++++++++++ NeoRuntime/Runtime/neo_runtime.py | 14 ++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') diff --git a/NeoRuntime/Runtime/luxcena_neo/__init__.py b/NeoRuntime/Runtime/luxcena_neo/__init__.py index fbbc670..606f365 100644 --- a/NeoRuntime/Runtime/luxcena_neo/__init__.py +++ b/NeoRuntime/Runtime/luxcena_neo/__init__.py @@ -1,2 +1,2 @@ -from .neo_behaviour import NeoBehaviour, VariableType, ColorVariable, FloatVariable, IntegerVariable -import luxcena_neo.color_utils as utils \ No newline at end of file +from .neo_behaviour import NeoBehaviour, VariableType, ColorVariable, FloatVariable, IntegerVariable, BooleanVariable +import luxcena_neo.color_utils as utils diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index 66678c4..dc4609c 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -55,6 +55,7 @@ class VariableType(Enum): INT = 2 FLOAT = 3 COLOR = 4 + BOOL = 5 class Variables: @@ -248,3 +249,17 @@ class FloatVariable(Variable): def to_dict(self): return {"name": self.name, "value": self.value, "type": self.var_type, "min": self.__min, "max": self.__max} + + + +class BooleanVariable(Variable): + + def __init__(self, name: str, default: bool, **kwargs): + super().__init__(name, default, VariableType.BOOL) + + @Variable.value.setter + def value(self, value): + try: + value = bool(value) + except: + print(f"Attempted to set {self.name} to \"{value}\", which is not a valid bool...") diff --git a/NeoRuntime/Runtime/neo_runtime.py b/NeoRuntime/Runtime/neo_runtime.py index 4ecbc97..d132bff 100644 --- a/NeoRuntime/Runtime/neo_runtime.py +++ b/NeoRuntime/Runtime/neo_runtime.py @@ -59,7 +59,7 @@ class NeoRuntime: self.__module_th = None self.__socket_file = socket_file self.__send_strip_buffer = False - + def start(self): # The mode is starting in it's own thread @@ -82,7 +82,7 @@ class NeoRuntime: def __bind_socket(self): if path.exists(self.__socket_file): remove(self.__socket_file) - + self.__s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.__s.bind(self.__socket_file) self.__s.listen(1) @@ -126,7 +126,7 @@ class NeoRuntime: rs.close() else: self.__execute_command(data) - + def __close_socket(self): if (self.__s is None): return r, w, e = select.select([self.__s, *self.__s_clients], self.__s_clients, [], 0) @@ -145,22 +145,20 @@ class NeoRuntime: """ command should be of type bytes first byte indicates command type (currently setglob or setvar) - + for command type 1 byte 1 indicates which globvar byte 2 indicates value for command type 2 first 32 bytes are the var name - + """ # print(command.hex(" ")) if command[0] == 0: if command[1] == 0: self.__strip.power_on = (command[2] == 1) - print(f"Strip power: {self.__strip.power_on}") elif command[1] == 1: self.__strip.brightness = command[2] - print(f"Strip brightness: {self.__strip.brightness}") else: print(f"Unknown globvar {command[1]}.") elif command[0] == 1: @@ -236,7 +234,7 @@ if __name__ == "__main__": 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}).") sys.exit(1) - + print(f"StripConfig: {args.strip_config}") print(f"Module : {args.mode_path}/{args.mode_entry}") -- cgit v1.2.3 From 3af8c73910997a0a6b766a3eb1d624a327356bbd Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 6 Oct 2021 17:29:58 +0200 Subject: :sparkles: Add Color helper class (should be made the default for ColorVariable) --- NeoRuntime/Runtime/luxcena_neo/color_utils.py | 65 ++++++++++++++++++++++++- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 2 +- 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') diff --git a/NeoRuntime/Runtime/luxcena_neo/color_utils.py b/NeoRuntime/Runtime/luxcena_neo/color_utils.py index a17d8e4..ab29092 100644 --- a/NeoRuntime/Runtime/luxcena_neo/color_utils.py +++ b/NeoRuntime/Runtime/luxcena_neo/color_utils.py @@ -8,7 +8,7 @@ def rgb_to_hex(rgb: tuple) -> str: """ Convert rgb color to hex string. """ return '#%02x%02x%02x' % rgb -def rgb_from_24bit(color: int) -> tuple: +def rgb_from_twentyfour_bit(color: int) -> tuple: """ Convert 24-bit color value into a tuple representing the rgb values. """ # w = (color & 0xFF000000) >> 24 r = (color & 0x00FF0000) >> 16 @@ -16,6 +16,67 @@ def rgb_from_24bit(color: int) -> tuple: b = (color & 0x000000FF) return (r, g, b) -def hex_from_24bit(color: int) -> str: +def hex_from_twentyfour_bit(color: int) -> str: """ Convert 24-bit color value into hex str. """ rgb_to_hex(rgb_from_24bit(color)) + +def twentyfour_bit_from_rgb(red, green, blue, white=0): + """ + Convert the provided red, green, blue color to a 24-bit color value. + Each color component should be a value 0-255 + where 0 is the lowest intensity and 255 is the highest intensity. + """ + return (white << 24) | (red << 16) | (green << 8) | blue + + +def twentyfour_bit_from_hex(hex_color: str): + """ Convert the provided hex code to a 24-bit color value. """ + 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]) + + +def detect_format_convert_color(*color) -> int: + """ + Detect format of a color and return its 24-bit color value. + + If parameter is only a str, it will be treated as a hex value. + If parameter is a tuple, the first three items in that tuple will be treated as a rgb value. + If parameter is a int, it will be treated as a 24-bit color value. + If there are 3 parameters, these will be treated as a rgb value. + """ + if (len(color) == 1) and (isinstance(color[0], str)): + return twentyfour_bit_from_hex(color[0]) + if (len(color) == 1) and (isinstance(color[0], tuple)): + return twentyfour_bit_from_rgb(*(color[0])) + if (len(color) == 1) and (isinstance(color[0], int)): + return color[0] + if (len(color) == 3): + return twentyfour_bit_from_rgb(*color) + raise ValueError("Invalid parameters provided, check documentation.") + + +class Color: + + def __init__(self, *color): + self.__color = detect_format_convert_color(*color) + + @property + def hex(self): return hex_from_twentyfour_bit(self.__color) + + @property + def rgb(self): return rgb_from_twentyfour_bit(self.__color) + + def __repr__(self): + return self.__color + + def __str__(self): + return str(repr(self)) + + def __int__(self): + return self.__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 diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index dc4609c..5c89ca0 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -2,7 +2,7 @@ import json from os import path from enum import Enum from .strip import detect_format_convert_color -from .color_utils import rgb_from_24bit, hex_from_24bit +from .color_utils import rgb_from_twentyfour_bit, hex_from_twentyfour_bit class NeoBehaviour: """ -- cgit v1.2.3 From 72ad29efeb4709572e789a57aa94d00a0eaeb97d Mon Sep 17 00:00:00 2001 From: jakobst1n Date: Sun, 10 Oct 2021 20:33:48 +0000 Subject: :construction: Make python 3.5 compatible and fix some weird bugs --- NeoRuntime/Runtime/luxcena_neo/color_utils.py | 7 +++-- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 20 +++++++------- NeoRuntime/Runtime/luxcena_neo/strip.py | 36 ++++++++++++++----------- NeoRuntime/Runtime/neo_runtime.py | 35 ++++++++++++------------ src/NeoRuntimeManager/RuntimeProcess.js | 1 + 5 files changed, 52 insertions(+), 47 deletions(-) (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') 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...") diff --git a/src/NeoRuntimeManager/RuntimeProcess.js b/src/NeoRuntimeManager/RuntimeProcess.js index 24614fa..c5c4749 100644 --- a/src/NeoRuntimeManager/RuntimeProcess.js +++ b/src/NeoRuntimeManager/RuntimeProcess.js @@ -28,6 +28,7 @@ class RuntimeProcess { this.isRunning = true; this.proc = spawn.spawn( `${__appdir}/NeoRuntime/Runtime/venv/bin/python`, + //"python", [ "-u", // This makes us able to get real-time output `${__appdir}/NeoRuntime/Runtime/neo_runtime.py`, -- cgit v1.2.3 From 14335646c5f31047e51cbaa038fc3cec92e49b90 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl <14180120+JakobST1n@users.noreply.github.com> Date: Mon, 11 Oct 2021 09:07:02 +0200 Subject: :construction: Add actual classtype to super calls --- NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py') diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index ce0fb62..4da3093 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -216,7 +216,7 @@ class IntegerVariable(Variable): try: value = int(value) if (self.__min <= value <= self.__max): - super(ColorVariable, type(self)).value.fset(self, value) + super(IntegerVariable, type(self)).value.fset(self, value) else: print("Attempted to set {} to {} but range is [{},{}].".format(self.name, value, self.__min, self.__max)) except ValueError: @@ -238,7 +238,7 @@ class FloatVariable(Variable): try: value = float(value) if (self.__min <= value <= self.__max): - super(ColorVariable, type(self)).value.fset(self, value) + super(FloatVariable, type(self)).value.fset(self, value) else: print("Attempted to set {} to {} but range is [{},{}].".format(self.name, value, self.__min, self.__max)) except ValueError: @@ -261,5 +261,6 @@ class BooleanVariable(Variable): def value(self, value): try: value = bool(value) + super(BooleanVariable, type(self)).value.fset(self, value) except: print("Attempted to set {} to \"{}\", which is not a valid bool...".format(self.name, value)) -- cgit v1.2.3