Skip to content

Commit fe53c49

Browse files
committedJun 2, 2017
Glue code for exposing the native stuff
1 parent c314994 commit fe53c49

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎index.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const jsonGate = require( 'json-gate' );
4+
const pigpio = require('./build/Release/pigpio.node');
5+
6+
const optSchema = jsonGate.createSchema( {
7+
type: 'object',
8+
additionalProperties: false,
9+
required: true,
10+
properties: {
11+
pinTx: { type: 'integer', required: true, minimum: 0, maximum: 53 },
12+
pinEn: { type: 'integer', required: true, minimum: 0, maximum: 53 },
13+
invTx: { type: 'boolean', default: false },
14+
invEn: { type: 'boolean', default: false }
15+
}
16+
} );
17+
18+
class DMX {
19+
20+
constructor( opt ) {
21+
22+
// Process options
23+
optSchema.validate( opt );
24+
25+
// Init pins
26+
// The init method of the native module will make sure only one instance is created successfully
27+
pigpio.init( opt.pinTx, opt.pinEn, opt.invTx, opt.invEn );
28+
29+
}
30+
31+
transmit( data ) { return new Promise( ( resolve ) => {
32+
33+
// Make sure data is a buffer and has the right size
34+
if( ! ( data instanceof Buffer ) ) throw new Error( "data must be a Buffer" );
35+
if( data.length < 1 ) throw new Error( "data must have at least one element" );
36+
if( data.length > 512 ) throw new Error( "Maximum size of data is 512" );
37+
38+
// Transmit buffer to driver
39+
pigpio.transmit( data, resolve );
40+
41+
} ); }
42+
43+
close() {
44+
45+
pigpio.close();
46+
47+
}
48+
49+
}
50+
51+
module.exports = function( opt ) {
52+
return new DMX( opt );
53+
}

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"license": "MIT",
1515
"gypfile": true,
1616
"dependencies": {
17+
"json-gate": "^0.8.23",
1718
"nan": "^2.6.2"
1819
}
1920
}

0 commit comments

Comments
 (0)
Please sign in to comment.