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

0001 /* GCompris - Bulb.qml
0002  *
0003  * SPDX-FileCopyrightText: 2020 Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 import "../analog_electricity.js" as Activity
0014 
0015 ElectricalComponent {
0016     id: redLed //to form an LED, connecting diode, resistor of 19 ohm and voltage source of 1.84v in series
0017     terminalSize: 0.2
0018     noOfConnectionPoints: 2
0019     information: qsTr("Red LED converts electrical energy into red light energy. It can glow only if the current flow is in the direction of the arrow. Electrical energy more than a certain limit can break it.")
0020     source: Activity.url + "red_led_off.svg"
0021 
0022     property var nodeVoltages: [0, 0]
0023     property double componentVoltage: 0
0024     property double power: 0
0025     property double powerThreshold: 0.01 //in W
0026     property double powerMax: 0.08
0027     property alias connectionPoints: connectionPoints
0028     property bool isBroken: false
0029     property var connectionPointPosX: [0.1, 0.9]
0030     property string componentName: "RedLed"
0031     property var internalNetlistIndex: [0, 0]
0032     property var externalNetlistIndex: [0, 0]
0033     property var netlistModel:
0034     [
0035         "d",
0036         [
0037         ],
0038         {
0039             "name": componentName,
0040             "area": "1",
0041             "_json_": 0
0042         },
0043         [
0044             0,
0045             0
0046         ]
0047     ]
0048 
0049     Item {
0050         id: resistor
0051         property int jsonNumber: 0
0052         property double current: 0
0053         property var netlistModel:
0054         [
0055             "r",
0056             [
0057             ],
0058             {
0059                 "name": "resistor-",
0060                 "r": "19",
0061                 "_json_": 0
0062             },
0063             [
0064                 0,
0065                 0
0066             ]
0067         ]
0068     }
0069 
0070     Item {
0071         id: vSource //Forward bias voltage of Red LED is 1.84v
0072         property int jsonNumber: 0
0073         property double current: 0
0074         property var netlistModel:
0075         [
0076             "v",
0077             [
0078             ],
0079             {
0080                 "name": "vSource-",
0081                 "value": "dc(1.84)",
0082                 "_json_": 0
0083             },
0084             [
0085                 0,
0086                 0
0087             ]
0088         ]
0089     }
0090 
0091     Repeater {
0092         id: connectionPoints
0093         model: 2
0094         delegate: connectionPoint
0095         Component {
0096             id: connectionPoint
0097             TerminalPoint {
0098                 posX: connectionPointPosX[index]
0099                 posY: 0.85
0100             }
0101         }
0102     }
0103 
0104     Image {
0105         id: ledLight
0106         source: Activity.url + "red_led_on.svg"
0107         anchors.fill: parent
0108         sourceSize.width: width
0109         sourceSize.height: height
0110         fillMode: Image.PreserveAspectFit
0111         opacity: 0
0112     }
0113 
0114     Image {
0115         id: ledArrow
0116         source: isBroken ? Activity.url + "red_led_arrowBroken.svg" : Activity.url + "red_led_arrow.svg"
0117         anchors.fill: parent
0118         sourceSize.width: width
0119         sourceSize.height: height
0120         fillMode: Image.PreserveAspectFit
0121         opacity: 1
0122     }
0123 
0124     function repairComponent() {
0125         redLed.source = Activity.url + "red_led_off.svg";
0126         isBroken = false;
0127     }
0128 
0129     function checkConnections() {
0130         terminalConnected = 0;
0131         for(var i = 0; i < noOfConnectionPoints; i++) {
0132             if(connectionPoints.itemAt(i).wires.length > 0)
0133                 terminalConnected += 1;
0134         }
0135     }
0136 
0137     function updateValues() {
0138         power = 1.84 * (vSource.current);
0139         if(power >= powerThreshold && power < powerMax) {
0140             ledLight.opacity = 1;
0141         } else if(power < powerThreshold) {
0142             ledLight.opacity = 0;
0143         } else if(power >= powerMax) {
0144             ledLight.opacity = 0;
0145             redLed.source = Activity.url + "red_led_broken.svg";
0146             isBroken = true;
0147             Activity.restartTimer();
0148         }
0149     }
0150 
0151     function initConnections() {
0152         var connectionIndex = Activity.connectionCount;
0153         redLed.externalNetlistIndex[0] = ++connectionIndex;
0154         connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
0155         redLed.internalNetlistIndex[0] = ++connectionIndex;
0156         redLed.internalNetlistIndex[1] = ++connectionIndex;
0157         redLed.externalNetlistIndex[1] = ++connectionIndex;
0158         connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
0159         Activity.connectionCount = connectionIndex;
0160     }
0161 
0162     function addToNetlist() {
0163         if(!isBroken) {
0164             var netlistItem = redLed.netlistModel;
0165             Activity.netlistComponents.push(redLed);
0166             netlistItem[2].name = componentName;
0167             netlistItem[2]._json = Activity.netlist.length;
0168             netlistItem[3][0] = redLed.externalNetlistIndex[0];
0169             netlistItem[3][1] = redLed.internalNetlistIndex[0];
0170             Activity.netlist.push(netlistItem);
0171 
0172             netlistItem = resistor.netlistModel;
0173             Activity.netlistComponents.push(resistor);
0174             netlistItem[2].name = "resistor-" + componentName;
0175             netlistItem[2]._json = Activity.netlist.length;
0176             netlistItem[3][0] = redLed.internalNetlistIndex[0];
0177             netlistItem[3][1] = redLed.internalNetlistIndex[1];
0178             Activity.netlist.push(netlistItem);
0179 
0180             netlistItem = vSource.netlistModel;
0181             Activity.netlistComponents.push(vSource);
0182             Activity.vSourcesList.push(vSource);
0183             netlistItem[2].name = "vSource-" + componentName;
0184             netlistItem[2]._json = Activity.netlist.length;
0185             netlistItem[3][0] = redLed.internalNetlistIndex[1]; //positive terminal to the resistor
0186             netlistItem[3][1] = redLed.externalNetlistIndex[1];
0187             Activity.netlist.push(netlistItem);
0188         }
0189     }
0190 
0191     function checkComponentAnswer() {
0192         if(ledLight.opacity === 1) {
0193             return "redLedGlows";
0194         } else if(terminalConnected >= 2 && isBroken) {
0195             return "redLedBroken";
0196         } else if(terminalConnected >= 2)
0197             return "redLedIn";
0198         else
0199             return "";
0200     }
0201 }