]> git.0d.be Git - PanikSwitch.git/blobdiff - FINAL2013V2.ino
factor udp notification into its (inline) function
[PanikSwitch.git] / FINAL2013V2.ino
index a486110e1625f991b2af29dc2c90fa1ffffd9f61..53e3ae9012149215e4d207f662afe70857adbe15 100644 (file)
@@ -107,6 +107,21 @@ IPAddress udp_remote_ip(192, 168, 17, 224);
 typedef enum responseStatus { NO_POST, POST_OK, POST_ERROR };
 
 
+inline void notify_udp()  // notify over UDP
+{
+#ifdef ENABLE_UDP
+  char str_selection[20];
+  snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
+  #ifdef SERIAL_DEBUG
+  Serial.println(F("Sending UDP... "));
+  Serial.println(str_selection);
+  #endif
+  Udp.beginPacket(udp_remote_ip, 1312);
+  Udp.write(str_selection);
+  Udp.endPacket();
+#endif
+}
+
 // web resource
 void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
 {
@@ -130,25 +145,12 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
     while (server.readPOSTparam(name, NAMELEN, value, VALUELEN))
     {
       if (strcmp(name, "s") == 0) {
-        char str_selection[20];
-
         digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
         blinkingSelection = static_cast<switchSelection_t>(atoi(value));
         activeSelection = static_cast<switchSelection_t>(atoi(value));
         response_status = POST_OK;
 
-        #ifdef ENABLE_UDP
-        // notify over UDP
-        snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
-        #ifdef SERIAL_DEBUG
-        Serial.println(F("Sending UDP... "));
-        Serial.println(str_selection);
-        #endif
-        Udp.beginPacket(udp_remote_ip, 1312);
-        Udp.write(str_selection);
-        Udp.endPacket();
-        #endif
-
+        notify_udp();
       }
     }
   }
@@ -291,24 +293,13 @@ void loop()
       #endif
       if (activeSelection != blinkingSelection)
       {
-        char str_selection[20];
         digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
         activeSelection = blinkingSelection;  // relay states must be changed now
         #ifdef SERIAL_DEBUG
         Serial.print(F("Active Selection: "));
         Serial.println(activeSelection);
         #endif
-        #ifdef ENABLE_UDP
-        // notify over UDP
-        snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
-        #ifdef SERIAL_DEBUG
-        Serial.println(F("Sending UDP... "));
-        Serial.println(str_selection);
-        #endif
-        Udp.beginPacket(udp_remote_ip, 1312);
-        Udp.write(str_selection);
-        Udp.endPacket();
-        #endif
+        notify_udp();
         break;
       }
   }