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
|
|
|
}
|
|
|
|
|
|
|
|
write( buffer ) {
|
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
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
exports.PimoroniUnicorn = PimoroniUnicorn;
|