7 // define network constants
8 #define MAC_DAVID { 0x90, 0xA2, 0xDA, 0x0D, 0xF4, 0x63 }
9 #define MAC_PANIK { 0x90, 0xA2, 0xDA, 0x00, 0x9A, 0x94 }
10 #define MAC_PANIK2 { 0x90, 0xA2, 0xDA, 0x0E, 0xF3, 0x77 }
11 #define MAC_ADDRESS MAC_PANIK2
13 // include Arduino libraries
19 #include <EthernetUdp.h>
22 // include third party libraries
23 #include "WebServer.h"
25 // include in-house libs
26 #include "PanikSwitch.h" // contains variable types for panik switch
28 // define SD and Ethernet select ports
30 #define ETHERNET_SELECT 10
32 // define machine variables
33 #define DEBOUNCE_TRESHOLD 10 // 10 milliseconds
34 #define ABORT_TRESHOLD 3000L // 3 seconds
35 #define PULSE_TRESHOLD 50 // 50 milliseconds
36 #define BLINK_TRESHOLD 100 // 100 milliseconds
39 #define RELAY_BUTTON_LEDS 5 // Button LED indicator
40 #define RELAY_GREEN_LEDS 6 // ``Non-Stop`` LED indicator
41 #define RELAY_RED_LEDS 8 // ``Studio 1`` LED indicator
42 #define RELAY_YELLOW_LEDS 7 // ``Studio 2`` LED indicator
44 #define RELAY_NONSTOP_OR_STUD 4 // Relay : Non-stop or Studio
45 #define RELAY_STUD1_OR_STUD2 9 // Relay : Studio 1 or Studio 2
48 #define BUTTON1 2 // ``Select`` button
49 #define BUTTON2 3 // ``Confirm`` button
50 #define NONSTOP_VIA_STUD1 14 // is nonstop coming out of studio 1
51 #define NONSTOP_VIA_STUD2 15 // is nonstop coming out of studio 2
55 /* define an array with led pins,
56 indexes in that array correspond to the values of possible selections
57 i.e.: RELAY_GREEN_LEDS: nonstop, RELAY_RED_LEDS: studio1, RELAY_YELLOW_LEDS: studio2
59 const int ledsArray[] = { RELAY_GREEN_LEDS, RELAY_RED_LEDS, RELAY_YELLOW_LEDS };
62 /* activeSelection is the variable that holds the active output of the switch
63 it is declared as an attribute that keeps its value between arduino resets
65 switchSelection_t activeSelection = 0; // __attribute__ ((section (".noinit")));
67 // variables and timers for blinking selection (selected but not confirmed)
68 switchSelection_t blinkingSelection = nonstop;
69 bool blinkingLedState = RELAY_STATE_CLOSED;
70 unsigned long blinkingStartTime = 0, blinkingAbortTime = 0;
73 // declaration of possible button states as enum type
74 typedef enum { nochange, pressed, released } buttonEvent_t;
76 // button event timers
77 unsigned long button1LastEventTime = 0, button2LastEventTime = 0;
79 // variables holding the state of each button
80 // these are used during the debouncing process
81 uint8_t button1State = LOW, button2State = LOW;
83 // variables holding the non-stop status from studios 1 & 2
84 bool nonstop_via_stud1, nonstop_via_stud2;
87 buttonEvent_t debounce( const uint8_t buttonPin,
89 unsigned long *buttonLastEventTime );
91 static uint8_t mac[] = MAC_ADDRESS;
94 // instanciate web server
95 WebServer webserver(PREFIX, 80);
98 // and EthernetUDP instance to send notifications
100 IPAddress udp_remote_ip(192, 168, 17, 224);
107 typedef enum responseStatus { NO_POST, POST_OK, POST_ERROR };
111 void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
114 server.httpSuccess("application/json");
116 if (type == WebServer::HEAD)
119 responseStatus response_status = NO_POST;
121 if (type == WebServer::POST)
125 char value[VALUELEN];
128 response_status = POST_ERROR;
130 while (server.readPOSTparam(name, NAMELEN, value, VALUELEN))
132 if (strcmp(name, "s") == 0) {
133 char str_selection[20];
135 digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
136 blinkingSelection = static_cast<switchSelection_t>(atoi(value));
137 activeSelection = static_cast<switchSelection_t>(atoi(value));
138 response_status = POST_OK;
142 snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
144 Serial.println(F("Sending UDP... "));
145 Serial.println(str_selection);
147 Udp.beginPacket(udp_remote_ip, 1312);
148 Udp.write(str_selection);
156 server.println(F("{"));
158 server.print(" \"response\": ");
159 server.print(response_status);
161 server.print(",\n \"active\": ");
162 server.print(activeSelection);
164 server.print(",\n \"nonstop-via-stud1\": ");
165 server.print(nonstop_via_stud1);
166 server.print(",\n \"nonstop-via-stud2\": ");
167 server.println(nonstop_via_stud2);
169 server.println(F("}"));
176 blinkingSelection = activeSelection = nonstop;
178 // open serial communication for debugging
180 Serial.println(F("Startup"));
182 // disable SD and Ethernet ports before setup
183 pinMode(SD_SELECT, OUTPUT);
184 digitalWrite(SD_SELECT, HIGH); // disable SD card
185 pinMode(ETHERNET_SELECT, OUTPUT);
186 digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet
188 // start SPI (because Ethernet shield needs it)
189 SPI.begin(); // start SPI
192 if (!(Ethernet.begin(mac) == 0)) // start network
193 Serial.println(Ethernet.localIP());
197 Serial.println(F("Network Error"));
202 if (! Udp.begin(1312)) {
204 Serial.println(F("Failed to initiate UDP"));
209 // set mode for used pins
210 pinMode(RELAY_RED_LEDS, OUTPUT);
211 digitalWrite(RELAY_RED_LEDS, RELAY_STATE_CLOSED);
212 pinMode(RELAY_YELLOW_LEDS, OUTPUT);
213 digitalWrite(RELAY_YELLOW_LEDS, RELAY_STATE_CLOSED);
214 pinMode(RELAY_GREEN_LEDS, OUTPUT);
215 digitalWrite(RELAY_GREEN_LEDS, RELAY_STATE_CLOSED);
216 pinMode(RELAY_BUTTON_LEDS, OUTPUT);
217 digitalWrite(RELAY_BUTTON_LEDS, RELAY_STATE_CLOSED);
219 pinMode(RELAY_NONSTOP_OR_STUD, OUTPUT);
220 digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_CLOSED);
221 pinMode(RELAY_STUD1_OR_STUD2, OUTPUT);
222 digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_CLOSED);
224 pinMode(BUTTON1, INPUT);
225 pinMode(BUTTON2, INPUT);
226 pinMode(NONSTOP_VIA_STUD1, INPUT);
227 pinMode(NONSTOP_VIA_STUD2, INPUT);
229 // configure web server pages
230 webserver.setDefaultCommand(&webCmd);
240 // update variables indicating if non-stop is selected in studios 1&2
241 nonstop_via_stud1 = digitalRead(NONSTOP_VIA_STUD1);
242 if (nonstop_via_stud1 == 0)
243 nonstop_via_stud1 = 1;
245 nonstop_via_stud1 = 0;
246 nonstop_via_stud2 = digitalRead(NONSTOP_VIA_STUD2);
247 if (nonstop_via_stud2 == 0)
248 nonstop_via_stud2 = 1;
250 nonstop_via_stud2 = 0;
252 // check if we have a HTTP request (and respond)
253 webserver.processConnection();
255 // handle button 1 (select button)
256 switch ( debounce(BUTTON1, &button1State, &button1LastEventTime) )
262 Serial.println(F("Button 1 pressed"));
264 digitalWrite(ledsArray[blinkingSelection], RELAY_STATE_CLOSED);
266 blinkingStartTime = millis(),
267 blinkingAbortTime = millis();
268 blinkingLedState = RELAY_STATE_OPEN;
272 Serial.println(F("Button 1 released"));
277 // handle button 2 (confirm button)
278 switch ( debounce(BUTTON2, &button2State, &button2LastEventTime) )
284 Serial.println(F("Button 2 pressed"));
286 blinkingAbortTime = 0; // disable blinking auto abort
290 Serial.println(F("Button 2 released"));
292 if (activeSelection != blinkingSelection)
294 char str_selection[20];
295 digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
296 activeSelection = blinkingSelection; // relay states must be changed now
298 Serial.print(F("Active Selection: "));
299 Serial.println(activeSelection);
303 snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
305 Serial.println(F("Sending UDP... "));
306 Serial.println(str_selection);
308 Udp.beginPacket(udp_remote_ip, 1312);
309 Udp.write(str_selection);
316 // handle auto abort for blinking led (if any)
317 if ( (blinkingAbortTime != 0) &&
318 ((millis() - blinkingAbortTime) >= ABORT_TRESHOLD) ) {
319 digitalWrite(ledsArray[blinkingSelection], RELAY_STATE_CLOSED);
320 blinkingAbortTime = 0;
321 blinkingSelection = activeSelection;
324 // handle blinking led (if selected is not current state)
325 if (activeSelection != blinkingSelection) {
326 if ((millis() - blinkingStartTime) >= BLINK_TRESHOLD) {
327 digitalWrite(ledsArray[blinkingSelection], blinkingLedState);
328 digitalWrite(RELAY_BUTTON_LEDS, !blinkingLedState);
329 blinkingStartTime = millis();
330 blinkingLedState = !blinkingLedState;
334 digitalWrite(RELAY_BUTTON_LEDS, RELAY_STATE_CLOSED);
337 digitalWrite(ledsArray[activeSelection], RELAY_STATE_OPEN);
339 // update audio channel relays
340 if (activeSelection == nonstop)
342 digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_CLOSED);
343 digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_CLOSED);
345 else if (activeSelection == studio1)
347 digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_OPEN);
348 digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_CLOSED);
350 else if (activeSelection == studio2)
352 digitalWrite(RELAY_NONSTOP_OR_STUD, RELAY_STATE_OPEN);
353 digitalWrite(RELAY_STUD1_OR_STUD2, RELAY_STATE_OPEN);
357 buttonEvent_t debounce( const uint8_t buttonPin,
358 uint8_t *buttonState,
359 unsigned long *buttonLastEventTime )
361 uint8_t buttonLastState = *buttonState;
362 uint8_t buttonReading = digitalRead(buttonPin);
364 if ( (*buttonLastEventTime == 0) &&
365 (*buttonState != buttonReading) ) {
366 // something happened, start the debouncing process
367 *buttonLastEventTime = millis();
370 if (*buttonLastEventTime != 0) {
371 long buttonEventTimer = millis() - *buttonLastEventTime;
373 if ( (buttonEventTimer < DEBOUNCE_TRESHOLD) &&
374 (*buttonState == buttonReading) ) {
375 // noise or bouncing, abort
376 *buttonLastEventTime = 0;
379 if ( (buttonEventTimer >= DEBOUNCE_TRESHOLD) &&
380 (buttonReading != *buttonState) ) {
381 // new button state was maintained
382 *buttonLastEventTime = 0;
383 *buttonState = buttonReading;
387 // debouncing finished, return button state
388 if (*buttonLastEventTime == 0) {
389 // comparing buttonState and buttonLastState tells whether the
390 // button is pressed or released
391 if ( (*buttonState == HIGH) &&
392 (buttonLastState == LOW) ) {
393 // button just pressed
394 buttonLastState = *buttonState;
397 if ( (*buttonState == LOW) &&
398 (buttonLastState == HIGH) ) {
399 // button just released
400 buttonLastState = *buttonState;