Patch for adding "toggle" command in Switch plugin

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Waldmensch
Normal user
Posts: 11
Joined: 28 Nov 2016, 17:12

Patch for adding "toggle" command in Switch plugin

#1 Post by Waldmensch » 02 Jan 2017, 15:09

Have fun

Code: Select all

From bd1d99e3cf9553fe189b9f290f2af1ba616a4b40 Mon Sep 17 00:00:00 2001
From: Tommy-LSA <>
Date: Mon, 2 Jan 2017 14:10:07 +0100
Subject: [PATCH] Add "toggle" command to P001_Switch plugin

Add "toggle" command to P001_Switch plugin
---
 _P001_Switch.ino | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/_P001_Switch.ino b/_P001_Switch.ino
index e465ea8..db8ab1d 100644
--- a/_P001_Switch.ino
+++ b/_P001_Switch.ino
@@ -226,6 +226,31 @@ boolean Plugin_001(byte function, struct EventStruct *event, String& string)
           }
         }
 
+        if (command == F("toggle"))
+        {
+          success = true;
+          if (event->Par1 >= 0 && event->Par1 <= 16)
+          {
+            // identify Pinmode
+            uint32_t bit = digitalPinToBitMask(event->Par1);
+            uint8_t port = digitalPinToPort(event->Par1);
+            volatile uint32_t *reg = portModeRegister(port);
+            
+            if (*reg & bit) {
+              // Pin is OUTPUT - Toggle
+              digitalWrite(event->Par1, !digitalRead(event->Par1));
+            } else {
+              // Pin is !OUTPUT -  set to OUTPUT and enable
+              pinMode(event->Par1, OUTPUT);
+              digitalWrite(event->Par1, 1);
+            }
+          }
+          setPinState(PLUGIN_ID_001, event->Par1, PIN_MODE_OUTPUT, digitalRead(event->Par1));
+          log = String(F("SW   : GPIO ")) + String(event->Par1) + String(F(" toggle to ")) + String(digitalRead(event->Par1));
+          addLog(LOG_LEVEL_INFO, log);
+          SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_001, event->Par1, log, 0));
+        }
+        
         if (command == F("pwm"))
         {
           success = true;

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest