DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
Motor.cs
Go to the documentation of this file.
1 using System.Xml.Serialization;
2 using DirectOutput.PinballSupport;
3 using DirectOutput.Cab.Toys.Layer;
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.Pinball.Alarms;
262 
263  }
264 
268  public override void Finish()
269  {
270  AlarmHandler.UnregisterAlarm(KickStartEnd);
271  AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
272  AlarmHandler = null;
273  base.Finish();
274  }
275 
276  }
277 }