]> git.0d.be Git - PanikSwitch.git/blobdiff - FINAL2013V2.ino
use a simple int for active selection
[PanikSwitch.git] / FINAL2013V2.ino
index 830e97dd718779f15d77b7a34169d483c1ae592a..c9f3829e6d9a0a00e13d93daf013b8b09181430f 100644 (file)
@@ -1,25 +1,26 @@
 // Panik Web Form Demo
 
+#define SERIAL_DEBUG
+#define ENABLE_UDP
 
 
 // define network constants
 #define MAC_DAVID { 0x90, 0xA2, 0xDA, 0x0D, 0xF4, 0x63 }
 #define MAC_PANIK { 0x90, 0xA2, 0xDA, 0x00, 0x9A, 0x94 }
 #define MAC_PANIK2 { 0x90, 0xA2, 0xDA, 0x0E, 0xF3, 0x77 }
-#define MAC_ADDRESS MAC_PANIK
-
+#define MAC_ADDRESS MAC_PANIK2
 
 // include Arduino libraries
 #include <Wire.h>
 #include <SPI.h>
 #include <SD.h>
 #include <Ethernet.h>
+#ifdef ENABLE_UDP
+#include <EthernetUdp.h>
+#endif
 
 // include third party libraries
-#include <WebServer.h>
-
-// include in-house libs
-#include "PanikSwitch.h"    // contains variable types for panik switch
+#include "WebServer.h"
 
 // define SD and Ethernet select ports
 #define  SD_SELECT            4
 #define NONSTOP_VIA_STUD1       14   // is nonstop coming out of studio 1
 #define NONSTOP_VIA_STUD2       15   // is nonstop coming out of studio 2
 
+/* status */
+#define NONSTOP 0
+#define STUDIO1 1
+#define STUDIO2 2
+
+
+/* relay positions */
+typedef enum { RELAY_STATE_OPEN, RELAY_STATE_CLOSED } relayState_t;
 
 
 /* define an array with led pins,
@@ -58,10 +67,10 @@ const int ledsArray[] = { RELAY_GREEN_LEDS, RELAY_RED_LEDS, RELAY_YELLOW_LEDS };
 /* activeSelection is the variable that holds the active output of the switch
    it is declared as an attribute that keeps its value between arduino resets
 */
-switchSelection_t activeSelection __attribute__ ((section (".noinit")));
+int activeSelection = NONSTOP;
 
 // variables and timers for blinking selection (selected but not confirmed)
-switchSelection_t blinkingSelection = nonstop;
+int blinkingSelection = NONSTOP;
 bool blinkingLedState = RELAY_STATE_CLOSED;
 unsigned long blinkingStartTime = 0, blinkingAbortTime = 0;
 
@@ -90,12 +99,34 @@ static uint8_t mac[] = MAC_ADDRESS;
 // instanciate web server
 WebServer webserver(PREFIX, 80);
 
+#ifdef ENABLE_UDP
+// and EthernetUDP instance to send notifications
+EthernetUDP Udp;
+IPAddress udp_remote_ip(192, 168, 17, 224);
+#endif
+
+
 #define NAMELEN 4
 #define VALUELEN 4
 
 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)
 {
@@ -109,7 +140,6 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
 
   if (type == WebServer::POST)
   {
-    return;
     char name[NAMELEN];
     int  name_len;
     char value[VALUELEN];
@@ -121,9 +151,11 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
     {
       if (strcmp(name, "s") == 0) {
         digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
-        blinkingSelection = static_cast<switchSelection_t>(atoi(value));
-        activeSelection = static_cast<switchSelection_t>(atoi(value));
+        blinkingSelection = atoi(value);
+        activeSelection = atoi(value);
         response_status = POST_OK;
+
+        notify_udp();
       }
     }
   }
@@ -148,10 +180,7 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
 // setup function
 void setup()
 {
-  // make sure that activeSelection holds something valid
-//  if (!(activeSelection > nonstop) || !(activeSelection < _NbrSwitchSelections))
-//    activeSelection = nonstop;
-  blinkingSelection = activeSelection = nonstop;
+  blinkingSelection = activeSelection = NONSTOP;
 
   // open serial communication for debugging
   Serial.begin(9600);
@@ -171,9 +200,18 @@ void setup()
     Serial.println(Ethernet.localIP());
   else
   {
+    #ifdef SERIAL_DEBUG
     Serial.println(F("Network Error"));
+    #endif
     while (1) ;
   }
+  #ifdef ENABLE_UDP
+  if (! Udp.begin(1312)) {
+    #ifdef SERIAL_DEBUG
+    Serial.println(F("Failed to initiate UDP"));
+    #endif
+  }
+  #endif
 
   // set mode for used pins
   pinMode(RELAY_RED_LEDS, OUTPUT);
@@ -227,15 +265,22 @@ void loop()
     case nochange:
       break;
     case pressed:
-      //Serial.println(F("Button 1 pressed"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 1 pressed"));
+      #endif
       digitalWrite(ledsArray[blinkingSelection], RELAY_STATE_CLOSED);
       blinkingSelection++;
+      if (blinkingSelection > STUDIO2) {
+        blinkingSelection = NONSTOP;
+      }
       blinkingStartTime = millis(),
       blinkingAbortTime = millis();
       blinkingLedState = RELAY_STATE_OPEN;
       break;
     case released:
-      //Serial.println(F("Button 1 released"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 1 released"));
+      #endif
       break;
   }
 
@@ -245,17 +290,24 @@ void loop()
     case nochange:
       break;
     case pressed:
-      //Serial.println(F("Button 2 pressed"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 2 pressed"));
+      #endif
       blinkingAbortTime = 0;        // disable blinking auto abort
       break;
     case released:
-      //Serial.println(F("Button 2 released"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 2 released"));
+      #endif
       if (activeSelection != blinkingSelection)
       {
         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
+        notify_udp();
         break;
       }
   }
@@ -284,17 +336,17 @@ void loop()
   digitalWrite(ledsArray[activeSelection], RELAY_STATE_OPEN);
 
   // update audio channel relays
-  if (activeSelection == nonstop)
+  if (activeSelection == NONSTOP)
   {
     digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_CLOSED);
     digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_CLOSED);
   }
-  else if (activeSelection == studio1)
+  else if (activeSelection == STUDIO1)
   {
     digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_OPEN);
     digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_CLOSED);
   }
-  else if (activeSelection == studio2)
+  else if (activeSelection == STUDIO2)
   {
     digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_OPEN);
     digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_OPEN);
@@ -350,4 +402,3 @@ buttonEvent_t debounce( const uint8_t buttonPin,
   }
   return nochange;
 }
-