From fdb03030b0c11c32058921b615e6553705afc047 Mon Sep 17 00:00:00 2001 From: Kostya Cholak Date: Mon, 7 Sep 2020 21:19:09 +0300 Subject: [PATCH 1/4] Add FlipFlop component --- app/index.html | 1 + app/js/components.js | 22 ++++++++++++++++++++++ app/js/localStorage2.js | 20 ++++++++++++-------- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/index.html b/app/index.html index 4872408a..8b872483 100755 --- a/app/index.html +++ b/app/index.html @@ -248,6 +248,7 @@

Tutorial

  • TimerStart
  • TimerEnd
  • ROM
  • +
  • Flip Flop
  • diff --git a/app/js/components.js b/app/js/components.js index 87fdd8c9..ef8e266a 100644 --- a/app/js/components.js +++ b/app/js/components.js @@ -2377,6 +2377,28 @@ class Counter extends Component { } } +class FlipFlop extends Component { + constructor(name,pos, properties) { + super(name,pos,2,1,{ type: "value" }); + this.addInputPort({ side: 3, pos: 0 }); + this.value = this.properties.data || 0; + this.ready_to_toggle = this.properties.ready || 1; + } + + function() { + if(this.ready_to_toggle && this.input[0].value == 1) { + this.ready_to_toggle = 0; + this.value = +(!this.value); + this.properties.data = this.value; + } + + if (this.input[0].value == 0) { + this.ready_to_toggle = 1; + } + this.properties.ready = this.ready_to_toggle; + } +} + class LED extends Component { constructor(name,pos,color = [100,0,0]) { super(name,pos,1,1,{ type: "value" }); diff --git a/app/js/localStorage2.js b/app/js/localStorage2.js index d3f4550c..787ba561 100644 --- a/app/js/localStorage2.js +++ b/app/js/localStorage2.js @@ -98,7 +98,7 @@ const constructors = { Button,Constant,Delay,Clock,Debug, Beep,Counter,LED,Display, Custom, TimerStart, TimerEnd, - ROM + ROM, FlipFlop }; /* @@ -248,7 +248,7 @@ function parse(data) { } } - const component = new constructors[constructor](); + const component = new constructors[constructor](data.name, data.pos, data.properties); if(constructor == "Custom") { const parsed = parse(JSON.stringify(data.componentData)); @@ -260,17 +260,21 @@ function parse(data) { const input = data.input; for(let i = 0; i < component.input.length; ++i) { - component.input[i].name = input[i].name; - component.input[i].value = input[i].value; - component.input[i].pos = input[i].pos; + if (input[i]) { + component.input[i].name = input[i].name; + component.input[i].value = input[i].value; + component.input[i].pos = input[i].pos; + } } delete data.input; const output = data.output; for(let i = 0; i < component.output.length; ++i) { - component.output[i].name = output[i].name; - component.output[i].value = output[i].value; - component.output[i].pos = output[i].pos; + if (output[i]) { + component.output[i].name = output[i].name; + component.output[i].value = output[i].value; + component.output[i].pos = output[i].pos; + } } delete data.output; From d1323e43807fa97c092e406b14e513ea801d8735 Mon Sep 17 00:00:00 2001 From: Kostya Cholak Date: Mon, 7 Sep 2020 21:22:51 +0300 Subject: [PATCH 2/4] Add output for flip flop --- app/js/components.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/js/components.js b/app/js/components.js index ef8e266a..53d81953 100644 --- a/app/js/components.js +++ b/app/js/components.js @@ -2381,6 +2381,7 @@ class FlipFlop extends Component { constructor(name,pos, properties) { super(name,pos,2,1,{ type: "value" }); this.addInputPort({ side: 3, pos: 0 }); + this.addOutputPort({ side: 1, pos: 0 }); this.value = this.properties.data || 0; this.ready_to_toggle = this.properties.ready || 1; } @@ -2389,6 +2390,7 @@ class FlipFlop extends Component { if(this.ready_to_toggle && this.input[0].value == 1) { this.ready_to_toggle = 0; this.value = +(!this.value); + this.output[0].value = this.value; this.properties.data = this.value; } From d84223ca0ab21eb69bac04b31406b17399b2eafc Mon Sep 17 00:00:00 2001 From: Kostya Cholak Date: Mon, 7 Sep 2020 21:32:43 +0300 Subject: [PATCH 3/4] Add manual toggle for flip flop --- app/js/components.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/js/components.js b/app/js/components.js index 53d81953..d6292692 100644 --- a/app/js/components.js +++ b/app/js/components.js @@ -2386,10 +2386,25 @@ class FlipFlop extends Component { this.ready_to_toggle = this.properties.ready || 1; } + onmousedown(sendToSocket = true) { + this.value = 1 - this.value; + this.ready_to_toggle = this.input[0].value == 0; + this.properties.data = this.value; + this.properties.ready = this.ready_to_toggle; + this.update(true); + + if(socket && sendToSocket) { + socket.send(JSON.stringify({ + type: "mousedown", + data: this.id + })); + } + } + function() { if(this.ready_to_toggle && this.input[0].value == 1) { this.ready_to_toggle = 0; - this.value = +(!this.value); + this.value = 1 - this.value; this.output[0].value = this.value; this.properties.data = this.value; } From 87512e7cbe8088936e4dadebc5252bd4a87b1684 Mon Sep 17 00:00:00 2001 From: Kostya Cholak Date: Mon, 7 Sep 2020 21:34:35 +0300 Subject: [PATCH 4/4] Set output equal to value on mouse click --- app/js/components.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/js/components.js b/app/js/components.js index d6292692..41c3cb42 100644 --- a/app/js/components.js +++ b/app/js/components.js @@ -2391,6 +2391,7 @@ class FlipFlop extends Component { this.ready_to_toggle = this.input[0].value == 0; this.properties.data = this.value; this.properties.ready = this.ready_to_toggle; + this.output[0].value = this.value; this.update(true); if(socket && sendToSocket) {