mirror of
https://github.com/sexybiggetje/pixdisp.git
synced 2024-12-03 18:11:02 +01:00
Add cleanup to driver, for tests. Add forceExit flag for jest because it doesn't close otherwise.
This commit is contained in:
parent
c5a169729d
commit
bc554e5ef1
7 changed files with 131 additions and 56 deletions
|
@ -37,15 +37,37 @@ test( 'Set a pixel', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 1 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
} );
|
||||
|
||||
test( 'Brightness evaluation', () => {
|
||||
driver = driverFactory.createFromConfig( config );
|
||||
|
||||
expect( driver.setBrightness( 0.5 ) ).toBeUndefined( );
|
||||
expect( driver.getBrightness() ).toEqual( 0.5 );
|
||||
|
||||
driver.setPixel( 0, 0, 255, 255, 255, 0.5 );
|
||||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 0.5 } );
|
||||
|
||||
let buffer = driver.getBuffer();
|
||||
expect( buffer[ 0 ] ).toEqual( 63 );
|
||||
expect( buffer[ 1 ] ).toEqual( 63 );
|
||||
expect( buffer[ 2 ] ).toEqual( 63 );
|
||||
expect( buffer[ 3 ] ).toEqual( 0 );
|
||||
expect( buffer[ 4 ] ).toEqual( 0 );
|
||||
expect( buffer[ 5 ] ).toEqual( 0 );
|
||||
|
||||
} );
|
||||
|
||||
|
@ -56,15 +78,15 @@ test( 'Draw a line', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
} );
|
||||
|
||||
|
@ -78,15 +100,15 @@ test( 'Draw a circle', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 1 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 1 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 7 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 0 ][ 7 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
} );
|
||||
|
||||
|
@ -97,15 +119,15 @@ test( 'Draw a rectangle', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
} );
|
||||
|
||||
|
@ -116,15 +138,15 @@ test( 'Draw a filled rectangle', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
} );
|
||||
|
||||
|
@ -135,23 +157,23 @@ test( 'Draw a line and clear the matrix', () => {
|
|||
|
||||
let matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1 } );
|
||||
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
driver.clear( 0, 0, 0, 1 );
|
||||
|
||||
matrix = driver.getMatrix();
|
||||
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} );
|
||||
expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
|
||||
|
||||
} );
|
|
@ -46,4 +46,6 @@ test( 'MotionJPEG driver to be properly created', () => {
|
|||
|
||||
expect( driver ).toBeInstanceOf( MotionJPEG );
|
||||
expect( driver.write( driver.getBuffer() ) ).toBeUndefined();
|
||||
|
||||
driver.cleanup();
|
||||
} );
|
|
@ -1,5 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Controller for maintaining and running sandboxed code as sent by the API.
|
||||
*/
|
||||
class VMController
|
||||
{
|
||||
constructor( driver ) {
|
||||
|
@ -15,10 +18,16 @@ class VMController
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Current running code
|
||||
*/
|
||||
getRunningCode() {
|
||||
return this.runningCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile step one, assign sandbox and set the runningScript
|
||||
*/
|
||||
compileScript( script ) {
|
||||
|
||||
let { NodeVM, VMScript } = require( 'vm2' );
|
||||
|
@ -34,6 +43,10 @@ class VMController
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the sandbox of available properties to keep things secure between script frames.
|
||||
* Every frame and compilation will have it's own new sandbox.
|
||||
*/
|
||||
resetSandbox() {
|
||||
this.previousTime = this.getTimeData();
|
||||
let matrixSize = this.driver.getSize();
|
||||
|
@ -59,6 +72,9 @@ class VMController
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize compilation, next frame calculation and delta time calculation
|
||||
*/
|
||||
runScript( ) {
|
||||
|
||||
let deltaLocal = 0;
|
||||
|
@ -78,6 +94,9 @@ class VMController
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the time passed
|
||||
*/
|
||||
getTimeData() {
|
||||
let hrTime = process.hrtime();
|
||||
return hrTime[ 0 ] * 1000000 + hrTime[ 1 ] / 1000;
|
||||
|
|
|
@ -240,6 +240,13 @@ class Driver {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement cleaning up of driver tasks here
|
||||
*/
|
||||
cleanup() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
exports.Driver = Driver;
|
|
@ -1,22 +1,25 @@
|
|||
'use strict';
|
||||
|
||||
let { Driver } = require( './driver' );
|
||||
let fs = require( 'fs' );
|
||||
let http = require( 'http' );
|
||||
let mjpegServer = require( 'mjpeg-server' );
|
||||
let jpeg = require( 'jpeg-js' );
|
||||
|
||||
let mjpegReqHandler = undefined;
|
||||
let httpServer = undefined;
|
||||
|
||||
class MotionJPEG extends Driver {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.httpServer = http.createServer( function( req, res ) {
|
||||
httpServer = http.createServer( function( req, res ) {
|
||||
mjpegReqHandler = mjpegServer.createReqHandler( req, res );
|
||||
} ).listen( 8081, '0.0.0.0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the current buffer to the motionjpeg stream
|
||||
*/
|
||||
write( buffer = false ) {
|
||||
if ( mjpegReqHandler === undefined) {
|
||||
return;
|
||||
|
@ -36,15 +39,25 @@ class MotionJPEG extends Driver {
|
|||
}
|
||||
}
|
||||
|
||||
// Buffor should be complete here
|
||||
var imageData = {
|
||||
data: buffer,
|
||||
width: this.width,
|
||||
height: this.height
|
||||
};
|
||||
|
||||
var dt = jpeg.encode( imageData, 50 );
|
||||
var dt = jpeg.encode( imageData, 100 );
|
||||
mjpegReqHandler.write( dt.data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all http server requests
|
||||
*/
|
||||
cleanup() {
|
||||
if ( httpServer !== undefined ) {
|
||||
httpServer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.MotionJPEG = MotionJPEG;
|
|
@ -11,6 +11,9 @@ class PimoroniUnicorn extends Driver {
|
|||
this.spi = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write buffer to the SPI device if possible
|
||||
*/
|
||||
write( buffer = false ) {
|
||||
if ( this.spi === false) {
|
||||
if ( fs.existsSync( '/dev/spidev0.0' ) ) {
|
||||
|
@ -41,6 +44,15 @@ class PimoroniUnicorn extends Driver {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close lingering SPI device for test runner
|
||||
*/
|
||||
cleanup() {
|
||||
if ( this.spi !== false ) {
|
||||
this.spi.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.PimoroniUnicorn = PimoroniUnicorn;
|
|
@ -19,7 +19,7 @@
|
|||
"jest": "^22.4.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"test": "jest --forceExit",
|
||||
"lint": "./node_modules/.bin/eslint drivers/* controllers/*"
|
||||
},
|
||||
"jest": {
|
||||
|
|
Loading…
Reference in a new issue