ArmyOfAnarchists

Untitled

Jan 31st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. "use strict";
  2.  
  3. //node --harmony_destructuring --harmony_default_parameters
  4.  
  5. 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"); } }; })();
  6.  
  7. 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; }; })();
  8.  
  9. 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); } }
  10.  
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12.  
  13. var ffi = require('ffi');
  14. var ref = require('ref');
  15. var StructType = require('ref-struct');
  16. var Enum = require('enum');
  17. var enums = require(__dirname + '/lib/enums.js');
  18. var path = require('path');
  19. var ArrayType = require('ref-array');
  20.  
  21. var CorsairLedId = ref.types.int;
  22. var CorsairAccessMode = ref.types.int;
  23. var CorsairError = ref.types.int;
  24.  
  25. var CorsairLedColor = StructType({
  26. ledId: CorsairLedId,
  27. r: ref.types.int,
  28. g: ref.types.int,
  29. b: ref.types.int
  30. });
  31.  
  32. var CorsairPhysicalLayout = ref.types.int;
  33. var CorsairLogicalLayout = ref.types.int;
  34.  
  35. var CorsairDeviceInfo = StructType({
  36. type: ref.types.int,
  37. model: ref.types.CString,
  38. physicalLayout: CorsairPhysicalLayout,
  39. logicalLayout: CorsairLogicalLayout,
  40. capsMask: ref.types.int
  41. });
  42.  
  43. var CorsairDeviceInfoPtr = ref.refType(CorsairDeviceInfo);
  44.  
  45. var CorsairLedPosition = StructType({
  46. ledId: CorsairLedId,
  47. top: ref.types.double,
  48. left: ref.types.double,
  49. height: ref.types.double,
  50. width: ref.types.double
  51. });
  52.  
  53. var CorsairLedPositionArr = ArrayType(CorsairLedPosition);
  54.  
  55. var CorsairLedPositions = StructType({
  56. numberOfLeds: ref.types.int,
  57. pLedPosition: CorsairLedPositionArr
  58. });
  59.  
  60. var CorsairLedPositionsPtr = ref.refType(CorsairLedPositions);
  61.  
  62. var CorsairProtocolDetails = StructType({
  63. sdkVersion: ref.types.CString,
  64. serverVersion: ref.types.CString,
  65. sdkProtocolVersion: ref.types.int,
  66. serverProtocolVersion: ref.types.int,
  67. breakingChanges: ref.types.bool
  68. });
  69.  
  70. function CueError(err) {
  71. this.name = "CueError";
  72. 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.' : '');
  73. var error = new Error(this.message);
  74. error.name = this.name;
  75. this.stack = error.stack.split('\n')[0] + '\n' + error.stack.split('\n').splice(3, error.stack.split('\n').length).join('\n');
  76. }
  77.  
  78. CueError.prototype = Error.prototype;
  79.  
  80. var CueSDK = (function () {
  81. function CueSDK() {
  82. var clear = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
  83.  
  84. _classCallCheck(this, CueSDK);
  85.  
  86. // libLocation = Full path of DLL file, if clear is true, the current lights will be cleared
  87. this.CueSDKLib = ffi.Library(path.join(__dirname, 'bin', process.arch, 'CUESDK_2013.dll'), {
  88. 'CorsairSetLedsColors': ['bool', ['int', 'pointer']],
  89. 'CorsairSetLedsColorsAsync': ['bool', ['int', 'pointer', 'pointer', 'pointer']],
  90. 'CorsairGetDeviceCount': ['int', []],
  91. 'CorsairGetDeviceInfo': [CorsairDeviceInfoPtr, ['int']],
  92. 'CorsairGetLedPositions': [CorsairLedPositionsPtr, []],
  93. 'CorsairGetLedIdForKeyName': [CorsairLedId, ['char']],
  94. 'CorsairRequestControl': ['bool', [CorsairAccessMode]],
  95. 'CorsairPerformProtocolHandshake': [CorsairProtocolDetails, []],
  96. 'CorsairGetLastError': [CorsairError, []]
  97. });
  98. this.details = this.CueSDKLib.CorsairPerformProtocolHandshake().toObject();
  99. this.lastError = 0;
  100. this._error();
  101.  
  102. if (clear) {
  103. this.clear();
  104. }
  105.  
  106. return this;
  107. }
  108.  
  109. _createClass(CueSDK, [{
  110. key: 'set',
  111. value: function set() {
  112. if (arguments[0] instanceof Array) {
  113. if (typeof arguments[1] === 'function') {
  114. return this.setAsync.apply(this, arguments);
  115. } else {
  116. return this.setSync.apply(this, arguments);
  117. }
  118. } else {
  119. if (typeof arguments[4] === 'function') {
  120. return this.setIndividualAsync.apply(this, arguments);
  121. } else {
  122. return this.setIndividualSync.apply(this, arguments);
  123. }
  124. }
  125. }
  126. }, {
  127. key: 'setSync',
  128. value: function setSync(a) {
  129. var ids = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
  130. // a = array of leds [key, r, g, b] (r, g, b should be [0..255])
  131. var l = [];
  132. if (ids) {
  133. for (var i = 0; i < a.length; i++) {
  134. l[i] = this._getLedColor.apply(this, _toConsumableArray(a[i])).ref();
  135. }
  136. } else {
  137. for (var i = 0; i < a.length; i++) {
  138. var _a$i = _slicedToArray(a[i], 4);
  139.  
  140. var key = _a$i[0];
  141. var _r = _a$i[1];
  142. var g = _a$i[2];
  143. var b = _a$i[3];
  144.  
  145. l[i] = this._getLedColor(this._getLedIdForKeyName(key), _r, g, b).ref();
  146. }
  147. }
  148. var r = this.CueSDKLib.CorsairSetLedsColors(l.length, Buffer.concat(l));
  149. if (r) {
  150. return this;
  151. } else {
  152. this._error();
  153. return this;
  154. }
  155. }
  156. }, {
  157. key: 'setIndividualSync',
  158. value: function setIndividualSync(key, r, g, b) {
  159. var ids = arguments.length <= 4 || arguments[4] === undefined ? false : arguments[4];
  160.  
  161. var l = this._getLedColor(ids ? key : this._getLedIdForKeyName(key), r, g, b).ref();
  162. var re = this.CueSDKLib.CorsairSetLedsColors(1, l);
  163. if (re) {
  164. return this;
  165. } else {
  166. this._error();
  167. return this;
  168. }
  169. }
  170. }, {
  171. key: 'setAsync',
  172. value: function setAsync(a, callback) {
  173. var ids = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];
  174. // a = array of leds [key, r, g, b] (r, g, b should be [0..255])
  175. var l = [];
  176. if (ids) {
  177. for (var i = 0; i < a.length; i++) {
  178. l[i] = this._getLedColor.apply(this, _toConsumableArray(a[i])).ref();
  179. }
  180. } else {
  181. for (var i = 0; i < a.length; i++) {
  182. var _a$i2 = _slicedToArray(a[i], 4);
  183.  
  184. var key = _a$i2[0];
  185. var r = _a$i2[1];
  186. var g = _a$i2[2];
  187. var b = _a$i2[3];
  188.  
  189. l[i] = this._getLedColor(this._getLedIdForKeyName(key), r, g, b).ref();
  190. }
  191. }
  192. var asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], function (context, succes, error) {
  193. if (succes) {
  194. callback();
  195. } else {
  196. this._error();
  197. }
  198. });
  199. var re = this.CueSDKLib.CorsairSetLedsColorsAsync(l.length, Buffer.concat(l), asyncFunc, ref.NULL);
  200. if (re) {
  201. return asyncFunc;
  202. } else {
  203. this._error();
  204. return asyncFunc;
  205. }
  206. }
  207. }, {
  208. key: 'setIndividualAsync',
  209. value: function setIndividualAsync(key, r, g, b, callback) {
  210. var ids = arguments.length <= 5 || arguments[5] === undefined ? false : arguments[5];
  211.  
  212. var l = this._getLedColor(ids ? key : this._getLedIdForKeyName(key), r, g, b).ref();
  213. var asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], function (context, succes, error) {
  214. if (succes) {
  215. callback();
  216. } else {
  217. this._error();
  218. }
  219. });
  220. var re = this.CueSDKLib.CorsairSetLedsColorsAsync(1, l, asyncFunc, ref.NULL);
  221. if (re) {
  222. return asyncFunc;
  223. } else {
  224. this._error();
  225. return asyncFunc;
  226. }
  227. }
  228. }, {
  229. key: 'clear',
  230. value: function clear() {
  231. var l = [];
  232. for (var i = 1; i <= 154; i++) {
  233. l.push([i, 0, 0, 0]);
  234. }
  235. this.set(l, true);
  236. }
  237. }, {
  238. key: 'getLeds',
  239. value: function getLeds() {
  240. var p = this.CueSDKLib.CorsairGetLedPositions().deref();
  241. var l = p['pLedPosition'];
  242. l.length = p['numberOfLeds'];
  243. return l;
  244. }
  245. }, {
  246. key: 'close',
  247. value: function close() {
  248. this.CueSDKLib._dl.close();
  249. this.CueSDKLib = {};
  250. }
  251. }, {
  252. key: '_getLedColor',
  253. value: function _getLedColor(ledId, r, g, b) {
  254. var keyColor = new CorsairLedColor({ ledId: ledId, r: r, g: g, b: b });
  255. return keyColor;
  256. }
  257. }, {
  258. key: '_getLedIdForKeyName',
  259. value: function _getLedIdForKeyName(key) {
  260. return enums.CorsairLedId.get('CLK_' + key).value;
  261. }
  262. }, {
  263. key: '_error',
  264. value: function _error() {
  265. this.lastError = this.CueSDKLib.CorsairGetLastError();
  266. if (this.lastError != 'CE_Success' && this.lastError != 0) {
  267. throw new CueError(this.lastError);
  268. }
  269. }
  270. }]);
  271.  
  272. return CueSDK;
  273. })();
  274.  
  275. module.exports = { CueSDK: CueSDK };
Add Comment
Please, Sign In to add comment