Warning, /education/gcompris/src/activities/digital_electricity/components/BCDToSevenSegment.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - BCDToSevenSegment.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 Pulkit Gupta <pulkitnsit@gmail.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Pulkit Gupta <pulkitnsit@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import "../digital_electricity.js" as Activity
0013 
0014 import GCompris 1.0
0015 
0016 ElectricalComponent {
0017     id: bcdTo7Segment
0018     terminalSize: 0.125
0019     noOfInputs: 4
0020     noOfOutputs: 7
0021 
0022     property var inputTerminalPosY: [0.125, 0.375, 0.625, 0.875]
0023     property var outputTerminalPosY: [0.066, 0.211, 0.355, 0.5, 0.645, 0.789, 0.934]
0024 
0025     property var redChar: ["BCDTo7SegmentA_on.svg","BCDTo7SegmentB_on.svg","BCDTo7SegmentC_on.svg",
0026                                "BCDTo7SegmentD_on.svg","BCDTo7SegmentE_on.svg","BCDTo7SegmentF_on.svg",
0027                                "BCDTo7SegmentG_on.svg"]
0028 
0029     information: qsTr("A BCD to 7 segment converter takes 4 binary inputs " +
0030                       "and gives 7 binary outputs which allow to light BCD number segments (binary-coded decimal) " +
0031                       "to display numbers between 0 and 9. The output for a BCD To 7 Segment converter is:")
0032 
0033     truthTable: [['D','C','B','A','a','b','c','d','e','f','g'],
0034                  ['0','0','0','0','1','1','1','1','1','1','0'],
0035                  ['0','0','0','1','0','1','1','0','0','0','0'],
0036                  ['0','0','1','0','1','1','0','1','1','0','1'],
0037                  ['0','0','1','1','1','1','1','1','0','0','1'],
0038                  ['0','1','0','0','0','1','1','0','0','1','1'],
0039                  ['0','1','0','1','1','0','1','1','0','1','1'],
0040                  ['0','1','1','0','1','0','1','1','1','1','1'],
0041                  ['0','1','1','1','1','1','1','0','0','0','0'],
0042                  ['1','0','0','0','1','1','1','1','1','1','1'],
0043                  ['1','0','0','1','1','1','1','1','0','1','1']]
0044 
0045     property alias inputTerminals: inputTerminals
0046     property alias outputTerminals: outputTerminals
0047 
0048     Repeater {
0049         id: inputTerminals
0050         model: 4
0051         delegate: inputTerminal
0052         Component {
0053             id: inputTerminal
0054             TerminalPoint {
0055                 posX: 0.05
0056                 posY: inputTerminalPosY[index]
0057                 type: "In"
0058             }
0059         }
0060     }
0061 
0062     Repeater {
0063         id: outputTerminals
0064         model: 7
0065         delegate: outputTerminal
0066         Component {
0067             id: outputTerminal
0068             TerminalPoint {
0069                 posX: 0.95
0070                 posY: outputTerminalPosY[index]
0071                 type: "Out"
0072             }
0073         }
0074     }
0075 
0076     function updateOutput(wireVisited) {
0077         var i
0078         for(i = 1 ; i < truthTable.length ; ++i) {
0079             var j
0080             for(j = 0 ; j < noOfInputs; ++j) {
0081                 if(inputTerminals.itemAt(j).value != truthTable[i][j])
0082                     break
0083             }
0084             if(j == noOfInputs)
0085                 break
0086         }
0087         if(i == truthTable.length) {
0088             for(var j = 0 ; j < noOfOutputs ; ++j) {
0089                 outputTerminals.itemAt(j).value = 0
0090                 outputChar.itemAt(j).source = "qrc:/gcompris/src/core/resource/empty.svg"
0091             }
0092         }
0093         else {
0094             for(var j = 0 ; j < noOfOutputs ; ++j) {
0095                 var terminal = outputTerminals.itemAt(j)
0096                 terminal.value = truthTable[i][j + noOfInputs]
0097                 outputChar.itemAt(j).source = (terminal.value == 0 ? "qrc:/gcompris/src/core/resource/empty.svg" : Activity.url + redChar[j])
0098             }
0099         }
0100 
0101         for(var i = 0 ; i < noOfOutputs ; ++i) {
0102             var terminal = outputTerminals.itemAt(i)
0103             for(var j = 0 ; j < terminal.wires.length ; ++j)
0104                 terminal.wires[j].to.value = terminal.value
0105         }
0106 
0107         var componentVisited = []
0108         for(var i = 0 ; i < noOfOutputs ; ++i) {
0109             var terminal = outputTerminals.itemAt(i)
0110             for(var j = 0 ; j < terminal.wires.length ; ++j) {
0111                 var wire = terminal.wires[j]
0112                 var component = wire.to.parent
0113                 if(componentVisited[component] != true && wireVisited[wire] != true) {
0114                     componentVisited[component] = true
0115                     wireVisited[wire] = true
0116                     component.updateOutput(wireVisited)
0117                 }
0118             }
0119         }
0120     }
0121 
0122     Repeater {
0123         id: outputChar
0124         model: 7
0125         delegate: outputCharImages
0126         Component {
0127             id: outputCharImages
0128             Image {
0129                 source: ""
0130                 anchors.centerIn: parent
0131                 height: parent.height
0132                 width: parent.width
0133                 fillMode: Image.PreserveAspectFit
0134                 mipmap: true
0135                 antialiasing: true
0136             }
0137         }
0138     }
0139 }