I have rewritten my code and cleaned it up a bit, but it still gives the same values as in my previous post
export default class MMA8451 { // @ts-ignore i2c: I2C = new I2C(); MMA8451_ADDR = 0x1D; MMA8451_REG_OUT_X_MSB = 0x01 //!< Read-only device output register MMA8451_REG_PL_CFG = 0x11 //!< Portrait/landscape configuration register MMA8451_REG_CTRL_REG1 = 0x2A //!< CTRL_REG1 system control 1 register MMA8451_REG_CTRL_REG2 = 0x2B //!< CTRL_REG2 system control 2 register MMA8451_REG_CTRL_REG4 = 0x2D //!< CTRL_REG4 system control 4 register MMA8451_REG_CTRL_REG5 = 0x2E //!< CTRL_REG5 system control 5 register writeRegister8(reg: number, value: number) { this.i2c.writeTo(this.MMA8451_ADDR, [reg, value]); } readRegister8(reg: number) { this.i2c.writeTo(this.MMA8451_ADDR, reg); return this.i2c.readFrom(this.MMA8451_ADDR, 1)[0]; } begin() { // @ts-ignore this.i2c.setup({ scl: B6, sda: B7 }); this.writeRegister8(this.MMA8451_REG_CTRL_REG2, 0x40)// reset setTimeout(() => { this.begin2(); }, 500); } begin2() { this.writeRegister8(0x0E, 0b01); // enable 4G range this.writeRegister8(this.MMA8451_REG_CTRL_REG2, 0x02); // High res // DRDY on INT1 this.writeRegister8(this.MMA8451_REG_CTRL_REG4, 0x01); this.writeRegister8(this.MMA8451_REG_CTRL_REG5, 0x01); // Turn on orientation config this.writeRegister8(this.MMA8451_REG_PL_CFG, 0x40); // Activate at max rate, low noise mode this.writeRegister8(this.MMA8451_REG_CTRL_REG1, 0x01 | 0x04); } logIt() { this.i2c.writeTo(this.MMA8451_ADDR, this.MMA8451_REG_OUT_X_MSB); let buffer = this.i2c.readFrom(this.MMA8451_ADDR, 6); let x = buffer[0]; x <<= 8; x |= buffer[1]; x >>= 2; let y = buffer[2]; y <<= 8; y |= buffer[3]; y >>= 2; let z = buffer[4]; z <<= 8; z |= buffer[5]; z >>= 2; const divider = 2048; let x_g = x / divider; let y_g = y / divider; let z_g = z / divider; console.log('x_g', x_g, 'y_g', y_g, 'z_g', z_g); console.log('buffer', buffer); } }
@TheLogan started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I have rewritten my code and cleaned it up a bit, but it still gives the same values as in my previous post