Debian Codegraph @

src/activities/digital_electricity/components/BCDToSevenSegment.qml

raw
1 /* GCompris - BCDToSevenSegment.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 "../digital_electricity.js" as Activity
13
14 import core 1.0
15
16 ElectricalComponent {
17 id: bcdTo7Segment
18 terminalSize: 0.125
19 noOfInputs: 4
20 noOfOutputs: 7
21
22 property var inputTerminalPosY: [0.125, 0.375, 0.625, 0.875]
23 property var outputTerminalPosY: [0.066, 0.211, 0.355, 0.5, 0.645, 0.789, 0.934]
24
25 property var redChar: ["BCDTo7SegmentA_on.svg","BCDTo7SegmentB_on.svg","BCDTo7SegmentC_on.svg",
26 "BCDTo7SegmentD_on.svg","BCDTo7SegmentE_on.svg","BCDTo7SegmentF_on.svg",
27 "BCDTo7SegmentG_on.svg"]
28
29 information: qsTr("A BCD to 7 segment converter takes 4 binary inputs " +
30 "and gives 7 binary outputs which allow to light BCD number segments (binary-coded decimal) " +
31 "to display numbers between 0 and 9. The output for a BCD To 7 Segment converter is:")
32
33 truthTable: [['D','C','B','A','a','b','c','d','e','f','g'],
34 ['0','0','0','0','1','1','1','1','1','1','0'],
35 ['0','0','0','1','0','1','1','0','0','0','0'],
36 ['0','0','1','0','1','1','0','1','1','0','1'],
37 ['0','0','1','1','1','1','1','1','0','0','1'],
38 ['0','1','0','0','0','1','1','0','0','1','1'],
39 ['0','1','0','1','1','0','1','1','0','1','1'],
40 ['0','1','1','0','1','0','1','1','1','1','1'],
41 ['0','1','1','1','1','1','1','0','0','0','0'],
42 ['1','0','0','0','1','1','1','1','1','1','1'],
43 ['1','0','0','1','1','1','1','1','0','1','1']]
44
45 property alias inputTerminals: inputTerminals
46 property alias outputTerminals: outputTerminals
47
48 Repeater {
49 id: inputTerminals
50 model: 4
51 delegate: inputTerminal
52 Component {
53 id: inputTerminal
54 TerminalPoint {
55 posX: 0.05
56 posY: inputTerminalPosY[index]
57 type: "In"
58 }
59 }
60 }
61
62 Repeater {
63 id: outputTerminals
64 model: 7
65 delegate: outputTerminal
66 Component {
67 id: outputTerminal
68 TerminalPoint {
69 posX: 0.95
70 posY: outputTerminalPosY[index]
71 type: "Out"
72 }
73 }
74 }
75
76 function updateOutput(wireVisited) {
77 var i
78 for(i = 1 ; i < truthTable.length ; ++i) {
79 var j
80 for(j = 0 ; j < noOfInputs; ++j) {
81 if(inputTerminals.itemAt(j).value != truthTable[i][j])
82 break
83 }
84 if(j == noOfInputs)
85 break
86 }
87 if(i == truthTable.length) {
88 for(var j = 0 ; j < noOfOutputs ; ++j) {
89 outputTerminals.itemAt(j).value = 0
90 outputChar.itemAt(j).source = "qrc:/gcompris/src/core/resource/empty.svg"
91 }
92 }
93 else {
94 for(var j = 0 ; j < noOfOutputs ; ++j) {
95 var terminal = outputTerminals.itemAt(j)
96 terminal.value = truthTable[i][j + noOfInputs]
97 outputChar.itemAt(j).source = (terminal.value == 0 ? "qrc:/gcompris/src/core/resource/empty.svg" : Activity.url + redChar[j])
98 }
99 }
100
101 for(var i = 0 ; i < noOfOutputs ; ++i) {
102 var terminal = outputTerminals.itemAt(i)
103 for(var j = 0 ; j < terminal.wires.length ; ++j)
104 terminal.wires[j].to.value = terminal.value
105 }
106
107 var componentVisited = []
108 for(var i = 0 ; i < noOfOutputs ; ++i) {
109 var terminal = outputTerminals.itemAt(i)
110 for(var j = 0 ; j < terminal.wires.length ; ++j) {
111 var wire = terminal.wires[j]
112 var component = wire.to.parent
113 if(componentVisited[component] != true && wireVisited[wire] != true) {
114 componentVisited[component] = true
115 wireVisited[wire] = true
116 component.updateOutput(wireVisited)
117 }
118 }
119 }
120 }
121
122 Repeater {
123 id: outputChar
124 model: 7
125 delegate: outputCharImages
126 Component {
127 id: outputCharImages
128 Image {
129 source: ""
130 anchors.centerIn: parent
131 height: parent.height
132 width: parent.width
133 sourceSize.height: height
134 sourceSize.width: width
135 fillMode: Image.PreserveAspectFit
136 antialiasing: true
137 }
138 }
139 }
140 }