2 using System.Collections.Generic;
16 private static readonly
string[] ControllerNameBase = {
"WS2811 Strip Controller",
"Direct Strip Controller" };
31 FT245R.Write(
new byte[] { (byte)
'C' }, 1, ref Dummy);
51 FT245R.Write(
new byte[] { (byte)
'O', (byte)(Length / 256), (byte)(Length & 255) }, 3, ref Dummy);
67 DisplayData(Data.Length);
79 byte[] Header = { (byte)
'R', (byte)(Data.Length / 256), (byte)(Data.Length & 255) };
87 FT245R.Write(Header, 3, ref Dummy);
88 FT245R.Write(Data, Data.Length, ref Dummy);
98 byte[] Data =
new byte[Length];
99 byte[] Header = { (byte)
'S', (byte)(Length / 256), (byte)(Length & 255) };
107 FT245R.CharReceived-=
new EventHandler<EventArgs>(FT245R_CharReceived);
108 FT245R.Write(Header, 3, ref Dummy);
112 FT245R.Read(Data, ((uint)Length), ref BytesRead);
115 FT245R.CharReceived +=
new EventHandler<EventArgs>(FT245R_CharReceived);
135 DisplayData(Data.Length);
149 int DataLength = Data.Length;
150 if (DataLength % 3 != 0)
152 DataLength -= (DataLength % 3);
156 byte[] Packed =
new byte[(int)(DataLength + ((
double)DataLength / 127) + 2)];
159 int BlockStartPos = 0;
162 while (DataPos < DataLength)
165 BlockStartPos = PackedPos++;
166 Packed[PackedPos++] = Data[DataPos++];
167 Packed[PackedPos++] = Data[DataPos++];
168 Packed[PackedPos++] = Data[DataPos++];
171 if (DataPos >= DataLength)
174 Packed[BlockStartPos] = BlockSize;
181 if (Packed[BlockStartPos + 1] != Data[DataPos] || Packed[BlockStartPos + 2] != Data[DataPos + 1] || Packed[BlockStartPos + 3] != Data[DataPos + 2])
185 while (BlockSize < 127 && DataPos < DataLength && ((DataPos >= DataLength - 3) || (Data[DataPos] != Data[DataPos + 3] || Data[DataPos + 1] != Data[DataPos + 4] || Data[DataPos + 2] != Data[DataPos + 5])))
187 Packed[PackedPos++] = Data[DataPos++];
188 Packed[PackedPos++] = Data[DataPos++];
189 Packed[PackedPos++] = Data[DataPos++];
192 Packed[BlockStartPos] = BlockSize;
198 while (BlockSize < 127 && DataPos < DataLength && Packed[BlockStartPos + 1] == Data[DataPos] && Packed[BlockStartPos + 2] == Data[DataPos + 1] && Packed[BlockStartPos + 3] == Data[DataPos + 2])
203 Packed[BlockStartPos] = (byte)(BlockSize + 128);
210 Packed[PackedPos++] = 0;
212 byte[] Header = { (byte)
'P', (byte)(PackedPos / 256), (byte)(PackedPos & 255) };
220 FT245R.Write(Header, 3, ref Dummy);
221 FT245R.Write(Data, Data.Length, ref Dummy);
245 Open(ControllerNumber);
249 private int _ControllerNumber;
257 public int ControllerNumber
259 get {
return _ControllerNumber; }
260 private set { _ControllerNumber = value; }
270 public bool DeviceIsPresent
275 return FT245R != null && FT245R.GetRxBytesAvailable(ref Dummy) ==
FTDI.
FT_STATUS.FT_OK;
283 private object FT245RLocker =
new object();
289 public void Open(
int ControllerNumber)
298 this.ControllerNumber = ControllerNumber;
342 if (ControllerNameBase.Any(N => DI.Description == N.Trim() +
" " + ControllerNumber))
345 Desc = DI.Description;
346 FT245R.
CharReceived +=
new EventHandler<EventArgs>(FT245R_CharReceived);
359 Log.
Exception(
"Purge failed for WS2811StripController {0}. Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
364 Log.
Exception(
"Open failed for WS2811StripController {0}. Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
373 Log.
Exception(
"Could not fetch devicelist for WS2811StripControllers. Error: {0}".Build(FTStatus.ToString()));
380 if (!Desc.IsNullOrWhiteSpace())
382 Log.
Warning(
"{0} detected, but could not open connection.".Build(Desc));
386 Log.
Warning(
"Direct Strip Controller with number {0} not found.".Build(ControllerNumber));
393 Log.
Write(
"{0} detected and connection opend.".Build(Desc));
399 private int ErrorCorrectionCnt = 0;
401 void FT245R_CharReceived(
object sender, EventArgs e)
403 uint CharsToRead = 0;
410 byte[] Response =
new Byte[CharsToRead];
411 FT245R.
Read(Response, CharsToRead, ref BytesRead);
414 for (
int i = 0; i < BytesRead; i++)
416 if (Response[i] == 0x4e)
426 ErrorCorrectionCnt++;
427 if (ErrorCorrectionCnt <= 30)
429 Log.
Warning(
"Received unexpected answer from Direct Strip Controller with number {0}. Will try to fix problem.".Build(ControllerNumber));
430 if (ErrorCorrectionCnt == 30)
432 Log.
Write(
"No further warnings will be recorded for unexpected answers from this unit.");
435 FT245R.
CharReceived -=
new EventHandler<EventArgs>(FT245R_CharReceived);
437 for (
int i = 0; i < 2000; i++)
439 FT245R.
Write(
new byte[10], 10, ref Dummy);
448 Response =
new Byte[CharsToRead];
450 FT245R.
Read(Response, CharsToRead, ref BytesRead);
456 FT245R.
CharReceived +=
new EventHandler<EventArgs>(FT245R_CharReceived);
482 FT245R.
CharReceived -=
new EventHandler<EventArgs>(FT245R_CharReceived);
499 List<int> L =
new List<int>();
506 FTStatus = FTD2xxWrapper.GetNumberOfDevices(ref DeviceCnt);
512 FTStatus = FTD2xxWrapper.GetDeviceList(Devices);
519 string B = ControllerNameBase.FirstOrDefault(N => DI.Description.StartsWith(N.Trim() +
" "));
520 int ControllerNr = 0;
521 if (B != null &&
int.TryParse(DI.Description.Substring(B.Length), out ControllerNr))
523 if (ControllerNr > 0)
byte[] ReadData(int Length)
const byte FT_PURGE_RX
Purge Rx buffer
FT_STATUS Purge(UInt32 purgemask)
Purge data from the devices transmit and/or receive buffers.
FT_STATUS
Status values for FTDI devices.
DirectStripControllerApi(int ControllerNumber)
Initializes a new instance of the DirectStripControllerApi class and opens a connection to the specif...
EventHandler< EventArgs > CharReceived
Occurs when the FDTI devices receives a byte.
FT_STATUS Write(byte[] dataBuffer, Int32 numBytesToWrite, ref UInt32 numBytesWritten)
Write data to an open FTDI device.
void Open(int ControllerNumber)
Opens a connection to the ledstrip controller with the specified controller number.
DirectStripControllerApi()
Initializes a new instance of the DirectStripControllerApi class.
FT_STATUS Read(byte[] dataBuffer, UInt32 numBytesToRead, ref UInt32 numBytesRead)
Read data from an open FTDI device.
static void Warning(string Message)
Writes a warning message to the log.
FT_STATUS GetNumberOfDevices(ref UInt32 devcount)
Gets the number of FTDI devices available.
bool IsOpen
Gets the open status of the device.
FT_STATUS GetDeviceList(FT_DEVICE_INFO_NODE[] devicelist)
Gets information on all of the FTDI devices available.
static void Write(string Message)
Writes the specified message to the logfile.
A simple logger used to record important events and exceptions.
void SetAndDisplayData(byte[] Data)
Sends the specified data to the ledstrip controller and displays the data.
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
void DisplayData(int Length)
Sends a display data command to the ledstrip controller.
static List< int > GetAvailableControllerNumbers()
Gets a list of available ledstrip controller numbers.
void SetPackedData(byte[] Data)
Packs the specified data using a IFF like compression and send the data to the ledstrip controller...
DirectOutput.Cab.Out is the namespace for all output controller related classes like different output...
void Close()
Closes a currently open connection to a ledstrip controller.
void SetData(byte[] Data)
Sends the specified data to the ledstripn controller.
void ClearData()
Sends a clear data buffer command to the Ledstrip controller
Purge buffer constant definitions
Namespace for the FTD2XX_NET class (used for communication with FTDI chips and for generic FTDI chip ...
const byte FT_PURGE_TX
Purge Tx buffer
Type that holds device information for GetDeviceInformation method. Used with FT_GetDeviceInfo and FT...
FT_DEVICE
List of FTDI device types
FT_STATUS GetRxBytesAvailable(ref UInt32 RxQueue)
Gets the number of bytes available in the receive buffer.
FT_STATUS OpenByLocation(UInt32 location)
Opens the FTDI device at the specified physical location.
void SetAndDisplayPackedData(byte[] Data)
Packs the specified data, sends the data to the controller and displays the data. ...
Class wrapper for FTD2XX.DLL
This class handles all communication with Direct StripControllers.
FT_STATUS Close()
Closes the handle to an open FTDI device.
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.