I've never written stuff for I2C or other protocols, but I have an MMA8451 chip on a breakout board from Adafruit. To my knowledge, there isn't an Espruino library for it, so I've looked at the one provided for Arduino by Adafruit and tried to write one in Espruino, but I'm not getting the expected results and I am rather confused about what I am even doing .. xD
Also, I am not proficient in C++, so I've been googling and guessing my way through trying to convert it.
This is my code, it's in typescript but gets transpiled into JS that the board understands :)
export default class MMA8451 {
// @ts-ignore
i2c: I2C = new I2C();
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
begin() {
// @ts-ignore
this.i2c.setup({ scl: B6, sda: B7 });
this.i2c.writeTo(this.MMA8451_REG_CTRL_REG2, 0x40); // reset
setTimeout(() => {
this.begin2();
}, 500);
}
begin2() {
this.i2c.writeTo(0x0E, 0b01); // enable 4G range
this.i2c.writeTo(this.MMA8451_REG_CTRL_REG2, 0x02); // High res
// DRDY on INT1
this.i2c.writeTo(this.MMA8451_REG_CTRL_REG4, 0x01);
this.i2c.writeTo(this.MMA8451_REG_CTRL_REG5, 0x01);
// Turn on orientation config
this.i2c.writeTo(this.MMA8451_REG_PL_CFG, 0x40);
// Activate at max rate, low noise mode
this.i2c.writeTo(this.MMA8451_REG_CTRL_REG1, 0x01 | 0x04);
for (let i = 0x00; i < 0x30; i++) {
console.log('index', i, this.i2c.readFrom(i, 16));
}
}
logAll() {
for (let i = 0x00; i < 0x30; i++) {
console.log('index', i, this.i2c.readFrom(i, 16));
}
}
logIt() {
let buffer = new Uint8Array([this.MMA8451_REG_OUT_X_MSB, 0, 0, 0, 0, 0]);
let val1 = this.i2c.writeTo(buffer, 1);
let val2 = this.i2c.readFrom(buffer, 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('val1', val1);
console.log('val2', val2);
console.log('buffer', buffer);
}
}
So one thing I am noticing right off the bat is that I never actually use the address of the chip (0x1D), but the Adafruit code also only uses two places, which aren't called in the code xD
If I run "runSensor" then I get 48 lines printed out that all look like this:
index 28 new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255])
Except for one that looks like this:
index 29 new Uint8Array([0, 41, 252, 163, 200, 152, 160, 0, 41, 252, 163, 200, 152, 160, 0, 41])
But that line doesn't change no matter how much I move the sensor around
And if I run "logIt"
I get the following data:
x_g 0.03125 y_g 0 z_g 0
val1 undefined
val2 new Uint8Array([255, 255, 255, 255, 255, 255])
buffer new Uint8Array([1, 0, 0, 0, 0, 0])
Again it's identical no matter how much I move the sensor around.
If I unplug the sensor, then line 29 in the array becomes an array of 255 like the others, however, the "logIt" still pumps out the same data.
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've never written stuff for I2C or other protocols, but I have an MMA8451 chip on a breakout board from Adafruit. To my knowledge, there isn't an Espruino library for it, so I've looked at the one provided for Arduino by Adafruit and tried to write one in Espruino, but I'm not getting the expected results and I am rather confused about what I am even doing .. xD
So quickly a few links:
This is the Adafruit guide that I used to hook it up
https://learn.adafruit.com/adafruit-mma8451-accelerometer-breakout/overview
This is the datasheet
https://cdn-shop.adafruit.com/datasheets/MMA8451Q-1.pdf
This is the header file for the Adafruit library
https://github.com/adafruit/Adafruit_MMA8451_Library/blob/master/Adafruit_MMA8451.h
And this is the actual code for the library
https://github.com/adafruit/Adafruit_MMA8451_Library/blob/master/Adafruit_MMA8451.cpp
Also, I am not proficient in C++, so I've been googling and guessing my way through trying to convert it.
This is my code, it's in typescript but gets transpiled into JS that the board understands :)
So one thing I am noticing right off the bat is that I never actually use the address of the chip (0x1D), but the Adafruit code also only uses two places, which aren't called in the code xD
If I run "runSensor" then I get 48 lines printed out that all look like this:
index 28 new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255])
Except for one that looks like this:
index 29 new Uint8Array([0, 41, 252, 163, 200, 152, 160, 0, 41, 252, 163, 200, 152, 160, 0, 41])
But that line doesn't change no matter how much I move the sensor around
And if I run "logIt"
I get the following data:
x_g 0.03125 y_g 0 z_g 0
val1 undefined
val2 new Uint8Array([255, 255, 255, 255, 255, 255])
buffer new Uint8Array([1, 0, 0, 0, 0, 0])
Again it's identical no matter how much I move the sensor around.
If I unplug the sensor, then line 29 in the array becomes an array of 255 like the others, however, the "logIt" still pumps out the same data.
Can anyone help me with this? :)