![](/media/cache/group/logo.001.png.50x50_q85.png)
key lock and rear light
Dependencies: BLE_API BMA250E mbed nRF51822
MtConnect04S Bike Proximity Lock
We have full tutorial, please visit our blog
BMA250/BMA250.cpp
- Committer:
- bcc6
- Date:
- 2016-11-23
- Revision:
- 0:3eeefb743ce3
File content as of revision 0:3eeefb743ce3:
#include "BMA250.h" BMA250::BMA250(PinName sda, PinName scl, PinName int1, PinName int2) : _i2c(sda, scl), _int1(int1), _int2(int2) { /* Basic */ RegWrite(0x14, 0xB6); // softreset RegWrite(0x0F, 0x03); // range((+/-)2G) RegWrite(0x10, 0x0C); // bandwidth(125Hz) /* Interrupt */ RegWrite(0x16, 0x00); // Disable all interrupts RegWrite(0x17, 0x00); // RegWrite(0x20, 0x00); // int1_od(PP), int1_lvl(Low active), int2_od(PP), int2_lvl(Low active) RegWrite(0x21, 0x80); // reset_int, latch_int(non_latched) } void BMA250::ReadXYZ(int16_t *xyz) { char val[6]; /* Read raw data */ RegRead(0x02, val, sizeof(val)); xyz[0] = ((int16_t)val[1] << 8) | (val[0] & 0xC0); xyz[1] = ((int16_t)val[3] << 8) | (val[2] & 0xC0); xyz[2] = ((int16_t)val[5] << 8) | (val[4] & 0xC0); /* Align right */ xyz[0] >>= 6; xyz[1] >>= 6; xyz[2] >>= 6; } void BMA250::NewData(void(*fptr)(void), int int_n) { // TODO } void BMA250::AnyMotion(void(*fptr)(void), int int_n) { // TODO } void BMA250::TapSening(void(*fptr)(void), int int_n, bool double_tap) { RegWrite(0x2A, 0x04); // tap_quiet(30ms), tap_shock(50ms), tap_dur(250ms) RegWrite(0x2B, 0x0A); // tap_samp(2samples), tap_th(0x0A) if (int_n == 1) { /* Interrupt 1 */ if (double_tap) { /* Double tap */ RegReadModifyWrite(0x19, 0x30, 0x10); // int1_d_tap RegReadModifyWrite(0x16, 0x30, 0x10); // d_tap_en } else { /* Single tap */ RegReadModifyWrite(0x19, 0x30, 0x20); // int1_s_tap RegReadModifyWrite(0x16, 0x30, 0x20); // s_tap_en } _int1.mode(PullUp); _int1.fall(fptr); } else { /* Interrupt 2 */ if (double_tap) { /* Double tap */ RegReadModifyWrite(0x1B, 0x30, 0x10); // int2_d_tap RegReadModifyWrite(0x16, 0x30, 0x10); // d_tap_en } else { /* Single tap */ RegReadModifyWrite(0x1B, 0x30, 0x20); // int2_s_tap RegReadModifyWrite(0x16, 0x30, 0x20); // s_tap_en } _int2.mode(PullUp); _int2.fall(fptr); } } void BMA250::OrientationRecognition(void(*fptr)(void), int int_n) { // TODO } void BMA250::FlatDetection(void(*fptr)(void), int int_n) { // TODO } void BMA250::LowHighGDetection(void(*fptr)(void), int int_n, bool high_g) { // TODO } void BMA250::ShakeDetection(void(*fptr)(void), int int_n) { RegWrite(0x28, 0x64); // slope_th(100) if (int_n == 1) { /* Interrupt 1 */ RegReadModifyWrite(0x19, 0x04, 0x04); // int1_slope RegReadModifyWrite(0x16, 0x07, 0x07); // slope_en_z/y/x _int1.mode(PullUp); _int1.fall(fptr); } else { /* Interrupt 2 */ RegReadModifyWrite(0x1B, 0x04, 0x04); // int2_slope RegReadModifyWrite(0x16, 0x07, 0x07); // slope_en_z/y/x _int2.mode(PullUp); _int2.fall(fptr); } } void BMA250::EnterStandbyMode(void) { RegReadModifyWrite(0x12, 0x40, 0x40); // lowpower_mode(1) RegReadModifyWrite(0x11, 0x80, 0x80); // suspend(1) } void BMA250::LeaveStandbyMode(void) { RegReadModifyWrite(0x12, 0x40, 0x40); // lowpower_mode(1) RegReadModifyWrite(0x11, 0x80, 0x00); // suspend(0) } void BMA250::RegWrite(char reg, char val) { char data[2]; data[0] = reg; data[1] = val; _i2c.write(BMA250_SLAVE_ADDR, data, 2, 0); } void BMA250::RegRead(char reg, char *val, int len) { _i2c.write(BMA250_SLAVE_ADDR, ®, 1, 0); _i2c.read (BMA250_SLAVE_ADDR, val, len); } void BMA250::RegReadModifyWrite(char reg, char clr_mask, char set_mask) { char val; RegRead (reg, &val, 1); // Read val = (val & ~clr_mask) | set_mask; // Modify RegWrite(reg, val); // Write }