diff --git a/__tests__/drawingoperations.js b/__tests__/drawingoperations.js index a119765..57acb8c 100644 --- a/__tests__/drawingoperations.js +++ b/__tests__/drawingoperations.js @@ -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 } ); } ); \ No newline at end of file diff --git a/__tests__/driverfactory.js b/__tests__/driverfactory.js index 6b93225..2b4c9af 100644 --- a/__tests__/driverfactory.js +++ b/__tests__/driverfactory.js @@ -46,4 +46,6 @@ test( 'MotionJPEG driver to be properly created', () => { expect( driver ).toBeInstanceOf( MotionJPEG ); expect( driver.write( driver.getBuffer() ) ).toBeUndefined(); + + driver.cleanup(); } ); \ No newline at end of file diff --git a/controllers/vmcontroller.js b/controllers/vmcontroller.js index 07b941f..858178a 100644 --- a/controllers/vmcontroller.js +++ b/controllers/vmcontroller.js @@ -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; diff --git a/drivers/driver.js b/drivers/driver.js index cf5b7e9..5279e59 100644 --- a/drivers/driver.js +++ b/drivers/driver.js @@ -240,6 +240,13 @@ class Driver { } } } + + /** + * Implement cleaning up of driver tasks here + */ + cleanup() { + + } } exports.Driver = Driver; \ No newline at end of file diff --git a/drivers/motionjpeg.js b/drivers/motionjpeg.js index c4bf0b1..817f5e1 100644 --- a/drivers/motionjpeg.js +++ b/drivers/motionjpeg.js @@ -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; \ No newline at end of file diff --git a/drivers/pimoroniunicorn.js b/drivers/pimoroniunicorn.js index 61b5415..7fce275 100644 --- a/drivers/pimoroniunicorn.js +++ b/drivers/pimoroniunicorn.js @@ -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; \ No newline at end of file diff --git a/package.json b/package.json index e8ca013..dc692e8 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "jest": "^22.4.4" }, "scripts": { - "test": "jest", + "test": "jest --forceExit", "lint": "./node_modules/.bin/eslint drivers/* controllers/*" }, "jest": {