Client Virtuel : Implémentation des PINS+CHIPS+STATES+GLOBAL_STATES+ACTIONS

This commit is contained in:
xdrm-brackets 2016-07-19 14:55:51 +02:00
parent 2f6c09dbae
commit f640cdf5f4
3 changed files with 18 additions and 13 deletions

View File

@ -282,7 +282,7 @@
/* (2) Si l'état correspond */
if( previous(STATES[s].global_state, STATE) ){
console.log('STATE', STATE, 'matches', STATES[s].global_state);
console.log('STATE', STATE.join(''), 'matches', STATES[s].global_state);
/* (3) On applique l'état à chaque CHIP */
for( var c in CHIPS ){
@ -296,8 +296,10 @@
var values = CHIPS[c].states[STATES[s].chips[c]];
/* (6) On applique les valeurs */
for( var p in pins )
for( var p in pins ){
board.set( parseInt(pins[p]), parseInt(values[p]) );
console.log('board.set(', parseInt(pins[p]), ',', parseInt(values[p]), ');');
}
}

View File

@ -1,8 +1,8 @@
var Pin;(function(){Pin=function(a){parseInt(a)==a&&0<=a&&255>=a?this.value=parseInt(a):this.value=0};Pin.prototype={value:this.value};Pin.prototype.set=function(a){parseInt(a)==a&&0<=a&&255>=a&&(this.value=parseInt(a))};Pin.prototype.get=function(){return this.value}})();var Board;
(function(){Board=function(a){if(!(parseInt(a)!=a||1>a)){this.maxPin=a;this.pins=[];this.listener=[];for(a=0;a<this.maxPin;a++)this.pins[a]=!1;this.listener[a]=null}};Board.prototype={maxPin:this.maxPin,pins:this.pins,listener:this.listener};Board.prototype.plug=function(a){if(!(a.chip instanceof Chip))return!1;for(var b=0;b<a.chip.pins.length;b++)if(this.pins[b]instanceof Pin)return!1;for(b=0;b<a.chip.pins.length;b++)this.pins[b]=a.chip.values[b],this.listener[b]=a};Board.prototype.set=function(a,
b){if(!(parseInt(a)!=a||a>this.maxPin)&&this.pins[a]instanceof Pin){if("boolean"==typeof b)b=b?255:0;else if(b!=parseInt(b)||0>b||255<b)return;b=parseInt(b);this.pins[a].set(b);this.listener[a].update()}}})();var Chip;
(function(){Board=function(a){if(!(parseInt(a)!=a||1>a)){this.maxPin=a;this.pins=[];this.listener=[];for(a=0;a<this.maxPin;a++)this.pins[a]=!1;this.listener[a]=null}};Board.prototype={maxPin:this.maxPin,pins:this.pins,listener:this.listener};Board.prototype.plug=function(a){if(!(a.chip instanceof Chip))return!1;for(var b=0;b<a.chip.pins.length;b++)if(this.pins[a.chip.pins[b]]instanceof Pin)return!1;for(b=0;b<a.chip.pins.length;b++)this.pins[a.chip.pins[b]]=a.chip.values[b],this.listener[a.chip.pins[b]]=
a};Board.prototype.set=function(a,b){if(!(parseInt(a)!=a||a>this.maxPin)&&this.pins[a]instanceof Pin){if("boolean"==typeof b)b=b?255:0;else if(b!=parseInt(b)||0>b||255<b)return;b=parseInt(b);this.pins[a].set(b);null!=this.listener[a]&&this.listener[a].update()}}})();var Chip;
(function(){Chip=function(a,b){if("string"==typeof a&&(a=a.toLowerCase(),-1!=["spi","i2c","serial"].indexOf(a)&&b instanceof Array)){for(var c=0;c<b.length;c++)if(parseInt(b[c])!=b[c])return;this.type=a;this.pins=b;this.values=[];for(c=0;c<this.pins.length;c++)this.values[c]=new Pin(0)}};Chip.prototype={type:this.type,pins:this.pins,values:this.values};Chip.prototype.setPin=function(a,b){if(!(parseInt(a)!=a||this.pins.length>=a)){if("boolean"==typeof b)b=b?255:0;else if(b!=parseInt(b)||0>b||255<b)return;
b=parseInt(b);this.values[a].set(b)}};Chip.prototype.setPins=function(a){for(var b=0;b<this.pins.length;b++)this.setPin(b,a[b])};Chip.prototype.getPin=function(a){return parseInt(a)!=a||this.pins.length<=a?!1:this.values[a].get()};Chip.prototype.getPins=function(){for(var a=[],b=0;b<this.pins.length;b++)a[b]=this.getPin(b);return a}})();var LedInterface;
(function(){LedInterface=function(a,b){a instanceof Chip&&b instanceof Element&&(this.chip=a,this.container=b)};LedInterface.prototype={chip:this.chip,container:this.container};LedInterface.prototype.update=function(){this.container.style.backgroundColor="rgb("+this.chip.getPin(0)+","+this.chip.getPin(1)+","+this.chip.getPin(2)+")"}})();var RelayInterface;
(function(){RelayInterface=function(a,b){a instanceof Chip&&b instanceof Element&&(this.chip=a,this.container=b)};RelayInterface.prototype={chip:this.chip,container:this.container};RelayInterface.prototype.update=function(){this.chip.pin[0]?this.container.addClass("active"):this.container.remClass("active")}})();var previous,action;
(function(){previous=function(a,b){a=a.toLowerCase();for(var c=0;c<a.length;c++)if("x"!=a[c]&&a[c]!=b[c])return!1;return!0};action=function(a,b){a=a.toLowerCase();for(var c=0;c<a.length;c++)"x"!=a[c]&&(b[c]=a[c])}})();
(function(){RelayInterface=function(a,b){a instanceof Chip&&b instanceof Element&&(this.chip=a,this.container=b)};RelayInterface.prototype={chip:this.chip,container:this.container};RelayInterface.prototype.update=function(){1<this.chip.getPin(0)?this.container.addClass("active"):this.container.remClass("active")}})();var previous,action;
(function(){previous=function(a,b){a=a.toLowerCase();for(var c=0;c<a.length;c++)if("x"!=a[c]&&a[c]!=b[c])return!1;return!0};action=function(a,b){a=a.toLowerCase();for(var c=0;c<a.length;c++)"x"!=a[c]&&(b[c]=parseInt(a[c]))}})();

View File

@ -73,14 +73,14 @@ var Board;
/* [1] Vérification des Pins
=========================================================*/
for( var i = 0 ; i < chipInterface.chip.pins.length ; i++ )
if( this.pins[i] instanceof Pin ) // si pin déja prise
if( this.pins[chipInterface.chip.pins[i]] instanceof Pin ) // si pin déja prise
return false;
/* [2] Branchement + on lie l'interface
=========================================================*/
for( i = 0 ; i < chipInterface.chip.pins.length ; i++ ){
this.pins[i] = chipInterface.chip.values[i];
this.listener[i] = chipInterface;
this.pins[chipInterface.chip.pins[i]] = chipInterface.chip.values[i];
this.listener[chipInterface.chip.pins[i]] = chipInterface;
}
};
@ -118,6 +118,7 @@ var Board;
/* [2] On lance le listener
=========================================================*/
if( this.listener[pinOrder] != null )
this.listener[pinOrder].update();
};
@ -336,7 +337,7 @@ var RelayInterface;
*
*/
RelayInterface.prototype.update = function(){
if( this.chip.pin[0] ) this.container.addClass('active');
if( this.chip.getPin(0) > 1) this.container.addClass('active');
else this.container.remClass('active');
};
@ -352,6 +353,7 @@ var action;
(function(){
/* RETOURNE SI UN ETAT EST CORRECT
*
* @previous<String> Condition d'état
@ -382,7 +384,8 @@ var action;
for( var i = 0 ; i < action.length ; i++ )
if( action[i] == 'x' ) continue;
else state[i] = action[i];
else state[i] = parseInt(action[i]);
};
})();