From 1a7c836ce65388ea0f56f77b040170a62bb6db7d Mon Sep 17 00:00:00 2001 From: Martijn de Boer Date: Fri, 5 Jan 2018 21:51:33 +0100 Subject: [PATCH] Make Pimoroni Unicorn driver test the spiPath before executing the full write() --- drivers/pimoroniunicorn.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/pimoroniunicorn.js b/drivers/pimoroniunicorn.js index 42c4be4..ecf2257 100644 --- a/drivers/pimoroniunicorn.js +++ b/drivers/pimoroniunicorn.js @@ -1,20 +1,26 @@ "use strict"; let { Driver } = require( './driver' ); +let fs = require( 'fs' ); + class PimoroniUnicorn extends Driver { constructor() { super(); - this.initializedSpi = false; + this.spi = false; + this.spiPath = '/dev/spidev0.0'; } write( buffer ) { - if ( this.initializedSpi !== true ) { - this.initializedSpi = true; - - let SPI = require( 'pi-spi' ); - this.spi = SPI.initialize( '/dev/spidev0.0' ); + if ( this.spi === false) { + if ( fs.existsSync( this.spiPath ) ) { + let SPI = require( 'pi-spi' ); + this.spi = SPI.initialize( this.spiPath ); + } else { + console.warn( 'Device path ' + this.spiPath + ' was unavailable.' ); + return; + } } this.spi.write(