### Install BlinkStick Node.js Example Dependencies Source: https://github.com/arvydas/blinkstick-node/wiki/Home Installs the necessary Node.js dependencies for the BlinkStick example projects. This command is run from within the example's directory. ```bash cd node_modules/blinkstick/examples/picker npm install ``` -------------------------------- ### Run BlinkStick Node.js Example Server Source: https://github.com/arvydas/blinkstick-node/wiki/Home Starts the example server for the BlinkStick Node.js project, typically used for interactive demos like a color picker. The URL to access the demo will be provided in the console output. ```bash node server ``` -------------------------------- ### Run General BlinkStick Node.js Example Source: https://github.com/arvydas/blinkstick-node/wiki/Home Executes a specific BlinkStick Node.js example script. Replace '[example_name]/[example.js]' with the actual path to the desired example file. ```bash node node_modules/blinkstick/examples/[example_name]/[example.js] ``` -------------------------------- ### Install BlinkStick Node Module Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Installs the BlinkStick Node.js module using the npm package manager. This is the primary step to start using the library in your Node.js project. ```bash $ npm install blinkstick ``` -------------------------------- ### Require BlinkStick Module Source: https://github.com/arvydas/blinkstick-node/wiki/Home Initializes the blinkstick module for use in your Node.js application. This is the first step to interact with BlinkStick devices. ```javascript var blinkstick = require('blinkstick'); ``` -------------------------------- ### Linux Package Installations Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Installs Node.js, npm, and libusb using the apt-get package manager on Debian-based Linux distributions. This command ensures the necessary runtime and system libraries are available. ```bash $ sudo apt-get install libusb nodejs npm ``` -------------------------------- ### Mac OSX Homebrew Installations Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Installs Node.js and libusb on Mac OSX using the Homebrew package manager. These are common prerequisites for development environments requiring USB device interaction. ```bash $ brew install node $ brew install libusb ``` -------------------------------- ### Control BlinkStick with Keyboard Input in Node.js Source: https://github.com/arvydas/blinkstick-node/wiki/Blink-indefinitely-and-stop-blinking-when-key-is-pressed This JavaScript code allows users to control a BlinkStick device using keyboard input. It finds the first connected BlinkStick, defines functions to blink the LED on and off with configurable color and delay, and uses Node.js's standard input to listen for key presses ('1' to start, '2' to stop). Pressing Ctrl+C exits the script. Dependencies include the 'blinkstick-node' library. ```javascript // --------------------- Imports --------------------- var blinkstick = require('blinkstick'); // --------------------- Configuration --------------------- //Delay between on/off var delay = 100; //Color to blink var r = 255; var g = 255; var b = 255; //Additional options var options = { channel: 0, index: 0 } // --------------------- Variables --------------------- //Variable to hold the blink timeout and cancel it when button is pressed var blinkTimeout; //Variable to keep the blinking state var blinking = false; // --------------------- Main Code --------------------- //Find first BlinkStick led = blinkstick.findFirst(); //Blinker function to turn led on/off var blinker = function () { led.setColor(r, g, b, options); blinkTimeout = setTimeout(function() { led.setColor(0, 0, 0, options); blinkTimeout = setTimeout(function() { blinker(); }, delay); }, delay); } //Call this function to start blinking var blink = function () { if (!blinking) { blinking = true; blinker(); } } //Call this function to stop blinking var stop = function () { if (blinking) { blinking = false; clearTimeout(blinkTimeout); led.setColor(0, 0, 0, options); } } // ---------------- Keyboard interaction ---------------- var stdin = process.stdin; stdin.setRawMode( true ); stdin.resume(); stdin.setEncoding( 'utf8' ); stdin.on( 'data', function( key ){ // ctrl-c ( end of text ) if ( key === '\u0003' ) { process.exit(); } if (key == '1') { blink(); } else if (key == '2') { stop(); } }); process.stdout.write("BlinkStick blink demo\r\n\r\n"); process.stdout.write("Press:\r\n"); process.stdout.write(" 1 to start blinking\r\n"); process.stdout.write(" 2 to stop blinking\r\n"); process.stdout.write(" Ctrl+C to exit\r\n"); ``` -------------------------------- ### Basic BlinkStick Node.js Usage Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Demonstrates basic operations for the BlinkStick Node.js module. It shows how to find the first connected BlinkStick device and set its LED color to red. Requires Node.js and the blinkstick module to be installed. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); led.setColor('red', function(){ }); ``` -------------------------------- ### Node.js LTS Version Installation Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Installs the latest Long Term Support (LTS) version of Node.js using the Node Version Manager (nvm). This ensures compatibility and stability for Node.js applications. ```bash $ nvm install --lts ``` -------------------------------- ### Raspberry Pi Development Package Installation Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Installs libusb-1.0 and libudev development packages on a Raspberry Pi using apt-get. These are necessary for USB device communication on embedded Linux systems. ```bash $ sudo apt-get install libusb-1.0-0-dev libudev-dev -y ``` -------------------------------- ### Find First BlinkStick Device Source: https://github.com/arvydas/blinkstick-node/wiki/Home Finds and returns the first available BlinkStick device connected to the system. This is useful when only one device is expected or needed. ```javascript var led = blinkstick.findFirst(); ``` -------------------------------- ### Set Multiple LEDs on BlinkStick Pro with Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This example demonstrates how to control multiple LEDs simultaneously on a BlinkStick Pro device. It involves providing an array of color data for individual LEDs using the 'setColors' method. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // LED data format: [g0, r0, b0, g1, r1, b1, ...] // Set first LED to red, second to green var ledData = [0, 255, 0, 255, 0, 0]; device.setColors(0, ledData, function(err) { if (!err) { console.log('Multiple LEDs set successfully'); } else { console.error('Error setting LEDs:', err); } }); } ``` -------------------------------- ### Get BlinkStick Device Info Source: https://github.com/arvydas/blinkstick-node/wiki/Home Retrieves specific information about a connected BlinkStick device, such as its serial number, manufacturer, or description. Each operation is asynchronous and uses a callback. ```javascript led.getSerial(function(err, data) { console.log(data); }); led.getManufacturer(function(err, data) { console.log(data); }); led.getDescription(function(err, data) { console.log(data); }); ``` -------------------------------- ### Create Morph Animation with BlinkStick Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This example shows how to smoothly transition the BlinkStick's LED color from one to another over a specified duration using configurable steps. It utilizes the 'morph' method from the 'blinkstick' module. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; // Morph smoothly through colors device.morph('red', {'duration': 1000, 'steps': 50}, function() { device.morph('green', {'duration': 1000, 'steps': 50}, function() { device.morph('blue', {'duration': 1000, 'steps': 50}, function() { finished = true; }); }); }); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Get Current Color as Hex String with BlinkStick Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This example shows how to get the current LED color of a BlinkStick device formatted as a hex string. This is useful for integrating color data with web applications. It requires the 'blinkstick' module. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { device.setColor('#ff6347', function() { device.getColorString(function(err, color) { if (!err) { console.log('Current color: ' + color); // #ff6347 } }); }); } ``` -------------------------------- ### Set LED Color with Hex Code (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Sets an LED's color using standard hex color notation. This example demonstrates setting the color sequentially to red, green, blue, and then off. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; // Set to red device.setColor('#ff0000'); setTimeout(function() { // Set to green device.setColor('#00ff00'); setTimeout(function() { // Set to blue device.setColor('#0000ff'); setTimeout(function() { // Turn off device.setColor('#000000', function() { finished = true; }); }, 500); }, 500); }, 500); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Get BlinkStick Color Source: https://github.com/arvydas/blinkstick-node/wiki/Home Retrieves the current color of a BlinkStick device. Provides methods to get the color as individual RGB values or as a hex string. ```javascript led.getColor(function(red, green, blue) { ... }); led.getColorString(function(rgb) { ... }); ``` -------------------------------- ### Blink Animation (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Creates a blinking effect by alternating between a specified color and the off state with configurable timing and repetitions. This example chains multiple blink animations with different colors and durations. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; // Blink red 5 times with 100ms delay device.blink('red', {'delay': 100, 'repeats': 5}, function() { // Blink green 10 times with 50ms delay device.blink('green', {'delay': 50, 'repeats': 10}, function() { // Blink blue 20 times with 25ms delay device.blink('blue', {'delay': 25, 'repeats': 20}, function() { finished = true; }); }); }); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Find All BlinkStick Devices Source: https://github.com/arvydas/blinkstick-node/wiki/Home Retrieves all connected BlinkStick devices. This function returns an array of device objects that can be further controlled. ```javascript var leds = blinkstick.findAll(); ``` -------------------------------- ### Set Specific LED Color with Channel and Index Options in Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This example demonstrates how to set the color of a specific LED on multi-LED BlinkStick devices by specifying the channel and index. It uses the 'setColor' method with an options object. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Set LED at channel 0, index 5 to purple var options = { 'channel': 0, 'index': 5 }; device.setColor(128, 0, 128, options, function(err) { if (!err) { console.log('LED 5 on channel 0 set to purple'); } }); } ``` -------------------------------- ### BlinkStick Animations (Pulse, Blink, Morph) Source: https://github.com/arvydas/blinkstick-node/wiki/Home Initiates color animations on a BlinkStick device, including pulsing, blinking, and morphing. These functions accept similar color and option parameters as setColor and notify upon completion. ```javascript //All color parameters and options work on these functions too led.pulse(rgb, function() { /* called when color animation is complete */ }); led.blink(rgb, function() { /* called when color animation is complete */ }); led.morph(rgb, function() { /* called when color animation is complete */ }); ``` -------------------------------- ### Find All BlinkStick Serials Source: https://github.com/arvydas/blinkstick-node/wiki/Home Retrieves all serial numbers for connected BlinkStick devices. It uses a callback function to handle the asynchronous retrieval of serial numbers. ```javascript blinkstick.findAllSerials(function(serials) { console.log(serials); }); ``` -------------------------------- ### Set LED Color with CSS Color Names (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Uses standard CSS color keywords for intuitive color selection, including 'random' for random colors. This example cycles through red, aqua, gold, and finally a random color. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Set using CSS color names device.setColor('red', function() { device.setColor('aqua', function() { device.setColor('gold', function() { // Random color device.setColor('random', function() { console.log('Animation complete'); }); }); }); }); } ``` -------------------------------- ### Get BlinkStick Device Information with Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This code retrieves various metadata about the connected BlinkStick device, including its description, manufacturer, serial number, mode, and current color. It uses multiple asynchronous methods from the 'blinkstick' module. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; console.log('Device found!'); device.getDescription(function(error, result) { console.log('Description: ' + result); device.getManufacturer(function(error, result) { console.log('Manufacturer: ' + result); device.getSerial(function(error, result) { console.log('Serial: ' + result); device.getMode(function(error, result) { console.log('Mode: ' + result); device.getColorString(function(error, result) { console.log('Color: ' + result); finished = true; }); }); }); }); }); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Get Multiple LED Colors from BlinkStick Pro with Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This code snippet shows how to retrieve color data from a specified number of LEDs on BlinkStick Pro devices. The 'getColors' method returns an array representing the color data for each LED. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; // Get color data for 64 LEDs device.getColors(64, function(err, data) { if (!err) { console.log('LED data:', data); // Data format: [g0, r0, b0, g1, r1, b1, ...] } finished = true; }); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Set LED Color with RGB Values (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Controls an LED's color using individual red, green, and blue intensity values (0-255). This example sets the color to pure red and includes error handling. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Set to pure red device.setColor(255, 0, 0, function(err) { if (err) { console.error('Error setting color:', err); } else { console.log('Color set successfully'); } }); } ``` -------------------------------- ### Untitled No description -------------------------------- ### Get Current LED Color with BlinkStick Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This code snippet demonstrates how to retrieve the current RGB color values from a BlinkStick device. It uses the 'getColor' method after potentially setting a color, requiring the 'blinkstick' module. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { device.setColor(128, 64, 255, function() { device.getColor(0, function(err, r, g, b) { if (!err) { console.log('Current color - R:' + r + ' G:' + g + ' B:' + b); } }); }); } ``` -------------------------------- ### Set BlinkStick Color Source: https://github.com/arvydas/blinkstick-node/wiki/Home Sets the color of a BlinkStick device using RGB values, hex strings, or a 'random' option. Supports optional callbacks for completion notification. Includes a specific method for BlinkStick Pro with channel and index options. ```javascript // rgb is a '#RRGGBB' string // red/green/blue are each numbers in [0..255] // function is optional led.setColor(rgb, function() { /* called when color is changed */ }); led.setColor(red, green, blue, function() { /* called when color is changed */ }); led.setColor('random', function() { /* called when color is changed */ }); led.turnOff(); // i.e., setColor(0, 0, 0) //Set random color for 4th LED on R channel led.setColor('random', { 'channel': 0, 'index': 4 }, function() { /* called when color is changed */ }); ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Find First BlinkStick Device (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Discovers and returns the first BlinkStick device connected to the computer. It checks if a device is found and sets its color to green if successful. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { console.log('BlinkStick device found!'); device.setColor('green'); } else { console.log('No BlinkStick devices found'); } ``` -------------------------------- ### Create Pulse Animation with BlinkStick Node.js Source: https://context7.com/arvydas/blinkstick-node/llms.txt This code demonstrates how to create a smooth pulsing effect on a BlinkStick device by transitioning between specified colors and back to off. It requires the 'blinkstick' module and a connected BlinkStick device. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { var finished = false; // Pulse through different colors device.pulse('red', function() { device.pulse('green', function() { device.pulse('blue', function() { finished = true; }); }); }); var wait = function() { if (!finished) setTimeout(wait, 100); }; wait(); } ``` -------------------------------- ### Untitled No description -------------------------------- ### Advanced BlinkStick Node.js Animations Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Illustrates more advanced usage of the BlinkStick Node.js module, including calling asynchronous functions for blinking, pulsing, and setting colors in sequence. This demonstrates chaining of operations. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); led.blink('random', function(){ led.pulse('random', function(){ led.setColor('red', function(){ }); }); }); ``` -------------------------------- ### Find All BlinkStick Devices (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Returns an array of all connected BlinkStick devices, allowing for multi-device control. It then iterates through the found devices and logs their serial numbers. ```javascript var blinkstick = require('blinkstick'); var devices = blinkstick.findAll(); console.log('Found ' + devices.length + ' BlinkStick device(s)'); devices.forEach(function(device, index) { device.getSerial(function(err, serial) { console.log('Device ' + index + ': ' + serial); }); }); ``` -------------------------------- ### Linux udev Rule for BlinkStick Permissions Source: https://github.com/arvydas/blinkstick-node/blob/master/README.md Provides a Linux udev rule to grant necessary permissions for the BlinkStick device. This is crucial for Node.js applications to access the device, especially when encountering 'cannot open device' errors. Requires root privileges to modify system rules. ```bash echo "KERNEL==\"hidraw*\",SUBSYSTEM==\"hidraw\",ATTRS{idVendor}==\"20a0\",ATTRS{idProduct}==\"41e5\",MODE=\"0666\"" | sudo tee /etc/udev/rules.d/85-blinkstick-hid.rules ``` -------------------------------- ### Set BlinkStick Pro Device Mode (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Configures the operating mode for BlinkStick Pro devices. Modes include Normal (0), Inverse (1), and WS2812 (2) for addressable LED strips. It takes a mode value and a callback function as arguments. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Set to WS2812 mode for addressable LED strips device.setMode(2, function(err) { if (!err) { console.log('Mode set to WS2812'); device.getMode(function(err, mode) { console.log('Current mode:', mode); }); } }); } ``` -------------------------------- ### Find Device by Serial Number (Node.js) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Locates a specific BlinkStick device using its serial number for targeted control. If the device is found, its color is set to blue. ```javascript var blinkstick = require('blinkstick'); blinkstick.findBySerial('BS012345-1.0', function(device) { if (device) { console.log('Target device found'); device.setColor('blue'); } else { console.log('Device with specified serial not found'); } }); ``` -------------------------------- ### Set BlinkStick Info Block Data (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Stores custom data (up to 32 bytes) in a device's Info Block memory. This data can be used for identification or configuration. The library provides methods like `setInfoBlock1` and `getInfoBlock1`. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Store device name in InfoBlock1 device.setInfoBlock1('Office Desk Light', function(err) { if (!err) { // Retrieve stored data device.getInfoBlock1(function(err, data) { console.log('Device name:', data); // "Office Desk Light" }); } }); } ``` -------------------------------- ### Web Color Picker with WebSockets (JavaScript) Source: https://github.com/arvydas/blinkstick-node/blob/master/examples/picker/public/index.html This JavaScript code handles the frontend logic for the color picker. It connects to a WebSocket server to send and receive color information. It supports two modes: a native HTML5 color input and separate sliders for Red, Green, and Blue values. It dynamically adjusts the background color of the page and sends the selected color to the server. ```javascript var socket = io.connect(), red, green, blue, i, html = '', picker = document.getElementById('picker'); function emitRGB () { var hex = '#' + red.value + green.value + blue.value; document.body.style.backgroundColor = hex; socket.emit('color', { hex: hex }); } if (picker.type == 'color') { document.body.className = 'picker'; socket.on('color', function (val) { picker.value = val; }); picker.addEventListener('input', function () { socket.emit('color', { hex: this.value }); }); } else { red = document.getElementById('red'); green = document.getElementById('green'); blue = document.getElementById('blue'); for (i = 0; i < 256; i++) html += ''; red.innerHTML = green.innerHTML = blue.innerHTML = html; socket.on('color', function (val) { red.value = parseInt(val.substr(1, 2), 16); green.value = parseInt(val.substr(3, 2), 16); blue.value = parseInt(val.substr(5, 2), 16); document.body.style.backgroundColor = val; }); red.addEventListener('change', emitRGB); green.addEventListener('change', emitRGB); blue.addEventListener('change', emitRGB); } ``` -------------------------------- ### Find All BlinkStick Device Serial Numbers (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Retrieves the serial numbers of all connected BlinkStick devices without creating individual device instances. This is useful for identifying devices before establishing a connection. ```javascript var blinkstick = require('blinkstick'); blinkstick.findAllSerials(function(serials) { console.log('Found devices with serials:', serials); // Output: ['BS012345-1.0', 'BS012346-2.0'] serials.forEach(function(serial) { console.log('Serial:', serial); }); }); ``` -------------------------------- ### Turn Off BlinkStick LED (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt A convenience method to turn off the LED on a BlinkStick device by setting its color to black. This is often used to reset the LED to an off state after a period of activity. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { device.setColor('red'); setTimeout(function() { device.turnOff(); console.log('LED turned off'); }, 2000); } ``` -------------------------------- ### Close BlinkStick Device Connection (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Closes the connection to a BlinkStick device, stopping all animations and releasing system resources. This should be called when the application no longer needs to interact with the device. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { device.setColor('green', function() { // Close device when done device.close(function(err) { if (!err) { console.log('Device closed successfully'); } }); }); } ``` -------------------------------- ### Stop All BlinkStick Animations (JavaScript) Source: https://context7.com/arvydas/blinkstick-node/llms.txt Immediately stops all running animations and color transitions on a BlinkStick device. This function is useful for resetting the device state or interrupting ongoing visual effects. ```javascript var blinkstick = require('blinkstick'); var device = blinkstick.findFirst(); if (device) { // Start a long-running pulse animation device.pulse('red', {'duration': 5000}); // Stop after 1 second setTimeout(function() { device.stop(); console.log('Animation stopped'); }, 1000); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.