You are reading a single comment by @ClearMemory041063 and its replies. Click here to read the full conversation.
  • Translating C to Espruino Javascript
    #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
    So my guess is it becomes this:
    function CHECK_BIT (val, pos) { return ((val) & (1<<(pos))); }
    And the init_i2c function is puzzling to me. Looks like init_i2c("/dev/i2c-1"); is akin to calling I2C1.setup().
    Some #define statements have to become functions.
    Having done this for the LSM9DS1 IMU these snippets may help.
    The other gotchas are handling the bytes read as unsigned or signed values, big or little endian and two’s compliment math. C programmers seem to grab a datasheet for a chip and #define everything in sight, at the end there tends to be a lot of unused stuff defined in the C code. I usually resort to commenting out blocks of stuff to see what complains and uncomment the complaints.

    void LSM9DS1::initI2C()
    {
    	Wire.begin();	// Initialize I2C library
    }
    Becomes
    //Configuration
    //The I2C pins that the LSM9D01 is connected to
    //PICO I2C pins
    //IC1  sda=B7  scl=B6
    //IC1  sda=B9  scl=B8  shim pins
    //IC3  sda=B4  scl=A8
    //IC2  sda=B3  scl=B10
    var W;
    function start(){
    //  console.log("start");
     I2C3.setup({ scl :A8, sda: B4} );
    //console.log(I2C3);
    var xgAddress= 0x6B;// Would be 0x1C if SDO_M is LOW
    var mAddress= 0x1e;// Would be 0x6A if SDO_AG is LOW 
     W =require("slimLSM9DS1").connect(I2C3,xgA­ddress,mAddress);
    W.run();//Get it started
    
    uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress)
    {
    	int timeout = LSM9DS1_COMMUNICATION_TIMEOUT;
    	uint8_t data; // `data` will store the register data	
    	
    	Wire.beginTransmission(address);         // Initialize the Tx buffer
    	Wire.write(subAddress);	                 // Put slave register address in Tx buffer
    	Wire.endTransmission(true);             // Send the Tx buffer, but send a restart to keep connection alive
    	Wire.requestFrom(address, (uint8_t) 1);  // Read one byte from slave register address 
    	while ((Wire.available() < 1) && (timeout-- > 0))
    		delay(1);
    	
    	if (timeout <= 0)
    		return 255;	//! Bad! 255 will be misinterpreted as a good value.
    	
    	data = Wire.read();                      // Fill Rx buffer with result
    	return data;                             // Return data read from slave register
    }
    Becomes
    /** 'mReadByte(subAddress) read a byte from the magnetometer at subaddress'*/
    LSM9DS1.prototype.mReadByte=function(sub­Address){
     var x=this.mAddress;
     var data=Uint8Array(1);
     this.i2c.writeTo(x, subAddress);
     data=this.i2c.readFrom(x, 1);
     return data[0];
    };//end mReadByte
    
    uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count)
    {  
    	int timeout = LSM9DS1_COMMUNICATION_TIMEOUT;
    	Wire.beginTransmission(address);   // Initialize the Tx buffer
    	// Next send the register to be read. OR with 0x80 to indicate multi-read.
    	Wire.write(subAddress | 0x80);     // Put slave register address in Tx buffer
    
    	Wire.endTransmission(true);             // Send the Tx buffer, but send a restart to keep connection alive
    	uint8_t i = 0;
    	Wire.requestFrom(address, count);  // Read bytes from slave register address 
    	while ((Wire.available() < count) && (timeout-- > 0))
    		delay(1);
    	if (timeout <= 0)
    		return -1;
    	
    	for (int i=0; i<count;)
    	{
    		if (Wire.available())
    		{
    			dest[i++] = Wire.read();
    		}
    	}
    	return count;
    }
    
    Becomes
    /** 'mReadBytes(subAddress,count) read count bytes from magnetometer at subAddress'*/
    LSM9DS1.prototype.mReadBytes=function(su­bAddress,count){
     var x=this.mAddress;
     var dest=new Uint8Array(count);
     this.i2c.writeTo(x, subAddress|0x80);
     dest=this.i2c.readFrom(x, count);
     return dest;
    };//end mReadBytes
    
    
    // Wire.h read and write protocols
    void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
    {
    	Wire.beginTransmission(address);  // Initialize the Tx buffer
    	Wire.write(subAddress);           // Put slave register address in Tx buffer
    	Wire.write(data);                 // Put data in Tx buffer
    	Wire.endTransmission();           // Send the Tx buffer
    }
    Becomes
    LSM9DS1.prototype.mWriteByte=function(su­bAddress,data){ 
      //console.log("mWriteByte ",this.mAddress, subAddress, data);
      var x=this.mAddress;
      this.i2c.writeTo(x, subAddress,data);
      return 0;
    };//end mWriteByte
    

    http://forum.espruino.com/conversations/­296285/

About