| 1 |
/* GCompris - Bulb.qml |
| 2 |
* |
| 3 |
* SPDX-FileCopyrightText: 2020 Aiswarya Kaitheri Kandoth <[email protected]> |
| 4 |
* |
| 5 |
* Authors: |
| 6 |
* Bruno Coudoin <[email protected]> (GTK+ version) |
| 7 |
* Aiswarya Kaitheri Kandoth <[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 |
import "../analog_electricity.js" as Activity |
| 14 |
|
| 15 |
ElectricalComponent { |
| 16 |
id: redLed //to form an LED, connecting diode, resistor of 19 ohm and voltage source of 1.84v in series |
| 17 |
terminalSize: 0.2 |
| 18 |
noOfConnectionPoints: 2 |
| 19 |
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.") |
| 20 |
source: Activity.url + "red_led_off.svg" |
| 21 |
|
| 22 |
property var nodeVoltages: [0, 0] |
| 23 |
property double componentVoltage: 0 |
| 24 |
property double power: 0 |
| 25 |
property double powerThreshold: 0.01 //in W |
| 26 |
property double powerMax: 0.08 |
| 27 |
property alias connectionPoints: connectionPoints |
| 28 |
property bool isBroken: false |
| 29 |
property var connectionPointPosX: [0.1, 0.9] |
| 30 |
property string componentName: "RedLed" |
| 31 |
property var internalNetlistIndex: [0, 0] |
| 32 |
property var externalNetlistIndex: [0, 0] |
| 33 |
property var netlistModel: |
| 34 |
[ |
| 35 |
"d", |
| 36 |
[ |
| 37 |
], |
| 38 |
{ |
| 39 |
"name": componentName, |
| 40 |
"area": "1", |
| 41 |
"_json_": 0 |
| 42 |
}, |
| 43 |
[ |
| 44 |
0, |
| 45 |
0 |
| 46 |
] |
| 47 |
] |
| 48 |
|
| 49 |
Item { |
| 50 |
id: resistor |
| 51 |
property int jsonNumber: 0 |
| 52 |
property double current: 0 |
| 53 |
property var netlistModel: |
| 54 |
[ |
| 55 |
"r", |
| 56 |
[ |
| 57 |
], |
| 58 |
{ |
| 59 |
"name": "resistor-", |
| 60 |
"r": "19", |
| 61 |
"_json_": 0 |
| 62 |
}, |
| 63 |
[ |
| 64 |
0, |
| 65 |
0 |
| 66 |
] |
| 67 |
] |
| 68 |
} |
| 69 |
|
| 70 |
Item { |
| 71 |
id: vSource //Forward bias voltage of Red LED is 1.84v |
| 72 |
property int jsonNumber: 0 |
| 73 |
property double current: 0 |
| 74 |
property var netlistModel: |
| 75 |
[ |
| 76 |
"v", |
| 77 |
[ |
| 78 |
], |
| 79 |
{ |
| 80 |
"name": "vSource-", |
| 81 |
"value": "dc(1.84)", |
| 82 |
"_json_": 0 |
| 83 |
}, |
| 84 |
[ |
| 85 |
0, |
| 86 |
0 |
| 87 |
] |
| 88 |
] |
| 89 |
} |
| 90 |
|
| 91 |
Repeater { |
| 92 |
id: connectionPoints |
| 93 |
model: 2 |
| 94 |
delegate: connectionPoint |
| 95 |
Component { |
| 96 |
id: connectionPoint |
| 97 |
TerminalPoint { |
| 98 |
posX: connectionPointPosX[index] |
| 99 |
posY: 0.85 |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
Image { |
| 105 |
id: ledLight |
| 106 |
source: Activity.url + "red_led_on.svg" |
| 107 |
anchors.fill: parent |
| 108 |
sourceSize.width: width |
| 109 |
sourceSize.height: height |
| 110 |
fillMode: Image.PreserveAspectFit |
| 111 |
opacity: 0 |
| 112 |
} |
| 113 |
|
| 114 |
Image { |
| 115 |
id: ledArrow |
| 116 |
source: isBroken ? Activity.url + "red_led_arrowBroken.svg" : Activity.url + "red_led_arrow.svg" |
| 117 |
anchors.fill: parent |
| 118 |
sourceSize.width: width |
| 119 |
sourceSize.height: height |
| 120 |
fillMode: Image.PreserveAspectFit |
| 121 |
opacity: 1 |
| 122 |
} |
| 123 |
|
| 124 |
function repairComponent() { |
| 125 |
redLed.source = Activity.url + "red_led_off.svg"; |
| 126 |
isBroken = false; |
| 127 |
} |
| 128 |
|
| 129 |
function checkConnections() { |
| 130 |
terminalConnected = 0; |
| 131 |
for(var i = 0; i < noOfConnectionPoints; i++) { |
| 132 |
if(connectionPoints.itemAt(i).wires.length > 0) |
| 133 |
terminalConnected += 1; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
function updateValues() { |
| 138 |
power = 1.84 * (vSource.current); |
| 139 |
if(power >= powerThreshold && power < powerMax) { |
| 140 |
ledLight.opacity = 1; |
| 141 |
} else if(power < powerThreshold) { |
| 142 |
ledLight.opacity = 0; |
| 143 |
} else if(power >= powerMax) { |
| 144 |
ledLight.opacity = 0; |
| 145 |
redLed.source = Activity.url + "red_led_broken.svg"; |
| 146 |
isBroken = true; |
| 147 |
Activity.restartTimer(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function initConnections() { |
| 152 |
var connectionIndex = Activity.connectionCount; |
| 153 |
redLed.externalNetlistIndex[0] = ++connectionIndex; |
| 154 |
connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex); |
| 155 |
redLed.internalNetlistIndex[0] = ++connectionIndex; |
| 156 |
redLed.internalNetlistIndex[1] = ++connectionIndex; |
| 157 |
redLed.externalNetlistIndex[1] = ++connectionIndex; |
| 158 |
connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex); |
| 159 |
Activity.connectionCount = connectionIndex; |
| 160 |
} |
| 161 |
|
| 162 |
function addToNetlist() { |
| 163 |
if(!isBroken) { |
| 164 |
var netlistItem = redLed.netlistModel; |
| 165 |
Activity.netlistComponents.push(redLed); |
| 166 |
netlistItem[2].name = componentName; |
| 167 |
netlistItem[2]._json = Activity.netlist.length; |
| 168 |
netlistItem[3][0] = redLed.externalNetlistIndex[0]; |
| 169 |
netlistItem[3][1] = redLed.internalNetlistIndex[0]; |
| 170 |
Activity.netlist.push(netlistItem); |
| 171 |
|
| 172 |
netlistItem = resistor.netlistModel; |
| 173 |
Activity.netlistComponents.push(resistor); |
| 174 |
netlistItem[2].name = "resistor-" + componentName; |
| 175 |
netlistItem[2]._json = Activity.netlist.length; |
| 176 |
netlistItem[3][0] = redLed.internalNetlistIndex[0]; |
| 177 |
netlistItem[3][1] = redLed.internalNetlistIndex[1]; |
| 178 |
Activity.netlist.push(netlistItem); |
| 179 |
|
| 180 |
netlistItem = vSource.netlistModel; |
| 181 |
Activity.netlistComponents.push(vSource); |
| 182 |
Activity.vSourcesList.push(vSource); |
| 183 |
netlistItem[2].name = "vSource-" + componentName; |
| 184 |
netlistItem[2]._json = Activity.netlist.length; |
| 185 |
netlistItem[3][0] = redLed.internalNetlistIndex[1]; //positive terminal to the resistor |
| 186 |
netlistItem[3][1] = redLed.externalNetlistIndex[1]; |
| 187 |
Activity.netlist.push(netlistItem); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
function checkComponentAnswer(): string { |
| 192 |
if(ledLight.opacity === 1) { |
| 193 |
return "redLedGlows"; |
| 194 |
} else if(terminalConnected >= 2 && isBroken) { |
| 195 |
return "redLedBroken"; |
| 196 |
} else if(terminalConnected >= 2) |
| 197 |
return "redLedIn"; |
| 198 |
else |
| 199 |
return ""; |
| 200 |
} |
| 201 |
} |