Skip to content

Instantly share code, notes, and snippets.

@billykwok
Last active September 17, 2021 04:37
Show Gist options
  • Save billykwok/244d424afa60545acc1ac96248ef0020 to your computer and use it in GitHub Desktop.
Save billykwok/244d424afa60545acc1ac96248ef0020 to your computer and use it in GitHub Desktop.
import SerialPort from 'serialport';
import Vibrant from 'node-vibrant';
import readline from 'readline';
import util from 'util';
(async () => {
const entries = await SerialPort.list();
const entry = entries.find(
(it) => it.manufacturer && it.manufacturer.includes('Arduino')
);
if (!entry) {
console.warn('No Arduino found');
return;
}
console.info(`Connecting to ${entry.path}...`);
const port = new SerialPort(entry.path, { baudRate: 9600 });
const parser = port.pipe(
new SerialPort.parsers.Readline({ delimiter: '\r\n', encoding: 'ascii' })
);
await new Promise((resolve, _) => {
const listener = () => {
resolve(undefined);
parser.removeListener('data', listener);
};
parser.addListener('data', listener);
});
console.info('Device is now ready for serial communication');
port.write(Buffer.from(Uint8ClampedArray.of(255, 255, 255, 0x0a)));
const rl = readline.createInterface({ input: process.stdin });
const question = util.promisify(rl.question).bind(rl);
while (true) {
const url = await question('Please enter Image URL: ');
const palette = await Vibrant.from(url).getPalette();
port.write(Buffer.from(Uint8ClampedArray.of(...palette.Vibrant.rgb, 0x0a)));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment