Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- //node --harmony_destructuring --harmony_default_parameters
- var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })();
- var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
- function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- var ffi = require('ffi');
- var ref = require('ref');
- var StructType = require('ref-struct');
- var Enum = require('enum');
- var enums = require(__dirname + '/lib/enums.js');
- var path = require('path');
- var ArrayType = require('ref-array');
- var CorsairLedId = ref.types.int;
- var CorsairAccessMode = ref.types.int;
- var CorsairError = ref.types.int;
- var CorsairLedColor = StructType({
- ledId: CorsairLedId,
- r: ref.types.int,
- g: ref.types.int,
- b: ref.types.int
- });
- var CorsairPhysicalLayout = ref.types.int;
- var CorsairLogicalLayout = ref.types.int;
- var CorsairDeviceInfo = StructType({
- type: ref.types.int,
- model: ref.types.CString,
- physicalLayout: CorsairPhysicalLayout,
- logicalLayout: CorsairLogicalLayout,
- capsMask: ref.types.int
- });
- var CorsairDeviceInfoPtr = ref.refType(CorsairDeviceInfo);
- var CorsairLedPosition = StructType({
- ledId: CorsairLedId,
- top: ref.types.double,
- left: ref.types.double,
- height: ref.types.double,
- width: ref.types.double
- });
- var CorsairLedPositionArr = ArrayType(CorsairLedPosition);
- var CorsairLedPositions = StructType({
- numberOfLeds: ref.types.int,
- pLedPosition: CorsairLedPositionArr
- });
- var CorsairLedPositionsPtr = ref.refType(CorsairLedPositions);
- var CorsairProtocolDetails = StructType({
- sdkVersion: ref.types.CString,
- serverVersion: ref.types.CString,
- sdkProtocolVersion: ref.types.int,
- serverProtocolVersion: ref.types.int,
- breakingChanges: ref.types.bool
- });
- function CueError(err) {
- this.name = "CueError";
- this.message = enums.CorsairError.get(Math.pow(2, err)).key + (err > 3 && err != 5 ? ' -- this might be an error in the CueSDK wrapper, please contact the developer.' : '');
- var error = new Error(this.message);
- error.name = this.name;
- this.stack = error.stack.split('\n')[0] + '\n' + error.stack.split('\n').splice(3, error.stack.split('\n').length).join('\n');
- }
- CueError.prototype = Error.prototype;
- var CueSDK = (function () {
- function CueSDK() {
- var clear = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
- _classCallCheck(this, CueSDK);
- // libLocation = Full path of DLL file, if clear is true, the current lights will be cleared
- this.CueSDKLib = ffi.Library(path.join(__dirname, 'bin', process.arch, 'CUESDK_2013.dll'), {
- 'CorsairSetLedsColors': ['bool', ['int', 'pointer']],
- 'CorsairSetLedsColorsAsync': ['bool', ['int', 'pointer', 'pointer', 'pointer']],
- 'CorsairGetDeviceCount': ['int', []],
- 'CorsairGetDeviceInfo': [CorsairDeviceInfoPtr, ['int']],
- 'CorsairGetLedPositions': [CorsairLedPositionsPtr, []],
- 'CorsairGetLedIdForKeyName': [CorsairLedId, ['char']],
- 'CorsairRequestControl': ['bool', [CorsairAccessMode]],
- 'CorsairPerformProtocolHandshake': [CorsairProtocolDetails, []],
- 'CorsairGetLastError': [CorsairError, []]
- });
- this.details = this.CueSDKLib.CorsairPerformProtocolHandshake().toObject();
- this.lastError = 0;
- this._error();
- if (clear) {
- this.clear();
- }
- return this;
- }
- _createClass(CueSDK, [{
- key: 'set',
- value: function set() {
- if (arguments[0] instanceof Array) {
- if (typeof arguments[1] === 'function') {
- return this.setAsync.apply(this, arguments);
- } else {
- return this.setSync.apply(this, arguments);
- }
- } else {
- if (typeof arguments[4] === 'function') {
- return this.setIndividualAsync.apply(this, arguments);
- } else {
- return this.setIndividualSync.apply(this, arguments);
- }
- }
- }
- }, {
- key: 'setSync',
- value: function setSync(a) {
- var ids = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
- // a = array of leds [key, r, g, b] (r, g, b should be [0..255])
- var l = [];
- if (ids) {
- for (var i = 0; i < a.length; i++) {
- l[i] = this._getLedColor.apply(this, _toConsumableArray(a[i])).ref();
- }
- } else {
- for (var i = 0; i < a.length; i++) {
- var _a$i = _slicedToArray(a[i], 4);
- var key = _a$i[0];
- var _r = _a$i[1];
- var g = _a$i[2];
- var b = _a$i[3];
- l[i] = this._getLedColor(this._getLedIdForKeyName(key), _r, g, b).ref();
- }
- }
- var r = this.CueSDKLib.CorsairSetLedsColors(l.length, Buffer.concat(l));
- if (r) {
- return this;
- } else {
- this._error();
- return this;
- }
- }
- }, {
- key: 'setIndividualSync',
- value: function setIndividualSync(key, r, g, b) {
- var ids = arguments.length <= 4 || arguments[4] === undefined ? false : arguments[4];
- var l = this._getLedColor(ids ? key : this._getLedIdForKeyName(key), r, g, b).ref();
- var re = this.CueSDKLib.CorsairSetLedsColors(1, l);
- if (re) {
- return this;
- } else {
- this._error();
- return this;
- }
- }
- }, {
- key: 'setAsync',
- value: function setAsync(a, callback) {
- var ids = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];
- // a = array of leds [key, r, g, b] (r, g, b should be [0..255])
- var l = [];
- if (ids) {
- for (var i = 0; i < a.length; i++) {
- l[i] = this._getLedColor.apply(this, _toConsumableArray(a[i])).ref();
- }
- } else {
- for (var i = 0; i < a.length; i++) {
- var _a$i2 = _slicedToArray(a[i], 4);
- var key = _a$i2[0];
- var r = _a$i2[1];
- var g = _a$i2[2];
- var b = _a$i2[3];
- l[i] = this._getLedColor(this._getLedIdForKeyName(key), r, g, b).ref();
- }
- }
- var asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], function (context, succes, error) {
- if (succes) {
- callback();
- } else {
- this._error();
- }
- });
- var re = this.CueSDKLib.CorsairSetLedsColorsAsync(l.length, Buffer.concat(l), asyncFunc, ref.NULL);
- if (re) {
- return asyncFunc;
- } else {
- this._error();
- return asyncFunc;
- }
- }
- }, {
- key: 'setIndividualAsync',
- value: function setIndividualAsync(key, r, g, b, callback) {
- var ids = arguments.length <= 5 || arguments[5] === undefined ? false : arguments[5];
- var l = this._getLedColor(ids ? key : this._getLedIdForKeyName(key), r, g, b).ref();
- var asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], function (context, succes, error) {
- if (succes) {
- callback();
- } else {
- this._error();
- }
- });
- var re = this.CueSDKLib.CorsairSetLedsColorsAsync(1, l, asyncFunc, ref.NULL);
- if (re) {
- return asyncFunc;
- } else {
- this._error();
- return asyncFunc;
- }
- }
- }, {
- key: 'clear',
- value: function clear() {
- var l = [];
- for (var i = 1; i <= 154; i++) {
- l.push([i, 0, 0, 0]);
- }
- this.set(l, true);
- }
- }, {
- key: 'getLeds',
- value: function getLeds() {
- var p = this.CueSDKLib.CorsairGetLedPositions().deref();
- var l = p['pLedPosition'];
- l.length = p['numberOfLeds'];
- return l;
- }
- }, {
- key: 'close',
- value: function close() {
- this.CueSDKLib._dl.close();
- this.CueSDKLib = {};
- }
- }, {
- key: '_getLedColor',
- value: function _getLedColor(ledId, r, g, b) {
- var keyColor = new CorsairLedColor({ ledId: ledId, r: r, g: g, b: b });
- return keyColor;
- }
- }, {
- key: '_getLedIdForKeyName',
- value: function _getLedIdForKeyName(key) {
- return enums.CorsairLedId.get('CLK_' + key).value;
- }
- }, {
- key: '_error',
- value: function _error() {
- this.lastError = this.CueSDKLib.CorsairGetLastError();
- if (this.lastError != 'CE_Success' && this.lastError != 0) {
- throw new CueError(this.lastError);
- }
- }
- }]);
- return CueSDK;
- })();
- module.exports = { CueSDK: CueSDK };
Add Comment
Please, Sign In to add comment