1
0
Fork 0
mirror of https://github.com/sexybiggetje/pixdisp.git synced 2024-12-04 19:21:03 +01:00

Add cleanup to driver, for tests. Add forceExit flag for jest because it doesn't close otherwise.

This commit is contained in:
Martijn de Boer 2018-07-03 20:34:21 +02:00
parent c5a169729d
commit bc554e5ef1
7 changed files with 131 additions and 56 deletions

View file

@ -37,15 +37,37 @@ test( 'Set a pixel', () => {
let matrix = driver.getMatrix(); let matrix = driver.getMatrix();
expect( matrix[ 0 ][ 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[ 3 ][ 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[ 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 ][ 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[ 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[ 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[ 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[ 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(); let matrix = driver.getMatrix();
expect( matrix[ 0 ][ 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[ 1 ][ 1 ] ).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[ 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[ 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[ 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[ 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[ 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[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
} ); } );
@ -78,15 +100,15 @@ test( 'Draw a circle', () => {
let matrix = driver.getMatrix(); let matrix = driver.getMatrix();
expect( matrix[ 1 ][ 0 ] ).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 ][ 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[ 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[ 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 ][ 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[ 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[ 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[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
} ); } );
@ -97,15 +119,15 @@ test( 'Draw a rectangle', () => {
let matrix = driver.getMatrix(); let matrix = driver.getMatrix();
expect( matrix[ 0 ][ 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[ 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 ][ 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[ 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 ][ 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[ 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 ][ 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[ 2 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
} ); } );
@ -116,15 +138,15 @@ test( 'Draw a filled rectangle', () => {
let matrix = driver.getMatrix(); let matrix = driver.getMatrix();
expect( matrix[ 0 ][ 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[ 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 ][ 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[ 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 ][ 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[ 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 ][ 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[ 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(); let matrix = driver.getMatrix();
expect( matrix[ 0 ][ 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[ 1 ][ 1 ] ).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[ 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[ 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[ 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[ 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[ 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[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
driver.clear( 0, 0, 0, 1 ); driver.clear( 0, 0, 0, 1 );
matrix = driver.getMatrix(); matrix = driver.getMatrix();
expect( matrix[ 0 ][ 0 ] ).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[ 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[ 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[ 3 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1 } );
} ); } );

View file

@ -46,4 +46,6 @@ test( 'MotionJPEG driver to be properly created', () => {
expect( driver ).toBeInstanceOf( MotionJPEG ); expect( driver ).toBeInstanceOf( MotionJPEG );
expect( driver.write( driver.getBuffer() ) ).toBeUndefined(); expect( driver.write( driver.getBuffer() ) ).toBeUndefined();
driver.cleanup();
} ); } );

View file

@ -1,5 +1,8 @@
'use strict'; 'use strict';
/**
* Controller for maintaining and running sandboxed code as sent by the API.
*/
class VMController class VMController
{ {
constructor( driver ) { constructor( driver ) {
@ -15,10 +18,16 @@ class VMController
} }
/**
* Current running code
*/
getRunningCode() { getRunningCode() {
return this.runningCode; return this.runningCode;
} }
/**
* Compile step one, assign sandbox and set the runningScript
*/
compileScript( script ) { compileScript( script ) {
let { NodeVM, VMScript } = require( 'vm2' ); 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() { resetSandbox() {
this.previousTime = this.getTimeData(); this.previousTime = this.getTimeData();
let matrixSize = this.driver.getSize(); let matrixSize = this.driver.getSize();
@ -59,6 +72,9 @@ class VMController
}; };
} }
/**
* Initialize compilation, next frame calculation and delta time calculation
*/
runScript( ) { runScript( ) {
let deltaLocal = 0; let deltaLocal = 0;
@ -78,6 +94,9 @@ class VMController
} }
} }
/**
* Calculate the time passed
*/
getTimeData() { getTimeData() {
let hrTime = process.hrtime(); let hrTime = process.hrtime();
return hrTime[ 0 ] * 1000000 + hrTime[ 1 ] / 1000; return hrTime[ 0 ] * 1000000 + hrTime[ 1 ] / 1000;

View file

@ -240,6 +240,13 @@ class Driver {
} }
} }
} }
/**
* Implement cleaning up of driver tasks here
*/
cleanup() {
}
} }
exports.Driver = Driver; exports.Driver = Driver;

View file

@ -1,22 +1,25 @@
'use strict'; 'use strict';
let { Driver } = require( './driver' ); let { Driver } = require( './driver' );
let fs = require( 'fs' );
let http = require( 'http' ); let http = require( 'http' );
let mjpegServer = require( 'mjpeg-server' ); let mjpegServer = require( 'mjpeg-server' );
let jpeg = require( 'jpeg-js' ); let jpeg = require( 'jpeg-js' );
let mjpegReqHandler = undefined; let mjpegReqHandler = undefined;
let httpServer = undefined;
class MotionJPEG extends Driver { class MotionJPEG extends Driver {
constructor() { constructor() {
super(); super();
this.httpServer = http.createServer( function( req, res ) { httpServer = http.createServer( function( req, res ) {
mjpegReqHandler = mjpegServer.createReqHandler( req, res ); mjpegReqHandler = mjpegServer.createReqHandler( req, res );
} ).listen( 8081, '0.0.0.0' ); } ).listen( 8081, '0.0.0.0' );
} }
/**
* Writes the current buffer to the motionjpeg stream
*/
write( buffer = false ) { write( buffer = false ) {
if ( mjpegReqHandler === undefined) { if ( mjpegReqHandler === undefined) {
return; return;
@ -36,15 +39,25 @@ class MotionJPEG extends Driver {
} }
} }
// Buffor should be complete here
var imageData = { var imageData = {
data: buffer, data: buffer,
width: this.width, width: this.width,
height: this.height height: this.height
}; };
var dt = jpeg.encode( imageData, 50 ); var dt = jpeg.encode( imageData, 100 );
mjpegReqHandler.write( dt.data ); mjpegReqHandler.write( dt.data );
} }
/**
* Close all http server requests
*/
cleanup() {
if ( httpServer !== undefined ) {
httpServer.close();
}
}
} }
exports.MotionJPEG = MotionJPEG; exports.MotionJPEG = MotionJPEG;

View file

@ -11,6 +11,9 @@ class PimoroniUnicorn extends Driver {
this.spi = false; this.spi = false;
} }
/**
* Write buffer to the SPI device if possible
*/
write( buffer = false ) { write( buffer = false ) {
if ( this.spi === false) { if ( this.spi === false) {
if ( fs.existsSync( '/dev/spidev0.0' ) ) { 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; exports.PimoroniUnicorn = PimoroniUnicorn;

View file

@ -19,7 +19,7 @@
"jest": "^22.4.4" "jest": "^22.4.4"
}, },
"scripts": { "scripts": {
"test": "jest", "test": "jest --forceExit",
"lint": "./node_modules/.bin/eslint drivers/* controllers/*" "lint": "./node_modules/.bin/eslint drivers/* controllers/*"
}, },
"jest": { "jest": {