85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
// Logic source (toggle area) 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/>.
|
|
|
|
interface LogicSourceParam extends BaseComponentParam {
|
|
tristate?: boolean;
|
|
state?: boolean | null;
|
|
floating?: string;
|
|
attach?: string;
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
class LogicSource extends BaseComponent {
|
|
private tristate: boolean;
|
|
private pressed: number;
|
|
private floating: WireState;
|
|
private text: NodeList | null = null;
|
|
private q = WireState.float;
|
|
constructor(simulator: Simulator, id: string, param: LogicSourceParam) {
|
|
super(simulator, id, param);
|
|
this.tristate = param.tristate === undefined || param.tristate;
|
|
if (this.tristate) {
|
|
this.pressed = param.state !== undefined ? (param.state === null ? 0 : param.state === false ? 1 : 2) : 0;
|
|
} else {
|
|
this.pressed = param.state === true ? 2 : 1;
|
|
}
|
|
let x = 10;
|
|
let y = 0;
|
|
switch (param.attach) {
|
|
case 'w':
|
|
x = -10;
|
|
y = 0;
|
|
}
|
|
|
|
this.floating = Wire.WireStateFromString(param.floating || 'down');
|
|
this.pins.set('q', new TriState(this, x, y));
|
|
this.update();
|
|
}
|
|
|
|
setup(canvas: SVGElement) {
|
|
super.doSetup('logicsource', canvas);
|
|
if (!this.element) return;
|
|
this.text = this.element.querySelectorAll('.inner');
|
|
this.text[0].textContent = this.pressed === 0 ? '-' : this.pressed === 1 ? '0' : '1';
|
|
this.element.addEventListener('mousedown', this.activate.bind(this));
|
|
}
|
|
|
|
activate(e: MouseEvent) {
|
|
e.preventDefault();
|
|
if (this.tristate) {
|
|
this.pressed = (this.pressed + 1) % 3;
|
|
} else {
|
|
this.pressed = this.pressed === 1 ? 2 : 1;
|
|
}
|
|
//document.text = this.text;
|
|
if (this.text) {
|
|
this.text[0].textContent = this.pressed === 0 ? '-' : this.pressed === 1 ? '0' : '1';
|
|
}
|
|
this.simulator.manualtick();
|
|
}
|
|
|
|
io() {
|
|
this.getPin('q').getAndReset();
|
|
}
|
|
|
|
update(): boolean {
|
|
const oldstate = this.q;
|
|
this.q = this.pressed === 0 ? this.floating : this.pressed === 1 ? WireState.down : WireState.up;
|
|
this.getPin('q').setState(this.q);
|
|
return oldstate !== this.q;
|
|
}
|
|
}
|