-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·393 lines (337 loc) · 13.2 KB
/
index.html
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<html>
<script src="jquery.js"></script>
<script src="qucsator.js"></script>
<script>qucsate.serverUrl = "backend/";</script>
<script>
(function(){
////////////////////////////////////////////////////////////////////////////////
//// GLOBAL DEFAULTS ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var defs = {
rows : 30,
powerRailHoles : 25,
debug : true
};
////////////////////////////////////////////////////////////////////////////////
//// HELPER FUNCTIONS //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Array Remove from John Resig http://ejohn.org
var remove = function(array, from, to) {
var rest = array.slice((to || from) + 1 || array.length);
array.length = from < 0 ? array.length + from : from;
return array.push.apply(array, rest);
};
var HTMLLog = undefined, HTMLbody = undefined;
this.debug = function(){
if(defs.debug){
if(typeof console!=='undefined'){
console.log(arguments[0]);
}else{
if(!HTMLLog){
HTMLLog = document.createElement('div');
HTMLLog.setAttribute("id", "HTMLLog");
HTMLBody = document.getElementsByTagName('body')[0];
HTMLBody.appendChild(HTMLLog);
HTMLLog.innerHTML += '<b>HTML-DEBUG-LOG:</b></b><br />';
}
HTMLLog.innerHTML += arguments[0] + '<br />';
}
}
};
////////////////////////////////////////////////////////////////////////////////
//// R E S I S T O R ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
this.Resistor = {
colorMap : { '-1': 'gold', '-2': 'silver',
0 : 'black', 1 : 'brown', 2 : 'red', 3 : 'orange',
4 : 'yellow', 5 : 'green', 6 : 'blue', 7 : 'violet', 8 : 'grey',
9 : 'white' },
toleranceColorMap : { 0.01 : 'brown', 0.02 : 'red', 5e-3 : 'green',
2.5e-3 : 'blue', 1e-3 : 'violet', 5e-4 : 'gray', 5e-2 : 'gold',
0.1 : 'silver', 0.2 : 'none' },
getColors4Band: function (ohms, tolerance) {
var s = ohms.toString();
var decIx = s.indexOf('.');
var decLoc = decIx > -1 ? decIx : s.length;
s = s.replace('.', '');
var len = s.length;
for (var i = 0; i < 2 - len; ++i){ s += '0'; }
var mult = decLoc > 1 ? decLoc - 2 : 10;
return [ this.colorMap[s.charAt(0)],
this.colorMap[s.charAt(1)],
this.colorMap[decLoc - 2],
this.toleranceColorMap[tolerance]
];
},
getColors5Band: function (ohms, tolerance) {
var s = ohms.toString();
var decIx = s.indexOf('.');
var decLoc = decIx > -1 ? decIx : s.length;
s = s.replace('.', '');
var len = s.length;
for (var i = 0; i < 3 - len; ++i) { s += '0'; }
return [ this.colorMap[s.charAt(0)],
this.colorMap[s.charAt(1)],
this.colorMap[s.charAt(2)],
this.colorMap[decLoc - 3],
this.toleranceColorMap[tolerance]
];
},
colorToNumber : function(color) {
for (n in Resistor.colorMap) {
if (Resistor.colorMap[n] == color) { return parseInt(n); }
}
},
getResistance: function(colors){
var resistance = Resistor.colorToNumber(colors[0]);
for ( var i = 1; i < colors.length - 2; i++) {
resistance = resistance * 10;
resistance += Resistor.colorToNumber(colors[i]);
}
return resistance * Math.pow(10, Resistor.colorToNumber(colors[i]));
}
};
////////////////////////////////////////////////////////////////////////////////
//// B R E A D - B O A R D - M O D E L /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//// BREADBOARD Prototype Model //////////////////////////////////////////////
this.breadBoard = {};
var Hole = function Hole( strip, name ){
this.type ='hole';
this.strip = strip;
this.name = name;
this.connections = [];
return this;
};
var Strip = function Strip( holes, name ){
this.type ='strip';
this.holes={};
this.name = name;
if( holes ){
for(var i=0, l=holes; i < l; i++){
this.holes[''+i] = new Hole();
this.holes[''+i].strip = this;
}
}
return this;
};
var Breadboard = function Breadboard(){
this.type ='Breadboard';
// Creaye power-rails
this.powerRail = { // I was told these were called power-rails
left:{
positive: new Strip( null, "powerPosL"),
negative: new Strip( null, "powerNegL")
},
right:{
positive: new Strip( null, "powerPosR" ),
negative: new Strip( null, "powerNegR" )
}
};
for(var i=0, l=defs.powerRailHoles; i < l; i++){
for(side in this.powerRail) {
for(end in this.powerRail[side]) {
var h = side + '_' + end + '_' + i
this.powerRail[side][end][h] = this.holes[h] = new Hole(this.powerRail[side][end], h);
}
}
}
// Create board
for(var i=0, l=defs.rows; i < l; i++ ){
newStripL = this.makeStrip("L" + i);
newStripR = this.makeStrip("R" + i);
for(var a=0, ll=5; a < ll; a++ ){
var mapCode = String.fromCharCode(a+97)+i;
newStripL.holes[mapCode] = this.holes[ mapCode ] = new Hole( newStripL, mapCode );
var mapCode = String.fromCharCode(a+102)+i;
newStripR.holes[mapCode] = this.holes[ mapCode ] = new Hole( newStripR, mapCode );
}
}
return this;
};
Breadboard.prototype.strips=[];
Breadboard.prototype.components={};
Breadboard.prototype.holes={};
Breadboard.prototype.makeStrip = function(name){
var stripLen = this.strips.length;
this.strips[ stripLen ] = new Strip(null, name);
return this.strips[ stripLen ];
};
Breadboard.prototype.component = function component(props){
if(typeof props=='string'){
return this.components[props];
}else {
return new Component(props);
}
};
Breadboard.prototype.clear = function() {
for (k in this.components) {
if(k.destroy) {k.destroy()};
}
}
//// COMPONENT MODEL /////////////////////////////////////////////////////////
var Component = function Component(props){
for(var i in props){
this[i]=props[i];
}
this.breadBoard = breadBoard;
this.breadBoard.components[props.UID] = this;
this.connections=[];
for(var i in props.connections){
this.connections[i] = this.breadBoard.holes[props.connections[i]];
this.breadBoard.holes[props.connections[i]].connections[this.breadBoard.holes[props.connections[i]].connections.length] = this;
}
return this;
};
Component.prototype.move = function move(connections){
for(var i in this.connections){
for( var j in this.connections[i].connections ){
if( this.connections[i].connections[j] === this ){
this.connections[i].connections = [];
}
}
this.connections[i] = [];
}
this.connections = [];
for(var i in connections){
this.connections[i] = this.breadBoard.holes[connections[i]];
this.breadBoard.holes[connections[i]].connections[this.breadBoard.holes[connections[i]].connections.length] = this;
}
return this;
};
Component.prototype.destroy = function destroy(){
for(var i in this.connections){
for( var j in this.connections[i].connections ){
if( this.connections[i].connections[j] === this ){
this.connections[i].connections = [];
}
}
this.connections[i] = [];
}
this.connections = [];
return delete this.breadBoard.components[this.name];
};
//// BreadBoard Instance & Interface /////////////////////////////////////////
var breadBoard = new Breadboard();
var interfaces = {
insert: function(){
var props = {
UID : arguments[0]+calls,
kind : arguments[0],
connections : arguments[1].split(",")
};
switch(props.kind){
case "resistor":
props.colors = arguments[2].split(",");
//props.resistance = 100; // TEMPORARY FIGURE TO BE CLACULATED FROM COLORS SOON!!
props.resistance = Resistor.getResistance(props.colors);
break;
}
var newComponent;
newComponent = breadBoard.component(props);
return newComponent.UID;
},
destroy: function(){
breadBoard.component(arguments[0]).destroy();
},
clear: function() {
breadBoard.clear();
},
move: function(){
breadBoard.component(arguments[0]).move(arguments[1].split(','));
},
query: function(){
// Current dummy function will pass query to model
// Model will then compile SPICE net list and send to server
// Server will respond with voltage at queried points
// Power and MultiMeter settings will be assumed
//debug( breadBoard.query( arguments[1].split(',') ) );
breadBoard.component({
UID: 'meter',
kind: {'current' : 'iprobe', 'voltage' : 'vprobe'}[arguments[0]],
connections: arguments[1].split(',')});
breadBoard.component({
UID: 'bat1',
kind: 'battery',
voltage: 9,
connections: ["left_positive_1", "left_negative_1"]});
breadBoard.component({
UID: 'bat2',
kind: 'battery',
voltage: 9,
connections: ["right_positive_1", "right_negative_1"]});
var result;
qucsate(qucsate.makeNetlist(breadBoard), function(r){ result = r.meter; } );
breadBoard.component('meter').destroy();
breadBoard.component('bat1').destroy();
breadBoard.component('bat2').destroy();
return result;
}
};
//Stored the number of calls made to the Model: used to create UID
var calls=0;
// The inward interface between Flash's ExternalInterface and JavaScript's BreadBoard prototype model instance
this.breadModel = function breadModel(){
calls++;
var newArgs = [];
for(var i=1,l=arguments.length;i< l;i++){
newArgs[newArgs.length] = arguments[i];
}
var func = arguments[0];
return interfaces[func].apply(window, newArgs);
};
// The outward interface between JS & Flash
/*
this.toFlash = function toFlash(){
console.log(arguments[0]);
document.getElementById('swf').fromBreadModel(arguments[0]);
};
document.addEventListener('click', function(){ flash("testing!","test2") }, false);
*/
})();
</script>
<style>
#HTMLLog{background:#000;color:#c00;height:100px;padding:10px;margin:10px;}
</style>
<embed style="width:100px;height:100px;border:1px solid black;" src="SparksUI-to-JSbreadModel-Syntax.swf" id="swf" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash">
<script>
$(function() {
////////////////////////////////////////////////////////////////////////////////
//// T E S T S /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
debug("Clear the board");
breadModel('clear');
debug("Current from one power rail to another");
debug(breadModel('query', 'current', 'left_negative_1,left_positive_1', '200k'));
debug("Voltage in between two 100 ohm resistors");
r1 = breadModel('insert', 'resistor', 'b1,b2', 'brown,black,brown,gold');
r2 = breadModel('insert', 'resistor', 'c2,c3', 'brown,black,brown,gold');
w1 = breadModel('insert', 'wire', 'left_negative_1,a1');
w2 = breadModel('insert', 'wire', 'left_positive_3,a3');
debug(breadModel('query', 'voltage', 'b2,left_negative_1', '200k'));
debug("Add another resistor in series");
r3 = breadModel('insert', 'resistor', 'b3,b4', 'brown,black,brown,gold');
breadModel('move', w2, 'left_positive_4,a4');
debug(breadModel('query', 'voltage', 'b2,left_negative_1', '200k'));
debug("Query the current");
debug(breadModel('query', 'current', 'b2,left_negative_1', '200k'));
debug("Change it to parallel");
breadModel('move', r3, 'd2,d3');
breadModel('move', w2, 'left_positive_3,a3');
debug(breadModel('query', 'voltage', 'b2,left_negative_1', '200k'));
var i,v;
debug("find the resistance of a brown green brown resistor");
debug("insert the resistor");
rx = breadModel('insert', 'resistor', 'b5,b6', 'brown,green,brown,gold');
debug("connect one end of it to positive");
wx = breadModel('insert', 'wire', 'left_positive_5,a5');
debug("voltage between the other end of the resistor and negative");
debug(i = breadModel('query', 'current', 'a6,left_negative_6', '200k'));
debug("voltage for the same");
debug(v = breadModel('query', 'voltage', 'a6,left_negative_6', '200k'));
debug("divide to get r");
debug(v/i)
});
</script>
<body></body>
</html>