This commit is contained in:
xdrm-brackets 2016-07-19 10:33:30 +02:00
parent d9e9374cb7
commit 6f83c677d4
3 changed files with 14 additions and 11 deletions

View File

@ -250,13 +250,17 @@
lsi.import('STATES.CNF', response.states);
});
/* (5) On initialise les puces */
/* (5) On initialise la carte + les puces */
var board = new Board(50);
var chip = {
led: new LedInterface( new Chip(1, 2, 3), led ),
r1: new RelayInterface( new Chip(4), r1 ),
r2: new RelayInterface( new Chip(5), r2 )
};
board.plug( chip.led )
/* [1] Fonctions
=========================================================*/

View File

@ -1,6 +1,6 @@
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,b){if(!(b instanceof Chip))return!1;for(var c=0;c<b.pins.length;c++)if(this.pins[c]instanceof Pin)return!1;for(c=0;c<b.pins.length;c++)this.pins[c]=b.values[c],this.listener[c]=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[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(){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;

View File

@ -61,27 +61,26 @@ var Board;
/* BRANCHE UNE PUCE SUR LA Board
*
* @listener<Object> Interface contenant la carte
* @chip<Chip> Carte à brancher
* @chipInterface<Object> Interface contenant la carte et la mise à jour
*
*/
Board.prototype.plug = function(listener, chip){
Board.prototype.plug = function(chipInterface){
/* [0] Vérification des paramètres
=========================================================*/
if( !(chip instanceof Chip) )
if( !(chipInterface.chip instanceof Chip) )
return false;
/* [1] Vérification des Pins
=========================================================*/
for( var i = 0 ; i < chip.pins.length ; i++ )
for( var i = 0 ; i < chipInterface.chip.pins.length ; i++ )
if( this.pins[i] instanceof Pin ) // si pin déja prise
return false;
/* [2] Branchement + on lie l'interface
=========================================================*/
for( var i = 0 ; i < chip.pins.length ; i++ ){
this.pins[i] = chip.values[i];
this.listener[i] = listener;
for( var i = 0 ; i < chipInterface.chip.pins.length ; i++ ){
this.pins[i] = chipInterface.chip.values[i];
this.listener[i] = chipInterface;
}
};