Skip to content

Instantly share code, notes, and snippets.

@ptitb
Created March 20, 2018 15:40
Show Gist options
  • Save ptitb/2aaf77a6cc1f0683a4e3aa82582a0591 to your computer and use it in GitHub Desktop.
Save ptitb/2aaf77a6cc1f0683a4e3aa82582a0591 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const five = require("johnny-five");
const board = new five.Board({
port: "/dev/ttyACM0"
});
const SerialPort = require('serialport');
const Printer = require('thermalprinter');
const nodeWebcam = require( "node-webcam" );
const player = require('play-sound')(opts = {});
const gm = require("gm");
const pixel = require("node-pixel");
let ledState = 0;
let snapshotDirectory = 'public/snapshots/';
const moment = require('moment');
moment.locale('nl');
var printer,
strip;
let printerOpts = {
maxPrintingDots: 7,
heatingTime: 190,
heatingInterval: 2,
commandDelay: 0
};
let webcamOpts = {
width: 1280,
height: 1024,
quality: 100,
skip: 60,
};
const webcam = nodeWebcam.create(webcamOpts);
/**
* how many frames per second do you want to try?
*/
var fps = 30;
let serialPort = new SerialPort('/dev/ttyUSB0', {
baudRate: 9600
});
serialPort.on('open',function() {
printer = new Printer(serialPort, printerOpts);
printer.on('ready', function() {
console.log("Printer ready");
board.on("ready", function() {
console.log("Board ready");
let button = new five.Button(2);
let led = new five.Led(3);
var servo = new five.Servo({
controller: "PCA9685",
pin: 0,
startAt: 10
});
strip = new pixel.Strip({
board: this,
controller: "FIRMATA",
// data: 6,
// length: 7
// strips: [ {pin: 6, length: 7}, {pin: 9, length: 7}, {pin: 10, length: 7}, {pin: 11, length: 7}],
strips: [ {pin: 6, length: 7}, {pin: 9, length: 7}, {pin: 10, length: 7}, {pin: 11, length: 7}],
});
strip.on("ready", function() {
console.log("Strip ready");
dynamicRainbow(fps);
});
// strip1 = new pixel.Strip({
// board: this,
// controller: "FIRMATA",
// data: 9,
// length: 7
// });
// strip1.on("ready", function() {
// console.log("Strip 1 ready, let's go");
// dynamicRainbow(12);
// });
// strip2 = new pixel.Strip({
// board: this,
// controller: "FIRMATA",
// data: 10,
// length: 7,
// });
// strip2.on("ready", function() {
// console.log("Strip 2 ready, let's go");
// dynamicRainbow(4);
// });
// strip3 = new pixel.Strip({
// board: this,
// controller: "FIRMATA",
// data: 11,
// length: 7,
// });
// strip3.on("ready", function() {
// console.log("Strip 3 ready, let's go");
// dynamicRainbow(6);
// });
led.strobe(700);
button.on("release", function() {
let timestamp = Date.now();
let date = moment().format("DD-MM-Y HH:mm:ss");
// led.stop().off();
led.strobe(50);
player.play('data/audio/soviet.wav', function(err){
if (err) throw err
});
servo.to(160, 200)
// .on("move:complete", function() {
// console.log("camera omhoog");
// // takePicture(servo);
// // servo.to(0, 1000).on("move:complete", function() {
// // console.log("camera omlaag");
// // });
// })
;
// player.play('data/audio/soviet2.wav', { timeout: 30000 }, function(err){
// if (err) throw err
// });
webcam.capture(snapshotDirectory + timestamp, function( err, data ) {
led.stop().off();
led.strobe(700);
servo.to(10, 500);
console.log(data);
player.play('data/audio/soviet2.wav', { timeout: 30000 }, function(err){
if (err) throw err
});
// led.pulse(2000);
alterImage(data, snapshotDirectory + timestamp);
});
});
});
});
});
function randomPartij() {
let partijenJson = JSON.parse(fs.readFileSync("data/partijen.json", 'utf8'));
let randomPartij = partijenJson.partijen[Math.floor(Math.random() * partijenJson.partijen.length)];
return randomPartij.naam;
}
// function takePicture(servo) {
// servo.to(0, 1000)
// // .on("move:complete", function() {
// // console.log("camera omlaag");
// // })
// ;
// }
function alterImage(source, fileName) {
// Create monochrome image from snapshot
gm(source)
.resize(384)
// .modulate(130) //brightness
// .contrast(+3)
.monochrome()
// .fill('#FFFFFF')
// .font("data/fonts/circular-medium.otf", 60)
// .drawText(0, 0, randomPartij(), 'Center')
.write(fileName + '-altered.png', () => {
// Image created
printer
// .printImage('time.png')
.printImage(fileName + '-altered.png')
.printLine(randomPartij())
.lineFeed('2')
.print(function() {
console.log('done');
// process.exit();
});
});
// printer
// // .printImage('time.png')
// // .printImage(fileName + '-altered.png')
// .printLine(randomPartij())
// .lineFeed('1')
// .print(function() {
// console.log('done');
// // process.exit();
// });
// create watermark with party logo
// gm(source)
// .command('composite')
// .gravity('SouthEast')
// .out('-geometry', '+20+10') // offset
// .in('watermark.png')
// .stream()
// .pipe(dest);
}
function dynamicRainbow( delay){
console.log( 'dynamicRainbow' );
var showColor;
var cwi = 0; // colour wheel index (current position on colour wheel)
var foo = setInterval(function(){
if (++cwi > 255) {
cwi = 0;
}
for(var i = 0; i < strip.length; i++) {
showColor = colorWheel( ( cwi+i ) & 255 );
strip.pixel( i ).color( showColor );
// strip1.pixel( i ).color( showColor );
// strip2.pixel( i ).color( showColor );
// strip3.pixel( i ).color( showColor );
}
strip.show();
// strip1.show();
// strip2.show();
// strip3.show();
}, 1000/delay);
}
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
function colorWheel( WheelPos ){
var r,g,b;
WheelPos = 255 - WheelPos;
if ( WheelPos < 85 ) {
r = 255 - WheelPos * 3;
g = 0;
b = WheelPos * 3;
} else if (WheelPos < 170) {
WheelPos -= 85;
r = 0;
g = WheelPos * 3;
b = 255 - WheelPos * 3;
} else {
WheelPos -= 170;
r = WheelPos * 3;
g = 255 - WheelPos * 3;
b = 0;
}
// returns a string with the rgb value to be used as the parameter
return "rgb(" + r +"," + g + "," + b + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment