81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
// 8x DIL switch Breadboard 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
|
|
function DILSwitchX8Init() {
|
|
class DILSwitchX8 extends DILBase {
|
|
pressed: boolean[];
|
|
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
|
|
super(simulator, id, 16, param);
|
|
this.pressed = [];
|
|
for (let i = 0; i < 8; ++i) {
|
|
this.pressed[i] = false;
|
|
}
|
|
}
|
|
|
|
setup(canvas: SVGElement) {
|
|
super.DILBASEsetup('dilswitchx8', canvas, '');
|
|
if (this.element) {
|
|
this.element.addEventListener('mousedown', this.activate.bind(this));
|
|
const on = this.element.querySelectorAll('.on');
|
|
for (let o = 0; o < on.length; ++o) {
|
|
on[o].setAttribute('style', 'display:none');
|
|
}
|
|
}
|
|
}
|
|
|
|
update(): boolean {
|
|
let mod = false;
|
|
for (let i = 1; i <= 8; ++i) {
|
|
const oldstate = this.getStateWire('p' + i);
|
|
let newstate = WireState.float;
|
|
if (this.pressed[i - 1]) {
|
|
newstate = this.getStateWire('p' + (17 - i));
|
|
}
|
|
this.stateMapWire.set('p' + i, newstate);
|
|
this.getPin('p' + i).setState(newstate);
|
|
mod ||= newstate !== oldstate;
|
|
}
|
|
return mod;
|
|
}
|
|
|
|
activate(e: MouseEvent) {
|
|
const parent = (<SVGGElement>e.target).parentElement;
|
|
if (parent === null || /*parent === this.element ||*/ parent.id === '') {
|
|
return;
|
|
}
|
|
const id = parseInt(parent.id.substr(1)) - 1;
|
|
this.pressed[id] = !this.pressed[id];
|
|
const off = parent.querySelectorAll('.off')[0];
|
|
const on = parent.querySelectorAll('.on')[0];
|
|
if (this.pressed[id]) {
|
|
off.setAttribute('style', 'display:none');
|
|
on.removeAttribute('style');
|
|
} else {
|
|
on.setAttribute('style', 'display:none');
|
|
off.removeAttribute('style');
|
|
}
|
|
this.simulator.manualtick();
|
|
}
|
|
}
|
|
window.DILSwitchX8 = DILSwitchX8;
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
function DILSwitchX8Depends() {
|
|
return 'dilbase';
|
|
}
|