RSeries astromech firmware
PeriscopeI2C.h
Go to the documentation of this file.
1 #ifndef PeriscopeI2C_h
2 #define PeriscopeI2C_h
3 
4 #include "ReelTwo.h"
5 #include "core/CommandEvent.h"
6 #include <Wire.h>
7 
17 class PeriscopeI2C :
18  public CommandEvent
19 {
20 public:
25  PeriscopeI2C(const byte i2cAddress = 0x20) :
26  fI2CAddress(i2cAddress),
27  fPeriscopeUp(false),
28  fToggleMode(true),
29  fLastCmdMillis(0)
30  {
31  }
32 
36  inline bool readyForNewCmd()
37  {
38  return (fLastCmdMillis + 4000 < millis());
39  }
40 
42  {
43  return fPeriscopeUp;
44  }
45 
46  inline void setToggleMode(bool toggleMode)
47  {
48  fToggleMode = toggleMode;
49  }
50 
51  void up()
52  {
53  if (!readyForNewCmd())
54  return;
55  if (fPeriscopeUp)
56  {
57  if (fToggleMode)
58  down();
59  }
60  else
61  {
62  sendCommand(kUp);
63  fPeriscopeUp = true;
64  }
65  }
66 
70  void down()
71  {
72  if (!readyForNewCmd())
73  return;
74  // Issue the down command even if our safe guard variable isn't set
75  // if (fPeriscopeUp)
76  {
77  sendCommand(kDown);
78  fPeriscopeUp = false;
79  }
80  }
81 
82  void randomFast()
83  {
84  if (!readyForNewCmd())
85  return;
86  if (fPeriscopeUp)
87  {
88  if (fToggleMode)
89  down();
90  }
91  else
92  {
93  sendCommand(kRandomFast);
94  fPeriscopeUp = true;
95  }
96  }
97 
98  void randomSlow()
99  {
100  if (!readyForNewCmd())
101  return;
102  if (fPeriscopeUp)
103  {
104  if (fToggleMode)
105  down();
106  }
107  else
108  {
109  sendCommand(kRandomSlow);
110  fPeriscopeUp = true;
111  }
112  }
113 
115  {
116  if (!readyForNewCmd())
117  return;
118  if (fPeriscopeUp)
119  {
120  sendCommand(kSearchLightCW);
121  }
122  }
123 
125  {
126  if (!readyForNewCmd())
127  return;
128  if (fPeriscopeUp)
129  {
130  sendCommand(kSearchLightCCW);
131  }
132  }
133 
134  void faceForward()
135  {
136  if (!readyForNewCmd())
137  return;
138  if (fPeriscopeUp)
139  {
140  sendCommand(kFaceForward);
141  }
142  }
143 
147  virtual void handleCommand(const char* cmd) override
148  {
149  if (*cmd++ == 'P' && *cmd++ == 'S')
150  {
151  if (cmd[0] == 'U' && cmd[1] == 'P')
152  {
153  up();
154  }
155  else if (cmd[0] == 'D' && cmd[1] == 'O' && cmd[2] == 'W' && cmd[3] == 'N')
156  {
157  down();
158  }
159  else if (cmd[0] == 'F' && cmd[1] == 'A' && cmd[2] == 'S' && cmd[3] == 'T')
160  {
161  randomFast();
162  }
163  else if (cmd[0] == 'S' && cmd[1] == 'L' && cmd[2] == 'O' && cmd[3] == 'W')
164  {
165  randomSlow();
166  }
167  else if (cmd[0] == 'S' && cmd[1] == 'C' && cmd[2] == 'W')
168  {
169  searchLightCW();
170  }
171  else if (cmd[0] == 'S' && cmd[1] == 'C' && cmd[2] == 'C' && cmd[3] == 'W')
172  {
173  searchLightCCW();
174  }
175  else if (cmd[0] == 'F' && cmd[1] == 'W' && cmd[2] == 'D')
176  {
177  faceForward();
178  }
179  }
180  }
181 
182 private:
183  enum PeriscopeCmd
184  {
185  kDown = 1,
186  kUp = 2,
187  kSearchLightCCW = 3,
188  kRandomFast = 4,
189  kRandomSlow = 5,
190  kFaceForward = 6,
191  kSearchLightCW = 7
192  };
193 
194  void sendCommand(int cmd)
195  {
196  Wire.beginTransmission(fI2CAddress);
197  Wire.write(cmd);
198  Wire.endTransmission();
199  fLastCmdMillis = millis();
200  }
201 
202  byte fI2CAddress;
203  bool fPeriscopeUp;
204  bool fToggleMode;
205  unsigned long fLastCmdMillis;
206 };
207 
208 #endif
PeriscopeI2C
Encapsulates the available i2c commands that can be sent to the ia-parts.com periscope lifter and per...
Definition: PeriscopeI2C.h:17
PeriscopeI2C::searchLightCCW
void searchLightCCW()
Definition: PeriscopeI2C.h:124
CommandEvent
Base class for all command enabled devices. CommandEvent::handleCommand() is called for each device e...
Definition: CommandEvent.h:17
ReelTwo.h
PeriscopeI2C::isPeriscopeUp
bool isPeriscopeUp()
Definition: PeriscopeI2C.h:41
PeriscopeI2C::randomSlow
void randomSlow()
Definition: PeriscopeI2C.h:98
PeriscopeI2C::searchLightCW
void searchLightCW()
Definition: PeriscopeI2C.h:114
PeriscopeI2C::PeriscopeI2C
PeriscopeI2C(const byte i2cAddress=0x20)
Constructor.
Definition: PeriscopeI2C.h:25
PeriscopeI2C::randomFast
void randomFast()
Definition: PeriscopeI2C.h:82
PeriscopeI2C::setToggleMode
void setToggleMode(bool toggleMode)
Definition: PeriscopeI2C.h:46
PeriscopeI2C::faceForward
void faceForward()
Definition: PeriscopeI2C.h:134
PeriscopeI2C::readyForNewCmd
bool readyForNewCmd()
Only allow a new command every 4 seconds.
Definition: PeriscopeI2C.h:36
PeriscopeI2C::down
void down()
Down just means down no toggle.
Definition: PeriscopeI2C.h:70
CommandEvent.h
PeriscopeI2C::up
void up()
Definition: PeriscopeI2C.h:51
PeriscopeI2C::handleCommand
virtual void handleCommand(const char *cmd) override
Periscope Commands start with 'PS'.
Definition: PeriscopeI2C.h:147