starting on oled printer

This commit is contained in:
Rudis Muiznieks 2022-04-22 13:53:26 -05:00
parent b5269f03de
commit a095dbad47
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
5 changed files with 58 additions and 19 deletions

4
src/chess/chess.ts Normal file
View File

@ -0,0 +1,4 @@
export class Chess {
public async runAsync(): Promise<void> {
}
}

View File

@ -2,10 +2,12 @@ import { Menu } from './menu/menu';
import { MenuCommand, MenuType } from './menu/interface';
import { Display } from './display/display';
import { Chess } from './chess/chess';
import { exec, spawn } from 'child_process';
class Main {
private readonly _menu = [
private readonly _menuConfig = [
{
display: "df -h",
type: MenuType.ExecCommand,
@ -17,8 +19,8 @@ class Main {
subMenu: [
{
display: "Chess",
type: MenuType.ExecCommand,
command: {exe: "df", args: ['-h']},
type: MenuType.ExecModule,
module: new Chess().runAsync,
},
],
},
@ -32,10 +34,12 @@ class Main {
},
];
private readonly _menu: Menu;
private readonly _display: Display;
private readonly _console: boolean = process.env['CONSOLE'] === '1';
constructor() {
this._menu = new Menu(this._menuConfig, this._console);
this._display = new Display(this._console);
}
@ -57,7 +61,7 @@ class Main {
public async runAsync(): Promise<void> {
while (true) {
const selected = await new Menu(this._menu, this._console).getSelection();
const selected = await this._menu.getSelection();
switch(selected.type) {
case MenuType.Shutdown:
this._display.clear(true);
@ -72,6 +76,11 @@ class Main {
await this.runCommand(selected.command);
}
break;
case MenuType.ExecModule:
if (selected.module) {
await selected.module();
}
break;
}
}
}

View File

@ -7,9 +7,11 @@ export class ConsolePrinter implements MenuPrinter {
private _path: number[] = [];
private _selectedIndex: number = 0;
private _config: MenuConfig[];
private _curMenu: MenuConfig[];
constructor(config: MenuConfig[]) {
this._config = config;
this._curMenu = config;
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
}
@ -24,22 +26,21 @@ export class ConsolePrinter implements MenuPrinter {
});
}
private printMenu(curMenu: MenuConfig[]): void {
private printMenu(): void {
console.clear();
for (let i = 0; i < curMenu.length; i++) {
for (let i = 0; i < this._curMenu.length; i++) {
const line = (i === this._selectedIndex
? "> "
: " ") + curMenu[i].display;
: " ") + this._curMenu[i].display;
console.log(line);
}
}
public async getSelection(): Promise<MenuConfig> {
return new Promise(async (resolve) => {
let curMenu = this._config;
while (true) {
// print menu
this.printMenu(curMenu);
this.printMenu();
// get user input
const key = await this.keypress();
@ -53,8 +54,8 @@ export class ConsolePrinter implements MenuPrinter {
}
} 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') {
if (this._path.length > 0) {
@ -63,14 +64,15 @@ export class ConsolePrinter implements MenuPrinter {
}
} else if (key.name === 'right' || key.name === 'return') {
this._path.push(this._selectedIndex);
this._selectedIndex = 0;
}
curMenu = this._config;
this._curMenu = this._config;
for (let i = 0; i < this._path.length; i++) {
if (curMenu[this._path[i]].type === MenuType.SubMenu) {
curMenu = curMenu[this._path[i]].subMenu!;
if (this._curMenu[this._path[i]].type === 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;
}
}

View File

@ -1,5 +1,6 @@
export enum MenuType {
ExecCommand,
ExecModule,
Reboot,
Shutdown,
SubMenu,
@ -16,6 +17,7 @@ export interface MenuConfig {
type: MenuType,
command?: MenuCommand,
subMenu?: MenuConfig[],
module?: () => Promise<void>,
}
export interface MenuPrinter {

View File

@ -1,6 +1,6 @@
/*import { openSync } from 'i2c-bus';
import { openSync } from 'i2c-bus';
import Oled from 'oled-i2c-bus';
import font from 'oled-font-5x7';*/
import font from 'oled-font-5x7';
import { MenuConfig, MenuPrinter } from './interface';
/*const i2cbus = openSync(1);
@ -18,13 +18,35 @@ oled.writeString(font, 1, "This is a string I am writing to the screen", 1, true
oled.turnOffDisplay();*/
export class OledPrinter implements MenuPrinter {
// private _path: number[] = [];
// private _selectedIndex: number = 0;
private _config: MenuConfig[];
// private _curMenu: MenuConfig[];
private _oled: Oled;
constructor(config: MenuConfig[]) {
this._config = config;
// this._curMenu = config;
const i2cbus = openSync(1);
const opts = {
width: 128,
height: 64,
address: 0x3C,
};
this._oled = new Oled(i2cbus, opts);
this._oled.clearDisplay();
}
private printMenu(): void {
this._oled.writeString(font, 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);
}
public async getSelection(): Promise<MenuConfig> {
return this._config[0];
return new Promise((resolve) => {
this.printMenu();
while (true) {
}
resolve(this._config[0]);
});
}
}