public Boolean AddIfDoesNotExist(BlinkStick led) { Boolean newRecord = true; foreach (BlinkStickDeviceSettings current in Devices) { if (current.Serial == led.Serial) { current.Touched = true; if (current.Led == null) { current.Led = led; current.Led.OpenDevice(); } newRecord = false; } } if (newRecord) { BlinkStickDeviceSettings settings = new BlinkStickDeviceSettings(led); settings.Touched = true; Devices.Add(settings); led.OpenDevice(); } return(newRecord); }
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(); }
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"); } }
/// <summary> /// Runs a memory monitor /// </summary> /// <param name="stick">The BlinkStick to use</param> /// <param name="keepGoing">A callback method; when this returns false, the loop stops</param> public static void Run(BlinkStick stick, Func<bool> keepGoing) { var bands = new SortedDictionary<float, Color> { { 20f, Color.Blue }, { 50f, Color.Green }, { 70f, Color.Yellow }, { 90f, Color.Orange }, { 100f, Color.Red } }; ulong totalMemoryInBytes = new ComputerInfo().TotalPhysicalMemory; float totalMemoryInMegabytes = (float)((double)totalMemoryInBytes / (1024 * 1024)); using (var pc = new PerformanceCounter("Memory", "Available Mbytes")) { while (keepGoing()) { float memoryUsagePercent = (100 * (totalMemoryInMegabytes - pc.NextValue())) / totalMemoryInMegabytes; stick.WriteLine("memoryUsage = {0}", memoryUsagePercent); stick.LedColor = ColorExtensions.ValueToColor(bands, memoryUsagePercent); Thread.Sleep(1000); } } }
private BlinkStickManager() { monitor = new UsbMonitor(); //Attach to connected event monitor.Connected += (object sender, DeviceModifiedArgs e) => { BlinkStick blinkStick = GetBlinkStick(); if (blinkStick == null) { return; } InitDevice(blinkStick); }; //Attach to disconnected event monitor.Disconnected += (object sender, DeviceModifiedArgs e) => { // reset device = null; deviceInfo = GetDeviceInformationInternal(); // Send notification to subscribers foreach (BlinkStickActionListener listener in blinkStickActionListener) { listener.OnDisconnect(); } }; //Start monitoring monitor.Start(); Reconnect(); }
/// <summary> /// Open BlinkStick device /// </summary> /// <param name="serial">SerialNo of BlinkStick to open</param> public void OpenDevice(string serial) { try { Debug.WriteLine("Serial: " + serial); blink = serial == "" ? BlinkStick.FindFirst() : BlinkStick.FindBySerial(serial); blink.OpenDevice(); } catch (Exception ex) { log.Debug(ex.Message); } try { Thread.Sleep(100); if (blink.GetMode() < 2) { blink.SetMode(2); } isOpen = true; } catch (Exception ex) { isOpen = false; log.Debug(ex.Message); }; }
public BlinkStick AddBlinkStick(string serial) { var stick = BlinkStick.FindBySerial(serial); AddBlinkStick(stick); return(stick); }
public void AdviceUserToGoForAWalk() { var led = BlinkStick.FindFirst(); led.OpenDevice(); led.Blink(RgbColor.FromString("red"), 2); }
public void StayAway() { var led = BlinkStick.FindFirst(); led.OpenDevice(); led.SetColor(RgbColor.FromString("green")); }
public void StayHere() { var led = BlinkStick.FindFirst(); led.OpenDevice(); led.Blink(RgbColor.FromString("blue"), 1); }
public void AddBlinkStick(BlinkStick stick) { if (BlinkSticks.Any(o => o.Serial == stick.Serial)) { return; } BlinkSticks.Add(stick); }
public void PleaseComeBack() { var led = BlinkStick.FindFirst(); led.OpenDevice(); led.Blink(RgbColor.FromString("blue"), 3); }
public BlinkStickControlFunction() { BlinkStick = BlinkStick.FindFirst(); if (BlinkStick != null) { BlinkStick.OpenDevice(); } }
/// <summary> /// Get all connected devices /// </summary> public List <string> FetchDevices() { var blinks = new List <string>(); foreach (var blink in BlinkStick.FindAll()) { blinks.Add(blink.Serial); } return(blinks); }
private bool OpenDevice(BlinkStick device) { if (device == null) { bool reconnect = Reconnect(); // check if we could reconnect if (reconnect == false) { return(false); } } bool result = false; lock (lockGlobalAccess) { lock (lockConcurrentModification) { // is alreads a thread running? if (isThreadRunning) { // set the cancel flag cancelThread = true; } } // and wait for cancel int i = 0; while (isThreadRunning) { i++; Thread.Sleep(10); // break if it is taking too long if (i > 200) { break; } } lock (lockConcurrentModification) { if (device != null) { isThreadRunning = true; cancelThread = false; result = device.OpenDevice(); } } } return(result); }
private void Form1_Load(object sender, EventArgs e) { stick = BlinkStick.FindFirst(); stick.Enable(); stick.OpenDevice(); for (int i = 0; i < ColorList.ColorsInHex.Count; i++) { listBoxColors.Items.Add(ColorList.ColorsInHex.Keys.ElementAt(i)); } }
private void CloseDevice(BlinkStick device) { lock (lockConcurrentModification) { lastState = currentState; device.CloseDevice(); isThreadRunning = false; cancelThread = false; } }
public static void PrintDeviceInfoBlock(BlinkStick device, byte blockId) { Console.Write(String.Format(" Info block{0}: ", blockId - 1)); if (blockId == 2) { Console.WriteLine(device.InfoBlock1); } else { Console.WriteLine(device.InfoBlock2); } }
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"); } }
private bool Reconnect() { // check if BlinkStick is already connected BlinkStick fistDevice = BlinkStick.FindFirst(); if (fistDevice == null) { return(false); } InitDevice(fistDevice); return(true); }
/// <summary> /// Runs a utility to change the LED to random colors at random durations /// </summary> /// <param name="stick">The BlinkStick to use</param> /// <param name="keepGoing">A callback method; when this returns false, the loop stops</param> public static void Run(BlinkStick stick, Func<bool> keepGoing) { var r = new Random(); while (keepGoing()) { Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); int duration = r.Next(100, 2000); stick.LedColor = c; Thread.Sleep(duration); } }
/// <summary> /// Runs a morse code flasher /// </summary> /// <param name="stick">The BlinkStick to use</param> /// <param name="message">The message to transmit</param> /// <param name="color">The color to flash</param> /// <param name="dotLength">The duration of a morse dot in milliseconds</param> public static void Run(BlinkStick stick, string message, Color color, int dotLength) { IEnumerable<MorseCodeElement> encodedMessage = MorseCode.Encode(message); foreach (MorseCodeElement morseCodeElement in encodedMessage) { Color colorToFlash = MorseCode.IsGap[morseCodeElement] ? Color.Black : color; int duration = MorseCode.RelativeLengths[morseCodeElement] * dotLength; stick.BlinkWait(colorToFlash, duration); } stick.TurnOff(); }
static void Main(string[] args) { Console.WriteLine("Test BlinkStick device"); var led = BlinkStick.FindFirst(); led.OpenDevice(); led.Blink(RgbColor.FromString("green"), 3); led.SetColor(RgbColor.FromString("red")); Console.WriteLine("Press a key to quit program"); Console.ReadLine(); led.TurnOff(); }
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); } } }
public BlinkStickDeviceSettings(BlinkStick led) { this.Led = led; this.Touched = true; this.BrightnessLimit = 100; this.Running = false; this.LedFrame[0] = new byte[64 * 3]; this.LedFrame[1] = new byte[64 * 3]; this.LedFrame[2] = new byte[64 * 3]; this.LedBusy[0] = new Boolean[64]; this.LedBusy[1] = new Boolean[64]; this.LedBusy[2] = new Boolean[64]; }
/// <summary> /// Runs a mouse-over color tracker /// </summary> /// <param name="stick">The BlinkStick to use</param> /// <param name="keepGoing">A callback method; when this returns false, the loop stops</param> public static void Run(BlinkStick stick, Func<bool> keepGoing) { if (stick == null) { throw new ArgumentNullException("stick"); } while (keepGoing()) { Point pos = Cursor.Position; Color c = GetColorAt(pos); stick.LedColor = c; stick.WriteLine("Color at {0} is {1}", pos, c); Thread.Sleep(200); } }
private void InitDevice(BlinkStick newDevice) { device = newDevice; OpenDevice(device); device.SetMode(2); CloseDevice(device); deviceInfo = GetDeviceInformationInternal(); // Send notification to subscribers foreach (BlinkStickActionListener listener in blinkStickActionListener) { listener.OnConnect(); } }
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 <BlinkStick> devices = new List <BlinkStick> (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(); }
void RefreshDevices() { DataModel.Untouch(); foreach (BlinkStick led in BlinkStick.FindAll()) { DataModel.AddIfDoesNotExist(led); } DataModel.ProcessUntouched(); if (overviewWidget is OverviewWidget) { ((OverviewWidget)overviewWidget).RefreshDevices(); } else { ((OverviewSingleWidget)overviewWidget).UpdateUI(); } }
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(); }
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"); } }
public static void PrintDeviceInfo(BlinkStick device, Boolean colorOnly) { 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)); if (colorOnly) { return; } Console.WriteLine(" Serial: " + device.Serial); Console.WriteLine(" Manufacturer: " + device.ManufacturerName); Console.WriteLine(" Product Name: " + device.ProductName); Console.WriteLine(" Mode: " + device.GetMode()); PrintDeviceInfoBlock(device, 2); PrintDeviceInfoBlock(device, 3); }
static void Main(string[] args) { var cancelHelper = new CancellableShellHelper(); cancelHelper.SetupCancelHandler(); cancelHelper.WaitAfterCancel = WaitAfterCancel; 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)); device.SetMode(0); Thread.Sleep(1); for (byte i = 0; i < NumberOfLights; i++) { device.SetColor(0, i, 0, 0, 0); Thread.Sleep(100); Random r = new Random(); device.Morph(Channel, i, (byte)r.Next(32), (byte)r.Next(32), (byte)r.Next(32), 500, 30); Thread.Sleep(1); } } } }
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(); }
/// <summary> /// Runs a CPU monitor /// </summary> /// <param name="stick">The BlinkStick to use</param> /// <param name="keepGoing">A callback method; when this returns false, the loop stops</param> public static void Run(BlinkStick stick, Func<bool> keepGoing) { var bands = new SortedDictionary<float, Color> { {20f, Color.Blue}, {50f, Color.Green}, {70f, Color.Yellow}, {90f, Color.Orange}, {100f, Color.Red} }; using (var pc = new PerformanceCounter("Processor", "% Processor Time", "_Total")) { while (keepGoing()) { float cpuUsagePercent = pc.NextValue(); stick.WriteLine("cpuUsage = {0}", cpuUsagePercent); stick.LedColor = ColorExtensions.ValueToColor(bands, cpuUsagePercent); Thread.Sleep(1000); } } }