forked from frg-fossee/eSim-Cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoResistor.ts
More file actions
158 lines (152 loc) · 4.15 KB
/
PhotoResistor.ts
File metadata and controls
158 lines (152 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { CircuitElement } from '../CircuitElement';
import { Slider } from './Slider';
/**
* Class Photoresistor
*/
export class PhotoResistor extends CircuitElement {
/**
* Slider to set the value of photo resistor
*/
slide: Slider;
/**
* The Value of the photo resitor
*/
valueText: any;
/**
* Maxmum value of 1000
*/
maxVal = 1000;
/**
* Minmum value of 1000
*/
minVal = 0;
/**
* RaphaelJs path for sun
*/
sun;
/**
* RaphaelJs path for moon
*/
moon;
/**
* svg path of sun
*/
sunPath: string = null;
/**
* svg path of moon
*/
moonPath: string = null;
/**
* Photoresistor Constructor
* @param canvas Raphael canvas
* @param x Position x
* @param y Position y
*/
constructor(public canvas: any, x: number, y: number) {
super('PhotoResistor', x, y, 'PhotoResistor.json', canvas);
// fetch sun and moon path from json file
fetch(`./assets/jsons/PhotoResistor.json`)
.then(v => v.json())
.then(obj => {
this.sunPath = obj.sliderIcons.sunPath;
this.moonPath = obj.sliderIcons.moonPath;
});
}
/**
* Function provides component details
* @param keyName Unique Class name
* @param id Component id
* @param body body of property box
* @param title Component title
*/
properties(): { keyName: string; id: number; body: HTMLElement; title: string; } {
const body = document.createElement('div');
return {
keyName: this.keyName,
id: this.id,
body,
title: 'Photo Resistor'
};
}
/**
* Returns a string on the basis of resistance
* @param r Resistance
*/
getValue(r: number) {
return `${Math.round((r) * 100) / 100} lum`;
}
/**
* Initialize Variable and callback when start simulation is pressed
*/
initSimulation(): void {
this.valueText = this.canvas.text(this.x + this.tx + 1, this.y + this.ty - 70, `${this.maxVal / 2} lum`);
this.valueText.attr({
'font-size': 15
});
// Draw Sun on slider
this.sun = this.canvas.path(this.sunPath);
this.sun.attr({ fill: '#000', stroke: 'none' });
this.sun.translate(this.x + this.tx + 70, this.y + this.ty - 55);
// Draw Moon on slider
this.moon = this.canvas.path(this.moonPath);
this.moon.attr({ fill: '#000', stroke: 'none' });
this.moon.translate(this.x + this.tx - 105, this.y + this.ty - 55);
// Slider ranging from minValue to maxValue
this.slide = new Slider(this.canvas, this.x + this.tx, this.y + this.ty - 10);
this.slide.setGradient('#69644b', '#ffd500');
const enable1 = this.nodes[1].value > this.nodes[0].value ? true : false;
// At starting set value to half of slider
this.changeVal(enable1, 0.5);
// Listen to value changes on slider
this.slide.setValueChangeListener((v) => {
// Change slider's ouput value
this.changeVal(enable1, v);
});
}
/**
* Call this function to change ouput value of photoresistor
* @param enable1 Pin to direct output on
* @param v value of slider, ranges from 0-1
*/
changeVal(enable1, v) {
// calculate lumens according slider
const lum = (v) * this.maxVal;
// if enable1 is true
if (enable1) {
// calculate voltage value
const incoming = this.nodes[1].value;
// calculate output voltage
const val = (lum / this.maxVal) * incoming;
// set output voltage
this.nodes[0].setValue(val, null);
// update text
this.valueText.attr({
text: this.getValue(lum)
});
} else {
// calculate voltage value
const incoming = this.nodes[0].value;
// calculate output voltage
const val = (lum / this.maxVal) * incoming;
// set output voltage
this.nodes[1].setValue(val, null);
// update text
this.valueText.attr({
text: this.getValue(lum)
});
}
}
/** Function removes all animations and callbacks */
closeSimulation(): void {
this.valueText.remove();
this.slide.remove();
delete this.slide;
delete this.valueText;
this.sun.remove();
// delete this.sun;
this.moon.remove();
delete this.moon;
// this.slide = null;
this.valueText = null;
}
}