RSeries astromech firmware
Zapper.h
Go to the documentation of this file.
1 #ifndef Zapper_h
2 #define Zapper_h
3 
4 #include "core/AnimatedEvent.h"
5 #include "core/CommandEvent.h"
6 
37 class Zapper :
39 {
40 public:
44  Zapper(byte idPin, byte relayPin) :
45  fID(idPin),
46  fRelayPin(relayPin)
47  {
48  pinMode(fID, INPUT_PULLUP);
49  pinMode(fRelayPin, OUTPUT);
50  zapperDisarm();
51  }
52 
53  bool isAttached()
54  {
55  return !digitalRead(fID);
56  }
57 
61  void zapperArm()
62  {
63  fArmed = true;
64  }
65 
69  void zapperDisarm()
70  {
71  fArmed = false;
72  zapperOff();
73  }
74 
78  void zapperOn()
79  {
80  if (fArmed && fCount == 0)
81  {
82  fCount = random(10, 20);
83  fCount += !(fCount&1); // ensure count is odd
84  fNextTime = millis();
85  }
86  }
87 
91  void zapperOff()
92  {
93  fCount = 0;
94  fNextTime = 0;
95  digitalWrite(fRelayPin, LOW);
96  }
97 
101  virtual void handleCommand(const char* cmd) override
102  {
103  if (*cmd++ == 'B' && *cmd++ == 'Z')
104  {
105  // Cannot arm/disarm zapper using commands. Zapper should be automatically
106  // armed after opening door and raising arm. As soon as arm starts lowering
107  // the zapper should be disarmed.
108  if (cmd[0] == 'O' && cmd[1] == 'N' && cmd[2] == '\0')
109  {
110  zapperOn();
111  }
112  else if (cmd[0] == 'O' && cmd[1] == 'F' && cmd[2] == 'F' && cmd[3] == '\0')
113  {
114  zapperOff();
115  }
116  }
117  }
118 
122  virtual void animate() override
123  {
124  if (!fArmed || fCount == 0)
125  return;
126  uint32_t currentTime = millis();
127  if (fNextTime < currentTime)
128  {
129  bool relayOn = (fCount-- & 1);
130  if (relayOn)
131  {
132  digitalWrite(fRelayPin, LOW);
133  fNextTime = currentTime + random(100,200);
134  }
135  else
136  {
137  digitalWrite(fRelayPin, HIGH);
138  fNextTime = currentTime + random(200, 800);
139  }
140  }
141  }
142 
143 private:
144  byte fID;
145  byte fRelayPin;
146  bool fArmed;
147  uint8_t fCount;
148  uint32_t fNextTime;
149 };
150 
152 
153 #endif
CommandEvent
Base class for all command enabled devices. CommandEvent::handleCommand() is called for each device e...
Definition: CommandEvent.h:17
Zapper::animate
virtual void animate() override
Perform a single frame of zapper animation.
Definition: Zapper.h:122
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
Zapper::zapperOff
void zapperOff()
Turn off the smoke machine.
Definition: Zapper.h:91
Zapper::zapperDisarm
void zapperDisarm()
Disarm zapper.
Definition: Zapper.h:69
Zapper
Controls a relay to a spark gap igniter to create some actual sparking.
Definition: Zapper.h:37
AnimatedEvent.h
Zapper::zapperOn
void zapperOn()
Turn on the zapper.
Definition: Zapper.h:78
Zapper::handleCommand
virtual void handleCommand(const char *cmd) override
Zapper Commands start with 'BZ'.
Definition: Zapper.h:101
Zapper::isAttached
bool isAttached()
Definition: Zapper.h:53
Zapper::Zapper
Zapper(byte idPin, byte relayPin)
Constructor.
Definition: Zapper.h:44
Zapper::zapperArm
void zapperArm()
Arm zapper.
Definition: Zapper.h:61
CommandEvent.h