mirror of
				https://github.com/martijndeb/pixdisp.git
				synced 2025-11-04 08:44:19 +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
				
			
		| 
						 | 
				
			
			@ -49,6 +49,28 @@ test( 'Set a pixel', () => {
 | 
			
		|||
 | 
			
		||||
} );
 | 
			
		||||
 | 
			
		||||
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 );
 | 
			
		||||
 | 
			
		||||
} );
 | 
			
		||||
 | 
			
		||||
test( 'Draw a line', () => {
 | 
			
		||||
	driver = driverFactory.createFromConfig( config );
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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…
	
	Add table
		
		Reference in a new issue