WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
Curve.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 
6 using System.Xml.Serialization;
8 using System.Xml;
9 
10 namespace DirectOutput.General
11 {
15  public class Curve : NamedItemBase, IXmlSerializable
16  {
20  public enum CurveTypeEnum
21  {
25  Linear,
26 
30  Linear0To224,
34  Linear0To192,
38  Linear0To160,
42  Linear0To128,
46  Linear0To96,
50  Linear0To64,
54  Linear0To32,
58  Linear0To16,
62  InvertedLinear,
66  SwissLizardsLedCurve
67  }
68 
69  private byte[] _Data;
70 
79  public byte[] Data
80  {
81  get { return _Data; }
82  set
83  {
84  if (value.Length != 256)
85  {
86  throw new Exception("The curve array must have 256 elements, but a array with {0} elements has been supplied.".Build(value.Length));
87  }
88  _Data = value; }
89  }
90 
91 
97  public byte MapValue(byte CurvePosition)
98  {
99  return _Data[CurvePosition];
100  }
101 
102 
108  public byte MapValue(int CurvePosition)
109  {
110  try
111  {
112  return _Data[CurvePosition];
113  }
114  catch
115  {
116  throw new ArgumentException("Positon {0} does not exist for the curve.".Build(CurvePosition),"CurvePosition");
117  }
118  }
119 
124  public void SetCurve(CurveTypeEnum CurveType)
125  {
126  Data = BuildCurve(CurveType);
127  }
128 
129  private byte[] BuildCurve(CurveTypeEnum CurveType)
130  {
131  byte[] C = new byte[256];
132 
133  switch (CurveType)
134  {
135  case CurveTypeEnum.SwissLizardsLedCurve:
136  C = new byte[256] { 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 28, 29, 30, 30, 31, 32, 33, 34, 35, 36, 36, 37, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 119, 120, 121, 122, 123, 124, 126, 127, 128, 129, 130, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 144, 145, 146, 147, 149, 150, 151, 152, 154, 155, 156, 158, 159, 160, 161, 162, 164, 165, 167, 168, 169, 171, 172, 173, 174, 176, 177, 178, 180, 181, 182, 184, 185, 187, 188, 189, 191, 192, 193, 195, 196, 197, 199, 200, 202, 203, 204, 206, 207, 208, 210, 211, 213, 214, 216, 217, 218, 220, 221, 223, 224, 226, 227, 228, 230, 231, 233, 234, 236, 237, 239, 240, 242, 243, 245, 246, 248, 249, 251, 252, 254, 255 };
137  break;
138  case CurveTypeEnum.InvertedLinear:
139  for (int i = 0; i <= 255; i++)
140  {
141  C[i] = (byte)(255-i);
142  }
143  break;
144  case CurveTypeEnum.Linear0To224:
145  for (int i = 0; i <= 255; i++)
146  {
147  C[i] = (byte)((double)224 / 255 * i).Limit(0, 255);
148  }
149  break;
150  case CurveTypeEnum.Linear0To192:
151  for (int i = 0; i <= 255; i++)
152  {
153  C[i] = (byte)((double)192/255*i).Limit(0,255);
154  }
155  break;
156  case CurveTypeEnum.Linear0To160:
157  for (int i = 0; i <= 255; i++)
158  {
159  C[i] = (byte)((double)160 / 255 * i).Limit(0, 255);
160  }
161  break;
162  case CurveTypeEnum.Linear0To128:
163  for (int i = 0; i <= 255; i++)
164  {
165  C[i] = (byte)((double)128 / 255 * i).Limit(0, 255);
166  }
167  break;
168  case CurveTypeEnum.Linear0To96:
169  for (int i = 0; i <= 255; i++)
170  {
171  C[i] = (byte)((double)96 / 255 * i).Limit(0, 255);
172  }
173  break;
174  case CurveTypeEnum.Linear0To64:
175  for (int i = 0; i <= 255; i++)
176  {
177  C[i] = (byte)((double)64 / 255 * i).Limit(0, 255);
178  }
179  break;
180  case CurveTypeEnum.Linear0To32:
181  for (int i = 0; i <= 255; i++)
182  {
183  C[i] = (byte)((double)32 / 255 * i).Limit(0, 255);
184  }
185  break;
186  case CurveTypeEnum.Linear0To16:
187  for (int i = 0; i <= 255; i++)
188  {
189  C[i] = (byte)((double)16 / 255 * i).Limit(0, 255);
190  }
191  break;
192  case CurveTypeEnum.Linear:
193  default:
194  for (int i = 0; i <= 255; i++)
195  {
196  C[i] = (byte)(i);
197  }
198  break;
199  }
200  return C;
201  }
202 
203  private CurveTypeEnum? GetCurveTypeEnum()
204  {
205  foreach (CurveTypeEnum FT in Enum.GetValues(typeof(CurveTypeEnum)))
206  {
207  byte[] C = BuildCurve(FT);
208  if (C.CompareContents(Data))
209  {
210  return FT;
211  }
212  }
213  return null;
214  }
215 
216 
220  public Curve()
221  {
222  Data = BuildCurve(CurveTypeEnum.Linear);
223  }
224 
225 
226 
231  public Curve(CurveTypeEnum CurveType)
232  {
233  Data = BuildCurve(CurveType);
234  }
235 
236 
237 
238 
239  #region IXmlSerializable Member
240 
248  public System.Xml.Schema.XmlSchema GetSchema()
249  {
250  return null;
251  }
252 
257  public void ReadXml(System.Xml.XmlReader reader)
258  {
259  if (reader.LocalName == GetType().Name)
260  {
261  while (reader.Read())
262  {
263  switch (reader.LocalName)
264  {
265  case "Name":
266 
267  Name = reader.ReadString();
268  break;
269  case "Curve":
270 
271  string XmlData = reader.ReadString();
272 
273  CurveTypeEnum CurveType = CurveTypeEnum.Linear;
274 
275  if (Enum.TryParse(XmlData, true, out CurveType))
276  {
277  Data = BuildCurve(CurveType);
278  }
279  else
280  {
281 
282  string[] V = XmlData.Split(new char[] { ',' });
283  if (V.Length == 256)
284  {
285  int Cnt = V.Count(Value => !Value.IsUInt() || Value.ToUInt() > 255);
286  if (Cnt == 0)
287  {
288  for (int i = 0; i < 255; i++)
289  {
290  Data[i] = (byte)V[i].ToInteger();
291  }
292  }
293  else
294  {
295  throw new Exception("{0} value(s) in the curve data for fading curve {1} are not numeric values between 0-255. Supplied data was {2}.".Build(Cnt, Name,XmlData));
296  }
297  }
298  else
299  {
300  if (V.Length > 1)
301  {
302  throw new Exception("Cant parse data for curve {0}. 256 values are required, but {1} values have been found. Supplied data was {2}.".Build(Name, V.Length,XmlData));
303  }
304  else
305  {
306  throw new Exception("Cant parse data for curve {0}. One of the values from the CurveTypeEnum or 256 values between 0-255 are expected. Supplied data was: {1}.".Build(Name,XmlData));
307  }
308  }
309  }
310  break;
311  default:
312  break;
313  }
314 
315  }
316  }
317  }
318 
323  public void WriteXml(System.Xml.XmlWriter writer)
324  {
325  CurveTypeEnum? CT = GetCurveTypeEnum();
326 
327  //writer.WriteStartElement(GetType().ToString());
328  writer.WriteStartElement("Name");
329  writer.WriteString(Name);
330  writer.WriteEndElement();
331 
332  writer.WriteStartElement("Curve");
333  if (CT.HasValue)
334  {
335  writer.WriteString(CT.ToString());
336  }
337  else
338  {
339  StringBuilder S = new StringBuilder();
340  for (int i = 0; i < 256; i++)
341  {
342  S.AppendFormat("{0}, ", Data[i]);
343  }
344  writer.WriteString(S.ToString().Substring(0, S.Length - 2));
345 
346  }
347  writer.WriteEndElement();
348  // writer.WriteEndElement();
349  }
350 
351  #endregion
352  }
353 }
byte MapValue(int CurvePosition)
Returns the value from the specified curve position.
Definition: Curve.cs:108
Curve(CurveTypeEnum CurveType)
Initializes a new instance of the Curve class.
Definition: Curve.cs:231
Abstract base class for named items. Implements the name property and the necessary events...
Curve()
Initializes a new instance of the Curve class.
Definition: Curve.cs:220
System.Xml.Schema.XmlSchema GetSchema()
Part of the IXmlSerializable interface. Must always return null.
Definition: Curve.cs:248
byte MapValue(byte CurvePosition)
Returns the value from the specified curve position.
Definition: Curve.cs:97
void SetCurve(CurveTypeEnum CurveType)
Sets the the fading curve to one of the predefined curves from the FadingCurveTypeEnum.
Definition: Curve.cs:124
void ReadXml(System.Xml.XmlReader reader)
Set the data of the object from its XML representation.
Definition: Curve.cs:257
void WriteXml(System.Xml.XmlWriter writer)
Converts the object to its xml representation.
Definition: Curve.cs:323
CurveTypeEnum
Enumeration of predefined curves.
Definition: Curve.cs:20
Represents a curve which can be used to map values (e.g. adjust a brighnetss value of a led to the br...
Definition: Curve.cs:15
The namespace DirectOutput.General contains classes for general use.