RSeries astromech firmware
Welder.h
Go to the documentation of this file.
1 #ifndef Welder_h
2 #define Welder_h
3 
4 #include "core/AnimatedEvent.h"
5 #include "core/CommandEvent.h"
6 
37 class Welder :
39 {
40 public:
44  Welder(byte idPin, byte relayPin) :
45  fID(idPin),
46  fRelayPin(relayPin)
47  {
48  pinMode(fID, INPUT_PULLUP);
49  pinMode(fRelayPin, OUTPUT);
50  welderDisarm();
51  }
52 
53  bool isAttached()
54  {
55  return !digitalRead(fID);
56  }
57 
61  void welderArm()
62  {
63  fArmed = true;
64  }
65 
69  void welderDisarm()
70  {
71  fArmed = false;
72  welderOff();
73  }
74 
78  void welderOn()
79  {
80  if (fArmed && fCount == 0)
81  {
82  fCount = random(80, 120);
83  fCount += !(fCount&1); // ensure count is odd
84  fNextTime = millis();
85  }
86  }
87 
91  void welderOff()
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++ == 'W' && *cmd++ == 'L')
104  {
105  // Cannot arm/disarm welder using commands. Welder should be automatically
106  // armed after opening door and raising arm. As soon as arm starts lowering
107  // the welder should be disarmed.
108  if (cmd[0] == 'O' && cmd[1] == 'N' && cmd[2] == '\0')
109  {
110  welderOn();
111  }
112  else if (cmd[0] == 'O' && cmd[1] == 'F' && cmd[2] == 'F' && cmd[3] == '\0')
113  {
114  welderOff();
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(200);
134  }
135  else
136  {
137  digitalWrite(fRelayPin, HIGH);
138  fNextTime = currentTime + random(20,120);
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
Welder::welderArm
void welderArm()
Arm Welder.
Definition: Welder.h:61
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
AnimatedEvent.h
Welder::welderOff
void welderOff()
Turn off the smoke machine.
Definition: Welder.h:91
Welder::Welder
Welder(byte idPin, byte relayPin)
Constructor.
Definition: Welder.h:44
Welder::animate
virtual void animate() override
Perform a single frame of welder animation.
Definition: Welder.h:122
Welder::welderDisarm
void welderDisarm()
Disarm welder.
Definition: Welder.h:69
Welder::welderOn
void welderOn()
Turn on the welder.
Definition: Welder.h:78
Welder::handleCommand
virtual void handleCommand(const char *cmd) override
Welder Commands start with 'WL'.
Definition: Welder.h:101
CommandEvent.h
Welder::isAttached
bool isAttached()
Definition: Welder.h:53
Welder
Controls a relay to a spark gap igniter to create some actual sparking.
Definition: Welder.h:37