From a5322b2a4812fb6a73d9f0c3ed1e76d93eb15a9d Mon Sep 17 00:00:00 2001 From: farfalleflickan <6597735+farfalleflickan@users.noreply.github.com> Date: Mon, 16 Aug 2021 18:51:35 +0200 Subject: [PATCH] Added support for multiple GPIO pins --- octoprint_hardwarepwm/__init__.py | 79 +++++++++++-------- .../static/js/hardwarepwm.js | 54 +++++++------ .../templates/hardwarepwm_settings.jinja2 | 18 +++-- .../templates/hardwarepwm_tab.jinja2 | 24 ++++-- 4 files changed, 105 insertions(+), 70 deletions(-) diff --git a/octoprint_hardwarepwm/__init__.py b/octoprint_hardwarepwm/__init__.py index caa9181..484e477 100755 --- a/octoprint_hardwarepwm/__init__.py +++ b/octoprint_hardwarepwm/__init__.py @@ -11,11 +11,19 @@ class HardwarepwmPlugin(octoprint.plugin.SettingsPlugin, octoprint.plugin.TemplatePlugin): def __init__(self): - self.IOpin = 19 - self.Freq = 512 - self.dutyCycle = 50 + self.pins=[12, 13, 18, 19] + self.freqs=[0, 0, 0, 0] + self.duties=[0, 0, 0, 0] self.GPIO = pigpio.pi() + def startALL(self): + for i in range(len(self.pins)): + self.startPWM(self.pins[i], self.freqs[i], self.duties[i]) + + def stopALL(self): + for i in range(len(self.pins)): + self.stopPWM(self.pins[i]) + def startPWM(self, pin, hz, percCycle): cycle=int(percCycle*10000) if (self.GPIO.connected): @@ -40,48 +48,44 @@ def shutOffPWM(self): self.GPIO.stop() def get_settings_defaults(self): - return dict( - IOpin=self.IOpin, - Freq=self.Freq, - dutyCycle=self.dutyCycle - ) + return dict(pins=self.pins,freqs=self.freqs,duties=self.duties) def on_after_startup(self): self.getVars() - self.startPWM(self.IOpin, self.Freq, self.dutyCycle) + self.startALL() def on_shutdown(self): - self.stopPWM(self.IOpin); - self.shutOffPWM(); + self.stopALL() + self.shutOffPWM() def getVars(self): - self.IOpin = int(self._settings.get(["IOpin"])) - self.Freq = int(self._settings.get(["Freq"])) - self.dutyCycle = int(self._settings.get(["dutyCycle"])) + self.pins[0] = int(self._settings.get(["pins"])[0]) + self.pins[1] = int(self._settings.get(["pins"])[1]) + self.pins[2] = int(self._settings.get(["pins"])[2]) + self.pins[3] = int(self._settings.get(["pins"])[3]) + self.freqs[0] = int(self._settings.get(["freqs"])[0]) + self.freqs[1] = int(self._settings.get(["freqs"])[1]) + self.freqs[2] = int(self._settings.get(["freqs"])[2]) + self.freqs[3] = int(self._settings.get(["freqs"])[3]) + self.duties[0] = int(self._settings.get(["duties"])[0]) + self.duties[1] = int(self._settings.get(["duties"])[1]) + self.duties[2] = int(self._settings.get(["duties"])[2]) + self.duties[3] = int(self._settings.get(["duties"])[3]) def on_settings_save(self, data): octoprint.plugin.SettingsPlugin.on_settings_save(self, data) - self.stopPWM(self.IOpin) + self.stopALL() self.getVars() - self.startPWM(self.IOpin, self.Freq, self.dutyCycle) + self.startALL() def get_assets(self): - return dict( - js=["js/hardwarepwm.js"] - ) + return dict(js=["js/hardwarepwm.js"]) def get_template_vars(self): - return dict( - IOpin = int(self._settings.get(["IOpin"])), - Freq = int(self._settings.get(["Freq"])), - dutyCycle = int(self._settings.get(["dutyCycle"])) - ) + return dict(pins=self._settings.get(["pins"]), freqs=self._settings.get(["freqs"]), duties=self._settings.get(["duties"])) def get_template_configs(self): - return [ - dict(type="settings", custom_bindings=False) - ] - + return [dict(type="settings", custom_bindings=False)] def get_update_information(self): return dict( @@ -98,16 +102,23 @@ def get_update_information(self): @octoprint.plugin.BlueprintPlugin.route("/setPWM", methods=["POST"]) def set_pwm(self): - new_cycle = int(flask.request.json['val']) - self.dutyCycle=new_cycle - self._settings.set(["dutyCycle"], new_cycle) + duty0=int(flask.request.json['val0']) + duty1=int(flask.request.json['val1']) + duty2=int(flask.request.json['val2']) + duty3=int(flask.request.json['val3']) + self.duties[0]=duty0 + self.duties[1]=duty1 + self.duties[2]=duty2 + self.duties[3]=duty3 + newDuties=[duty0, duty1, duty2, duty3] + self._logger.info("/setPWM args: "+str(duty0)+" "+str(duty1)+" "+str(duty2)+" "+str(duty3)) + self._settings.set(["duties"], newDuties) self._settings.save() + self.stopALL() self.getVars() - self.startPWM(self.IOpin, self.Freq, self.dutyCycle) + self.startALL() return flask.jsonify(success=True) - - __plugin_name__ = "hardwarePWM" __plugin_pythoncompat__ = ">=3,<4" diff --git a/octoprint_hardwarepwm/static/js/hardwarepwm.js b/octoprint_hardwarepwm/static/js/hardwarepwm.js index fa4f430..70ce742 100644 --- a/octoprint_hardwarepwm/static/js/hardwarepwm.js +++ b/octoprint_hardwarepwm/static/js/hardwarepwm.js @@ -6,32 +6,40 @@ */ $(function() { function HardwarepwmViewModel(parameters) { - var self = this; - self.settings = parameters[0]; - self.perc = ko.observable(); + this.settings = parameters[0]; + this.perc0 = ko.observable(); + this.perc1 = ko.observable(); + this.perc2 = ko.observable(); + this.perc3 = ko.observable(); - self.onBeforeBinding = function() { - self.perc(self.settings.settings.plugins.hardwarepwm.dutyCycle()); + this.onBeforeBinding = function() { + this.perc0(this.settings.settings.plugins.hardwarepwm.duties()[0]); + this.perc1(this.settings.settings.plugins.hardwarepwm.duties()[1]); + this.perc2(this.settings.settings.plugins.hardwarepwm.duties()[2]); + this.perc3(this.settings.settings.plugins.hardwarepwm.duties()[3]); } - self.handlePWM = function (item) { - var pwmVal = item.perc(); - if (pwmVal < 0 || pwmVal > 100 || isNaN(pwmVal)) { - new PNotify({ - title: "hardwarePWM", - text: "Duty cycle value needs to be between 0 and 100!", - type: "error" - }); - } else { - $.ajax({ - type: "POST", - contentType: "application/json; charset=utf-8", - dataType: "json", - data: JSON.stringify({"val":pwmVal}), - url: window.PLUGIN_BASEURL + "hardwarepwm/setPWM" - }); - } - }; + this.handlePWM = function (item) { + var pwmVal0 = parseInt(item.perc0()); + var pwmVal1 = parseInt(item.perc1()); + var pwmVal2 = parseInt(item.perc2()); + var pwmVal3 = parseInt(item.perc3()); + if (pwmVal0 < 0 || pwmVal0 > 100 || isNaN(pwmVal0) || pwmVal1 < 0 || pwmVal1 > 100 || isNaN(pwmVal1) || pwmVal2 < 0 || pwmVal2 > 100 || isNaN(pwmVal2) || pwmVal3 < 0 || pwmVal3 > 100 || isNaN(pwmVal3)) { + new PNotify({ + title: "hardwarePWM", + text: "Duty cycle value needs to be between 0 and 100!", + type: "error" + }); + } else { + $.ajax({ + type: "POST", + contentType: "application/json; charset=utf-8", + dataType: "json", + data: JSON.stringify({"val0":pwmVal0, "val1":pwmVal1, "val2":pwmVal2, "val3":pwmVal3}), + url: window.PLUGIN_BASEURL + "hardwarepwm/setPWM" + }); + } + }; } OCTOPRINT_VIEWMODELS.push({ diff --git a/octoprint_hardwarepwm/templates/hardwarepwm_settings.jinja2 b/octoprint_hardwarepwm/templates/hardwarepwm_settings.jinja2 index b9a56b8..359a355 100644 --- a/octoprint_hardwarepwm/templates/hardwarepwm_settings.jinja2 +++ b/octoprint_hardwarepwm/templates/hardwarepwm_settings.jinja2 @@ -1,20 +1,26 @@
diff --git a/octoprint_hardwarepwm/templates/hardwarepwm_tab.jinja2 b/octoprint_hardwarepwm/templates/hardwarepwm_tab.jinja2 index 35886c7..f4aa01b 100644 --- a/octoprint_hardwarepwm/templates/hardwarepwm_tab.jinja2 +++ b/octoprint_hardwarepwm/templates/hardwarepwm_tab.jinja2 @@ -1,9 +1,19 @@