logicsimulator/components/jumper3.ts

91 lines
3.0 KiB
TypeScript

// 3-pin jumper component for the LogicSimulator
// Copyright (C) 2022 Sascha Nitsch
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Jumper3 extends BaseComponent {
private pressed = false;
private text: NodeList | null = null;
private l: HTMLElement | null = null;
private r: HTMLElement | null = null;
private p1 = WireState.float;
private p2 = WireState.float;
private p3 = WireState.float;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
if (this.breadboard) {
this.pins.set('p1', new TriState(this, 0, -5.08 * 3));
this.pins.set('p2', new TriState(this, 10.16, 5.08 * 3));
this.pins.set('p3', new TriState(this, 0, -5.08 * 3));
} else {
this.pins.set('p1', new TriState(this, -30, 0));
this.pins.set('p2', new TriState(this, 0, 20));
this.pins.set('p3', new TriState(this, 30, 0));
}
}
setup(canvas: SVGElement) {
super.doSetup('jumper3', canvas);
if (!this.element) return;
this.text = this.element.querySelectorAll('.inner');
this.element.addEventListener('mousedown', this.activate.bind(this));
this.l = <HTMLElement>this.element.querySelectorAll('.l')[0];
this.r = <HTMLElement>this.element.querySelectorAll('.r')[0];
this.updateSwitch();
}
updateSwitch() {
if (!this.r || !this.l) return;
if (this.pressed) {
this.r.classList.add('hidden');
this.l.classList.remove('hidden');
} else {
this.l.classList.add('hidden');
this.r.classList.remove('hidden');
}
}
activate(e: MouseEvent) {
e.preventDefault();
this.pressed = !this.pressed;
if (this.text && this.text.length > 0) {
// document.text = this.text;
if (this.breadboard) {
this.text[1].textContent = this.pressed ? '1' : '0';
} else {
this.text[0].textContent = this.pressed ? '---' : '] [';
}
}
this.updateSwitch();
this.simulator.manualtick();
}
io() {
/*this.state.a = this.a.getAndReset();
this.state.b = this.b.getAndReset();*/
this.p1 = this.getPin('p1').getAndReset();
this.p2 = this.getPin('p2').getAndReset();
this.p3 = this.getPin('p3').getAndReset();
}
update(): boolean {
const oldstate = this.p2;
const state = this.pressed ? this.p1 : this.p3;
this.getPin('p2').setState(state);
return state !== oldstate;
}
}