diff --git a/BlinkStick.Hid/BlinkStick.Hid.sln b/BlinkStick.Hid/BlinkStick.Hid.sln deleted file mode 100644 index 9991f59..0000000 --- a/BlinkStick.Hid/BlinkStick.Hid.sln +++ /dev/null @@ -1,20 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlinkStick.Hid", "BlinkStick.Hid.csproj", "{7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = BlinkStick.Hid.csproj - EndGlobalSection -EndGlobal diff --git a/BlinkStick.Hid/BlinkstickHid.cs b/BlinkStick.Hid/BlinkstickHid.cs deleted file mode 100644 index 20007c2..0000000 --- a/BlinkStick.Hid/BlinkstickHid.cs +++ /dev/null @@ -1,629 +0,0 @@ -#region License -// Copyright 2013 by Agile Innovative Ltd -// -// This file is part of BlinkStick.HID library. -// -// BlinkStick.HID library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) any -// later version. -// -// BlinkStick.HID library is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with -// BlinkStick.HID library. If not, see http://www.gnu.org/licenses/. -#endregion - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; -using HidSharp; - -namespace BlinkStick.Hid -{ - /// - /// Main class to access Blinkstick HID devices. - /// - public class BlinkstickHid : IDisposable - { - protected const int VendorId = 0x20A0; - protected const int ProductId = 0x41E5; - - private HidDevice device; - private HidStream stream; - - private bool disposed = false; - - protected bool connectedToDriver = false; - - /// - /// Gets a value indicating whether this is connected. - /// - /// true if connected; otherwise, false. - public Boolean Connected { - get { - return connectedToDriver; - } - } - - /// - /// Gets the serial number of BlinkStick. - /// - /// The serial. - public String Serial { - get { - return device.SerialNumber; - } - } - - /// - /// Gets the name of the manufacturer. - /// - /// The name of the manufacturer. - public String ManufacturerName { - get { - return device.Manufacturer; - } - } - - /// - /// Gets the product name of the device. - /// - /// The name of the product. - public String ProductName { - get { - return device.ProductName; - } - } - - private String _InfoBlock1; - /// - /// Gets or sets the name of the device (InfoBlock1). - /// - /// The name. - public String InfoBlock1 { - get { - if (_InfoBlock1 == null) { - GetInfoBlock (2, out _InfoBlock1); - } - - return _InfoBlock1; - } - set { - if (_InfoBlock1 != value) - { - _InfoBlock1 = value; - SetInfoBlock(2, _InfoBlock1); - } - } - } - - private String _InfoBlock2; - /// - /// Gets or sets the data of the device (InfoBlock2). - /// - /// The data. - public String InfoBlock2 { - get { - if (_InfoBlock2 == null) { - GetInfoBlock (3, out _InfoBlock2); - } - - return _InfoBlock2; - } - set { - if (_InfoBlock2 != value) - { - _InfoBlock2 = value; - SetInfoBlock(3, _InfoBlock2); - } - } - } - - /// - /// Sets the info block. - /// - /// 2 - InfoBlock1, 3 - InfoBlock2 - /// Maximum 32 bytes of data - private void SetInfoBlock (byte id, string data) - { - SetInfoBlock(id, Encoding.ASCII.GetBytes(data)); - } - - private Boolean GetInfoBlock (byte id, out string data) - { - byte[] dataBytes; - Boolean result = GetInfoBlock (id, out dataBytes); - - if (result) { - for (int i = 1; i < dataBytes.Length; i++) { - if (dataBytes [i] == 0) { - Array.Resize (ref dataBytes, i); - break; - } - } - - data = Encoding.ASCII.GetString (dataBytes, 1, dataBytes.Length - 1); - } else { - data = ""; - } - - return result; - } - - /// - /// Sets the color of the led. - /// - /// Must be in #rrggbb format - public void SetColor(String color) - { - if (!IsValidColor(color)) - throw new Exception("Color value is invalid"); - - SetColor( - Convert.ToByte(color.Substring(1, 2), 16), - Convert.ToByte(color.Substring(3, 2), 16), - Convert.ToByte(color.Substring(5, 2), 16)); - } - - /// - /// Sets the color of the led. - /// - /// Channel (0 - R, 1 - G, 2 - B) - /// Index of the LED - /// The red component. - /// The green component. - /// The blue component. - public void SetColor(byte channel, byte index, byte r, byte g, byte b) - { - if (connectedToDriver) - { - byte [] data = new byte[6]; - data[0] = 5; - data[1] = channel; - data[2] = index; - data[3] = r; - data[4] = g; - data[5] = b; - - stream.SetFeature(data); - } - } - - /// - /// Sets the color of the led. - /// - /// Channel (0 - R, 1 - G, 2 - B) - /// Index of the LED - /// Must be in #rrggbb format - public void SetColor(byte channel, byte index, string color) - { - if (!IsValidColor(color)) - throw new Exception("Color value is invalid"); - - SetColor( - channel, - index, - Convert.ToByte(color.Substring(1, 2), 16), - Convert.ToByte(color.Substring(3, 2), 16), - Convert.ToByte(color.Substring(5, 2), 16)); - } - - /// - /// Sets the mode for BlinkStick Pro. - /// - /// 0 - Normal, 1 - Inverse, 2 - WS2812 - public void SetMode(byte mode) - { - if (connectedToDriver) - { - byte [] data = new byte[2]; - data[0] = 4; - data[1] = mode; - stream.SetFeature(data); - } - } - - /// - /// Turn BlinkStick off. - /// - public void TurnOff() - { - SetColor(0, 0, 0); - } - - /// - /// Send a packet of data to LEDs - /// - /// Channel (0 - R, 1 - G, 2 - B) - /// Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] - public void SetColors(byte channel, byte[] reportData) - { - int max_leds = 64; - byte reportId = 9; - - //Automatically determine the correct report id to send the data to - if (reportData.Length <= 8 * 3) - { - max_leds = 8; - reportId = 6; - } - else if (reportData.Length <= 16 * 3) - { - max_leds = 16; - reportId = 7; - } - else if (reportData.Length <= 32 * 3) - { - max_leds = 32; - reportId = 8; - } - else if (reportData.Length <= 64 * 3) - { - max_leds = 64; - reportId = 9; - } - else if (reportData.Length <= 128 * 3) - { - max_leds = 64; - reportId = 10; - } - - byte [] data = new byte[max_leds * 3 + 2]; - data[0] = reportId; - data[1] = channel; // chanel index - - for (int i = 0; i < Math.Min(reportData.Length, data.Length - 2); i++) - { - data[i + 2] = reportData[i]; - } - - for (int i = reportData.Length + 2; i < data.Length; i++) - { - data[i] = 0; - } - - stream.SetFeature(data); - - if (reportId == 10) - { - //System.Threading.Thread.Sleep(1); - - for (int i = 0; i < Math.Min(data.Length - 2, reportData.Length - 64 * 3); i++) - { - data[i + 2] = reportData[64 * 3 + i]; - } - - for (int i = reportData.Length + 2 - 64 * 3; i < data.Length; i++) - { - data[i] = 0; - } - - data[0] = (byte)(reportId + 1); - - stream.SetFeature(data); - } - } - - /// - /// Determines if is valid color format of the specified string (#rrggbb). - /// - /// true if is valid color the specified color; otherwise, false. - /// Color. - public static Boolean IsValidColor (String color) - { - return Regex.IsMatch(color, "^#[A-Fa-f0-9]{6}$"); - } - - /// - /// Occurs when BlinkStick device is attached. - /// - public event EventHandler DeviceAttached; - - /// - /// Occurs when a BlinkStick device is removed. - /// - public event EventHandler DeviceRemoved; - - /// - /// Initializes a new instance of the BlinkstickHid class. - /// - public BlinkstickHid() - { - } - - /// - /// Attempts to connect to a BlinkStick device. - /// - /// After a successful connection, a DeviceAttached event will normally be sent. - /// - /// True if a Blinkstick device is connected, False otherwise. - public bool OpenDevice () - { - if (this.device == null) { - HidDeviceLoader loader = new HidDeviceLoader(); - HidDevice adevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault(); - return OpenDevice (adevice); - } else { - return OpenCurrentDevice(); - } - } - - /// - /// Opens the device. - /// - /// true, if device was opened, false otherwise. - /// Pass the parameter of HidDevice to open it directly - public bool OpenDevice(HidDevice adevice) - { - if (adevice != null) - { - this.device = adevice; - - return OpenCurrentDevice(); - } - - return false; - } - - /// - /// Opens the current device. - /// - /// true, if current device was opened, false otherwise. - private bool OpenCurrentDevice() - { - connectedToDriver = true; - device.TryOpen(out stream); - - //!!!device.Inserted += DeviceAttachedHandler; - //!!!device.Removed += DeviceRemovedHandler; - - return true; - } - - /// - /// Find all BlinkStick devices. - /// - /// The devices. - public static BlinkstickHid[] AllDevices () - { - List result = new List(); - - HidDeviceLoader loader = new HidDeviceLoader(); - foreach (HidDevice adevice in loader.GetDevices(VendorId, ProductId).ToArray()) - { - BlinkstickHid hid = new BlinkstickHid(); - hid.device = adevice; - result.Add(hid); - } - - return result.ToArray(); - } - - /// - /// Find first BlinkStick. - /// - /// BlinkStickHid device if found, otherwise null if no devices found - public static BlinkstickHid FirstDevice() - { - BlinkstickHid[] devices = AllDevices(); - - return devices.Length > 0 ? devices[0] : null; - } - - /// - /// Finds BlinkStick by serial number. - /// - /// BlinkStickHid device if found, otherwise null if no devices found - /// Serial number to search for - public static BlinkstickHid FindBySerial(String serial) - { - foreach (BlinkstickHid device in AllDevices()) - { - if (device.Serial == serial) - return device; - } - - return null; - } - - - /// - /// Closes the connection to the device. - /// - public void CloseDevice() - { - stream.Close(); - device = null; - connectedToDriver = false; - } - - /// - /// Closes the connection to the device. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - private void DeviceAttachedHandler() - { - if (DeviceAttached != null) - DeviceAttached(this, EventArgs.Empty); - } - - private void DeviceRemovedHandler() - { - if (DeviceRemoved != null) - DeviceRemoved(this, EventArgs.Empty); - } - - /// - /// Sets the color of the led. - /// - /// The red component. - /// The green component. - /// The blue component. - public void SetColor(byte r, byte g, byte b) - { - if (connectedToDriver) - { - byte [] data = new byte[4]; - data[0] = 1; - data[1] = r; - data[2] = g; - data[3] = b; - - stream.SetFeature(data); - } - } - - /// - /// Gets the color of the led. - /// - /// true, if led color was gotten, false otherwise. - /// The red component. - /// The green component. - /// The blue component. - public Boolean GetColor (out byte r, out byte g, out byte b) - { - byte[] report = new byte[33]; - report[0] = 1; - - if (connectedToDriver) { - stream.GetFeature(report, 0, 33); - - r = report [1]; - g = report [2]; - b = report [3]; - - return true; - } else { - r = 0; - g = 0; - b = 0; - - return false; - } - } - - /// - /// Gets the led data. - /// - /// true, if led data was gotten, false otherwise. - /// Data. - public Boolean GetData (out byte[] data) - { - if (connectedToDriver) - { - data = new byte[3 * 8 * 8 + 1]; - data[0] = 9; - stream.GetFeature(data, 0, data.Length); - return true; - } - else - { - data = new byte[0]; - return false; - } - - } - - protected void SetInfoBlock (byte id, byte[] data) - { - if (id == 2 || id == 3) { - if (data.Length > 32) - { - Array.Resize(ref data, 32); - } - else if (data.Length < 32) - { - int size = data.Length; - - Array.Resize(ref data, 32); - - //pad with zeros - for (int i = size; i < 32; i++) - { - data[i] = 0; - } - } - - Array.Resize(ref data, 33); - - - for (int i = 32; i >0; i--) - { - data[i] = data[i-1]; - } - - data[0] = id; - - stream.SetFeature(data); - } else { - throw new Exception("Invalid info block id"); - } - } - - /// - /// Gets the info block. - /// - /// true, if info block was gotten, false otherwise. - /// Identifier. - /// Data. - public Boolean GetInfoBlock (byte id, out byte[] data) - { - if (id == 2 || id == 3) { - data = new byte[33]; - data[0] = id; - - if (connectedToDriver) - { - stream.GetFeature(data, 0, data.Length); - return true; - } - else - { - data = new byte[0]; - return false; - } - } else { - throw new Exception("Invalid info block id"); - } - } - - - /// - /// Closes any connected devices. - /// - /// - private void Dispose(bool disposing) - { - if(!this.disposed) - { - if(disposing) - { - CloseDevice(); - } - - disposed = true; - } - } - - /// - /// Destroys instance and frees device resources (if not freed already) - /// - ~BlinkstickHid() - { - Dispose(false); - } - - } -} - diff --git a/BlinkStick.Hid/RgbColor.cs b/BlinkStick.Hid/RgbColor.cs deleted file mode 100644 index efd60f6..0000000 --- a/BlinkStick.Hid/RgbColor.cs +++ /dev/null @@ -1,113 +0,0 @@ -#region License -// Copyright 2013 by Agile Innovative Ltd -// -// This file is part of BlinkStick application. -// -// BlinkStick application is free software: you can redistribute -// it and/or modify it under the terms of the GNU General Public License as published -// by the Free Software Foundation, either version 3 of the License, or (at your option) -// any later version. -// -// BlinkStick application is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with -// BlinkStick application. If not, see http://www.gnu.org/licenses/. -#endregion - -using System; - -namespace BlinkStick.Hid -{ - public class RgbColor - { - /// - /// The Red byte component. - /// - public Byte R; - - /// - /// The Green byte component. - /// - public Byte G; - - /// - /// The Blue byte component. - /// - public Byte B; - - public RgbColor () - { - } - - /// - /// Froms the rgb value from int components. - /// - /// The rgb. - /// The red component. - /// The green component. - /// The blue component. - public static RgbColor FromRgb(int r, int g, int b) - { - RgbColor color = new RgbColor(); - color.R = (byte)r; - color.G = (byte)g; - color.B = (byte)b; - - return color; - } - - /// - /// Froms the color of the GDK color. - /// - /// The gdk color. - /// The red component. - /// The green component. - /// The blue component. - public static RgbColor FromGdkColor(ushort r, ushort g, ushort b) - { - RgbColor color = new RgbColor(); - - color.R = (byte)(r / 0x100); - color.G = (byte)(g / 0x100); - color.B = (byte)(b / 0x100); - - return color; - } - - /// - /// Converts HEX string to RGB color. For example #123456 - /// - /// The string. - /// Color string. - public static RgbColor FromString (String colorStr) - { - RgbColor color = new RgbColor(); - color.R = Convert.ToByte(colorStr.Substring(1, 2), 16); - color.G = Convert.ToByte(colorStr.Substring(3, 2), 16); - color.B = Convert.ToByte(colorStr.Substring(5, 2), 16); - - return color; - } - - /// - /// Get black color. - /// - public static RgbColor Black () - { - return RgbColor.FromRgb(0, 0, 0); - } - - /// - /// Returns a string that represents the current object. - /// - /// A string that represents the current object. - /// 2 - public override string ToString() - { - return string.Format("{0:x2}{1:x2}{2:x2}", this.R, this.G, this.B); - } - } -} - diff --git a/BlinkStick.Hid/UsbMonitor.cs b/BlinkStick.Hid/UsbMonitor.cs deleted file mode 100644 index 25c9df9..0000000 --- a/BlinkStick.Hid/UsbMonitor.cs +++ /dev/null @@ -1,106 +0,0 @@ -#region License -// Copyright 2013 by Agile Innovative Ltd -// -// This file is part of BlinkStick.HID library. -// -// BlinkStick.HID library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) any -// later version. -// -// BlinkStick.HID library is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with -// BlinkStick.HID library. If not, see http://www.gnu.org/licenses/. -#endregion - -using System; -using LibUsbDotNet.DeviceNotify; - -namespace BlinkStick.Hid -{ - public class UsbMonitor - { - /// - /// Occurs when usb device added. - /// - public event EventHandler UsbDeviceAdded; - - /// - /// Raises the usb device added event. - /// - protected void OnUsbDeviceAdded() - { - if (UsbDeviceAdded != null) - { - UsbDeviceAdded(this, new EventArgs()); - } - } - - private WinUsbDeviceMonitor winUsbDeviceMonitor; - public IDeviceNotifier UsbDeviceNotifier; - - public Boolean Monitoring { - get; - private set; - } - - public UsbMonitor (IntPtr mainWindowHandle) - { - switch (HidSharp.PlatformDetector.RunningPlatform()) - { - case HidSharp.PlatformDetector.Platform.Windows: - winUsbDeviceMonitor = new WinUsbDeviceMonitor(); - winUsbDeviceMonitor.DeviceListChanged += HandleDeviceListChanged; - break; - case HidSharp.PlatformDetector.Platform.Linux: - UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier(); - UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent; - break; - } - } - - private void HandleDeviceListChanged (object sender, EventArgs e) - { - OnUsbDeviceAdded(); - } - - /// - /// Start monitoring for added/removed BlinkStick devices. - /// - public void Start () - { - if (UsbDeviceNotifier != null) { - UsbDeviceNotifier.Enabled = true; - } - - Monitoring = true; - } - - /// - /// Stop monitoring for added/removed BlinkStick devices. - /// - public void Stop () - { - if (UsbDeviceNotifier != null) { - UsbDeviceNotifier.Enabled = false; // Disable the device notifier - - UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent; - } - - Monitoring = false; - } - - private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e) - { - OnUsbDeviceAdded(); - } - - ~UsbMonitor () - { - } - } -} - diff --git a/BlinkStickDotNet.sln b/BlinkStickDotNet.sln index 4b7b833..ff3f7c2 100644 --- a/BlinkStickDotNet.sln +++ b/BlinkStickDotNet.sln @@ -5,7 +5,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibUsbDotNet", "Components\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp", "Components\HidSharp\HidSharp.csproj", "{0DB86674-2A7B-4BDC-93C1-3F7DC771426C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlinkStick.Hid", "BlinkStick.Hid\BlinkStick.Hid.csproj", "{7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlinkStickDotNet", "BlinkStickDotNet\BlinkStickDotNet.csproj", "{7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/BlinkStick.Hid/AssemblyInfo.cs b/BlinkStickDotNet/AssemblyInfo.cs similarity index 96% rename from BlinkStick.Hid/AssemblyInfo.cs rename to BlinkStickDotNet/AssemblyInfo.cs index aaa106c..8aa7ebf 100644 --- a/BlinkStick.Hid/AssemblyInfo.cs +++ b/BlinkStickDotNet/AssemblyInfo.cs @@ -17,7 +17,7 @@ // The form "{Major}.{Minor}.*" will automatically update the build and revision, // and "{Major}.{Minor}.{Build}.*" will update just the revision. -[assembly: AssemblyVersion("1.0.3.*")] +[assembly: AssemblyVersion("1.0.*")] // The following attributes are used to specify the signing key for the assembly, // if desired. See the Mono documentation for more information about signing. diff --git a/BlinkStickDotNet/BlinkStick.cs b/BlinkStickDotNet/BlinkStick.cs new file mode 100644 index 0000000..09c16dd --- /dev/null +++ b/BlinkStickDotNet/BlinkStick.cs @@ -0,0 +1,1391 @@ +#region License +// Copyright 2013 by Agile Innovative Ltd +// +// This file is part of BlinkStick.HID library. +// +// BlinkStick.HID library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) any +// later version. +// +// BlinkStick.HID library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// BlinkStick.HID library. If not, see http://www.gnu.org/licenses/. +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.Threading; +using HidSharp; + +namespace BlinkStickDotNet +{ + /// + /// BlinkStick class is designed to control regular BlinkStick devices. + /// Code examples on how you can use this class are available + /// wiki. + /// + public class BlinkStick : IDisposable + { + #region Private Properties + protected const int VendorId = 0x20A0; + protected const int ProductId = 0x41E5; + + private HidDevice device; + private HidStream stream; + + private bool disposed = false; + + private bool stopped = false; + + protected bool connectedToDriver = false; + + private bool _RequiresSoftwareColorPatch = false; + #endregion + + #region Events + public event SendColorEventHandler SendColor; + + public Boolean OnSendColor(byte channel, byte index, byte r, byte g, byte b) + { + if (SendColor != null) + { + SendColorEventArgs args = new SendColorEventArgs(channel, index, r, g, b); + SendColor(this, args); + return args.SendToDevice; + } + + return true; + } + + public event ReceiveColorEventHandler ReceiveColor; + + public Boolean OnReceiveColor(byte index, out byte r, out byte g, out byte b) + { + if (ReceiveColor != null) + { + ReceiveColorEventArgs args = new ReceiveColorEventArgs(index); + ReceiveColor(this, args); + r = args.R; + g = args.G; + b = args.B; + return true; + } + r = 0; + g = 0; + b = 0; + return false; + } + #endregion + + #region Device Properties + /// + /// Gets a value indicating whether this is connected. + /// + /// true if connected; otherwise, false. + public Boolean Connected { + get { + return connectedToDriver; + } + } + + /// + /// Returns the serial number of BlinkStick. + ///
+        /// BSnnnnnn-1.0
+        /// ||  |    | |- Software minor version
+        /// ||  |    |--- Software major version
+        /// ||  |-------- Denotes sequential number
+        /// ||----------- Denotes BlinkStick device
+        /// 
+ /// + /// Software version defines the capabilities of the device + ///
+ /// Returns the serial. + public String Serial { + get { + return device.SerialNumber; + } + } + + private int _VersionMajor = -1; + + /// + /// Gets the major version number from serial number. + /// + /// Returns the major version number. + public int VersionMajor { + get { + if (_VersionMajor == -1) + { + try + { + _VersionMajor = Convert.ToInt32(this.Serial.Substring(this.Serial.Length - 3, 1)); + } + catch + { + _VersionMajor = 0; + } + } + + return _VersionMajor; + } + } + + private int _VersionMinor = -1; + + /// + /// Gets the minor version number from serial number. + /// + /// Returns the version minor. + public int VersionMinor { + get { + if (_VersionMinor == -1) + { + try + { + _VersionMinor = Convert.ToInt32(this.Serial.Substring(this.Serial.Length - 1, 1)); + } + catch + { + _VersionMinor = 0; + } + } + + return _VersionMinor; + } + } + + /// + /// Gets the device type + /// + /// Returns the device type. + public BlinkStickDeviceEnum BlinkStickDevice + { + get + { + if (VersionMajor == 1) + { + return BlinkStickDeviceEnum.BlinkStick; + } + else if (VersionMajor == 2) + { + return BlinkStickDeviceEnum.BlinkStickPro; + } + else if (VersionMajor == 3) + { + if (device.ProductVersion == 0x0200) + { + return BlinkStickDeviceEnum.BlinkStickSquare; + } + else if (device.ProductVersion == 0x0201) + { + return BlinkStickDeviceEnum.BlinkStickStrip; + } + else if (device.ProductVersion == 0x0202) + { + return BlinkStickDeviceEnum.BlinkStickNano; + } + else if (device.ProductVersion == 0x0203) + { + return BlinkStickDeviceEnum.BlinkStickFlex; + } + } + + return BlinkStickDeviceEnum.Unknown; + } + } + + public static BlinkStickDeviceEnum BlinkStickDeviceFromSerial(string serial) + { + int versionMajor = Convert.ToInt32(serial.Substring(serial.Length - 3, 1)); + + if (versionMajor == 1) + { + return BlinkStickDeviceEnum.BlinkStick; + } + else if (versionMajor == 2) + { + return BlinkStickDeviceEnum.BlinkStickPro; + } + else if (versionMajor == 3) + { + return BlinkStickDeviceEnum.BlinkStickSquare; + } + + return BlinkStickDeviceEnum.Unknown; + } + + /// + /// Gets the name of the manufacturer. + /// + /// Returns the name of the manufacturer. + public String ManufacturerName { + get { + return device.Manufacturer; + } + } + + /// + /// Gets the product name of the device. + /// + /// Returns the name of the product. + public String ProductName { + get { + return device.ProductName; + } + } + + private String _InfoBlock1; + /// + /// Gets or sets the name of the device (InfoBlock1). + /// + /// String value of InfoBlock1 + public String InfoBlock1 { + get { + if (_InfoBlock1 == null) { + GetInfoBlock (2, out _InfoBlock1); + } + + return _InfoBlock1; + } + set { + if (_InfoBlock1 != value) + { + _InfoBlock1 = value; + SetInfoBlock(2, _InfoBlock1); + } + } + } + + private String _InfoBlock2; + /// + /// Gets or sets the data of the device (InfoBlock2). + /// + /// String value of InfoBlock2 + public String InfoBlock2 { + get { + if (_InfoBlock2 == null) { + GetInfoBlock (3, out _InfoBlock2); + } + + return _InfoBlock2; + } + set { + if (_InfoBlock2 != value) + { + _InfoBlock2 = value; + SetInfoBlock(3, _InfoBlock2); + } + } + } + + public int SetColorDelay { get; set; } + + private int _Mode = -1; + + /// + /// Gets or sets the mode of BlinkStick device. + /// + /// The mode to set or get. + public int Mode + { + get + { + if (_Mode == -1) + { + _Mode = GetMode(); + } + + return _Mode; + } + set + { + if (_Mode != value) + { + _Mode = value; + SetMode((byte)_Mode); + } + } + } + #endregion + + #region Constructor/Destructor + /// + /// Initializes a new instance of the BlinkStick class. + /// + public BlinkStick() + { + SetColorDelay = 0; + } + + /// + /// Disposes of the device and closes the connection. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Closes any connected devices. + /// + /// + private void Dispose(bool disposing) + { + if(!this.disposed) + { + if(disposing) + { + CloseDevice(); + } + + disposed = true; + } + } + + /// + /// Destroys instance and frees device resources (if not freed already) + /// + ~BlinkStick() + { + Dispose(false); + } + #endregion + + #region Device Open/Close functions + /// + /// Attempts to connect to a BlinkStick device. + /// + /// After a successful connection, a DeviceAttached event will normally be sent. + /// + /// True if a Blinkstick device is connected, False otherwise. + public bool OpenDevice () + { + bool result; + + this._VersionMajor = -1; + this._VersionMinor = -1; + + if (this.device == null) { + HidDeviceLoader loader = new HidDeviceLoader(); + HidDevice adevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault(); + result = OpenDevice (adevice); + } else { + result = OpenCurrentDevice(); + } + + CheckRequiresSoftwareColorPatch(); + + return result; + } + + /// + /// Opens the device. + /// + /// true, if device was opened, false otherwise. + /// Pass the parameter of HidDevice to open it directly + public bool OpenDevice(HidDevice adevice) + { + if (adevice != null) + { + this.device = adevice; + + return OpenCurrentDevice(); + } + + return false; + } + + /// + /// Opens the current device. + /// + /// true, if current device was opened, false otherwise. + private bool OpenCurrentDevice() + { + connectedToDriver = true; + device.TryOpen(out stream); + + return true; + } + + /// + /// Closes the connection to the device. + /// + public void CloseDevice() + { + stream.Close(); + device = null; + connectedToDriver = false; + } + #endregion + + #region Helper functions for InfoBlocks + /// + /// Sets the info block. + /// + /// 2 - InfoBlock1, 3 - InfoBlock2 + /// Maximum 32 bytes of data + private void SetInfoBlock (byte id, string data) + { + SetInfoBlock(id, Encoding.ASCII.GetBytes(data)); + } + + private Boolean GetInfoBlock (byte id, out string data) + { + byte[] dataBytes; + Boolean result = GetInfoBlock (id, out dataBytes); + + if (result) { + for (int i = 1; i < dataBytes.Length; i++) { + if (dataBytes [i] == 0) { + Array.Resize (ref dataBytes, i); + break; + } + } + + data = Encoding.ASCII.GetString (dataBytes, 1, dataBytes.Length - 1); + } else { + data = ""; + } + + return result; + } + + protected void SetInfoBlock (byte id, byte[] data) + { + if (id == 2 || id == 3) { + if (data.Length > 32) + { + Array.Resize(ref data, 32); + } + else if (data.Length < 32) + { + int size = data.Length; + + Array.Resize(ref data, 32); + + //pad with zeros + for (int i = size; i < 32; i++) + { + data[i] = 0; + } + } + + Array.Resize(ref data, 33); + + + for (int i = 32; i >0; i--) + { + data[i] = data[i-1]; + } + + data[0] = id; + + SetFeature(data); + } else { + throw new Exception("Invalid info block id"); + } + } + + /// + /// Gets the info block. + /// + /// true, if info block was received, false otherwise. + /// Identifier. + /// Data. + public Boolean GetInfoBlock (byte id, out byte[] data) + { + if (id == 2 || id == 3) { + data = new byte[33]; + data[0] = id; + + if (connectedToDriver) + { + int attempt = 0; + while (attempt < 5) + { + attempt++; + try + { + stream.GetFeature(data, 0, data.Length); + break; + } + catch (System.IO.IOException e) + { + if (e.InnerException is System.ComponentModel.Win32Exception) + { + System.ComponentModel.Win32Exception win32Exception = e.InnerException as System.ComponentModel.Win32Exception; + + if (win32Exception != null && win32Exception.NativeErrorCode == 0) + return true; + } + + if (attempt == 5) + throw; + + if (!WaitThread(20)) + return false; + } + } + + return true; + } + else + { + data = new byte[0]; + return false; + } + } else { + throw new Exception("Invalid info block id"); + } + } + #endregion + + #region Color manipulation functions + /// + /// Sets the color of the led. + /// + /// Must be in #rrggbb format + public void SetColor(String color) + { + SetColor(RgbColor.FromString(color)); + } + + /// + /// Sets the color of the led. + /// + /// Color as RgbColor class. + public void SetColor(RgbColor color) + { + SetColor(color.R, color.G, color.B); + } + + /// + /// Sets the color of the led. + /// + /// The red component. + /// The green component. + /// The blue component. + public void SetColor(byte r, byte g, byte b) + { + if (!OnSendColor(0, 0, r, g, b)) + return; + + if (connectedToDriver) + { + if (_RequiresSoftwareColorPatch) + { + byte cr, cg, cb; + if (GetColor(out cr, out cg, out cb)) + { + if (r == cg && g == cr && b == cb) + { + if (cr > 0) + { + SetFeature(new byte[4] { 1, (byte)(cr - 1), cg, cb }); + } + else if (cg > 0) + { + SetFeature(new byte[4] { 1, cr, (byte)(cg - 1), cb }); + } + } + } + } + + SetFeature(new byte[4] {1, r, g, b}); + } + } + + /// + /// Gets the color of the led. + /// + /// true, if led color was received, false otherwise. + /// The red component. + /// The green component. + /// The blue component. + public Boolean GetColor (out byte r, out byte g, out byte b) + { + if (OnReceiveColor(0, out r, out g, out b)) + return true; + + byte[] report = new byte[33]; + report[0] = 1; + + if (connectedToDriver) { + int attempt = 0; + while (attempt < 5) + { + attempt++; + try + { + stream.GetFeature(report, 0, 33); + break; + } + catch + { + if (attempt == 5) + throw; + + if (!WaitThread(20)) + return false; + } + } + + + r = report [1]; + g = report [2]; + b = report [3]; + + return true; + } else { + r = 0; + g = 0; + b = 0; + + return false; + } + } + + /// + /// Turn BlinkStick off. + /// + public void TurnOff() + { + SetColor(0, 0, 0); + } + #endregion + + #region Color manipulation functions for BlinkStick Pro + /// + /// Sets the color of the led. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// The red component. + /// The green component. + /// The blue component. + public void SetColor(byte channel, byte index, byte r, byte g, byte b) + { + if (!OnSendColor(channel, index, r, g, b)) + return; + + if (connectedToDriver) + { + SetFeature(new byte[6] { 5, channel, index, r, g, b }); + } + } + + /// + /// Sets the color of the led. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Must be in #rrggbb format or named color ("red", "green", "blue") + public void SetColor(byte channel, byte index, string color) + { + SetColor(channel, index, RgbColor.FromString(color)); + } + + /// + /// Sets the color of the led. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Color parameter as RgbColor class instance + public void SetColor(byte channel, byte index, RgbColor color) + { + SetColor(channel, index, color.R, color.G, color.B); + } + + /// + /// Send a packet of data to LEDs + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + public void SetColors(byte channel, byte[] colorData) + { + int max_leds = 64; + byte reportId = 9; + + //Automatically determine the correct report id to send the data to + if (colorData.Length <= 8 * 3) + { + max_leds = 8; + reportId = 6; + } + else if (colorData.Length <= 16 * 3) + { + max_leds = 16; + reportId = 7; + } + else if (colorData.Length <= 32 * 3) + { + max_leds = 32; + reportId = 8; + } + else if (colorData.Length <= 64 * 3) + { + max_leds = 64; + reportId = 9; + } + else if (colorData.Length <= 128 * 3) + { + max_leds = 64; + reportId = 10; + } + + byte [] data = new byte[max_leds * 3 + 2]; + data[0] = reportId; + data[1] = channel; // chanel index + + for (int i = 0; i < Math.Min(colorData.Length, data.Length - 2); i++) + { + data[i + 2] = colorData[i]; + } + + for (int i = colorData.Length + 2; i < data.Length; i++) + { + data[i] = 0; + } + + SetFeature(data); + + if (reportId == 10) + { + for (int i = 0; i < Math.Min(data.Length - 2, colorData.Length - 64 * 3); i++) + { + data[i + 2] = colorData[64 * 3 + i]; + } + + for (int i = colorData.Length + 2 - 64 * 3; i < data.Length; i++) + { + data[i] = 0; + } + + data[0] = (byte)(reportId + 1); + + SetFeature(data); + } + } + + /// + /// Gets led data. + /// + /// true, if led data was received, false otherwise. + /// LED data as an array of colors [g0, r0, b0, g1, r1, b1 ...] + public Boolean GetColors (out byte[] colorData) + { + if (connectedToDriver) + { + byte[] data = new byte[3 * 8 * 8 + 2]; + data[0] = 9; + stream.GetFeature(data, 0, data.Length); + + colorData = new byte[3 * 8 * 8]; + Array.Copy(data, 2, colorData, 0, colorData.Length); + + return true; + } + else + { + colorData = new byte[0]; + return false; + } + + } + + + /// + /// Gets the color of the led. + /// + /// true, if led color was received, false otherwise. + /// The red component. + /// The green component. + /// The blue component. + public Boolean GetColor (byte index, out byte r, out byte g, out byte b) + { + if (OnReceiveColor(index, out r, out g, out b)) + return true; + + if (index == 0) + { + return this.GetColor(out r, out g, out b); + } + + byte[] colors; + this.GetColors(out colors); + + if (colors.Length >= (index + 1) * 3) + { + r = colors[index * 3 + 1]; + g = colors[index * 3]; + b = colors[index * 3 + 2]; + + return true; + } + else + { + r = 0; + g = 0; + b = 0; + + return false; + } + } + #endregion + + #region BlinkStick Pro mode selection + /// + /// Sets the mode for BlinkStick Pro. + /// + /// 0 - Normal, 1 - Inverse, 2 - WS2812 + public void SetMode(byte mode) + { + if (connectedToDriver) + { + SetFeature(new byte[2] {4, mode}); + } + } + + /// + /// Gets the mode on BlinkStick Pro. + /// + /// 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + public int GetMode() + { + if (connectedToDriver) + { + byte[] data = new byte[2]; + data[0] = 4; + GetFeature(data); + return data[1]; + } + + return -1; + } + #endregion + + #region BlinkStick Flex features + public void SetLedCount(byte count) + { + if (connectedToDriver) + { + SetFeature(new byte[2] {0x81, count}); + } + } + + public int GetLedCount() + { + if (connectedToDriver) + { + byte[] data = new byte[2]; + data[0] = 0x81; + stream.GetFeature(data, 0, data.Length); + return data[1]; + } + return -1; + } + #endregion + + #region Animation Control + public void Stop() + { + this.stopped = true; + } + + public void Enable() + { + this.stopped = false; + } + #endregion + + #region Blink Animation + /// + /// Blink the LED on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// The red component. + /// The green component. + /// The blue component. + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(byte channel, byte index, byte r, byte g, byte b, int repeats=1, int delay=500) + { + for (int i = 0; i < repeats; i++) + { + this.InternalSetColor(channel, index, r, g, b); + + if (!WaitThread(delay)) + return; + + this.InternalSetColor(channel, index, 0, 0, 0); + + if (!WaitThread(delay)) + return; + } + } + + /// + /// Blink the LED on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Color parameter as RgbColor class instance + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(byte channel, byte index, RgbColor color, int repeats=1, int delay=500) + { + this.Blink(channel, index, color.R, color.G, color.B, repeats, delay); + } + + /// + /// Blink the LED on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(byte channel, byte index, string color, int repeats=1, int delay=500) + { + this.Blink(channel, index, RgbColor.FromString(color), repeats, delay); + } + + /// + /// Blink the LED. + /// + /// The red component. + /// The green component. + /// The blue component. + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(byte r, byte g, byte b, int repeats=1, int delay=500) + { + this.Blink(0, 0, r, g, b, repeats, delay); + } + + /// + /// Blink the LED. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(RgbColor color, int repeats=1, int delay=500) + { + this.Blink(0, 0, color, repeats, delay); + } + + /// + /// Blink the LED. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How many times to repeat (default 1) + /// Delay delay between on/off sequences (default 500) + public void Blink(string color, int repeats=1, int delay=500) + { + this.Blink(0, 0, color, repeats, delay); + } + #endregion + + #region Morph Animation + /// + /// Morph from current color to new color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// The red component. + /// The green component. + /// The blue component. + /// How long should the morph last + /// How many steps for color changes + public void Morph(byte channel, byte index, byte r, byte g, byte b, int duration=1000, int steps=50) + { + if (stopped) + return; + + byte cr, cg, cb; + GetColor(index, out cr, out cg, out cb); + + for (int i = 1; i <= steps; i++) + { + this.InternalSetColor(channel, index, + (byte)(1.0 * cr + (r - cr) / 1.0 / steps * i), + (byte)(1.0 * cg + (g - cg) / 1.0 / steps * i), + (byte)(1.0 * cb + (b - cb) / 1.0 / steps * i)); + + if (!WaitThread(duration / steps)) + return; + } + } + + /// + /// Morph from current color to new color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Color parameter as RgbColor class instance + /// How long should the morph last + /// How many steps for color changes + public void Morph(byte channel, byte index, RgbColor color, int duration=1000, int steps=50) + { + this.Morph(channel, index, color.R, color.G, color.B, duration, steps); + } + + /// + /// Morph from current color to new color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Morph(byte channel, byte index, string color, int duration=1000, int steps=50) + { + this.Morph(channel, index, RgbColor.FromString(color), duration, steps); + } + + /// + /// Morph from current color to new color. + /// + /// The red component. + /// The green component. + /// The blue component. + /// How long should the morph last + /// How many steps for color changes + public void Morph(byte r, byte g, byte b, int duration=1000, int steps=50) + { + this.Morph(0, 0, r, g, b, duration, steps); + } + + /// + /// Morph from current color to new color. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Morph(RgbColor color, int duration=1000, int steps=50) + { + this.Morph(0, 0, color, duration, steps); + } + + /// + /// Morph from current color to new color. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Morph(string color, int duration=1000, int steps=50) + { + this.Morph(0, 0, color, duration, steps); + } + #endregion + + #region Pulse Animation + /// + /// Pulse specified color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// The red component. + /// The green component. + /// The blue component. + /// How long should the morph last + /// How many steps for color changes + public void Pulse(byte channel, byte index, byte r, byte g, byte b, int repeats=1, int duration=1000, int steps=50) + { + this.InternalSetColor(channel, index, 0, 0, 0); + + if (this.SetColorDelay > 0) + { + if (!WaitThread(this.SetColorDelay)) + return; + } + + for (int i = 0; i < repeats; i++) + { + if (this.stopped) + break; + + this.Morph(channel, index, r, g, b, duration, steps); + + if (this.stopped) + break; + + this.Morph(channel, index, 0, 0, 0, duration, steps); + } + } + + /// + /// Pulse specified color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Color parameter as RgbColor class instance + /// How long should the morph last + /// How many steps for color changes + public void Pulse(byte channel, byte index, RgbColor color, int repeats=1, int duration=1000, int steps=50) + { + this.Pulse(channel, index, color.R, color.G, color.B, repeats, duration, steps); + } + + /// + /// Pulse specified color on BlinkStick Pro. + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Pulse(byte channel, byte index, string color, int repeats=1, int duration=1000, int steps=50) + { + this.Pulse(channel, index, RgbColor.FromString(color), repeats, duration, steps); + } + + /// + /// Pulse specified color. + /// + /// The red component. + /// The green component. + /// The blue component. + /// How long should the morph last + /// How many steps for color changes + public void Pulse(byte r, byte g, byte b, int repeats=1, int duration=1000, int steps=50) + { + this.Pulse(0, 0, r, g, b, repeats, duration, steps); + } + + /// + /// Pulse specified color. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Pulse(RgbColor color, int repeats=1, int duration=1000, int steps=50) + { + this.Pulse(0, 0, color, repeats, duration, steps); + } + + /// + /// Pulse specified color. + /// + /// Must be in #rrggbb format or named color ("red", "green", "blue") + /// How long should the morph last + /// How many steps for color changes + public void Pulse(string color, int repeats=1, int duration=1000, int steps=50) + { + this.Pulse(0, 0, color, repeats, duration, steps); + } + #endregion + + #region Static Functions to initialize BlinkSticks + /// + /// Find all BlinkStick devices. + /// + /// An array of BlinkStick devices + public static BlinkStick[] FindAll () + { + List result = new List(); + + HidDeviceLoader loader = new HidDeviceLoader(); + foreach (HidDevice adevice in loader.GetDevices(VendorId, ProductId).ToArray()) + { + BlinkStick hid = new BlinkStick(); + hid.device = adevice; + result.Add(hid); + } + + return result.ToArray(); + } + + /// + /// Find first BlinkStick. + /// + /// BlinkStick device if found, otherwise null if no devices found + public static BlinkStick FindFirst() + { + BlinkStick[] devices = FindAll(); + + return devices.Length > 0 ? devices[0] : null; + } + + /// + /// Finds BlinkStick by serial number. + /// + /// BlinkStick device if found, otherwise null if no devices found + /// Serial number to search for + public static BlinkStick FindBySerial(String serial) + { + foreach (BlinkStick device in FindAll()) + { + if (device.Serial == serial) + return device; + } + + return null; + } + #endregion + + #region Misc helper functions + /// + /// Checks if BlinkStick requires software color patch due to hardware bug. + /// + /// true, if requires software color patch, false otherwise. + private void CheckRequiresSoftwareColorPatch() + { + _RequiresSoftwareColorPatch = VersionMajor == 1 && VersionMinor >= 1 && VersionMinor <= 3; + } + + /// + /// Automatically sets the color of the device using either BlinkStick or BlinkStick Pro API + /// + /// Channel (0 - R, 1 - G, 2 - B) + /// Index of the LED + /// The red component. + /// The green component. + /// The blue component. + private void InternalSetColor(byte channel, byte index, byte r, byte g, byte b) + { + if (channel == 0 && index == 0) + { + this.SetColor(r, g, b); + } + else + { + this.SetColor(channel, index, r, g, b); + } + } + + private void SetFeature(byte[] buffer) + { + int attempt = 0; + while (attempt < 5) + { + attempt++; + try + { + stream.SetFeature(buffer); + break; + } + catch (System.IO.IOException e) + { + if (e.InnerException is System.ComponentModel.Win32Exception) + { + System.ComponentModel.Win32Exception win32Exception = e.InnerException as System.ComponentModel.Win32Exception; + + if (win32Exception != null && win32Exception.NativeErrorCode == 0) + return; + } + + if (attempt == 5) + throw; + + if (!WaitThread(20)) + return; + } + } + } + + private void GetFeature(byte[] buffer) + { + int attempt = 0; + while (attempt < 5) + { + attempt++; + try + { + stream.GetFeature(buffer); + break; + } + catch (System.IO.IOException e) + { + if (e.InnerException is System.ComponentModel.Win32Exception) + { + System.ComponentModel.Win32Exception win32Exception = e.InnerException as System.ComponentModel.Win32Exception; + + if (win32Exception != null && win32Exception.NativeErrorCode == 0) + return; + } + + if (attempt == 5) + throw; + + if (!WaitThread(20)) + return; + } + } + } + + public Boolean WaitThread(long milliseconds) + { + DateTime nextRetry = DateTime.Now + new TimeSpan(TimeSpan.TicksPerMillisecond * milliseconds); + + while (nextRetry > DateTime.Now) + { + if (this.stopped) + return false; + + Thread.Sleep(20); + } + + return true; + } + + #endregion + } + + public delegate void SendColorEventHandler(object sender, SendColorEventArgs e); + + public delegate void ReceiveColorEventHandler(object sender, ReceiveColorEventArgs e); + + public class SendColorEventArgs: EventArgs { + public byte Channel; + public byte Index; + public byte R; + public byte G; + public byte B; + public Boolean SendToDevice; + + public SendColorEventArgs(byte channel, byte index, byte r, byte g, byte b) + { + this.Channel = channel; + this.Index = index; + this.R = r; + this.G = g; + this.B = b; + this.SendToDevice = true; + } + } + + public class ReceiveColorEventArgs: EventArgs { + public byte Index; + public byte R; + public byte G; + public byte B; + + public ReceiveColorEventArgs(byte index) + { + this.Index = index; + } + } + + public enum BlinkStickDeviceEnum + { + Unknown, + BlinkStick, + BlinkStickPro, + BlinkStickStrip, + BlinkStickSquare, + BlinkStickNano, + BlinkStickFlex + } +} + diff --git a/BlinkStick.Hid/BlinkStick.Hid.csproj b/BlinkStickDotNet/BlinkStickDotNet.csproj similarity index 90% rename from BlinkStick.Hid/BlinkStick.Hid.csproj rename to BlinkStickDotNet/BlinkStickDotNet.csproj index 06b745f..ff4760e 100644 --- a/BlinkStick.Hid/BlinkStick.Hid.csproj +++ b/BlinkStickDotNet/BlinkStickDotNet.csproj @@ -5,8 +5,12 @@ AnyCPU {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} Library - BlinkStick.Hid - BlinkStick.Hid + BlinkStickDotNet + BlinkStickDotNet + true + BlinkStickDotNet.snk + 8.0.30703 + 2.0 true @@ -17,7 +21,6 @@ prompt 4 false - x86 none @@ -38,7 +41,7 @@ - + diff --git a/BlinkStickDotNet/BlinkStickDotNet.snk b/BlinkStickDotNet/BlinkStickDotNet.snk new file mode 100644 index 0000000..cb720e7 Binary files /dev/null and b/BlinkStickDotNet/BlinkStickDotNet.snk differ diff --git a/BlinkStick.Hid/HSLColor.cs b/BlinkStickDotNet/HSLColor.cs similarity index 95% rename from BlinkStick.Hid/HSLColor.cs rename to BlinkStickDotNet/HSLColor.cs index 4278aed..6593d12 100644 --- a/BlinkStick.Hid/HSLColor.cs +++ b/BlinkStickDotNet/HSLColor.cs @@ -20,7 +20,7 @@ using System.Collections.Generic; using System.Text; -namespace BlinkStick.Hid +namespace BlinkStickDotNet { public struct HSLColor { @@ -177,5 +177,15 @@ public override string ToString() s += string.Format ("RGB({0:x2}{1:x2}{2:x2})", Color.R, Color.G, Color.B); return s; } + + public override bool Equals(object obj) + { + return base.Equals(obj); + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } } diff --git a/BlinkStickDotNet/RgbColor.cs b/BlinkStickDotNet/RgbColor.cs new file mode 100644 index 0000000..6c38b75 --- /dev/null +++ b/BlinkStickDotNet/RgbColor.cs @@ -0,0 +1,289 @@ +#region License +// Copyright 2013 by Agile Innovative Ltd +// +// This file is part of BlinkStick application. +// +// BlinkStick application is free software: you can redistribute +// it and/or modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at your option) +// any later version. +// +// BlinkStick application is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// BlinkStick application. If not, see http://www.gnu.org/licenses/. +#endregion + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace BlinkStickDotNet +{ + public class RgbColor + { + #region Colors + static Dictionary _NamesToHex = new Dictionary + { + {"aliceblue", "#f0f8ff"}, + {"antiquewhite", "#faebd7"}, + {"aqua", "#00ffff"}, + {"aquamarine", "#7fffd4"}, + {"azure", "#f0ffff"}, + {"beige", "#f5f5dc"}, + {"bisque", "#ffe4c4"}, + {"black", "#000000"}, + {"blanchedalmond", "#ffebcd"}, + {"blue", "#0000ff"}, + {"blueviolet", "#8a2be2"}, + {"brown", "#a52a2a"}, + {"burlywood", "#deb887"}, + {"cadetblue", "#5f9ea0"}, + {"chartreuse", "#7fff00"}, + {"chocolate", "#d2691e"}, + {"coral", "#ff7f50"}, + {"cornflowerblue", "#6495ed"}, + {"cornsilk", "#fff8dc"}, + {"crimson", "#dc143c"}, + {"cyan", "#00ffff"}, + {"darkblue", "#00008b"}, + {"darkcyan", "#008b8b"}, + {"darkgoldenrod", "#b8860b"}, + {"darkgray", "#a9a9a9"}, + {"darkgrey", "#a9a9a9"}, + {"darkgreen", "#006400"}, + {"darkkhaki", "#bdb76b"}, + {"darkmagenta", "#8b008b"}, + {"darkolivegreen", "#556b2f"}, + {"darkorange", "#ff8c00"}, + {"darkorchid", "#9932cc"}, + {"darkred", "#8b0000"}, + {"darksalmon", "#e9967a"}, + {"darkseagreen", "#8fbc8f"}, + {"darkslateblue", "#483d8b"}, + {"darkslategray", "#2f4f4f"}, + {"darkslategrey", "#2f4f4f"}, + {"darkturquoise", "#00ced1"}, + {"darkviolet", "#9400d3"}, + {"deeppink", "#ff1493"}, + {"deepskyblue", "#00bfff"}, + {"dimgray", "#696969"}, + {"dimgrey", "#696969"}, + {"dodgerblue", "#1e90ff"}, + {"firebrick", "#b22222"}, + {"floralwhite", "#fffaf0"}, + {"forestgreen", "#228b22"}, + {"fuchsia", "#ff00ff"}, + {"gainsboro", "#dcdcdc"}, + {"ghostwhite", "#f8f8ff"}, + {"gold", "#ffd700"}, + {"goldenrod", "#daa520"}, + {"gray", "#808080"}, + {"grey", "#808080"}, + {"green", "#008000"}, + {"greenyellow", "#adff2f"}, + {"honeydew", "#f0fff0"}, + {"hotpink", "#ff69b4"}, + {"indianred", "#cd5c5c"}, + {"indigo", "#4b0082"}, + {"ivory", "#fffff0"}, + {"khaki", "#f0e68c"}, + {"lavender", "#e6e6fa"}, + {"lavenderblush", "#fff0f5"}, + {"lawngreen", "#7cfc00"}, + {"lemonchiffon", "#fffacd"}, + {"lightblue", "#add8e6"}, + {"lightcoral", "#f08080"}, + {"lightcyan", "#e0ffff"}, + {"lightgoldenrodyellow", "#fafad2"}, + {"lightgray", "#d3d3d3"}, + {"lightgrey", "#d3d3d3"}, + {"lightgreen", "#90ee90"}, + {"lightpink", "#ffb6c1"}, + {"lightsalmon", "#ffa07a"}, + {"lightseagreen", "#20b2aa"}, + {"lightskyblue", "#87cefa"}, + {"lightslategray", "#778899"}, + {"lightslategrey", "#778899"}, + {"lightsteelblue", "#b0c4de"}, + {"lightyellow", "#ffffe0"}, + {"lime", "#00ff00"}, + {"limegreen", "#32cd32"}, + {"linen", "#faf0e6"}, + {"magenta", "#ff00ff"}, + {"maroon", "#800000"}, + {"mediumaquamarine", "#66cdaa"}, + {"mediumblue", "#0000cd"}, + {"mediumorchid", "#ba55d3"}, + {"mediumpurple", "#9370d8"}, + {"mediumseagreen", "#3cb371"}, + {"mediumslateblue", "#7b68ee"}, + {"mediumspringgreen", "#00fa9a"}, + {"mediumturquoise", "#48d1cc"}, + {"mediumvioletred", "#c71585"}, + {"midnightblue", "#191970"}, + {"mintcream", "#f5fffa"}, + {"mistyrose", "#ffe4e1"}, + {"moccasin", "#ffe4b5"}, + {"navajowhite", "#ffdead"}, + {"navy", "#000080"}, + {"oldlace", "#fdf5e6"}, + {"olive", "#808000"}, + {"olivedrab", "#6b8e23"}, + {"orange", "#ffa500"}, + {"orangered", "#ff4500"}, + {"orchid", "#da70d6"}, + {"palegoldenrod", "#eee8aa"}, + {"palegreen", "#98fb98"}, + {"paleturquoise", "#afeeee"}, + {"palevioletred", "#d87093"}, + {"papayawhip", "#ffefd5"}, + {"peachpuff", "#ffdab9"}, + {"peru", "#cd853f"}, + {"pink", "#ffc0cb"}, + {"plum", "#dda0dd"}, + {"powderblue", "#b0e0e6"}, + {"purple", "#800080"}, + {"red", "#ff0000"}, + {"rosybrown", "#bc8f8f"}, + {"royalblue", "#4169e1"}, + {"saddlebrown", "#8b4513"}, + {"salmon", "#fa8072"}, + {"sandybrown", "#f4a460"}, + {"seagreen", "#2e8b57"}, + {"seashell", "#fff5ee"}, + {"sienna", "#a0522d"}, + {"silver", "#c0c0c0"}, + {"skyblue", "#87ceeb"}, + {"slateblue", "#6a5acd"}, + {"slategray", "#708090"}, + {"slategrey", "#708090"}, + {"snow", "#fffafa"}, + {"springgreen", "#00ff7f"}, + {"steelblue", "#4682b4"}, + {"tan", "#d2b48c"}, + {"teal", "#008080"}, + {"thistle", "#d8bfd8"}, + {"tomato", "#ff6347"}, + {"turquoise", "#40e0d0"}, + {"violet", "#ee82ee"}, + {"wheat", "#f5deb3"}, + {"white", "#ffffff"}, + {"whitesmoke", "#f5f5f5"}, + {"yellow", "#ffff00"}, + {"yellowgreen", "#9acd32"} + }; + #endregion + + /// + /// The Red byte component. + /// + public Byte R; + + /// + /// The Green byte component. + /// + public Byte G; + + /// + /// The Blue byte component. + /// + public Byte B; + + public RgbColor () + { + } + + /// + /// Froms the rgb value from int components. + /// + /// The rgb. + /// The red component. + /// The green component. + /// The blue component. + public static RgbColor FromRgb(int r, int g, int b) + { + RgbColor color = new RgbColor(); + color.R = (byte)r; + color.G = (byte)g; + color.B = (byte)b; + + return color; + } + + /// + /// Froms the color of the GDK color. + /// + /// The gdk color. + /// The red component. + /// The green component. + /// The blue component. + public static RgbColor FromGdkColor(ushort r, ushort g, ushort b) + { + RgbColor color = new RgbColor(); + + color.R = (byte)(r / 0x100); + color.G = (byte)(g / 0x100); + color.B = (byte)(b / 0x100); + + return color; + } + + /// + /// Converts HEX string or name of the RGB color. For example #123456, blue, red, orange + /// + /// The string. + /// Color string. + public static RgbColor FromString (String color) + { + RgbColor result = new RgbColor(); + + if (_NamesToHex.ContainsKey(color.ToLower())) + { + color = _NamesToHex[color.ToLower()]; + } + else + { + if (!IsValidColor(color)) + throw new Exception("Color value is invalid"); + } + + result.R = Convert.ToByte(color.Substring(1, 2), 16); + result.G = Convert.ToByte(color.Substring(3, 2), 16); + result.B = Convert.ToByte(color.Substring(5, 2), 16); + + return result; + } + + /// + /// Get black color. + /// + public static RgbColor Black () + { + return RgbColor.FromRgb(0, 0, 0); + } + + /// + /// Determines if is valid color format of the specified string (#rrggbb). + /// + /// true if is valid color the specified color; otherwise, false. + /// Color. + public static Boolean IsValidColor (String color) + { + return Regex.IsMatch(color, "^#[A-Fa-f0-9]{6}$"); + } + + /// + /// Returns a string that represents the current object. + /// + /// A string that represents the current object. + /// 2 + public override string ToString() + { + return string.Format("{0:x2}{1:x2}{2:x2}", this.R, this.G, this.B); + } + } +} + diff --git a/BlinkStickDotNet/UsbMonitor.cs b/BlinkStickDotNet/UsbMonitor.cs new file mode 100644 index 0000000..2991df2 --- /dev/null +++ b/BlinkStickDotNet/UsbMonitor.cs @@ -0,0 +1,234 @@ +#region License +// Copyright 2013 by Agile Innovative Ltd +// +// This file is part of BlinkStick.HID library. +// +// BlinkStick.HID library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) any +// later version. +// +// BlinkStick.HID library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// BlinkStick.HID library. If not, see http://www.gnu.org/licenses/. +#endregion + +using System; +using System.Collections.Generic; +using LibUsbDotNet.DeviceNotify; + +namespace BlinkStickDotNet +{ + public class UsbMonitor + { + /// + /// Occurs when BlinkStick is connected. + /// + public event EventHandler BlinkStickConnected; + + /// + /// Raises the BlinkStick connected event. + /// + /// Device which has been connected. + protected void OnBlinkStickConnected(BlinkStick device) + { + if (BlinkStickConnected != null) + { + BlinkStickConnected(this, new DeviceModifiedArgs(device)); + } + } + + /// + /// Occurs when BlinkStick disconnected. + /// + public event EventHandler BlinkStickDisconnected; + + /// + /// Raises the BlinkStick disconnected event. + /// + /// Device which has been disconnected. + protected void OnBlinkStickDisconnected(BlinkStick device) + { + if (BlinkStickDisconnected != null) + { + BlinkStickDisconnected(this, new DeviceModifiedArgs(device)); + } + } + + /// + /// Occurs when usb devices change. + /// + public event EventHandler UsbDevicesChanged; + + /// + /// Raises the usb device changed event. + /// + protected void OnUsbDevicesChanged() + { + if (UsbDevicesChanged != null) + { + UsbDevicesChanged(this, new EventArgs()); + } + + List newDevices = new List(); + + List scannedDevices = new List(BlinkStick.FindAll()); + + foreach (BlinkStick newDevice in scannedDevices) + { + Boolean found = false; + + for (int i = devices.Count - 1; i >= 0; i--) + { + if (devices[i].Serial == newDevice.Serial) + { + devices.RemoveAt(i); + found = true; + break; + } + } + + if (!found) + { + OnBlinkStickConnected(newDevice); + } + } + + foreach (BlinkStick device in devices) + { + OnBlinkStickDisconnected(device); + } + + devices = scannedDevices; + } + + /// + /// Internal list of tracked devices. + /// + List devices; + + /// + /// USB device monitor for Windows. + /// + private WinUsbDeviceMonitor winUsbDeviceMonitor; + + /// + /// USB device monitor for Linux/Mac. + /// + public IDeviceNotifier UsbDeviceNotifier; + + /// + /// Gets a value indicating whether this is monitoring. + /// + /// true if monitoring; otherwise, false. + public Boolean Monitoring { + get; + private set; + } + + public UsbMonitor () + { + switch (HidSharp.PlatformDetector.RunningPlatform()) + { + case HidSharp.PlatformDetector.Platform.Windows: + winUsbDeviceMonitor = new WinUsbDeviceMonitor(); + winUsbDeviceMonitor.DeviceListChanged += HandleDeviceListChanged; + break; + case HidSharp.PlatformDetector.Platform.Linux: + UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier(); + UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent; + break; + } + } + + /// + /// Handles the device list change on Windows. + /// + /// Sender object + /// Event args + private void HandleDeviceListChanged (object sender, EventArgs e) + { + OnUsbDevicesChanged(); + } + + /// + /// Handles device list change on Linux/Mac. + /// + /// Sender. + /// E. + private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e) + { + OnUsbDevicesChanged(); + } + + /// + /// Start monitoring for added/removed BlinkStick devices. + /// + public void Start () + { + //Get the list of already connected BlinkSticks + devices = new List(BlinkStick.FindAll()); + + if (UsbDeviceNotifier != null) + { + UsbDeviceNotifier.Enabled = true; + } + else if (winUsbDeviceMonitor != null) + { + winUsbDeviceMonitor.Enabled = true; + } + + Monitoring = true; + } + + /// + /// Stop monitoring for added/removed BlinkStick devices. + /// + public void Stop () + { + if (UsbDeviceNotifier != null) { + UsbDeviceNotifier.Enabled = false; // Disable the device notifier + + UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent; + } + else if (winUsbDeviceMonitor != null) + { + winUsbDeviceMonitor.Enabled = false; + } + + Monitoring = false; + } + + /// + /// Releases unmanaged resources and performs other cleanup operations before the + /// is reclaimed by garbage collection. + /// + ~UsbMonitor () + { + } + } + + /// + /// Device modified arguments. + /// + public class DeviceModifiedArgs : EventArgs + { + /// + /// The device which has been modified. + /// + public BlinkStick Device; + + /// + /// Initializes a new instance of the class. + /// + /// Device passed as an argument + public DeviceModifiedArgs(BlinkStick device) + { + this.Device = device; + } + } +} + diff --git a/BlinkStick.Hid/WinUsbDeviceMonitor.cs b/BlinkStickDotNet/WinUsbDeviceMonitor.cs similarity index 83% rename from BlinkStick.Hid/WinUsbDeviceMonitor.cs rename to BlinkStickDotNet/WinUsbDeviceMonitor.cs index bef4763..816b5a7 100644 --- a/BlinkStick.Hid/WinUsbDeviceMonitor.cs +++ b/BlinkStickDotNet/WinUsbDeviceMonitor.cs @@ -21,7 +21,7 @@ using System.Security.Permissions; using System.Windows.Forms; -namespace BlinkStick.Hid +namespace BlinkStickDotNet { public class WinUsbDeviceMonitor { @@ -41,6 +41,8 @@ protected void OnDeviceListChanged() } } + public Boolean Enabled { get; set; } + /// /// Private property to keep reference to Windows form for getting /// notification from the OS about changes to USB devices @@ -65,7 +67,8 @@ protected override void WndProc(ref Message m) || m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE || m.WParam.ToInt32() == DBT_DEVNODES_CHANGED)) { - Monitor.OnDeviceListChanged(); + if (this.Enabled) + Monitor.OnDeviceListChanged(); } base.WndProc(ref m); @@ -75,8 +78,18 @@ protected override void WndProc(ref Message m) public WinUsbDeviceMonitor () { - form = new MyForm(); + this.Enabled = false; + + form = new MyForm(); form.Monitor = this; + + //Move main form off the screen + form.StartPosition = FormStartPosition.Manual; + form.Width = 10; + form.Height = 10; + form.Left = -200; + form.ShowInTaskbar = false; + form.Visible = true; form.Visible = false; } diff --git a/Components/HidSharp/HidSharp.csproj b/Components/HidSharp/HidSharp.csproj index e80993d..d4e7b29 100644 --- a/Components/HidSharp/HidSharp.csproj +++ b/Components/HidSharp/HidSharp.csproj @@ -14,6 +14,10 @@ 3.5 + true + HidSharp.snk + 8.0.30703 + 2.0 true @@ -25,11 +29,8 @@ 4 true false - ..\bin\HidSharp.XML - x86 - pdbonly true ..\bin\ TRACE @@ -37,7 +38,6 @@ 4 true false - ..\bin\HidSharp.XML diff --git a/Components/HidSharp/HidSharp.snk b/Components/HidSharp/HidSharp.snk new file mode 100644 index 0000000..c0589c9 Binary files /dev/null and b/Components/HidSharp/HidSharp.snk differ diff --git a/Components/HidSharp/Properties/AssemblyInfo.cs b/Components/HidSharp/Properties/AssemblyInfo.cs index 7362613..97d5072 100644 --- a/Components/HidSharp/Properties/AssemblyInfo.cs +++ b/Components/HidSharp/Properties/AssemblyInfo.cs @@ -7,8 +7,7 @@ [assembly: AssemblyDescription("C# HID wrappers")] [assembly: AssemblyProduct("HidSharp")] [assembly: AssemblyTitle("HidSharp")] -[assembly: AssemblyFileVersion("1.5.1.0")] -[assembly: AssemblyVersion("1.5.1.0")] +[assembly: AssemblyVersion("1.5.*")] [assembly: ComVisible(false)] [assembly: Guid("1b874372-4c6c-4acb-9f4a-906f6738cff9")] diff --git a/Components/LibWinUsb/LibUsbDotNet.csproj b/Components/LibWinUsb/LibUsbDotNet.csproj index 6daf845..00567fb 100644 --- a/Components/LibWinUsb/LibUsbDotNet.csproj +++ b/Components/LibWinUsb/LibUsbDotNet.csproj @@ -13,8 +13,10 @@ 2.0 - false LibUsbDotNet.snk + 8.0.30703 + 2.0 + true true diff --git a/Components/LibWinUsb/Properties/AssemblyInfo.cs b/Components/LibWinUsb/Properties/AssemblyInfo.cs index 7aa4bb7..8d430b5 100644 --- a/Components/LibWinUsb/Properties/AssemblyInfo.cs +++ b/Components/LibWinUsb/Properties/AssemblyInfo.cs @@ -57,7 +57,6 @@ // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("2.2.8.104")] -[assembly: AssemblyFileVersion("2.2.8.104")] +[assembly: AssemblyVersion("2.2.*")] [assembly: CLSCompliant(true)] [assembly: NeutralResourcesLanguage("en-US")] \ No newline at end of file diff --git a/Examples.sln b/Examples.sln new file mode 100644 index 0000000..ff8d4b5 --- /dev/null +++ b/Examples.sln @@ -0,0 +1,112 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibUsbDotNet", "Components\LibWinUsb\LibUsbDotNet.csproj", "{0A78F6FF-5586-4052-8104-E23FF83A7CE1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp", "Components\HidSharp\HidSharp.csproj", "{0DB86674-2A7B-4BDC-93C1-3F7DC771426C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlinkStickDotNet", "BlinkStickDotNet\BlinkStickDotNet.csproj", "{7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{E9CD0471-DE68-4F37-91A5-9ECD760F01AE}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BlinkStick", "BlinkStick", "{17C84E65-0D70-4312-8033-D055F86146A3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlinkTest", "Examples\BlinkStick\BlinkTest\BlinkTest.csproj", "{84F24818-696F-4739-8D43-15CEDCCB517E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FindBySerial", "Examples\BlinkStick\FindBySerial\FindBySerial.csproj", "{850D0A4D-5DDF-46BB-A8B8-3C74E94875EA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetInfo", "Examples\BlinkStick\GetInfo\GetInfo.csproj", "{BE6C3C99-3ECE-4D21-A673-151867345B74}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MorphTest", "Examples\BlinkStick\MorphTest\MorphTest.csproj", "{9E974C01-06CC-4AE9-9341-5B3A42EB6F78}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PulseTest", "Examples\BlinkStick\PulseTest\PulseTest.csproj", "{F570009D-7E5A-48F3-90E9-33AB8E512447}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SetRandomColor", "Examples\BlinkStick\SetRandomColor\SetRandomColor.csproj", "{8187B197-5226-4A0D-8716-CE60DBE439BF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TurnOff", "Examples\BlinkStick\TurnOff\TurnOff.csproj", "{E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonitorTest", "Examples\BlinkStick\MonitorTest\MonitorTest.csproj", "{F48BE451-412A-4AF9-8A47-CFECC578C54A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BlinkStickPro", "BlinkStickPro", "{1C9FD0B2-C8A2-4B6A-9288-5B6D2E00DED4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IndexedColorFrame", "Examples\BlinkStickPro\IndexedColorFrame\IndexedColorFrame.csproj", "{61527D2E-C94F-4EA5-8725-02EA6247EB8B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IndexedColors", "Examples\BlinkStickPro\IndexedColors\IndexedColors.csproj", "{725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0A78F6FF-5586-4052-8104-E23FF83A7CE1}.Debug|x86.ActiveCfg = Debug|x86 + {0A78F6FF-5586-4052-8104-E23FF83A7CE1}.Debug|x86.Build.0 = Debug|x86 + {0A78F6FF-5586-4052-8104-E23FF83A7CE1}.Release|x86.ActiveCfg = Release|x86 + {0A78F6FF-5586-4052-8104-E23FF83A7CE1}.Release|x86.Build.0 = Release|x86 + {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|x86.ActiveCfg = Debug|Any CPU + {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|x86.Build.0 = Debug|Any CPU + {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|x86.ActiveCfg = Release|Any CPU + {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|x86.Build.0 = Release|Any CPU + {61527D2E-C94F-4EA5-8725-02EA6247EB8B}.Debug|x86.ActiveCfg = Debug|x86 + {61527D2E-C94F-4EA5-8725-02EA6247EB8B}.Debug|x86.Build.0 = Debug|x86 + {61527D2E-C94F-4EA5-8725-02EA6247EB8B}.Release|x86.ActiveCfg = Release|x86 + {61527D2E-C94F-4EA5-8725-02EA6247EB8B}.Release|x86.Build.0 = Release|x86 + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE}.Debug|x86.ActiveCfg = Debug|x86 + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE}.Debug|x86.Build.0 = Debug|x86 + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE}.Release|x86.ActiveCfg = Release|x86 + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE}.Release|x86.Build.0 = Release|x86 + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Debug|x86.ActiveCfg = Debug|Any CPU + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Debug|x86.Build.0 = Debug|Any CPU + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Release|x86.ActiveCfg = Release|Any CPU + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757}.Release|x86.Build.0 = Release|Any CPU + {8187B197-5226-4A0D-8716-CE60DBE439BF}.Debug|x86.ActiveCfg = Debug|x86 + {8187B197-5226-4A0D-8716-CE60DBE439BF}.Debug|x86.Build.0 = Debug|x86 + {8187B197-5226-4A0D-8716-CE60DBE439BF}.Release|x86.ActiveCfg = Release|x86 + {8187B197-5226-4A0D-8716-CE60DBE439BF}.Release|x86.Build.0 = Release|x86 + {84F24818-696F-4739-8D43-15CEDCCB517E}.Debug|x86.ActiveCfg = Debug|x86 + {84F24818-696F-4739-8D43-15CEDCCB517E}.Debug|x86.Build.0 = Debug|x86 + {84F24818-696F-4739-8D43-15CEDCCB517E}.Release|x86.ActiveCfg = Release|x86 + {84F24818-696F-4739-8D43-15CEDCCB517E}.Release|x86.Build.0 = Release|x86 + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA}.Debug|x86.ActiveCfg = Debug|x86 + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA}.Debug|x86.Build.0 = Debug|x86 + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA}.Release|x86.ActiveCfg = Release|x86 + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA}.Release|x86.Build.0 = Release|x86 + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78}.Debug|x86.ActiveCfg = Debug|x86 + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78}.Debug|x86.Build.0 = Debug|x86 + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78}.Release|x86.ActiveCfg = Release|x86 + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78}.Release|x86.Build.0 = Release|x86 + {BE6C3C99-3ECE-4D21-A673-151867345B74}.Debug|x86.ActiveCfg = Debug|x86 + {BE6C3C99-3ECE-4D21-A673-151867345B74}.Debug|x86.Build.0 = Debug|x86 + {BE6C3C99-3ECE-4D21-A673-151867345B74}.Release|x86.ActiveCfg = Release|x86 + {BE6C3C99-3ECE-4D21-A673-151867345B74}.Release|x86.Build.0 = Release|x86 + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5}.Debug|x86.ActiveCfg = Debug|x86 + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5}.Debug|x86.Build.0 = Debug|x86 + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5}.Release|x86.ActiveCfg = Release|x86 + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5}.Release|x86.Build.0 = Release|x86 + {F48BE451-412A-4AF9-8A47-CFECC578C54A}.Debug|x86.ActiveCfg = Debug|x86 + {F48BE451-412A-4AF9-8A47-CFECC578C54A}.Debug|x86.Build.0 = Debug|x86 + {F48BE451-412A-4AF9-8A47-CFECC578C54A}.Release|x86.ActiveCfg = Release|x86 + {F48BE451-412A-4AF9-8A47-CFECC578C54A}.Release|x86.Build.0 = Release|x86 + {F570009D-7E5A-48F3-90E9-33AB8E512447}.Debug|x86.ActiveCfg = Debug|x86 + {F570009D-7E5A-48F3-90E9-33AB8E512447}.Debug|x86.Build.0 = Debug|x86 + {F570009D-7E5A-48F3-90E9-33AB8E512447}.Release|x86.ActiveCfg = Release|x86 + {F570009D-7E5A-48F3-90E9-33AB8E512447}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {17C84E65-0D70-4312-8033-D055F86146A3} = {E9CD0471-DE68-4F37-91A5-9ECD760F01AE} + {1C9FD0B2-C8A2-4B6A-9288-5B6D2E00DED4} = {E9CD0471-DE68-4F37-91A5-9ECD760F01AE} + {84F24818-696F-4739-8D43-15CEDCCB517E} = {17C84E65-0D70-4312-8033-D055F86146A3} + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA} = {17C84E65-0D70-4312-8033-D055F86146A3} + {BE6C3C99-3ECE-4D21-A673-151867345B74} = {17C84E65-0D70-4312-8033-D055F86146A3} + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78} = {17C84E65-0D70-4312-8033-D055F86146A3} + {F570009D-7E5A-48F3-90E9-33AB8E512447} = {17C84E65-0D70-4312-8033-D055F86146A3} + {8187B197-5226-4A0D-8716-CE60DBE439BF} = {17C84E65-0D70-4312-8033-D055F86146A3} + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5} = {17C84E65-0D70-4312-8033-D055F86146A3} + {F48BE451-412A-4AF9-8A47-CFECC578C54A} = {17C84E65-0D70-4312-8033-D055F86146A3} + {61527D2E-C94F-4EA5-8725-02EA6247EB8B} = {1C9FD0B2-C8A2-4B6A-9288-5B6D2E00DED4} + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE} = {1C9FD0B2-C8A2-4B6A-9288-5B6D2E00DED4} + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Examples\BlinkStick\MonitorTest\MonitorTest.csproj + EndGlobalSection +EndGlobal diff --git a/Examples/BlinkStick/BlinkTest/BlinkTest.csproj b/Examples/BlinkStick/BlinkTest/BlinkTest.csproj new file mode 100644 index 0000000..6b707bb --- /dev/null +++ b/Examples/BlinkStick/BlinkTest/BlinkTest.csproj @@ -0,0 +1,46 @@ + + + + Debug + x86 + {84F24818-696F-4739-8D43-15CEDCCB517E} + Exe + BlinkTest + BlinkTest + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/BlinkTest/Program.cs b/Examples/BlinkStick/BlinkTest/Program.cs new file mode 100644 index 0000000..d837db4 --- /dev/null +++ b/Examples/BlinkStick/BlinkTest/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading; +using BlinkStickDotNet; + +namespace BlinkTest +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Blink test for BlinkStick.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + if (device != null && device.OpenDevice ()) { + foreach (string color in new string[] {"red", "green", "blue"}) + { + Console.WriteLine (color); + device.Blink(color); + } + } + } + } +} diff --git a/Examples/BlinkStick/BlinkTest/Properties/AssemblyInfo.cs b/Examples/BlinkStick/BlinkTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..17988f2 --- /dev/null +++ b/Examples/BlinkStick/BlinkTest/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("BlinkTest")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/FindBySerial/FindBySerial.csproj b/Examples/BlinkStick/FindBySerial/FindBySerial.csproj new file mode 100644 index 0000000..7cd9876 --- /dev/null +++ b/Examples/BlinkStick/FindBySerial/FindBySerial.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {850D0A4D-5DDF-46BB-A8B8-3C74E94875EA} + Exe + FindBySerial + FindBySerial + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/FindBySerial/Program.cs b/Examples/BlinkStick/FindBySerial/Program.cs new file mode 100644 index 0000000..ef42e41 --- /dev/null +++ b/Examples/BlinkStick/FindBySerial/Program.cs @@ -0,0 +1,24 @@ +using System; +using BlinkStickDotNet; + +namespace FindBySerial +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Find by serial.\r\n"); + + String serial = "BS010000-1.1"; + + if (BlinkStick.FindBySerial (serial) != null) { + Console.WriteLine ("BlinkStick found!"); + } else { + Console.WriteLine ("BlinkStick not found"); + } + + Console.WriteLine ("\r\nPress Enter to exit..."); + Console.ReadLine (); + } + } +} diff --git a/Examples/BlinkStick/FindBySerial/Properties/AssemblyInfo.cs b/Examples/BlinkStick/FindBySerial/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c26abef --- /dev/null +++ b/Examples/BlinkStick/FindBySerial/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("FindBySerial")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/GetInfo/GetInfo.csproj b/Examples/BlinkStick/GetInfo/GetInfo.csproj new file mode 100644 index 0000000..81397dc --- /dev/null +++ b/Examples/BlinkStick/GetInfo/GetInfo.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {BE6C3C99-3ECE-4D21-A673-151867345B74} + Exe + GetInfo + GetInfo + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/GetInfo/Program.cs b/Examples/BlinkStick/GetInfo/Program.cs new file mode 100644 index 0000000..ef4efdc --- /dev/null +++ b/Examples/BlinkStick/GetInfo/Program.cs @@ -0,0 +1,46 @@ +using System; +using BlinkStickDotNet; +using System.Collections.Generic; + +namespace GetInfo +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Information about BlinkSticks.\r\n"); + + BlinkStick[] devices = BlinkStick.FindAll(); + + if (devices.Length == 0) { + Console.WriteLine ("Could not find any BlinkStick devices..."); + return; + } + + //Iterate through all of them + foreach (BlinkStick device in devices) + { + //Open the device + if (device.OpenDevice ()) + { + Console.WriteLine (String.Format ("Device {0} opened successfully", device.Serial)); + + byte cr; + byte cg; + byte cb; + + device.GetColor(out cr, out cg, out cb); + + Console.WriteLine (String.Format (" Device color: #{0:X2}{1:X2}{2:X2}", cr, cg, cb)); + Console.WriteLine (" Serial: " + device.Serial); + Console.WriteLine (" Manufacturer: " + device.ManufacturerName); + Console.WriteLine (" Product Name: " + device.ProductName); + Console.WriteLine (" InfoBlock1: " + device.InfoBlock1); + Console.WriteLine (" InfoBlock2: " + device.InfoBlock2); } + } + + Console.WriteLine ("\r\nPress Enter to exit..."); + Console.ReadLine (); + } + } +} diff --git a/Examples/BlinkStick/GetInfo/Properties/AssemblyInfo.cs b/Examples/BlinkStick/GetInfo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ef3636f --- /dev/null +++ b/Examples/BlinkStick/GetInfo/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("GetInfo")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/MonitorTest/MonitorTest.csproj b/Examples/BlinkStick/MonitorTest/MonitorTest.csproj new file mode 100644 index 0000000..e61ae91 --- /dev/null +++ b/Examples/BlinkStick/MonitorTest/MonitorTest.csproj @@ -0,0 +1,47 @@ + + + + Debug + x86 + {F48BE451-412A-4AF9-8A47-CFECC578C54A} + Exe + MonitorTest + MonitorTest + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/MonitorTest/Program.cs b/Examples/BlinkStick/MonitorTest/Program.cs new file mode 100644 index 0000000..27bd00d --- /dev/null +++ b/Examples/BlinkStick/MonitorTest/Program.cs @@ -0,0 +1,54 @@ +using System; +using BlinkStickDotNet; +using System.Windows.Forms; +using System.Collections.Generic; + +namespace MonitorTest +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Monitor BlinkSticks inserted and removed"); + + UsbMonitor monitor = new UsbMonitor(); + + //Attach to connected event + monitor.BlinkStickConnected += (object sender, DeviceModifiedArgs e) => { + Console.WriteLine("BlinkStick " + e.Device.Serial + " connected!"); + }; + + //Attach to disconnected event + monitor.BlinkStickDisconnected += (object sender, DeviceModifiedArgs e) => { + Console.WriteLine("BlinkStick " + e.Device.Serial + " disconnected..."); + }; + + List devices = new List (BlinkStick.FindAll()); + + //List BlinkSticks already connected + foreach (BlinkStick device in devices) + { + Console.WriteLine("BlinkStick " + device.Serial + " already connected"); + } + + //Start monitoring + monitor.Start (); + + Console.WriteLine ("Monitoring for BlinkStick devices... Press any key to exit."); + + //Start application event loop. Alternatively you can run main form: + // Application.Run ([Your form]); + while (true) { + //Process messages + Application.DoEvents (); + + //Exit if key is pressed + if (Console.KeyAvailable) + break; + } + + //Stop monitoring + monitor.Stop (); + } + } +} diff --git a/Examples/BlinkStick/MonitorTest/Properties/AssemblyInfo.cs b/Examples/BlinkStick/MonitorTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..45ff53a --- /dev/null +++ b/Examples/BlinkStick/MonitorTest/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("MonitorTest")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/MorphTest/MorphTest.csproj b/Examples/BlinkStick/MorphTest/MorphTest.csproj new file mode 100644 index 0000000..0038d98 --- /dev/null +++ b/Examples/BlinkStick/MorphTest/MorphTest.csproj @@ -0,0 +1,46 @@ + + + + Debug + x86 + {9E974C01-06CC-4AE9-9341-5B3A42EB6F78} + Exe + MorphTest + MorphTest + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/MorphTest/Program.cs b/Examples/BlinkStick/MorphTest/Program.cs new file mode 100644 index 0000000..ecfd6ca --- /dev/null +++ b/Examples/BlinkStick/MorphTest/Program.cs @@ -0,0 +1,20 @@ +using System; +using BlinkStickDotNet; + +namespace MorphTest +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Morph test for BlinkStick.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + if (device != null && device.OpenDevice ()) { + device.Morph ("red"); + device.Morph ("green"); + device.Morph ("blue"); + } + } + } +} diff --git a/Examples/BlinkStick/MorphTest/Properties/AssemblyInfo.cs b/Examples/BlinkStick/MorphTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4cfecd3 --- /dev/null +++ b/Examples/BlinkStick/MorphTest/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("MorphTest")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/PulseTest/Program.cs b/Examples/BlinkStick/PulseTest/Program.cs new file mode 100644 index 0000000..e02626a --- /dev/null +++ b/Examples/BlinkStick/PulseTest/Program.cs @@ -0,0 +1,20 @@ +using System; +using BlinkStickDotNet; + +namespace PulseTest +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Pulse test for BlinkStick.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + if (device != null && device.OpenDevice ()) { + device.Pulse ("red"); + device.Pulse ("green"); + device.Pulse ("blue"); + } + } + } +} diff --git a/Examples/BlinkStick/PulseTest/Properties/AssemblyInfo.cs b/Examples/BlinkStick/PulseTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..da83b90 --- /dev/null +++ b/Examples/BlinkStick/PulseTest/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("PulseTest")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/PulseTest/PulseTest.csproj b/Examples/BlinkStick/PulseTest/PulseTest.csproj new file mode 100644 index 0000000..0c8eddf --- /dev/null +++ b/Examples/BlinkStick/PulseTest/PulseTest.csproj @@ -0,0 +1,46 @@ + + + + Debug + x86 + {F570009D-7E5A-48F3-90E9-33AB8E512447} + Exe + PulseTest + PulseTest + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/SetRandomColor/Program.cs b/Examples/BlinkStick/SetRandomColor/Program.cs new file mode 100644 index 0000000..5110f05 --- /dev/null +++ b/Examples/BlinkStick/SetRandomColor/Program.cs @@ -0,0 +1,36 @@ +using System; +using BlinkStickDotNet; +using System.Collections.Generic; + +namespace SetRandomColor +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Set random color.\r\n"); + + BlinkStick[] devices = BlinkStick.FindAll(); + + if (devices.Length == 0) { + Console.WriteLine ("Could not find any BlinkStick devices..."); + return; + } + + //Iterate through all of them + foreach (BlinkStick device in devices) + { + //Open the device + if (device.OpenDevice ()) + { + Console.WriteLine (String.Format ("Device {0} opened successfully", device.Serial)); + Random r = new Random (); + device.SetColor ((byte)r.Next(), (byte)r.Next(), (byte)r.Next()); + } + } + + Console.WriteLine ("\r\nPress Enter to exit..."); + Console.ReadLine (); + } + } +} diff --git a/Examples/BlinkStick/SetRandomColor/Properties/AssemblyInfo.cs b/Examples/BlinkStick/SetRandomColor/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..df9b046 --- /dev/null +++ b/Examples/BlinkStick/SetRandomColor/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("SetRandomColor")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/SetRandomColor/SetRandomColor.csproj b/Examples/BlinkStick/SetRandomColor/SetRandomColor.csproj new file mode 100644 index 0000000..6f75c60 --- /dev/null +++ b/Examples/BlinkStick/SetRandomColor/SetRandomColor.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {8187B197-5226-4A0D-8716-CE60DBE439BF} + Exe + SetRandomColor + SetRandomColor + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStick/TurnOff/Program.cs b/Examples/BlinkStick/TurnOff/Program.cs new file mode 100644 index 0000000..3bc22d0 --- /dev/null +++ b/Examples/BlinkStick/TurnOff/Program.cs @@ -0,0 +1,26 @@ +using System; +using BlinkStickDotNet; + +namespace TurnOff +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Turn Off.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + + if (device != null) { + if (device.OpenDevice ()) { + device.TurnOff (); + Console.WriteLine ("BlinkStick was turned off"); + } else { + Console.WriteLine ("Could not open the device"); + } + } else { + Console.WriteLine ("BlinkStick not found"); + } + } + } +} diff --git a/Examples/BlinkStick/TurnOff/Properties/AssemblyInfo.cs b/Examples/BlinkStick/TurnOff/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4e4aed3 --- /dev/null +++ b/Examples/BlinkStick/TurnOff/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("TurnOff")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStick/TurnOff/TurnOff.csproj b/Examples/BlinkStick/TurnOff/TurnOff.csproj new file mode 100644 index 0000000..7f9412c --- /dev/null +++ b/Examples/BlinkStick/TurnOff/TurnOff.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {E1C6B2F8-45E3-490D-B3D1-750ACBAD76F5} + Exe + TurnOff + TurnOff + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStickPro/IndexedColorFrame/IndexedColorFrame.csproj b/Examples/BlinkStickPro/IndexedColorFrame/IndexedColorFrame.csproj new file mode 100644 index 0000000..da3cd7b --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColorFrame/IndexedColorFrame.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {61527D2E-C94F-4EA5-8725-02EA6247EB8B} + Exe + IndexedColorFrame + IndexedColorFrame + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStickPro/IndexedColorFrame/Program.cs b/Examples/BlinkStickPro/IndexedColorFrame/Program.cs new file mode 100644 index 0000000..61d4f97 --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColorFrame/Program.cs @@ -0,0 +1,45 @@ +using System; +using BlinkStickDotNet; +using System.Threading; + +namespace IndexedColorFrame +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Set indexed color frame. \r\nThis example requires BlinkStick Pro with 8 smart pixels connected to R channel.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + + if (device != null) { + if (device.OpenDevice ()) { + //Set mode to WS2812. Read more about modes here: + //http://www.blinkstick.com/help/tutorials/blinkstick-pro-modes + + device.SetMode (2); + Thread.Sleep (100); + + byte[] data = new byte[3*8] + {0, 0, 255, //GRB for led0 + 0, 128, 0, //GRB for led1 + 128, 0, 0, //... + 128, 255, 0, + 0, 255, 128, + 128, 0, 128, + 0, 128, 255, + 128, 0, 0 //GRB for led7 + }; + + + device.SetColors (0, data); + + } else { + Console.WriteLine ("Could not open the device"); + } + } else { + Console.WriteLine ("BlinkStick not found"); + } + } + } +} diff --git a/Examples/BlinkStickPro/IndexedColorFrame/Properties/AssemblyInfo.cs b/Examples/BlinkStickPro/IndexedColorFrame/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7de3d64 --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColorFrame/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("IndexedColorFrame")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Examples/BlinkStickPro/IndexedColors/IndexedColors.csproj b/Examples/BlinkStickPro/IndexedColors/IndexedColors.csproj new file mode 100644 index 0000000..ed1426d --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColors/IndexedColors.csproj @@ -0,0 +1,45 @@ + + + + Debug + x86 + {725BBC2C-A4D3-4F67-ACC8-C65D92A3ECFE} + Exe + IndexedColors + IndexedColors + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + full + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + + {7AAEBEBE-E38D-47B1-A04C-A055DCEB0757} + BlinkStickDotNet + + + \ No newline at end of file diff --git a/Examples/BlinkStickPro/IndexedColors/Program.cs b/Examples/BlinkStickPro/IndexedColors/Program.cs new file mode 100644 index 0000000..bacdd20 --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColors/Program.cs @@ -0,0 +1,39 @@ +using System; +using BlinkStickDotNet; +using System.Threading; + +namespace IndexedColors +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Set indexed color. \r\nThis example requires BlinkStick Pro with 8 smart pixels connected to R channel.\r\n"); + + BlinkStick device = BlinkStick.FindFirst (); + + if (device != null) { + if (device.OpenDevice ()) { + //Set mode to WS2812. Read more about modes here: + //http://www.blinkstick.com/help/tutorials/blinkstick-pro-modes + + device.SetMode (2); + Thread.Sleep (100); + + int numberOfLeds = 8; + + for (byte i = 0; i < numberOfLeds; i++) { + device.SetColor (0, i, "#ff0000"); + + Thread.Sleep (500); + } + + } else { + Console.WriteLine ("Could not open the device"); + } + } else { + Console.WriteLine ("BlinkStick not found"); + } + } + } +} diff --git a/Examples/BlinkStickPro/IndexedColors/Properties/AssemblyInfo.cs b/Examples/BlinkStickPro/IndexedColors/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..051acaf --- /dev/null +++ b/Examples/BlinkStickPro/IndexedColors/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("IndexedColors")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Agile Innovative Ltd")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("(c) Agile Innovative Ltd")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/README.rst b/README.rst index 939b40e..606aa19 100644 --- a/README.rst +++ b/README.rst @@ -1,5 +1,5 @@ -BlinkStickDotNet -================= +.. image:: http://www.blinkstick.com/images/logos/blinkstick-dotnet.png + :alt: BlinkStickDotNet This is BlinkStick .NET interface to control devices connected to the computer. The library is written in Mono/.NET 4.0 C#. @@ -8,6 +8,11 @@ What is BlinkStick? It's smart USB LED pixel. More info about it here: http://www.blinkstick.com +API Reference +------------ + +http://arvydas.github.io/BlinkStickDotNet + Supported platforms ------------