logicsimulator/components/pull.ts

76 lines
2.8 KiB
TypeScript

// Pull-Up or Pull-Down 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 Pull extends BaseComponent {
private a = WireState.float;
private b = WireState.float;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
const r = (this.rotate / 180) * Math.PI;
this.pins.set('a', new TriState(this, 0, 0, undefined, { id: id + '-a' }));
this.pins.set(
'b',
new TriState(this, Math.sin(r) * -40 * this.scaleX, Math.cos(r) * 40 * this.scaleY, undefined, {
type: 'Pull',
id: id + '-b',
})
);
}
setup(canvas: SVGElement) {
super.doSetup('pull', canvas);
if (this.breadboard) {
this.simulator.wire(null, [this.getPin('a'), this.breadboard.getPin(this.pin)]);
}
}
io() {
this.a = this.getPin('a').getAndReset();
this.b = this.getPin('b').getAndReset();
}
update() {
let mod = false;
let s: WireState;
if (this.b === WireState.high || this.b === WireState.low) {
// up, down driven by b
s = this.b === WireState.high ? WireState.up : WireState.down;
mod ||= s !== this.a;
this.getPin('a').setState(s);
this.getPin('b').setState(WireState.float);
} else if (this.a === WireState.high || this.a === WireState.low) {
// up, down driven by a
s = this.a === WireState.high ? WireState.up : this.a === WireState.low ? WireState.down : this.a;
this.getPin('b').setState(s);
mod ||= s !== this.b;
this.getPin('a').setState(WireState.float);
} else if (this.b !== WireState.float) {
// b is pushed up or pulled down
s = this.b === WireState.up ? WireState.up : WireState.down;
mod ||= s !== this.a;
this.getPin('a').setState(s);
} else if (this.a !== WireState.float) {
// s is pushed up or pulled down
s = this.a === WireState.up ? WireState.up : WireState.down;
mod ||= s !== <WireState>this.b;
this.getPin('b').setState(s);
}
//console.log(this.id,"a:",this.state.a, "b:",this.state.b, "new a:", this.a.get(), "new b:", this.b.get());
return mod;
}
}