Debian Codegraph @

src/activities/digital_electricity/components/BcdCounter.qml

raw
1 /* GCompris - BcdCounter.qml
2 *
3 * SPDX-FileCopyrightText: 2016 Pulkit Gupta <[email protected]>
4 *
5 * Authors:
6 * Bruno Coudoin <[email protected]> (GTK+ version)
7 * Pulkit Gupta <[email protected]> (Qt Quick port)
8 *
9 * SPDX-License-Identifier: GPL-3.0-or-later
10 */
11 import QtQuick
12 import core 1.0
13
14
15 ElectricalComponent {
16 id: bcdTo7Segment
17 terminalSize: 0.125
18 noOfInputs: 1
19 noOfOutputs: 4
20 property var outputTerminalPosY: [0.125, 0.375, 0.625, 0.875]
21 property int count: 0
22 property int previousInput: 0
23
24 information: qsTr("A BCD counter usually takes a signal generator as input. " +
25 "The output is a BCD number starting from 0 which is increased by one at each tick.")
26
27 truthTable: []
28 property var outputTable: [['0','0','0','0'],
29 ['0','0','0','1'],
30 ['0','0','1','0'],
31 ['0','0','1','1'],
32 ['0','1','0','0'],
33 ['0','1','0','1'],
34 ['0','1','1','0'],
35 ['0','1','1','1'],
36 ['1','0','0','0'],
37 ['1','0','0','1']]
38
39 property alias inputTerminals: inputTerminals
40 property alias outputTerminals: outputTerminals
41
42 Repeater {
43 id: inputTerminals
44 model: 1
45 delegate: inputTerminal
46 Component {
47 id: inputTerminal
48 TerminalPoint {
49 posX: 0.07
50 posY: 0.5
51 type: "In"
52 }
53 }
54 }
55
56 Repeater {
57 id: outputTerminals
58 model: 4
59 delegate: outputTerminal
60 Component {
61 id: outputTerminal
62 TerminalPoint {
63 posX: 0.93
64 posY: outputTerminalPosY[index]
65 type: "Out"
66 }
67 }
68 }
69
70 function updateOutput(wireVisited) {
71 for(var i = 0 ; i < noOfOutputs ; ++i) {
72 var terminal = outputTerminals.itemAt(i)
73 terminal.value = outputTable[count][i]
74 for(var j = 0 ; j < terminal.wires.length ; ++j)
75 terminal.wires[j].to.value = terminal.value
76 }
77
78 if(previousInput != inputTerminals.itemAt(0).value) {
79 previousInput = inputTerminals.itemAt(0).value
80 count = (count + 1) % 10;
81
82 var componentVisited = []
83 for(var i = 0 ; i < noOfOutputs ; ++i) {
84 var terminal = outputTerminals.itemAt(i)
85 for(var j = 0 ; j < terminal.wires.length ; ++j) {
86 var wire = terminal.wires[j]
87 var component = wire.to.parent
88 if(componentVisited[component] != true && wireVisited[wire] != true) {
89 componentVisited[component] = true
90 wireVisited[wire] = true
91 component.updateOutput(wireVisited)
92 }
93 }
94 }
95 }
96 }
97 }