From 30ee76bf7e1f7d9d0a7dca6a5a92df691c9e7800 Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 03:52:55 +1000 Subject: [PATCH 1/8] add a limiter --- octoprint_fanspeedslider/__init__.py | 54 ++++++++++++++++--- .../static/js/fanslider.js | 8 +-- .../templates/fanspeedslider_settings.jinja2 | 30 +++++++++-- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 7cb3e58..2b6be64 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -1,6 +1,8 @@ # coding=utf-8 from __future__ import absolute_import +from decimal import * +import re import octoprint.plugin class FanSliderPlugin(octoprint.plugin.StartupPlugin, @@ -8,8 +10,24 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.SettingsPlugin, octoprint.plugin.AssetPlugin): + def on_after_startup(self): + self.fanSpeed = self._settings.get(["fanSpeed"]) + self.minSpeed = self._settings.get(["minSpeed"]) + self.maxSpeed = self._settings.get(["maxSpeed"]) + def get_settings_defaults(self): - return dict(fanSpeed=100) + return dict( + fanSpeed=100, + minSpeed=0, + maxSpeed=100 + ) + + def on_settings_save(self, data): + octoprint.plugin.SettingsPlugin.on_settings_save(self, data) + + self.fanSpeed = self._settings.get(["fanSpeed"]) + self.minSpeed = self._settings.get(["minSpeed"]) + self.maxSpeed = self._settings.get(["maxSpeed"]) def get_assets(self): return dict( @@ -20,12 +38,29 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, def get_template_configs(self): return [ dict(type="settings", custom_bindings=False) - ] + ] + + def rewrite_m106(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs): + if gcode and gcode.startswith('M106'): + getcontext().prec = 5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? + self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(255) / Decimal(100) ) + self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(255) / Decimal(100) ) + fanPwm = re.search("S(\d+.\d+)", cmd) + if fanPwm and fanPwm.group(1): + fanPwm = fanPwm.group(1) + if Decimal(fanPwm) < self.minPWM and Decimal(fanPwm) != 0: + self._logger.info("fan pwm value " + str(fanPwm) + " is below threshold, increasing to " + str(self.minPWM)) + cmd = "M106 S" + str(self.minPWM) + return cmd, + elif Decimal(fanPwm) > self.maxPWM: + self._logger.info("fan pwm value " + str(fanPwm) + " is above threshold, decreasing to " + str(self.maxPWM)) + cmd = "M106 S" + str(self.maxPWM) + return cmd, def get_update_information(self): return dict( fanspeedslider=dict( - displayName="Fan Speed Slider", + displayName="Fan Speed Control", displayVersion=self._plugin_version, # version check: github repository @@ -38,8 +73,15 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, pip="https://github.com/ntoff/OctoPrint-FanSpeedSlider/archive/{target_version}.zip" ) ) -__plugin_name__ = "Fan Speed Slider" -__plugin_implementation__ = FanSliderPlugin() -__plugin_hooks__ = { + +__plugin_name__ = "Fan Speed Control" + +def __plugin_load__(): + global __plugin_implementation__ + __plugin_implementation__ = __plugin_implementation__ = FanSliderPlugin() + + global __plugin_hooks__ + __plugin_hooks__ = { + "octoprint.comm.protocol.gcode.queuing": __plugin_implementation__.rewrite_m106, "octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information } \ No newline at end of file diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index efcd923..1a431f1 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -12,11 +12,13 @@ $(function() { self.control = parameters[2]; self.settings = parameters[3]; - //default to 100% fan speed - fanSpeed = ko.observable(undefined); + fanSpeed = ko.observable(undefined); + //convert percentage into PWM fanPWM = ko.pureComputed(function () { - return Math.round(fanSpeed() * 255 / 100); + self.speed = fanSpeed() * 255 / 100 + + return self.speed; }); //send gcode to set fan speed sendFanSpeed = function () { diff --git a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 index b23d2cf..4ca7b1f 100644 --- a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 +++ b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 @@ -1,9 +1,7 @@ -

{{ _('Fan Speed Slider') }}

-
-

{{ _('Set the default value for the fan speed slider.') }}

-
+

{{ _('Fan Speed Control') }}

+

{{ _('Set the default value for the speed slider.') }}

@@ -11,8 +9,30 @@ %
- {{ _('This value does not affect the maximum or minimum speed of the fan. To limit the power the fan will use during a print, see your slicer\'s min / max fan speed setting.') }} + {{ _('The default value the slider will be set to when opening OctoPrint\'s UI') }}
+

{{ _('The settings below can be used to limit the fan\'s output, this is useful if your slicer doesn\'t allow min / max fan speeds.') }}

+
+ +
+
+ + % +
+ {{ _('Any value set by the slicer which is below this value will be increased to meet this speed.') }} +
+
+
+ +
+
+ + % +
+ {{ _('Any value set by the slicer which is above this value will be decreased to meet this speed.') }} +
+
+

{{ _('NOTE: The min/max setting has no effect when you are printing from an SD card attached directly to the printer as the gcode does not pass through OctoPrint. In the case of printing from SD, you will be required to use a slicer that allows for seting the min / max speed.') }}

From 2cc132213f4471faead65fd76e282f29a3712e34 Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 09:37:05 +1000 Subject: [PATCH 2/8] added some comments --- octoprint_fanspeedslider/__init__.py | 5 +++-- octoprint_fanspeedslider/static/js/fanslider.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 2b6be64..4f836ee 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -43,8 +43,9 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, def rewrite_m106(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs): if gcode and gcode.startswith('M106'): getcontext().prec = 5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? - self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(255) / Decimal(100) ) - self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(255) / Decimal(100) ) + self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(255) / Decimal(100) ) #convoluted mess, could this be reduced to a function + self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(255) / Decimal(100) ) #so basically the same thing isn't written twice? + #Also move it out of here so it doesn't get calculated every single time the speed is rewritten fanPwm = re.search("S(\d+.\d+)", cmd) if fanPwm and fanPwm.group(1): fanPwm = fanPwm.group(1) diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index 1a431f1..a72f340 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -16,7 +16,7 @@ $(function() { //convert percentage into PWM fanPWM = ko.pureComputed(function () { - self.speed = fanSpeed() * 255 / 100 + self.speed = fanSpeed() * 255 / 100 //don't forget to limit this to 2 decimal places at some point. return self.speed; }); From 525a08c20e09e9d95f4225b808e08954ef41ff56 Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 10:56:39 +1000 Subject: [PATCH 3/8] update settings page help text --- .../templates/fanspeedslider_settings.jinja2 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 index 4ca7b1f..7835a30 100644 --- a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 +++ b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 @@ -12,7 +12,7 @@ {{ _('The default value the slider will be set to when opening OctoPrint\'s UI') }} -

{{ _('The settings below can be used to limit the fan\'s output, this is useful if your slicer doesn\'t allow min / max fan speeds.') }}

+

{{ _('The settings below can be used to limit the fan\'s output without having to re-slice and re-upload your gcode.') }}

@@ -20,7 +20,7 @@ %
- {{ _('Any value set by the slicer which is below this value will be increased to meet this speed.') }} + {{ _('Any value sent which is below this value will be increased to meet this speed. Useful if your fan doesn\'t work below a certain threshold.') }}
@@ -30,9 +30,9 @@ %
- {{ _('Any value set by the slicer which is above this value will be decreased to meet this speed.') }} + {{ _('Any value sent which is above this value will be decreased to meet this speed. Useful if your fan is too strong on full speed.') }} -

{{ _('NOTE: The min/max setting has no effect when you are printing from an SD card attached directly to the printer as the gcode does not pass through OctoPrint. In the case of printing from SD, you will be required to use a slicer that allows for seting the min / max speed.') }}

+

{{ _('NOTE: The min/max setting has no effect when you are printing from an SD card that is attached directly to the printer as the gcode does not pass through OctoPrint.') }}

From 554d78efb75d018388c956f823b8b544541ff1cb Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 10:57:55 +1000 Subject: [PATCH 4/8] make same code into a function --- octoprint_fanspeedslider/__init__.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 4f836ee..a2f2b6c 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -11,9 +11,7 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.AssetPlugin): def on_after_startup(self): - self.fanSpeed = self._settings.get(["fanSpeed"]) - self.minSpeed = self._settings.get(["minSpeed"]) - self.maxSpeed = self._settings.get(["maxSpeed"]) + self.get_settings_updates() def get_settings_defaults(self): return dict( @@ -24,10 +22,7 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, def on_settings_save(self, data): octoprint.plugin.SettingsPlugin.on_settings_save(self, data) - - self.fanSpeed = self._settings.get(["fanSpeed"]) - self.minSpeed = self._settings.get(["minSpeed"]) - self.maxSpeed = self._settings.get(["maxSpeed"]) + self.get_settings_updates() def get_assets(self): return dict( @@ -40,12 +35,17 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, dict(type="settings", custom_bindings=False) ] + def get_settings_updates(self): + self.fanSpeed = self._settings.get(["fanSpeed"]) + self.minSpeed = self._settings.get(["minSpeed"]) + self.maxSpeed = self._settings.get(["maxSpeed"]) + + getcontext().prec=5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? + self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(2.55) ) + self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(2.55) ) + def rewrite_m106(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs): if gcode and gcode.startswith('M106'): - getcontext().prec = 5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? - self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(255) / Decimal(100) ) #convoluted mess, could this be reduced to a function - self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(255) / Decimal(100) ) #so basically the same thing isn't written twice? - #Also move it out of here so it doesn't get calculated every single time the speed is rewritten fanPwm = re.search("S(\d+.\d+)", cmd) if fanPwm and fanPwm.group(1): fanPwm = fanPwm.group(1) From 6124bba2e7eb52582466e3f614895a1e6a4f0480 Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 11:13:39 +1000 Subject: [PATCH 5/8] try rounding --- octoprint_fanspeedslider/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index a2f2b6c..7442a2b 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -41,8 +41,8 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, self.maxSpeed = self._settings.get(["maxSpeed"]) getcontext().prec=5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? - self.minPWM = Decimal( Decimal(self.minSpeed) * Decimal(2.55) ) - self.maxPWM = Decimal( Decimal(self.maxSpeed) * Decimal(2.55) ) + self.minPWM = round( Decimal(self.minSpeed) * Decimal(2.55), 2 ) + self.maxPWM = round( Decimal(self.maxSpeed) * Decimal(2.55), 2 ) def rewrite_m106(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs): if gcode and gcode.startswith('M106'): From b03a045b24c713d60654271ed7ae7c1264bcc0c4 Mon Sep 17 00:00:00 2001 From: ntoff Date: Tue, 5 Sep 2017 22:57:53 +1000 Subject: [PATCH 6/8] self fanpwn --- octoprint_fanspeedslider/static/js/fanslider.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index a72f340..0b07aca 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -7,22 +7,20 @@ $(function() { function FanSliderPluginViewModel(parameters) { var self = this; - self.printerstate = parameters[0]; - self.loginstate = parameters[1]; - self.control = parameters[2]; - self.settings = parameters[3]; + self.settings = parameters[0]; + self.control = parameters[1]; + self.loginState = parameters[2]; fanSpeed = ko.observable(undefined); //convert percentage into PWM - fanPWM = ko.pureComputed(function () { + self.fanPWM = ko.pureComputed(function () { self.speed = fanSpeed() * 255 / 100 //don't forget to limit this to 2 decimal places at some point. - return self.speed; }); //send gcode to set fan speed sendFanSpeed = function () { - self.control.sendCustomCommand({ command: "M106 S" + fanPWM() }); + self.control.sendCustomCommand({ command: "M106 S" + self.fanPWM() }); }; //extra classes $("#control > div.jog-panel").eq(0).addClass("controls"); @@ -61,7 +59,6 @@ $(function() { } OCTOPRINT_VIEWMODELS.push([ FanSliderPluginViewModel, - - ["printerStateViewModel", "loginStateViewModel", "controlViewModel", "settingsViewModel"] + ["settingsViewModel", "controlViewModel", "loginStateViewModel"] ]); }); \ No newline at end of file From 988512f522e98f0ac988d979b04483fcf052471f Mon Sep 17 00:00:00 2001 From: ntoff Date: Wed, 6 Sep 2017 17:52:23 +1000 Subject: [PATCH 7/8] rename default fan speed variable to be more clear --- octoprint_fanspeedslider/__init__.py | 4 ++-- octoprint_fanspeedslider/static/js/fanslider.js | 2 +- .../templates/fanspeedslider_settings.jinja2 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 7442a2b..09d104b 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -15,7 +15,7 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, def get_settings_defaults(self): return dict( - fanSpeed=100, + defaultFanSpeed=100, minSpeed=0, maxSpeed=100 ) @@ -36,7 +36,7 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, ] def get_settings_updates(self): - self.fanSpeed = self._settings.get(["fanSpeed"]) + self.defaultFanSpeed = self._settings.get(["defaultFanSpeed"]) self.minSpeed = self._settings.get(["minSpeed"]) self.maxSpeed = self._settings.get(["maxSpeed"]) diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index 0b07aca..192e9c9 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -54,7 +54,7 @@ $(function() { } //retrieve settings self.onBeforeBinding = function() { - fanSpeed(self.settings.settings.plugins.fanspeedslider.fanSpeed()); + fanSpeed(self.settings.settings.plugins.fanspeedslider.defaultFanSpeed()); } } OCTOPRINT_VIEWMODELS.push([ diff --git a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 index 7835a30..c05a308 100644 --- a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 +++ b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 @@ -6,7 +6,7 @@
- + %
{{ _('The default value the slider will be set to when opening OctoPrint\'s UI') }} From 6699679ca211f4e4747268b51d784b21c49f4790 Mon Sep 17 00:00:00 2001 From: ntoff Date: Mon, 25 Sep 2017 06:22:27 +1000 Subject: [PATCH 8/8] update version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1c9c839..f1ce404 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_package = "octoprint_fanspeedslider" plugin_name = "OctoPrint-FanSpeedSlider" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.1.2" +plugin_version = "0.1.3" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module