1
0
Fork 0
mirror of https://github.com/sexybiggetje/pixdisp.git synced 2024-11-25 16:51:02 +01:00
pixdisp/drivers/pimoroniunicorn.js

58 lines
952 B
JavaScript
Raw Normal View History

2018-01-07 09:24:32 +01:00
'use strict';
2017-12-12 20:41:32 +01:00
let { Driver } = require( './driver' );
let fs = require( 'fs' );
2017-12-12 20:41:32 +01:00
class PimoroniUnicorn extends Driver {
2017-12-12 20:41:32 +01:00
constructor() {
super();
this.spi = false;
2017-12-12 20:41:32 +01:00
}
/**
* Write buffer to the SPI device if possible
*/
write( buffer = false ) {
if ( this.spi === false) {
2018-01-07 09:41:17 +01:00
if ( fs.existsSync( '/dev/spidev0.0' ) ) {
let SPI = require( 'pi-spi' );
2018-01-07 09:41:17 +01:00
this.spi = SPI.initialize( '/dev/spidev0.0' );
} else {
2018-01-07 09:41:17 +01:00
console.warn( 'Device path \'/dev/spidev0.0\' was unavailable.' );
return;
}
}
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';
}
}
);
}
/**
* Close lingering SPI device for test runner
*/
cleanup() {
if ( this.spi !== false ) {
this.spi.close();
}
}
2017-12-12 20:41:32 +01:00
}
exports.PimoroniUnicorn = PimoroniUnicorn;