2 using System.Collections.Generic;
6 using System.Threading;
7 using DirectOutput.Cab.Out.FTDIChip;
9 namespace DirectOutput.Cab.Out.AdressableLedStrip
13 const string ControllerNameBase =
"WS2811 Strip Controller ";
16 public void ClearData()
25 FT245R.Write(
new byte[] { (byte)
'C' }, 1, ref Dummy);
32 public void DisplayData(
int Length)
41 FT245R.Write(
new byte[] { (byte)
'O', (byte)(Length / 256), (byte)(Length & 255) }, 3, ref Dummy);
48 public void SetAndDisplayData(byte[] Data)
53 DisplayData(Data.Length);
58 public void SetData(byte[] Data)
60 byte[] Header = { (byte)
'R', (byte)(Data.Length / 256), (byte)(Data.Length & 255) };
68 FT245R.Write(Header, 3, ref Dummy);
69 FT245R.Write(Data, Data.Length, ref Dummy);
70 if (Dummy != Data.Length)
72 Console.WriteLine(
"Stop");
85 Open(ControllerNumber);
89 private int _ControllerNumber;
91 public int ControllerNumber
93 get {
return _ControllerNumber; }
94 private set { _ControllerNumber = value; }
98 public bool DeviceIsPresent
103 return FT245R != null && FT245R.GetRxBytesAvailable(ref Dummy) ==
FTDI.
FT_STATUS.FT_OK;
111 private object FT245RLocker =
new object();
113 public void Open(
int ControllerNumber)
121 this.ControllerNumber = ControllerNumber;
133 FTDI.FT_DEVICE_INFO_NODE[] Devices =
new FTDI.FT_DEVICE_INFO_NODE[DeviceCnt];
135 for (uint i = 0; i < DeviceCnt; i++)
138 Log.
Write(
"Open {0}: Result: {1}".Build(i, FTStatus.ToString()));
146 FTStatus = FT245R.
Close();
147 Log.
Write(
"Close {i}: Result: {1}".Build(i, FTStatus.ToString()));
157 foreach (
FTDI.FT_DEVICE_INFO_NODE DI in Devices)
159 Log.
Write(
"Found {0}".Build(DI.Description));
161 foreach (
FTDI.FT_DEVICE_INFO_NODE DI in Devices)
165 if (DI.Description == ControllerNameBase + ControllerNumber)
168 FT245R.
CharReceived +=
new EventHandler<EventArgs>(FT245R_CharReceived);
173 FTStatus = FT245R.
Purge(
FTDI.FT_PURGE.FT_PURGE_RX +
FTDI.FT_PURGE.FT_PURGE_TX);
181 Log.
Exception(
"Purge failed for WS2811StripController {0} Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
186 Log.
Exception(
"Open failed for WS2811StripController {0}. Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
195 Log.
Exception(
"Could not fetch devicelist for WS2811StripControllers. Error: {0}".Build(FTStatus.ToString()));
207 void FT245R_CharReceived(
object sender, EventArgs e)
209 uint CharsToRead = 0;
216 byte[] Response =
new Byte[CharsToRead];
217 FT245R.
Read(Response, CharsToRead, ref BytesRead);
220 for (
int i = 0; i < BytesRead; i++)
222 if (Response[i] == 0x4e)
233 for (
int i = 0; i < 2000; i++)
235 FT245R.
Write(
new byte[10], 10, ref Dummy);
243 Response =
new Byte[CharsToRead];
245 FT245R.
Read(Response, CharsToRead, ref BytesRead);
268 FT245R.
CharReceived -=
new EventHandler<EventArgs>(FT245R_CharReceived);
277 public static List<int> GetAvailableControllerNumbers()
279 List<int> L =
new List<int>();
286 FTStatus = FTD2xxWrapper.GetNumberOfDevices(ref DeviceCnt);
290 FTDI.FT_DEVICE_INFO_NODE[] Devices =
new FTDI.FT_DEVICE_INFO_NODE[DeviceCnt];
295 foreach (
FTDI.FT_DEVICE_INFO_NODE DI in Devices)
299 int ControllerNr = 0;
300 if (DI.Description.StartsWith(ControllerNameBase) &&
int.TryParse(DI.Description.Substring(ControllerNameBase.Length), out ControllerNr))
302 if (ControllerNr > 0)