• I just tried this out on an MCP23008... it actually seems to work

    
    exports.connect = function(i2c,rst,i2ca) {
    	
        return new MCP32x08(i2c,rst,i2ca);
    };
    
    function MCP32x08(i2c,rst, i2ca) {
      rst.write(0);
      this.i2c = i2c;
      this.i2ca = (i2ca===undefined) ? 32:32+i2ca;
      this.rst=rst;
      this.m=255;
      this.pu=0;
      this.olat=0;
      this.rst.write(1);
      this.A0=new PEP(0,this);
      this.A1=new PEP(1,this);
      this.A2=new PEP(2,this);
      this.A3=new PEP(3,this);
      this.A4=new PEP(4,this);
      this.A5=new PEP(5,this);
      this.A6=new PEP(6,this);
      this.A7=new PEP(7,this);
    }
    
    MCP32x08.prototype.s=function(r,d){this.­i2c.writeTo(this.i2ca,r,d);};
    MCP32x08.prototype.r=function(r){this.i2­c.writeTo(this.i2ca,r);return this.i2c.readFrom(this.i2ca,1)};
    
    MCP32x08.prototype.mode=function(pin,mod­e) {
    	var bv=1<<pin;
    	this.s(0,mode=='output'?(this.m&=~bv):(t­his.m |=bv));
    	this.s(6,mode=='input_pullup'?(this.pu|=­bv):(this.pu&=~bv));
    };
    MCP32x08.prototype.write=function(pin,va­l) {
    	var bv=1<<pin;
    	this.olat&=~bv;
    	this.s(9,this.olat|=(bv*val));
    }; 
    MCP32x08.prototype.read=function(pin) {
    	var bv=1<<pin;
    	return (this.r(9)[0]&bv)>>pin;
    }; 
    
    function PEP (b,p){
    	this.b=b;
    	this.p=p;
    }
    PEP.prototype.set=function(){this.p.writ­e(this.b,1)};
    PEP.prototype.unset=function(){this.p.wr­ite(this.b,0)};
    PEP.prototype.write=function(v){this.p.w­rite(this.b,v)};
    PEP.prototype.read=function(){return this.p.read(this.b)};
    PEP.prototype.mode=function(m){this.p.mo­de(this.b,m)};
    
    
    
    >process.memory();
    ={ "free": 4824, "usage": 276, "total": 5100, "history": 121,
      "stackEndAddress": 536958396, "flash_start": 134217728, "flash_binary_end": 317952, "flash_code_start": 134234112, "flash_length": 393216 }
    >pe.A4.mode('input_pullup');
    =undefined
    >pe.A4.read()
    =1
    >pe.A4.mode('output');
    =undefined
    >pe.A4.read();
    =0
    >pe.A4.set()
    =undefined
    >pe.A4.read();
    =1
    >pe.A4.unset();
    =undefined
    >pe.A4.read();
    =0
    > 
    
    

    much easier than I'd expected (though there's still some more to to, including expanding to the 16-bit version (there are two ways the data can be organized...)

About

Avatar for DrAzzy @DrAzzy started