2018-01-07 09:24:32 +01:00
|
|
|
'use strict';
|
2017-12-12 20:41:32 +01:00
|
|
|
|
|
|
|
let { Driver } = require( './driver' );
|
2018-01-05 21:51:33 +01:00
|
|
|
let fs = require( 'fs' );
|
|
|
|
|
2017-12-12 20:41:32 +01:00
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
class PimoroniUnicorn extends Driver {
|
2017-12-12 20:41:32 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2018-01-05 21:51:33 +01:00
|
|
|
this.spi = false;
|
2017-12-12 20:41:32 +01:00
|
|
|
}
|
|
|
|
|
2018-07-03 20:34:21 +02:00
|
|
|
/**
|
|
|
|
* Write buffer to the SPI device if possible
|
|
|
|
*/
|
2018-01-07 16:18:58 +01:00
|
|
|
write( buffer = false ) {
|
2018-01-05 21:51:33 +01:00
|
|
|
if ( this.spi === false) {
|
2018-01-07 09:41:17 +01:00
|
|
|
if ( fs.existsSync( '/dev/spidev0.0' ) ) {
|
2018-01-05 21:51:33 +01:00
|
|
|
let SPI = require( 'pi-spi' );
|
2018-01-07 09:41:17 +01:00
|
|
|
this.spi = SPI.initialize( '/dev/spidev0.0' );
|
2018-01-05 21:51:33 +01:00
|
|
|
} else {
|
2018-01-07 09:41:17 +01:00
|
|
|
console.warn( 'Device path \'/dev/spidev0.0\' was unavailable.' );
|
2018-01-05 21:51:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-05 19:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 16:18:58 +01:00
|
|
|
if ( buffer === false ) {
|
|
|
|
buffer = this.getBuffer();
|
|
|
|
}
|
|
|
|
|
2017-12-12 20:41:32 +01:00
|
|
|
this.spi.write(
|
|
|
|
Buffer.concat(
|
2018-01-07 09:24:32 +01:00
|
|
|
[
|
|
|
|
new Buffer( [ 0x72 ] ),
|
|
|
|
buffer
|
|
|
|
]
|
2017-12-12 20:41:32 +01:00
|
|
|
),
|
|
|
|
|
|
|
|
function ( err ) {
|
|
|
|
if ( err ) {
|
|
|
|
throw 'Failed writing buffer';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-07-03 20:34:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close lingering SPI device for test runner
|
|
|
|
*/
|
|
|
|
cleanup() {
|
|
|
|
if ( this.spi !== false ) {
|
|
|
|
this.spi.close();
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 20:41:32 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
exports.PimoroniUnicorn = PimoroniUnicorn;
|