UNPKG

tiny-pi-camera

Version:

A very lightweight promise based Node.js wrapper for the native Raspberry Pi Camera CLI tools.

77 lines (61 loc) 1.7 kB
const Execute = require('./lib/Execute'); const FLAGS = require('./flags'); const OPTS = require('./options'); class PiCamera { constructor(config) { // Ensure config is an object config = config || {}; if (!config.capture) { throw new Error('PiCamera requires a mode to be created'); } // Set the mode prop this.capture = config.capture; // Remove mode and set config delete config.capture; this.config = config; } /* * Getter */ get(prop) { if (!this.config[prop]) { throw new Error(`${ prop } isn't set on current object!`); } return this.config[prop]; } /* * Setter */ set(prop, value) { this.config[prop] = value; return this.config[prop]; } // Take a picture snap() { if (this.capture !== 'photo') { throw new Error(`snap() can only be called when Pi-Camera is in 'photo' mode`); } return Execute.run(Execute.cmd('raspistill', this.configToArray())); } // Record a video record() { if (this.capture !== 'video') { throw new Error(`record() can only be called when Pi-Camera is in 'video' mode`); } return Execute.run(Execute.cmd('raspivid', this.configToArray())); } } // Takes internal config object and return array version PiCamera.prototype.configToArray = function() { let configArray = [] Object.keys(this.config).forEach((key, i) => { // Only include flags if they're set to true if (FLAGS.includes(key) && this.config[key]) { configArray.push(`--${key}`) } else if (OPTS.includes(key)) { configArray.push(`--${key}`, this.config[key]) } }) return configArray; } module.exports = PiCamera;