From 28edc70855d2a39a1e6777ea8f3fa39562b6c95c Mon Sep 17 00:00:00 2001 From: ntoff Date: Fri, 7 Dec 2018 03:29:09 +1000 Subject: [PATCH] Add option to remember last fan speed Add the option to remember the last speed value and set the slider on load/refresh --- README.md | 2 ++ octoprint_fanspeedslider/__init__.py | 8 +++++++- octoprint_fanspeedslider/static/js/fanslider.js | 16 +++++++++++++++- .../templates/fanspeedslider_settings.jinja2 | 9 +++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d9a1c09..fd27b78 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Slide the slider, click the button. There really isn't much else to do :) * The default value of the slider is user configurable, this is the value that the slider will be set to upon loading OctoPrint's UI, and any time you refresh the page. +* The remember last speed checkbox will tell the plugin to save the fan speed as it gets sent to the printer, and set the slider to that value on load / refresh (overrides the default value setting). + * The minimum fan speed setting will limit how slow the fan runs, this is useful since some fans don't work below a certain speed. * The maximum fan speed setting will limit how fast the fan runs, this is useful if your fan is too strong, or you wish to limit the speed post-slice without having to re-slice your file. diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 7524879..5b196cf 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -22,7 +22,9 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, defaultFanSpeed=100, minSpeed=0, maxSpeed=100, - notifyDelay=4000 + notifyDelay=4000, + lastSentSpeed=0, + defaultLastSpeed=False ) def on_settings_save(self, data): @@ -35,6 +37,10 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, s.setInt(["maxSpeed"], data["maxSpeed"]) if "notifyDelay" in data.keys(): s.setInt(["notifyDelay"], data["notifyDelay"]) + if "lastSentSpeed" in data.keys(): + s.setInt(["lastSentSpeed"], data["lastSentSpeed"]) + if "defaultLastSpeed" in data.keys(): + s.set(["defaultLastSpeed"], data["defaultLastSpeed"]) self.get_settings_updates() #clean up settings if everything's default self.on_settings_cleanup() diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index 1ce241c..d4042e3 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -17,13 +17,16 @@ $(function () { self.settings.minFanSpeed = new ko.observable(0); //this, self.settings.maxFanSpeed = new ko.observable(100); //and this are percents 0 - 100% self.settings.notifyDelay = new ko.observable(4000); //time in milliseconds + self.settings.defaultLastSpeed = new ko.observable(false); //options page option to set the slider to the last sent fan speed value on load/refresh + self.settings.lastSentSpeed = new ko.observable(null); //the last speed value that was sent to the printer self.settings.commonTitle = ko.observable(gettext("\n\nThis allows limiting the cooling fan without having to re-slice your model.\n\nLimited to prints controlled by OctoPrint.")); self.settings.defaultTitle = ko.observable(gettext("This is the value the slider will default to when the UI is loaded / refreshed.")); self.settings.minTitle = ko.observable(gettext("Set this to the lowest value at which your fan will spin.") + self.settings.commonTitle()); self.settings.maxTitle = ko.observable(gettext("Set this <100% if your cooling fan is too strong on full.") + self.settings.commonTitle()); self.settings.noticeTitle = ko.observable(gettext("Notifications only apply when setting the speed via the slider + button in the UI. Set to 0 (zero) to disable notifications.")); - + self.settings.lastspeedTitle = ko.observable(gettext("Instead of defaulting to the speed set by \"Default Value\", the slider will be set to the last sent speed on load / refresh. \n\n Note: It takes into account the min/max value setting and overrides the \"Default Value\" setting.")); + self.showNotify = function (self, options) { options.hide = true; options.title = "Fan Speed Control"; @@ -68,6 +71,12 @@ $(function () { self.control.sendFanSpeed = function () { self.control.checkSliderValue(); self.control.sendCustomCommand({ command: "M106 S" + self.control.fanSpeedToPwm() }); + + if (self.settings.defaultLastSpeed()) { + self.settings.settings.plugins.fanspeedslider.lastSentSpeed(self.control.fanSpeed()); + self.settings.saveData(); + self.updateSettings(); + } }; //ph34r @@ -109,6 +118,7 @@ $(function () { self.settings.minFanSpeed(parseInt(self.settings.settings.plugins.fanspeedslider.minSpeed())); self.settings.maxFanSpeed(parseInt(self.settings.settings.plugins.fanspeedslider.maxSpeed())); self.settings.notifyDelay(parseInt(self.settings.settings.plugins.fanspeedslider.notifyDelay())); + self.settings.defaultLastSpeed(self.settings.settings.plugins.fanspeedslider.defaultLastSpeed()); } catch (error) { console.log(error); @@ -117,6 +127,7 @@ $(function () { self.onBeforeBinding = function () { self.settings.defaultFanSpeed(parseInt(self.settings.settings.plugins.fanspeedslider.defaultFanSpeed())); + self.settings.lastSentSpeed(parseInt(self.settings.settings.plugins.fanspeedslider.lastSentSpeed())); self.updateSettings(); //if the default fan speed is above or below max/min then set to either max or min if (self.settings.defaultFanSpeed() < self.settings.minFanSpeed()) { @@ -125,6 +136,9 @@ $(function () { else if (self.settings.defaultFanSpeed() > self.settings.maxFanSpeed()) { self.control.fanSpeed(self.settings.maxFanSpeed()); } + else if (self.settings.defaultLastSpeed()) { + self.control.fanSpeed(self.settings.lastSentSpeed()); + } else { self.control.fanSpeed(self.settings.defaultFanSpeed()); } diff --git a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 index b2811b6..1d6cfe3 100644 --- a/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 +++ b/octoprint_fanspeedslider/templates/fanspeedslider_settings.jinja2 @@ -11,6 +11,15 @@ {{ _('The default value the slider will be set to when opening OctoPrint\'s UI') }} +
+ +
+
+ +
+ {{ _('Instead of the default speed value, the slider will use the last sent speed as the default value') }} +
+