logicsimulator/components/ledbar8.ts

64 lines
2.2 KiB
TypeScript

// 8x LED Bar display 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 LEDBar8Param extends BaseComponentParam {
color: string;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function LEDBar8Init() {
class LEDBar8 extends DILBase {
private color: string;
constructor(simulator: Simulator, id: string, param: LEDBar8Param) {
super(simulator, id, 16, param);
this.color = param.color;
for (let i = 0; i < 8; ++i) {
this.getPin('p' + (i + 1)).move(i * 5.08, 12.4);
this.stateMapBool.set('l' + i, false);
}
}
setup(canvas: SVGElement) {
super.DILBASEsetup('ledbar8', canvas, '');
if (this.element) {
this.element.classList.add(this.color);
}
}
update(): boolean {
for (let i = 0; i < 8; ++i) {
const a = this.getStateWire('p' + (i + 1));
const b = this.getStateWire('p' + (16 - i));
const state = this.tri(a) !== this.tri(b) && a !== WireState.float && b !== WireState.float;
if (state !== this.getStateBool('l' + i) && this.element) {
if (state) {
this.element.querySelectorAll('#l' + i)[0].classList.add('on');
} else {
this.element.querySelectorAll('#l' + i)[0].classList.remove('on');
}
}
this.stateMapBool.set('l' + i, state);
}
return false;
}
}
window.LEDBar8 = LEDBar8;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function LEDBar8Depends() {
return 'dilbase';
}