
key lock and rear light
Dependencies: BLE_API BMA250E mbed nRF51822
MtConnect04S Bike Proximity Lock
We have full tutorial, please visit our blog
Revision 2:7920d3227906, committed 2016-12-19
- Comitter:
- mtmkimi
- Date:
- Mon Dec 19 11:06:00 2016 +0000
- Parent:
- 1:9f5ecbaa7606
- Commit message:
- Pack bma250e into library.
Changed in this revision
diff -r 9f5ecbaa7606 -r 7920d3227906 BMA250/BMA250.cpp --- a/BMA250/BMA250.cpp Thu Nov 24 07:55:46 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,127 +0,0 @@ -#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 -}
diff -r 9f5ecbaa7606 -r 7920d3227906 BMA250/BMA250.h --- a/BMA250/BMA250.h Thu Nov 24 07:55:46 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -#ifndef BMA250_h -#define BMA250_h - -#include "mbed.h" - - -#define BMA250_SLAVE_ADDR 0x30 - - -class BMA250 { -public: - BMA250(PinName sda, PinName scl, PinName int1 = NC, PinName int2 = NC); - - void ReadXYZ(int16_t *xyz); - - void NewData (void(*fptr)(void), int int_n); - void AnyMotion (void(*fptr)(void), int int_n); - void TapSening (void(*fptr)(void), int int_n, bool double_tap); - void OrientationRecognition (void(*fptr)(void), int int_n); - void FlatDetection (void(*fptr)(void), int int_n); - void LowHighGDetection (void(*fptr)(void), int int_n, bool high_g); - void ShakeDetection (void(*fptr)(void), int int_n); - - void EnterStandbyMode(void); - void LeaveStandbyMode(void); - -private: - I2C _i2c; - InterruptIn _int1; - InterruptIn _int2; - - void RegWrite(char reg, char val); - void RegRead (char reg, char *val, int len); - void RegReadModifyWrite(char reg, char clr_mask, char set_mask); -}; - -#endif
diff -r 9f5ecbaa7606 -r 7920d3227906 BMA250E.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BMA250E.lib Mon Dec 19 11:06:00 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/MtM/code/BMA250E/#8273e9b80c39
diff -r 9f5ecbaa7606 -r 7920d3227906 main.cpp --- a/main.cpp Thu Nov 24 07:55:46 2016 +0000 +++ b/main.cpp Mon Dec 19 11:06:00 2016 +0000 @@ -1,8 +1,23 @@ - +/* Copyright (c) 2016 MtM Technology Corporation, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ #include "mbed.h" #include "ble/BLE.h" -#include "BMA250.h" - +#include "BMA250E.h" /* UART printf */ Serial pc(p5, p4); @@ -14,13 +29,13 @@ PwmOut bz(p1); /* Sensor */ -BMA250 bma250(p14, p13, p0, NC); +BMA250E acclerameter(p14, p13, p0, NC); /* UUID, Device name */ -uint16_t syncServUUID = 0x180A; /* Synchronize the status of Rear light with Head light */ +uint16_t syncServUUID = GattService::UUID_DEVICE_INFORMATION_SERVICE; /* Synchronize the status of Rear light with Head light */ uint16_t syncCharUUID = 0x2B03; -static const char DEVICE_NAME[] = "ASE_proximity"; //"mbed Proximity"; -static const uint16_t uuid16_list[] = { 0x180A }; +static const char DEVICE_NAME[] = "MtM Proximity"; +static const uint16_t uuid16_list[] = { syncServUUID }; /* Setup custom characteristics */ uint8_t syncPayload[1]; @@ -32,7 +47,6 @@ GattCharacteristic *characteristics[] = {&syncChar}; GattService syncServ(syncServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); - void rl_on(void) { rl = 1; @@ -65,7 +79,7 @@ { pc.printf("connection\n"); - bma250.EnterStandbyMode(); // sensor enters standby mode + acclerameter.EnterStandbyMode(); // sensor enters standby mode /* flash and beep 1 times */ rl_on(); @@ -98,7 +112,7 @@ rl_off(); bz_off(); - bma250.LeaveStandbyMode(); // sensor leaves standby mode (enters normal mode) + acclerameter.LeaveStandbyMode(); // sensor leaves standby mode (enters normal mode) } void dataWrittenCallback(const GattWriteCallbackParams *params) @@ -177,7 +191,7 @@ wait(0.8); } -void BMA250_int1Callback() { +void AcclerameterOnShaked() { pc.printf("BMA250_int1\n"); #if 1 @@ -199,6 +213,7 @@ return; } + ble.setDeviceName((const uint8_t *)DEVICE_NAME); ble.gap().onConnection(connectionCallback); ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(dataWrittenCallback); @@ -232,7 +247,7 @@ bz_off(); /* Config sensor */ - bma250.ShakeDetection(&BMA250_int1Callback, 1); + acclerameter.ShakeDetection(&AcclerameterOnShaked); /* Main loop */ while (1) {