diff --git a/README.md b/README.md index d76ee2c..d9a1c09 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,15 @@ Add a slider to control the speed of a parts cooling fan. 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. +## Settings -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 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 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. +* 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. + +* Notification autohide delay controls how long any notifications will remain on the screen for. If the user manually sets a speed outside of the set range, a notification will be displayed informing the user the fan speed has been modified. Print jobs shouldn't trigger these notifications, and so popup spam shouldn't occur, however if a user wishes not to receive notifications when setting fan speeds outside of the set range, this value can be set to 0 (zero) to disable notifications. (this setting won't / shouldn't affect OctoPrint's global notifications, it only applies to info popups generated by this plugin). *Note: Slider does __not__ follow the speed of the fan. If the fan speed is set via gcode or an LCD panel on the printer, the slider will not respond to the change. It is a __setting__, not an indicator, and functions the same way the feedrate and flowrate sliders do.* diff --git a/octoprint_fanspeedslider/__init__.py b/octoprint_fanspeedslider/__init__.py index 09d104b..9e7c3f9 100644 --- a/octoprint_fanspeedslider/__init__.py +++ b/octoprint_fanspeedslider/__init__.py @@ -17,12 +17,49 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, return dict( defaultFanSpeed=100, minSpeed=0, - maxSpeed=100 + maxSpeed=100, + notifyDelay=4000 ) def on_settings_save(self, data): - octoprint.plugin.SettingsPlugin.on_settings_save(self, data) + s = self._settings + if "defaultFanSpeed" in data.keys(): + s.setInt(["defaultFanSpeed"], data["defaultFanSpeed"]) + if "minSpeed" in data.keys(): + s.setInt(["minSpeed"], data["minSpeed"]) + if "maxSpeed" in data.keys(): + s.setInt(["maxSpeed"], data["maxSpeed"]) + if "notifyDelay" in data.keys(): + s.setInt(["notifyDelay"], data["notifyDelay"]) self.get_settings_updates() + #clean up settings if everything's default + self.on_settings_cleanup() + s.save() + + #function stolen...err borrowed :D from types.py @ 1663 + def on_settings_cleanup(self): + import octoprint.util + from octoprint.settings import NoSuchSettingsPath + + try: + config = self._settings.get_all_data(merged=False, incl_defaults=False, error_on_path=True) + except NoSuchSettingsPath: + return + + if config is None: + self._settings.clean_all_data() + return + + if self.config_version_key in config and config[self.config_version_key] is None: + del config[self.config_version_key] + + defaults = self.get_settings_defaults() + diff = octoprint.util.dict_minimal_mergediff(defaults, config) + + if not diff: + self._settings.clean_all_data() + else: + self._settings.set([], diff) def get_assets(self): return dict( @@ -36,9 +73,9 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, ] def get_settings_updates(self): - self.defaultFanSpeed = self._settings.get(["defaultFanSpeed"]) - self.minSpeed = self._settings.get(["minSpeed"]) - self.maxSpeed = self._settings.get(["maxSpeed"]) + self.defaultFanSpeed = self._settings.getInt(["defaultFanSpeed"]) + self.minSpeed = self._settings.getInt(["minSpeed"]) + self.maxSpeed = self._settings.getInt(["maxSpeed"]) getcontext().prec=5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas? self.minPWM = round( Decimal(self.minSpeed) * Decimal(2.55), 2 ) @@ -50,11 +87,11 @@ class FanSliderPlugin(octoprint.plugin.StartupPlugin, 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)) + self._logger.info("fan pwm value " + str(fanPwm) + " is below threshold, increasing to " + str(self.minPWM) + " (" + str(self.minSpeed) + "%)") 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)) + self._logger.info("fan pwm value " + str(fanPwm) + " is above threshold, decreasing to " + str(self.maxPWM) + " (" + str(self.maxSpeed) + "%)") cmd = "M106 S" + str(self.maxPWM) return cmd, diff --git a/octoprint_fanspeedslider/static/js/fanslider.js b/octoprint_fanspeedslider/static/js/fanslider.js index e4f610c..878cf68 100644 --- a/octoprint_fanspeedslider/static/js/fanslider.js +++ b/octoprint_fanspeedslider/static/js/fanslider.js @@ -2,63 +2,143 @@ * Author: ntoff * License: AGPLv3 */ -$(function() { +$(function () { function FanSliderPluginViewModel(parameters) { + //'use strict'; var self = this; self.settings = parameters[0]; self.control = parameters[1]; self.loginState = parameters[2]; - fanSpeed = ko.observable(undefined); + self.settings.defaultFanSpeed = new ko.observable(100); //this, + self.control.fanSpeed = new ko.observable(100); //this, + 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 - //convert percentage into PWM - self.fanPWM = ko.pureComputed(function () { - self.speed = fanSpeed() * 255 / 100 //don't forget to limit this to 2 decimal places at some point. + self.showNotify = function (self, options) { + options.hide = true; + options.title = "Fan Speed Control"; + options.delay = self.settings.notifyDelay(); + options.type = "info"; + if (options.delay != "0") { + new PNotify(options); + } + }; + + self.control.fanSpeedToPwm = ko.pureComputed(function () { + self.speed = self.control.fanSpeed() * 255 / 100 //don't forget to limit this to 2 decimal places at some point. return self.speed; }); + + self.control.checkSliderValue = ko.pureComputed(function () { + if (self.control.fanSpeed() < self.settings.minFanSpeed() && self.control.fanSpeed() != "0") { + console.log("Fan Speed Control Plugin: " + self.control.fanSpeed() + "% is less than the minimum speed (" + self.settings.minFanSpeed() + "%), increasing."); + self.control.fanSpeed(self.settings.minFanSpeed()); + var options = { + text: gettext('Fan speed increased to meet minimum speed requirement.'), + addclass: 'fan_speed_notice_low', + } + if ($(".fan_speed_notice_low").length <1) { + self.showNotify(self, options); + } + } + else if (self.control.fanSpeed() > self.settings.maxFanSpeed()) { + console.log("Fan Speed Control Plugin: " + self.control.fanSpeed() + "% is more than the maximum speed (" + self.settings.maxFanSpeed() + "%), decreasing."); + self.control.fanSpeed(self.settings.maxFanSpeed()); + var options = { + text: gettext('Fan speed decreased to meet maximum speed requirement.'), + addclass: 'fan_speed_notice_high', + } + if ($(".fan_speed_notice_high").length <1) { + self.showNotify(self, options); + } + } + }); + //send gcode to set fan speed - sendFanSpeed = function () { - self.control.sendCustomCommand({ command: "M106 S" + self.fanPWM() }); - }; - //extra classes - $("#control > div.jog-panel").eq(0).addClass("controls"); - $("#control > div.jog-panel").eq(1).addClass("tools"); - $("#control > div.jog-panel").eq(2).addClass("general"); - //If !TouchUI then remove standard buttons + add slider + new buttons - if ($("#touch body").length ==0 ) { - //add ID to buttons - $("#control > div.general").find("button").eq(0).attr("id", "motors-off"); - $("#control > div.general").find("button").eq(1).attr("id", "fan-on"); - $("#control > div.general").find("button").eq(2).attr("id", "fan-off"); - //remove original fan on/off buttons - $("#fan-on").remove(); - $("#fan-off").remove(); - //add new fan controls - $("#control > div.jog-panel.general").find("button").eq(0).before("\ - \ - \ - \ - "); - } else { //if TouchUI is active we only add the speed input + fan on button in a new section. - console.log("Fan Speed Slider: NOTICE! TouchUI is active, adding simplified control."); - $("#control > div.jog-panel.general").after("\ -