2 using System.Collections.Generic;
5 using DirectOutput.Cab.Out;
6 using System.Xml.Serialization;
7 using DirectOutput.General;
8 using DirectOutput.Cab.Toys.Layer;
9 using DirectOutput.General.Color;
11 namespace DirectOutput.Cab.Toys.Hardware
20 #region Config properties
21 private int _Width = 1;
31 get {
return _Width; }
32 set { _Width = value.Limit(0,
int.MaxValue); }
35 private int _Height = 1;
45 get {
return _Height; }
46 set { _Height = value.Limit(0,
int.MaxValue); }
56 public int NumberOfLeds
58 get {
return Width * Height; }
68 public int NumberOfOutputs
70 get {
return NumberOfLeds * 3; }
86 get {
return _LedStripAranggement; }
87 set { _LedStripAranggement = value; }
103 get {
return _ColorOrder; }
104 set { _ColorOrder = value; }
108 private int _FirstLedNumber = 1;
115 public int FirstLedNumber
117 get {
return _FirstLedNumber; }
118 set { _FirstLedNumber = value.Limit(1,
int.MaxValue); }
121 private string _FadingCurveName =
"Linear";
122 private Curve FadingCurve = null;
131 public string FadingCurveName
133 get {
return _FadingCurveName; }
134 set { _FadingCurveName = value; }
139 if (Cabinet.
Curves.Contains(FadingCurveName))
141 FadingCurve = Cabinet.
Curves[FadingCurveName];
143 else if (!FadingCurveName.IsNullOrWhiteSpace())
148 Enum.TryParse(FadingCurveName, out T);
149 FadingCurve =
new Curve(T);
154 Cabinet.
Curves.Add(FadingCurveName);
171 public string OutputControllerName {
get;
set; }
185 public MatrixDictionaryBase<RGBAColor> Layers {
get;
private set; }
195 public override void Init(Cabinet Cabinet)
197 this.Cabinet = Cabinet;
204 BuildMappingTables();
205 OutputData =
new byte[NumberOfOutputs];
206 InitFadingCurve(Cabinet);
208 Layers =
new MatrixDictionaryBase<RGBAColor>() { Width = Width, Height = Height };
216 public override void Reset()
218 OutputController.SetValues((FirstLedNumber-1) * 3,
new byte[NumberOfLeds]);
224 public override void UpdateOutputs()
226 if (OutputController != null && Layers.Count > 0)
229 OutputController.SetValues((FirstLedNumber-1)*3, OutputData);
248 return Layers[LayerNr];
255 private int[,] OutputMappingTable =
new int[0, 0];
257 private void BuildMappingTables()
260 OutputMappingTable =
new int[Width, Height];
261 bool FirstException =
true;
264 for (
int Y = 0; Y < Height; Y++)
266 for (
int X = 0; X < Width; X++)
269 switch (LedStripArrangement)
272 LedNr = (Y * Width) + X;
275 LedNr = ((Height - 1 - Y) * Width) + X;
278 LedNr = (Y * Width) + (Width - 1 - X);
281 LedNr = ((Height - 1 - Y) * Width) + (Width - 1 - X);
284 LedNr = X * Height + Y;
287 LedNr = ((Width - 1 - X) * Height) + Y;
290 LedNr = (X * Height) + (Height - 1 - Y);
293 LedNr = ((Width - 1 - X) * Height) + (Height - 1 - Y);
296 LedNr = (Width * Y) + ((Y & 1) == 0 ? X : (Width - 1 - X));
299 LedNr = (Width * (Height - 1 - Y)) + (((Height - 1 - Y) & 1) == 0 ? X : (Width - 1 - X));
302 LedNr = (Width * Y) + ((Y & 1) == 1 ? X : (Width - 1 - X));
305 LedNr = (Width * (Height - 1 - Y)) + (((Height - 1 - Y) & 1) == 1 ? X : (Width - 1 - X));
308 LedNr = (Height * X) + ((X & 1) == 0 ? Y : (Height - 1 - Y));
311 LedNr = (Height * (Width - 1 - X)) + ((X & 1) == 1 ? Y : (Height - 1 - Y));
314 LedNr = (Height * X) + ((X & 1) == 1 ? Y : (Height - 1 - Y));
317 LedNr = (Height * (Width - 1 - X)) + ((X & 1) == 0 ? Y : (Height - 1 - Y));
322 Log.
Exception(
"Unknow LedStripArrangement value ({0}) found. Will use LeftRightTopDown mapping as fallback.".Build(LedStripArrangement.ToString()));
323 FirstException =
false;
325 LedNr = (Y * Width) + X;
329 OutputMappingTable[X, Y] = LedNr * 3;
336 byte[] OutputData =
new byte[0];
342 private void SetOutputData()
344 if (Layers.Count > 0)
347 float[, ,] Value =
new float[Width, Height, 3];
349 foreach (KeyValuePair<
int,
RGBAColor[,]> KV
in Layers)
354 for (
int y = 0; y < Height; y++)
356 for (
int x = 0; x < Width; x++)
358 int Alpha = D[x, y].
Alpha.Limit(0, 255);
361 Value[x, y, 0] = AlphaMappingTable.AlphaMapping[255 - Alpha, (int)Value[x, y, 0]] + AlphaMappingTable.AlphaMapping[Alpha, D[x, y].
Red.Limit(0, 255)];
362 Value[x, y, 1] = AlphaMappingTable.AlphaMapping[255 - Alpha, (int)Value[x, y, 1]] + AlphaMappingTable.AlphaMapping[Alpha, D[x, y].
Green.Limit(0, 255)];
363 Value[x, y, 2] = AlphaMappingTable.AlphaMapping[255 - Alpha, (int)Value[x, y, 2]] + AlphaMappingTable.AlphaMapping[Alpha, D[x, y].
Blue.Limit(0, 255)];
374 byte[] FadingTable = FadingCurve.Data;
378 for (
int y = 0; y < Height; y++)
380 for (
int x = 0; x < Width; x++)
382 int OutputNumber = OutputMappingTable[x, y];
383 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 0]];
384 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 1]];
385 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 2]];
390 for (
int y = 0; y < Height; y++)
392 for (
int x = 0; x < Width; x++)
394 int OutputNumber = OutputMappingTable[x, y];
395 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 0]];
396 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 1]];
397 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 2]];
402 for (
int y = 0; y < Height; y++)
404 for (
int x = 0; x < Width; x++)
406 int OutputNumber = OutputMappingTable[x, y];
407 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 0]];
408 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 1]];
409 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 2]];
414 for (
int y = 0; y < Height; y++)
416 for (
int x = 0; x < Width; x++)
418 int OutputNumber = OutputMappingTable[x, y];
419 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 0]];
420 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 1]];
421 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 2]];
426 for (
int y = 0; y < Height; y++)
428 for (
int x = 0; x < Width; x++)
430 int OutputNumber = OutputMappingTable[x, y];
431 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 0]];
432 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 1]];
433 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 2]];
439 for (
int y = 0; y < Height; y++)
441 for (
int x = 0; x < Width; x++)
443 int OutputNumber = OutputMappingTable[x, y];
444 OutputData[OutputNumber] = FadingTable[(int)Value[x, y, 0]];
445 OutputData[OutputNumber + 1] = FadingTable[(int)Value[x, y, 1]];
446 OutputData[OutputNumber + 2] = FadingTable[(int)Value[x, y, 2]];
462 Layers =
new MatrixDictionaryBase<RGBAColor>();