hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
133 lines (98 loc) • 3.96 kB
JavaScript
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
;
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BuiltInGate = require('../BuiltInGate');
var EventEmitter = require('events');
/**
* Main static emitter used for static methods on `Keyboard`.
*/
var keyboardEmitter = new EventEmitter();
/**
* A keyboard, implemented as a 16 bit register that stores
* the currently pressed key code.
*/
var Keyboard = function (_BuiltInGate) {
_inherits(Keyboard, _BuiltInGate);
function Keyboard(options) {
_classCallCheck(this, Keyboard);
var _this = _possibleConstructorReturn(this, (Keyboard.__proto__ || Object.getPrototypeOf(Keyboard)).call(this, options));
Keyboard.on('key', function (key) {
// Ctrl-c
if (key === '\x03') {
_this._listening = false;
process.exit();
}
_this.getOutputPins()[0].setValue(key.charCodeAt(0));
});
return _this;
}
/**
* Default blocking listener for CLI.
*
* Other clients should call `Keyboard.emit('key', key)`
* in their listeners.
*/
_createClass(Keyboard, [{
key: 'listen',
value: function listen() {
if (this._listening) {
return;
}
var _process = process,
stdin = _process.stdin;
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.on('data', function (key) {
return Keyboard.emit('key', key);
});
stdin.resume();
this._listening = true;
return this;
}
/**
* Facade method for subscription.
*/
}], [{
key: 'emit',
value: function emit(eventName, data) {
keyboardEmitter.emit(eventName, data);
return this;
}
/**
* Facade method for subscription.
*/
}, {
key: 'on',
value: function on(eventName, listener) {
keyboardEmitter.on(eventName, listener);
return this;
}
/**
* Facade method for removing subscription.
*/
}, {
key: 'removeListener',
value: function removeListener(eventName, listener) {
keyboardEmitter.removeListener(eventName, listener);
return this;
}
}]);
return Keyboard;
}(BuiltInGate);
/**
* Specification of the `Keyboard` gate.
*/
Keyboard.Spec = {
name: 'Keyboard',
description: 'A keyboard, implemented as a 16 bit register that stores\nthe currently pressed key code.',
inputPins: [],
outputPins: [{ name: 'out', size: 16 }],
truthTable: []
};
module.exports = Keyboard;