From: Frédéric Péters Date: Thu, 25 Mar 2021 07:03:53 +0000 (+0100) Subject: use a simple int for active selection X-Git-Url: https://git.0d.be/?p=PanikSwitch.git;a=commitdiff_plain;h=0a8625bfaab493d5704ffce4134f62c0283d5afd use a simple int for active selection --- diff --git a/FINAL2013V2.ino b/FINAL2013V2.ino index 53e3ae9..c9f3829 100644 --- a/FINAL2013V2.ino +++ b/FINAL2013V2.ino @@ -22,9 +22,6 @@ // include third party libraries #include "WebServer.h" -// include in-house libs -#include "PanikSwitch.h" // contains variable types for panik switch - // define SD and Ethernet select ports #define SD_SELECT 4 #define ETHERNET_SELECT 10 @@ -50,6 +47,14 @@ #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, @@ -62,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 = 0; // __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; @@ -146,8 +151,8 @@ 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(atoi(value)); - activeSelection = static_cast(atoi(value)); + blinkingSelection = atoi(value); + activeSelection = atoi(value); response_status = POST_OK; notify_udp(); @@ -175,7 +180,7 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b // setup function void setup() { - blinkingSelection = activeSelection = nonstop; + blinkingSelection = activeSelection = NONSTOP; // open serial communication for debugging Serial.begin(9600); @@ -265,6 +270,9 @@ void loop() #endif digitalWrite(ledsArray[blinkingSelection], RELAY_STATE_CLOSED); blinkingSelection++; + if (blinkingSelection > STUDIO2) { + blinkingSelection = NONSTOP; + } blinkingStartTime = millis(), blinkingAbortTime = millis(); blinkingLedState = RELAY_STATE_OPEN; @@ -328,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); diff --git a/PanikSwitch.h b/PanikSwitch.h deleted file mode 100644 index 08af446..0000000 --- a/PanikSwitch.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - Definition of new types for Switch Kontrol -*/ - -#include "avr/pgmspace.h" // Needed for PROGMEM stuff - - -/* declaration of relay postions -*/ -typedef enum { RELAY_STATE_OPEN, RELAY_STATE_CLOSED } relayState_t; - - -/* declaration of possible state selections for the switch (new enum type) -*/ -typedef enum { nonstop, studio1, studio2, _NbrSwitchSelections } switchSelection_t; - -/* explicit conversions from int to switchSelection_t for +, ++, and += operators - this makes possible to increment values of type switchSelection_t like this: - switchSelection_t selection = studio1; // variable declaration & initialization - selection = selection + 1; // => selection is studio2 (one way to do it) - selection += 1; // => selection is nonstop (that works, too) - selection++; // => selection is studio1 (other way) - ++selection; // => selection is studio2 (yet another way) - etc. -*/ -// `+` operator in `selection = selection + i` -switchSelection_t operator+(switchSelection_t lhs, int rhs) { - return static_cast( - (static_cast(lhs) + rhs) % _NbrSwitchSelections - ); -} -// `+` operator in `selection = i + selection` -switchSelection_t operator+(int lhs, switchSelection_t rhs) { - return static_cast( - (lhs + static_cast(rhs)) % _NbrSwitchSelections - ); -} -// `++` operator in `++selection` -switchSelection_t &operator++(switchSelection_t &s) { - s = static_cast((s + 1) % _NbrSwitchSelections); - return s; -} -// `++` operator in `selection++` -switchSelection_t operator++(switchSelection_t &s, int) { - return static_cast(++s - 1); -} -// `+=` operator in `selection += i` -switchSelection_t operator+=(switchSelection_t &lhs, int rhs) { - lhs = static_cast( - (static_cast(lhs) + rhs) % _NbrSwitchSelections - ); - return lhs; -} - - -/* store switch state labels as an array in program memory - see http://arduino.cc/en/Reference/PROGMEM -*/ -/* -prog_char label_nonstop[] PROGMEM = "Non-Stop"; -prog_char label_studio1[] PROGMEM = "Studio 1"; -prog_char label_studio2[] PROGMEM = "Studio 2"; - -PROGMEM const char *selection_labels[] = { - label_nonstop, label_studio1, label_studio2 -}; - -*/