RSeries astromech firmware
TeecesPSI.h
Go to the documentation of this file.
1 
2 #ifndef TeecesPSI_h
3 #define TeecesPSI_h
4 
5 #include "ReelTwo.h"
6 #include "core/SetupEvent.h"
7 #include "core/AnimatedEvent.h"
8 #include "core/JawaEvent.h"
10 
23 {
24 public:
25  TeecesPSI(LedControl& ledControl) :
26  fLC(ledControl),
27  fID(ledControl.addDevice()),
28  fState(0),
29  fPauseTime(0),
30  fPause(1000+2000*random(3)),
31  fAnimate(true),
32  fDelay(50 + random(3) * 25),
33  fStuck(15)
34  {
35  JawaID addr = kJawaOther;
36  switch (getNextID())
37  {
38  case 1:
39  addr = kJawaFrontPSI;
40  break;
41  case 2:
42  addr = kJawaRearPSI;
43  break;
44  }
45  setJawaAddress(addr);
46  }
47 
48  virtual void setup() override
49  {
50  fLC.clearDisplay(fID);
51  fLC.setIntensity(fID, 15);
52  fLC.setPower(fID, true);
53  fMillis = millis();
54  }
55 
56  void setPower(bool on)
57  {
58  fLC.setPower(fID, on);
59  }
60 
61  inline void setPause(unsigned int pause)
62  {
63  fPause = pause;
64  }
65 
66  inline void setDelay(byte delay)
67  {
68  fDelay = delay;
69  }
70 
71  void setStuckChance(byte chance)
72  {
73  fStuck = chance;
74  }
75 
76  void setState(byte state)
77  {
78  //set PSI (0 or 1) to a state between 0 (full blue) and 6 (full red)
79  // states 7-11 are moving backwards
80  if (state > 6)
81  state = 12 - state;
82  for (byte col = 0; col < 6; col++)
83  {
84  byte mask = (col < state) ? B10101010 : B01010101;
85  fLC.setColumn(fID, col, ((col & 1) == 0) ? mask : ~mask);
86  }
87  fState = state;
88  }
89 
90  void setAnimate(bool animate)
91  {
92  fAnimate = animate;
93  }
94 
95  void setSolidState(byte mask)
96  {
97  setAnimate(false);
98  for (byte col = 0; col < 6; col++)
99  fLC.setColumn(fID, col, mask);
100  }
101 
102  virtual void jawaCommand(char cmd, int arg, int value) override
103  {
104  UNUSED_ARG(value)
105  switch (cmd)
106  {
107  case 'S':
108  switch (arg)
109  {
110  case 0:
111  // xS0: Test, all PSI leds on
112  setPower(true);
113  setSolidState(~0);
114  setAnimate(false);
115  break;
116  case 1:
117  // xS1: Normal, random mode
118  setPower(true);
119  setAnimate(true);
120  break;
121  case 2:
122  // xS2: Color 1
123  setPower(true);
124  setState(0);
125  setAnimate(false);
126  break;
127  case 3:
128  // xS3: Color 2
129  setPower(true);
130  setState(6);
131  setAnimate(false);
132  break;
133  case 4:
134  // xS4: Off
135  setPower(false);
136  setAnimate(false);
137  break;
138  case 5:
139  // xS5: Color Split
140  setPower(true);
141  setState(3);
142  setAnimate(false);
143  break;
144  }
145  break;
146  }
147  }
148 
149  virtual void animate()
150  {
151  unsigned long currentMillis = millis();
152  if (fLC.isPowered(fID) && fAnimate && fDelay > 0 &&
153  currentMillis - fMillis >= fPauseTime)
154  {
155  //time's up, do something...
156  if (++fState == 12)
157  fState = 0;
158  if (fState != 0 && fState != 6)
159  {
160  //we're swiping...
161  fPauseTime = fDelay;
162  }
163  else
164  {
165  //we're pausing
166  fPauseTime = random(fPause);
167  //decide if we're going to get 'stuck'
168  if (random(100) <= fStuck)
169  {
170  fState = (fState == 0) ? random(1, 3) : random(3, 5);
171  }
172  }
173  setState(fState);
174  fMillis = currentMillis;
175  }
176  }
177 
178 private:
179  static int getNextID()
180  {
181  static int sID;
182  return ++sID;
183  }
184 
185  LedControl& fLC;
186  byte fID;
187  byte fState;
188  unsigned long fMillis;
189  unsigned int fPauseTime;
190  int fPause;
191  bool fAnimate;
192  byte fDelay;
193  byte fStuck;
194 };
195 
196 #endif
kJawaOther
@ kJawaOther
Definition: JawaEvent.h:17
JawaID
JawaID
Definition: JawaEvent.h:6
TeecesPSI::setPower
void setPower(bool on)
Definition: TeecesPSI.h:56
ReelTwo.h
SetupEvent.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
SetupEvent
Base class for all devices that require setup that cannot happen in the constructor....
Definition: SetupEvent.h:15
kJawaRearPSI
@ kJawaRearPSI
Definition: JawaEvent.h:13
LedControlMAX7221.h
AnimatedEvent.h
kJawaFrontPSI
@ kJawaFrontPSI
Definition: JawaEvent.h:12
TeecesPSI::setStuckChance
void setStuckChance(byte chance)
Definition: TeecesPSI.h:71
TeecesPSI::setSolidState
void setSolidState(byte mask)
Definition: TeecesPSI.h:95
TeecesPSI::setAnimate
void setAnimate(bool animate)
Definition: TeecesPSI.h:90
TeecesPSI::jawaCommand
virtual void jawaCommand(char cmd, int arg, int value) override
Subclasses should override this method to handle commands specifying a value.
Definition: TeecesPSI.h:102
TeecesPSI::setPause
void setPause(unsigned int pause)
Definition: TeecesPSI.h:61
JawaEvent
Base class for all devices implementing JAWA lite support.
Definition: JawaEvent.h:31
JawaEvent::setJawaAddress
void setJawaAddress(int addr)
Specify the JAWA address of this device.
Definition: JawaEvent.h:84
JawaEvent.h
TeecesPSI::setDelay
void setDelay(byte delay)
Definition: TeecesPSI.h:66
TeecesPSI::animate
virtual void animate()
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: TeecesPSI.h:149
TeecesPSI
Process State Indicator.
Definition: TeecesPSI.h:22
TeecesPSI::setState
void setState(byte state)
Definition: TeecesPSI.h:76
UNUSED_ARG
#define UNUSED_ARG(arg)
Definition: ReelTwo.h:25
TeecesPSI::TeecesPSI
TeecesPSI(LedControl &ledControl)
Definition: TeecesPSI.h:25
TeecesPSI::setup
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: TeecesPSI.h:48