This commit is contained in:
Rudis Muiznieks 2022-04-22 14:00:05 -05:00
parent a095dbad47
commit 8f9dd3e6d6
Signed by: rudism
GPG key ID: CABF2F86EF7884F9
9 changed files with 363 additions and 42 deletions

52
bin/chess/chess.js Normal file
View file

@ -0,0 +1,52 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.Chess = void 0;
var Chess = /** @class */ (function () {
function Chess() {
}
Chess.prototype.runAsync = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
return Chess;
}());
exports.Chess = Chess;

63
bin/display/console.js Normal file
View file

@ -0,0 +1,63 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.ConsolePrinter = void 0;
var ConsolePrinter = /** @class */ (function () {
function ConsolePrinter() {
}
ConsolePrinter.prototype.displayContent = function (content) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
console.log(content);
console.log("Press a key...");
return [2 /*return*/, new Promise(function (resolve) {
var listener = function (_letter, _key) {
process.stdin.removeListener('keypress', listener);
resolve();
};
process.stdin.on('keypress', listener);
})];
});
});
};
ConsolePrinter.prototype.clear = function (_) {
console.clear();
};
return ConsolePrinter;
}());
exports.ConsolePrinter = ConsolePrinter;

69
bin/display/display.js Normal file
View file

@ -0,0 +1,69 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.Display = void 0;
var console_1 = require("./console");
var oled_1 = require("./oled");
var Display = /** @class */ (function () {
function Display(useConsole) {
this._display = useConsole
? new console_1.ConsolePrinter()
: new oled_1.OledPrinter();
}
Display.prototype.displayContent = function (content, wrap) {
if (wrap === void 0) { wrap = true; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this._display.clear(false);
return [4 /*yield*/, this._display.displayContent(content, wrap)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
Display.prototype.clear = function (turnOff) {
if (turnOff === void 0) { turnOff = false; }
this._display.clear(turnOff);
};
return Display;
}());
exports.Display = Display;

2
bin/display/interface.js Normal file
View file

@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;

54
bin/display/oled.js Normal file
View file

@ -0,0 +1,54 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.OledPrinter = void 0;
var OledPrinter = /** @class */ (function () {
function OledPrinter() {
}
OledPrinter.prototype.displayContent = function (_content, _wrap) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
OledPrinter.prototype.clear = function (_turnOff) {
};
return OledPrinter;
}());
exports.OledPrinter = OledPrinter;

View file

@ -38,13 +38,16 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
exports.__esModule = true;
var menu_1 = require("./menu/menu");
var interface_1 = require("./menu/interface");
var display_1 = require("./display/display");
var chess_1 = require("./chess/chess");
var child_process_1 = require("child_process");
var Main = /** @class */ (function () {
function Main() {
this._menu = [
this._menuConfig = [
{
display: "df -h",
type: interface_1.MenuType.ExecCommand,
command: ["df", "-h"]
command: { exe: "df", args: ['-h'] }
},
{
display: "Games",
@ -52,8 +55,8 @@ var Main = /** @class */ (function () {
subMenu: [
{
display: "Chess",
type: interface_1.MenuType.ExecCommand,
command: ["df", "-h"]
type: interface_1.MenuType.ExecModule,
module: new chess_1.Chess().runAsync
},
]
},
@ -66,27 +69,81 @@ var Main = /** @class */ (function () {
type: interface_1.MenuType.Shutdown
},
];
this._console = process.env['CONSOLE'] === '1';
this._menu = new menu_1.Menu(this._menuConfig, this._console);
this._display = new display_1.Display(this._console);
}
Main.prototype.runCommand = function (cmd) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
var _a;
_this._display.displayContent("Executing command:\n> ".concat(cmd.exe, " ").concat((_a = cmd.args) === null || _a === void 0 ? void 0 : _a.join(' ')));
var output = '';
var child = (0, child_process_1.spawn)(cmd.exe, cmd.args);
child.stdout.on('data', function (data) {
output += data;
});
child.on('exit', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._display.displayContent(output, cmd.wrap)];
case 1:
_a.sent();
resolve();
return [2 /*return*/];
}
});
}); });
})];
});
});
};
Main.prototype.runAsync = function () {
return __awaiter(this, void 0, void 0, function () {
var selected;
return __generator(this, function (_a) {
switch (_a.label) {
var selected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!true) return [3 /*break*/, 2];
return [4 /*yield*/, new menu_1.Menu(this._menu, process.env['CONSOLE'] === '1').getSelection()];
if (!true) return [3 /*break*/, 11];
return [4 /*yield*/, this._menu.getSelection()];
case 1:
selected = _a.sent();
switch (selected.type) {
case interface_1.MenuType.Shutdown:
process.exit();
case interface_1.MenuType.Reboot:
process.exit();
case interface_1.MenuType.ExecCommand:
break;
selected = _b.sent();
_a = selected.type;
switch (_a) {
case interface_1.MenuType.Shutdown: return [3 /*break*/, 2];
case interface_1.MenuType.Reboot: return [3 /*break*/, 3];
case interface_1.MenuType.ExecCommand: return [3 /*break*/, 4];
case interface_1.MenuType.ExecModule: return [3 /*break*/, 7];
}
return [3 /*break*/, 0];
case 2: return [2 /*return*/];
return [3 /*break*/, 10];
case 2:
this._display.clear(true);
(0, child_process_1.exec)('sudo shutdown now');
process.exit();
_b.label = 3;
case 3:
this._display.clear();
(0, child_process_1.exec)('sudo reboot');
process.exit();
_b.label = 4;
case 4:
if (!selected.command) return [3 /*break*/, 6];
return [4 /*yield*/, this.runCommand(selected.command)];
case 5:
_b.sent();
_b.label = 6;
case 6: return [3 /*break*/, 10];
case 7:
if (!selected.module) return [3 /*break*/, 9];
return [4 /*yield*/, selected.module()];
case 8:
_b.sent();
_b.label = 9;
case 9: return [3 /*break*/, 10];
case 10: return [3 /*break*/, 0];
case 11: return [2 /*return*/];
}
});
});

View file

@ -67,6 +67,7 @@ var ConsolePrinter = /** @class */ (function () {
this._path = [];
this._selectedIndex = 0;
this._config = config;
this._curMenu = config;
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
}
@ -83,12 +84,12 @@ var ConsolePrinter = /** @class */ (function () {
});
});
};
ConsolePrinter.prototype.printMenu = function (curMenu) {
ConsolePrinter.prototype.printMenu = function () {
console.clear();
for (var i = 0; i < curMenu.length; i++) {
for (var i = 0; i < this._curMenu.length; i++) {
var line = (i === this._selectedIndex
? "> "
: " ") + curMenu[i].display;
: " ") + this._curMenu[i].display;
console.log(line);
}
};
@ -97,18 +98,15 @@ var ConsolePrinter = /** @class */ (function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
var curMenu, key, i;
var key, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
curMenu = this._config;
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 3];
if (!true) return [3 /*break*/, 2];
// print menu
this.printMenu(curMenu);
this.printMenu();
return [4 /*yield*/, this.keypress()];
case 2:
case 1:
key = _a.sent();
if (key.ctrl && key.name === 'c') {
process.exit();
@ -121,8 +119,8 @@ var ConsolePrinter = /** @class */ (function () {
}
else if (key.name === 'down') {
this._selectedIndex++;
if (this._selectedIndex > curMenu.length - 1) {
this._selectedIndex = curMenu.length - 1;
if (this._selectedIndex > this._curMenu.length - 1) {
this._selectedIndex = this._curMenu.length - 1;
}
}
else if (key.name === 'left') {
@ -133,20 +131,21 @@ var ConsolePrinter = /** @class */ (function () {
}
else if (key.name === 'right' || key.name === 'return') {
this._path.push(this._selectedIndex);
this._selectedIndex = 0;
}
curMenu = this._config;
this._curMenu = this._config;
for (i = 0; i < this._path.length; i++) {
if (curMenu[this._path[i]].type === interface_1.MenuType.SubMenu) {
curMenu = curMenu[this._path[i]].subMenu;
if (this._curMenu[this._path[i]].type === interface_1.MenuType.SubMenu) {
this._curMenu = this._curMenu[this._path[i]].subMenu;
this._selectedIndex = 0;
}
else {
resolve(curMenu[this._path[i]]);
resolve(this._curMenu[this._path[i]]);
this._path.pop();
return [2 /*return*/];
}
}
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
return [3 /*break*/, 0];
case 2: return [2 /*return*/];
}
});
}); })];

View file

@ -4,7 +4,8 @@ exports.MenuType = void 0;
var MenuType;
(function (MenuType) {
MenuType[MenuType["ExecCommand"] = 0] = "ExecCommand";
MenuType[MenuType["Reboot"] = 1] = "Reboot";
MenuType[MenuType["Shutdown"] = 2] = "Shutdown";
MenuType[MenuType["SubMenu"] = 3] = "SubMenu";
MenuType[MenuType["ExecModule"] = 1] = "ExecModule";
MenuType[MenuType["Reboot"] = 2] = "Reboot";
MenuType[MenuType["Shutdown"] = 3] = "Shutdown";
MenuType[MenuType["SubMenu"] = 4] = "SubMenu";
})(MenuType = exports.MenuType || (exports.MenuType = {}));

View file

@ -35,8 +35,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.OledPrinter = void 0;
var i2c_bus_1 = require("i2c-bus");
var oled_i2c_bus_1 = __importDefault(require("oled-i2c-bus"));
var oled_font_5x7_1 = __importDefault(require("oled-font-5x7"));
/*const i2cbus = openSync(1);
const opts = {
width: 128,
@ -53,11 +59,29 @@ oled.turnOffDisplay();*/
var OledPrinter = /** @class */ (function () {
function OledPrinter(config) {
this._config = config;
// this._curMenu = config;
var i2cbus = (0, i2c_bus_1.openSync)(1);
var opts = {
width: 128,
height: 64,
address: 0x3C
};
this._oled = new oled_i2c_bus_1["default"](i2cbus, opts);
this._oled.clearDisplay();
}
OledPrinter.prototype.printMenu = function () {
this._oled.writeString(oled_font_5x7_1["default"], 1, "how many lines does this display hold let's write a whole bunch of stuff with wrapping on and then count the number of lines", 1, true);
};
OledPrinter.prototype.getSelection = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this._config[0]];
return [2 /*return*/, new Promise(function (resolve) {
_this.printMenu();
while (true) {
}
resolve(_this._config[0]);
})];
});
});
};