WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
Motor.cs
Go to the documentation of this file.
1 using System.Xml.Serialization;
4 
5 namespace DirectOutput.Cab.Toys.Hardware
6 {
7 
17  public class Motor : AnalogAlphaToy
18  {
19  private int _MaxRuntimeMs = 300000;
20 
29  public int MaxRunTimeMs
30  {
31  get { return _MaxRuntimeMs; }
32  set { _MaxRuntimeMs = value.Limit(0, int.MaxValue); }
33  }
34 
35 
36 
37 
38  private int _KickstartPower = 255;
39 
50  public int KickstartPower
51  {
52  get { return _KickstartPower; }
53  set { _KickstartPower = value.Limit(0, 255); }
54  }
55 
56  private int _KickstartDurationMs = 100;
57 
66  public int KickstartDurationMs
67  {
68  get { return _KickstartDurationMs; }
69  set { _KickstartDurationMs = value.Limit(0, 5000); }
70  }
71 
72  private int _MinPower = 1;
73 
80  public int MinPower
81  {
82  get { return _MinPower; }
83  set { _MinPower = value.Limit(0, 255); }
84  }
85 
86 
87  private int _MaxPower = 255;
88 
95  public int MaxPower
96  {
97  get { return _MaxPower; }
98  set { _MaxPower = value.Limit(0, 255); }
99  }
100 
101 
102 
107  //[XmlIgnoreAttribute]
108  //public int Power
109  //{
110  // get
111  // {
112 
113  // return (int)((Value - MinPower) * ((double)(MaxPower - MinPower) / 255));
114  // }
115  //}
116 
117 
118  int CurrentMotorPower = 0;
119  int TargetMotorPower = 0;
120  bool KickstartActive = false;
121  bool TurnedOffAfterMaxRunTime = false;
122 
126  public override void UpdateOutputs()
127  {
128  if (Output != null)
129  {
130  int P = FadingCurve.MapValue(GetResultingValue().Limit(0, 255));
131 
132  if (P != 0)
133  {
134  P = ((int)((double)(MaxPower >= MinPower ? MaxPower - MinPower : MinPower - MaxPower) / 255 * P) + MinPower).Limit(MinPower, MaxPower);
135  }
136 
137 
138 
139 
140  if (P == 0)
141  {
142  TurnedOffAfterMaxRunTime = false;
143  }
144 
145  if (!TurnedOffAfterMaxRunTime)
146  {
147  if (CurrentMotorPower == 0)
148  {
149  //Motor is currently off
150  if (P > 0)
151  {
152  //need to turn the motor on
153 
154  if (KickstartDurationMs > 0 && KickstartPower > 0 && P <= KickstartPower)
155  {
156  //Kickstart is defined, start with kickstart
157 
158  TargetMotorPower = P;
159 
160  if (!KickstartActive)
161  {
162  CurrentMotorPower = KickstartPower;
163  Output.Value = (byte)CurrentMotorPower;
164  KickstartActive = true;
165  AlarmHandler.RegisterAlarm(KickstartDurationMs, KickStartEnd);
166  }
167 
168  }
169  else
170  {
171  //Just turn the motor on
172  CurrentMotorPower = P;
173  TargetMotorPower = P;
174  Output.Value = (byte)P;
175  KickstartActive = false;
176 
177  }
178 
179  if (MaxRunTimeMs > 0)
180  {
181  AlarmHandler.RegisterAlarm(MaxRunTimeMs, MaxRunTimeMotorStop);
182 
183  }
184 
185  }
186  }
187  else if (KickstartActive)
188  {
189  //Motor is in kickstart phase
190  if (P > 0)
191  {
192  //Need to change the motor power
193  TargetMotorPower = P;
194  }
195  else
196  {
197  //Turn off motor
198  AlarmHandler.UnregisterAlarm(KickStartEnd);
199  AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
200  TargetMotorPower = 0;
201  CurrentMotorPower = 0;
202  Output.Value = 0;
203  }
204  }
205  else
206  {
207  //Motor is on
208  if (P == 0)
209  {
210  //Turn of motor
211  AlarmHandler.UnregisterAlarm(KickStartEnd);
212  AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
213  TargetMotorPower = 0;
214  CurrentMotorPower = 0;
215  Output.Value = 0;
216  }
217  else if (P != CurrentMotorPower)
218  {
219  //Power has changed
220  CurrentMotorPower = P;
221  TargetMotorPower = P;
222  Output.Value = (byte)P;
223  }
224  }
225 
226  }
227  }
228  }
229 
230  private void MaxRunTimeMotorStop()
231  {
232  AlarmHandler.UnregisterAlarm(KickStartEnd);
233  KickstartActive = false;
234  CurrentMotorPower = 0;
235  TargetMotorPower = 0;
236  Output.Value = 0;
237  TurnedOffAfterMaxRunTime = true;
238  }
239 
240 
241  private void KickStartEnd()
242  {
243  KickstartActive = false;
244  CurrentMotorPower = TargetMotorPower;
245  Output.Value = (byte)CurrentMotorPower;
246  }
247 
248 
249 
250 
251 
252  private AlarmHandler AlarmHandler;
253 
258  public override void Init(Cabinet Cabinet)
259  {
260  base.Init(Cabinet);
261  AlarmHandler = Cabinet.Alarms;
262 
263  }
264 
268  public override void Finish()
269  {
270  if (AlarmHandler != null)
271  {
272  AlarmHandler.UnregisterAlarm(KickStartEnd);
273  AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
274  AlarmHandler = null;
275  }
276  base.Finish();
277  }
278 
279  }
280 }
The AlarmHandler classed is used to execute scheduled events (e.g. regular updates on a effect) in th...
Definition: AlarmHandler.cs:14
The Cabinet object describes the parts of a pinball cabinet (toys, outputcontrollers, outputs and more).
Definition: Cabinet.cs:17
The namespace DirectOutput.Cab.Toys contains all toy related classes.
override void Finish()
Finishes the Motor toy and releases used references.
Definition: Motor.cs:268
void RegisterAlarm(int DurationMs, Action AlarmHandler, bool DontUnregister=false)
Registers the specied AlarmHandler for a alarm after the specified duration.
Motor toy supporting max. and min. power, max. runtime and kickstart settings. The settings of this ...
Definition: Motor.cs:17
override void UpdateOutputs()
Updates the output of the toy.
Definition: Motor.cs:126
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
Definition: Cab.cs:16
void UnregisterAlarm(Action AlarmHandler)
Unregisters all alarm for the specified alarm handler.
This toy handles analog values (0-255) in a layer structure including alpha value (0=completely trans...
void Finish()
Finishes the object. Clears all alarm lists.
Namespace for objects dealing with layers
override void Init(Cabinet Cabinet)
Initalizes the Motor toy.
Definition: Motor.cs:258
AlarmHandler Alarms
Gets the AlarmHandler object for the cabinet object.
Definition: Cabinet.cs:80
Support classes used by the Pinball object.
Definition: AlarmHandler.cs:6