-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCDC_RES_CALCULATOR.pde
157 lines (131 loc) · 3.74 KB
/
DCDC_RES_CALCULATOR.pde
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
//gui控制库
import controlP5.*;
ControlP5 cp5;
Textlabel myTextlabelA;
Textlabel myTextlabelSet;
Textlabel myTextlabelSwitch;
Textlabel myTextlabelCalaulator;
String vOut ;
String vRef ;
String setR1 ;
String setR2 ;
//声明一个图片类
PImage img;
float imgSize = 2;
void setup() {
size(1200, 900);
background(0);
PFont font = loadFont("3ds-Light-24.vlw");
img = loadImage("image/DCDC.png");
//imageMode(CENTER);
image(img,0,0,width/imgSize, height); // Display at full opacity
cp5 = new ControlP5(this);
myTextlabelSet = cp5.addTextlabel("setlabel")
.setText("1.Please Set Value First: ")
.setPosition(700,40)
.setColorValue(0xffffff00)
.setFont(font)
;
cp5.addTextfield("Vout")
.setPosition(800,90)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
cp5.addTextfield("Vref")
.setPosition(800,160)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
myTextlabelSwitch = cp5.addTextlabel("switchlabel")
.setText("2. Please Set R1 OR R2 Value Second: ")
.setPosition(700,240)
.setColorValue(0xffffff00)
.setFont(font)
;
cp5.addTextfield("R1")
.setPosition(750,280)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
cp5.addTextfield("R2")
.setPosition(950,280)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
myTextlabelCalaulator = cp5.addTextlabel("calculatorlabel")
.setText("3. Calculator R1 OR R2 Value Finally: ")
.setPosition(700,380)
.setColorValue(0xffffff00)
.setFont(font)
;
cp5.addTextfield("C_R1")
.setPosition(750,460)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
cp5.addTextfield("C_R2")
.setPosition(950,460)
.setSize(80,30)
.setFont(font)
.setFocus(false)
.setColor(color(255,0,0))
.setText("0.0")
.setAutoClear(false)
;
cp5.addBang("calculator")
.setPosition(1090,480)
.setSize(80,40)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
}
void draw() {
}
public void calculator() {
double myout,myref;
double tempR1 = 0,tempR2 = 0;
double tempOutR1 = 0, tempOutR2 = 0;
vRef = cp5.get(Textfield.class,"Vref").getText();
vOut = cp5.get(Textfield.class,"Vout").getText();
setR1 = cp5.get(Textfield.class,"R1").getText();
setR2 = cp5.get(Textfield.class,"R2").getText();
myout = Double.parseDouble(vOut); //将String转换为double类型
myref = Double.parseDouble(vRef); //将String转换为double类型
tempR1 = Double.parseDouble(setR1); //将String转换为double类型
tempR2 = Double.parseDouble(setR2); //将String转换为double类型
//由R1计算R2
if(tempR1 > 0){
tempOutR2 = tempR1 * ( myout / myref - 1 ); //计算R2的选择值
tempOutR2 = fixDec(tempOutR2,2); //保留小数位数
cp5.get(Textfield.class,"C_R2").setText(Double.toString(tempOutR2)); //更新窗口文字
}
//由R2计算R1
if(tempR2 > 0){
tempOutR1 = tempR2 * myref /( myout - myref );
tempOutR1 = fixDec(tempOutR1,2);
cp5.get(Textfield.class,"C_R1").setText(Double.toString(tempOutR1));
}
}
//控制小数的位数
double fixDec(double n, int d) {
return Double.parseDouble(String.format("%." + d + "f", n));
}