key lock and rear light
Dependencies: BLE_API BMA250E mbed nRF51822
MtConnect04S Bike Proximity Lock
We have full tutorial, please visit our blog
main.cpp@2:7920d3227906, 2016-12-19 (annotated)
- Committer:
- mtmkimi
- Date:
- Mon Dec 19 11:06:00 2016 +0000
- Revision:
- 2:7920d3227906
- Parent:
- 1:9f5ecbaa7606
Pack bma250e into library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mtmkimi | 2:7920d3227906 | 1 | /* Copyright (c) 2016 MtM Technology Corporation, MIT License |
mtmkimi | 2:7920d3227906 | 2 | * |
mtmkimi | 2:7920d3227906 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mtmkimi | 2:7920d3227906 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mtmkimi | 2:7920d3227906 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mtmkimi | 2:7920d3227906 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mtmkimi | 2:7920d3227906 | 7 | * furnished to do so, subject to the following conditions: |
mtmkimi | 2:7920d3227906 | 8 | * |
mtmkimi | 2:7920d3227906 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mtmkimi | 2:7920d3227906 | 10 | * substantial portions of the Software. |
mtmkimi | 2:7920d3227906 | 11 | * |
mtmkimi | 2:7920d3227906 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mtmkimi | 2:7920d3227906 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mtmkimi | 2:7920d3227906 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mtmkimi | 2:7920d3227906 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mtmkimi | 2:7920d3227906 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mtmkimi | 2:7920d3227906 | 17 | */ |
bcc6 | 0:3eeefb743ce3 | 18 | #include "mbed.h" |
bcc6 | 0:3eeefb743ce3 | 19 | #include "ble/BLE.h" |
mtmkimi | 2:7920d3227906 | 20 | #include "BMA250E.h" |
bcc6 | 0:3eeefb743ce3 | 21 | |
bcc6 | 0:3eeefb743ce3 | 22 | /* UART printf */ |
bcc6 | 0:3eeefb743ce3 | 23 | Serial pc(p5, p4); |
bcc6 | 0:3eeefb743ce3 | 24 | |
bcc6 | 0:3eeefb743ce3 | 25 | /* Rear light */ |
bcc6 | 0:3eeefb743ce3 | 26 | DigitalOut rl(p16, 0); |
bcc6 | 0:3eeefb743ce3 | 27 | |
bcc6 | 0:3eeefb743ce3 | 28 | /* Buzzer */ |
bcc6 | 0:3eeefb743ce3 | 29 | PwmOut bz(p1); |
bcc6 | 0:3eeefb743ce3 | 30 | |
bcc6 | 0:3eeefb743ce3 | 31 | /* Sensor */ |
mtmkimi | 2:7920d3227906 | 32 | BMA250E acclerameter(p14, p13, p0, NC); |
bcc6 | 0:3eeefb743ce3 | 33 | |
bcc6 | 0:3eeefb743ce3 | 34 | /* UUID, Device name */ |
mtmkimi | 2:7920d3227906 | 35 | uint16_t syncServUUID = GattService::UUID_DEVICE_INFORMATION_SERVICE; /* Synchronize the status of Rear light with Head light */ |
bcc6 | 0:3eeefb743ce3 | 36 | uint16_t syncCharUUID = 0x2B03; |
mtmkimi | 2:7920d3227906 | 37 | static const char DEVICE_NAME[] = "MtM Proximity"; |
mtmkimi | 2:7920d3227906 | 38 | static const uint16_t uuid16_list[] = { syncServUUID }; |
bcc6 | 0:3eeefb743ce3 | 39 | |
bcc6 | 0:3eeefb743ce3 | 40 | /* Setup custom characteristics */ |
bcc6 | 0:3eeefb743ce3 | 41 | uint8_t syncPayload[1]; |
bcc6 | 0:3eeefb743ce3 | 42 | GattCharacteristic syncChar( syncCharUUID, syncPayload, |
bcc6 | 0:3eeefb743ce3 | 43 | sizeof(syncPayload), sizeof(syncPayload), |
bcc6 | 0:3eeefb743ce3 | 44 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
bcc6 | 0:3eeefb743ce3 | 45 | |
bcc6 | 0:3eeefb743ce3 | 46 | /* Setup custom service */ |
bcc6 | 0:3eeefb743ce3 | 47 | GattCharacteristic *characteristics[] = {&syncChar}; |
bcc6 | 0:3eeefb743ce3 | 48 | GattService syncServ(syncServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
bcc6 | 0:3eeefb743ce3 | 49 | |
bcc6 | 0:3eeefb743ce3 | 50 | void rl_on(void) |
bcc6 | 0:3eeefb743ce3 | 51 | { |
bcc6 | 0:3eeefb743ce3 | 52 | rl = 1; |
bcc6 | 0:3eeefb743ce3 | 53 | } |
bcc6 | 0:3eeefb743ce3 | 54 | |
bcc6 | 0:3eeefb743ce3 | 55 | void rl_off(void) |
bcc6 | 0:3eeefb743ce3 | 56 | { |
bcc6 | 0:3eeefb743ce3 | 57 | rl = 0; |
bcc6 | 0:3eeefb743ce3 | 58 | } |
bcc6 | 0:3eeefb743ce3 | 59 | |
bcc6 | 1:9f5ecbaa7606 | 60 | void bz_on_494hz(void) |
bcc6 | 0:3eeefb743ce3 | 61 | { |
bcc6 | 1:9f5ecbaa7606 | 62 | bz.period(1.0f / 494); |
bcc6 | 1:9f5ecbaa7606 | 63 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 64 | } |
bcc6 | 1:9f5ecbaa7606 | 65 | |
bcc6 | 1:9f5ecbaa7606 | 66 | void bz_on_988hz(void) |
bcc6 | 1:9f5ecbaa7606 | 67 | { |
bcc6 | 1:9f5ecbaa7606 | 68 | bz.period(1.0f / 988); |
bcc6 | 0:3eeefb743ce3 | 69 | bz.write(0.5); |
bcc6 | 0:3eeefb743ce3 | 70 | } |
bcc6 | 0:3eeefb743ce3 | 71 | |
bcc6 | 0:3eeefb743ce3 | 72 | void bz_off(void) |
bcc6 | 0:3eeefb743ce3 | 73 | { |
bcc6 | 1:9f5ecbaa7606 | 74 | bz.period(0); |
bcc6 | 1:9f5ecbaa7606 | 75 | bz.write(1); // output low |
bcc6 | 0:3eeefb743ce3 | 76 | } |
bcc6 | 0:3eeefb743ce3 | 77 | |
bcc6 | 0:3eeefb743ce3 | 78 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
bcc6 | 0:3eeefb743ce3 | 79 | { |
bcc6 | 0:3eeefb743ce3 | 80 | pc.printf("connection\n"); |
bcc6 | 0:3eeefb743ce3 | 81 | |
mtmkimi | 2:7920d3227906 | 82 | acclerameter.EnterStandbyMode(); // sensor enters standby mode |
bcc6 | 0:3eeefb743ce3 | 83 | |
bcc6 | 0:3eeefb743ce3 | 84 | /* flash and beep 1 times */ |
bcc6 | 0:3eeefb743ce3 | 85 | rl_on(); |
bcc6 | 1:9f5ecbaa7606 | 86 | bz_on_988hz(); |
bcc6 | 0:3eeefb743ce3 | 87 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 88 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 89 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 90 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 91 | } |
bcc6 | 0:3eeefb743ce3 | 92 | |
bcc6 | 0:3eeefb743ce3 | 93 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
bcc6 | 0:3eeefb743ce3 | 94 | { |
bcc6 | 0:3eeefb743ce3 | 95 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
bcc6 | 0:3eeefb743ce3 | 96 | |
bcc6 | 0:3eeefb743ce3 | 97 | pc.printf("disconnection\n"); |
bcc6 | 0:3eeefb743ce3 | 98 | |
bcc6 | 0:3eeefb743ce3 | 99 | /* flash and beep 2 times */ |
bcc6 | 0:3eeefb743ce3 | 100 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 101 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 102 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 103 | rl_on(); |
bcc6 | 1:9f5ecbaa7606 | 104 | bz_on_988hz(); |
bcc6 | 0:3eeefb743ce3 | 105 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 106 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 107 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 108 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 109 | rl_on(); |
bcc6 | 1:9f5ecbaa7606 | 110 | bz_on_988hz(); |
bcc6 | 0:3eeefb743ce3 | 111 | wait(0.5); |
bcc6 | 0:3eeefb743ce3 | 112 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 113 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 114 | |
mtmkimi | 2:7920d3227906 | 115 | acclerameter.LeaveStandbyMode(); // sensor leaves standby mode (enters normal mode) |
bcc6 | 0:3eeefb743ce3 | 116 | } |
bcc6 | 0:3eeefb743ce3 | 117 | |
bcc6 | 0:3eeefb743ce3 | 118 | void dataWrittenCallback(const GattWriteCallbackParams *params) |
bcc6 | 0:3eeefb743ce3 | 119 | { |
bcc6 | 0:3eeefb743ce3 | 120 | pc.printf("dataWritten\n"); |
bcc6 | 0:3eeefb743ce3 | 121 | |
bcc6 | 0:3eeefb743ce3 | 122 | if ((params->handle == syncChar.getValueHandle()) && (params->len == 1)) { |
bcc6 | 0:3eeefb743ce3 | 123 | pc.printf("data(%d)\n", *(params->data)); |
bcc6 | 0:3eeefb743ce3 | 124 | |
bcc6 | 0:3eeefb743ce3 | 125 | uint8_t head_light_status = *(params->data); |
bcc6 | 0:3eeefb743ce3 | 126 | if (head_light_status) rl_on(); |
bcc6 | 0:3eeefb743ce3 | 127 | else rl_off(); |
bcc6 | 0:3eeefb743ce3 | 128 | } |
bcc6 | 0:3eeefb743ce3 | 129 | } |
bcc6 | 0:3eeefb743ce3 | 130 | |
bcc6 | 1:9f5ecbaa7606 | 131 | void _alarm(void) |
bcc6 | 1:9f5ecbaa7606 | 132 | { |
bcc6 | 1:9f5ecbaa7606 | 133 | for(int i=0; i<3; i++){ |
bcc6 | 1:9f5ecbaa7606 | 134 | rl_on(); |
bcc6 | 1:9f5ecbaa7606 | 135 | bz_on_494hz(); |
bcc6 | 1:9f5ecbaa7606 | 136 | wait(0.5); |
bcc6 | 1:9f5ecbaa7606 | 137 | |
bcc6 | 1:9f5ecbaa7606 | 138 | rl_off(); |
bcc6 | 1:9f5ecbaa7606 | 139 | bz_on_988hz(); |
bcc6 | 1:9f5ecbaa7606 | 140 | wait(0.5); |
bcc6 | 1:9f5ecbaa7606 | 141 | } |
bcc6 | 1:9f5ecbaa7606 | 142 | } |
bcc6 | 1:9f5ecbaa7606 | 143 | |
bcc6 | 1:9f5ecbaa7606 | 144 | void _two_tiger(void) |
bcc6 | 1:9f5ecbaa7606 | 145 | { |
bcc6 | 1:9f5ecbaa7606 | 146 | bz.period(1.0f / 262); //do |
bcc6 | 1:9f5ecbaa7606 | 147 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 148 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 149 | bz.period(1.0f / 294); //re |
bcc6 | 1:9f5ecbaa7606 | 150 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 151 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 152 | bz.period(1.0f / 330); //mi |
bcc6 | 1:9f5ecbaa7606 | 153 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 154 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 155 | bz.period(1.0f / 262); //do |
bcc6 | 1:9f5ecbaa7606 | 156 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 157 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 158 | |
bcc6 | 1:9f5ecbaa7606 | 159 | bz.period(1.0f / 262); //do |
bcc6 | 1:9f5ecbaa7606 | 160 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 161 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 162 | bz.period(1.0f / 294); //re |
bcc6 | 1:9f5ecbaa7606 | 163 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 164 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 165 | bz.period(1.0f / 330); //mi |
bcc6 | 1:9f5ecbaa7606 | 166 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 167 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 168 | bz.period(1.0f / 262); //do |
bcc6 | 1:9f5ecbaa7606 | 169 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 170 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 171 | |
bcc6 | 1:9f5ecbaa7606 | 172 | |
bcc6 | 1:9f5ecbaa7606 | 173 | bz.period(1.0f / 330); //mi |
bcc6 | 1:9f5ecbaa7606 | 174 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 175 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 176 | bz.period(1.0f / 349); //fa |
bcc6 | 1:9f5ecbaa7606 | 177 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 178 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 179 | bz.period(1.0f / 392); //so |
bcc6 | 1:9f5ecbaa7606 | 180 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 181 | wait(0.8); |
bcc6 | 1:9f5ecbaa7606 | 182 | |
bcc6 | 1:9f5ecbaa7606 | 183 | bz.period(1.0f / 330); //mi |
bcc6 | 1:9f5ecbaa7606 | 184 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 185 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 186 | bz.period(1.0f / 349); //fa |
bcc6 | 1:9f5ecbaa7606 | 187 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 188 | wait(0.4); |
bcc6 | 1:9f5ecbaa7606 | 189 | bz.period(1.0f / 392); //so |
bcc6 | 1:9f5ecbaa7606 | 190 | bz.write(0.5); |
bcc6 | 1:9f5ecbaa7606 | 191 | wait(0.8); |
bcc6 | 1:9f5ecbaa7606 | 192 | } |
bcc6 | 1:9f5ecbaa7606 | 193 | |
mtmkimi | 2:7920d3227906 | 194 | void AcclerameterOnShaked() { |
bcc6 | 0:3eeefb743ce3 | 195 | pc.printf("BMA250_int1\n"); |
bcc6 | 0:3eeefb743ce3 | 196 | |
bcc6 | 1:9f5ecbaa7606 | 197 | #if 1 |
bcc6 | 1:9f5ecbaa7606 | 198 | _alarm(); |
bcc6 | 1:9f5ecbaa7606 | 199 | #else |
bcc6 | 1:9f5ecbaa7606 | 200 | _two_tiger(); |
bcc6 | 1:9f5ecbaa7606 | 201 | #endif |
bcc6 | 1:9f5ecbaa7606 | 202 | |
bcc6 | 0:3eeefb743ce3 | 203 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 204 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 205 | } |
bcc6 | 0:3eeefb743ce3 | 206 | |
bcc6 | 0:3eeefb743ce3 | 207 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
bcc6 | 0:3eeefb743ce3 | 208 | { |
bcc6 | 0:3eeefb743ce3 | 209 | BLE &ble = params->ble; |
bcc6 | 0:3eeefb743ce3 | 210 | ble_error_t error = params->error; |
bcc6 | 0:3eeefb743ce3 | 211 | |
bcc6 | 0:3eeefb743ce3 | 212 | if (error != BLE_ERROR_NONE) { |
bcc6 | 0:3eeefb743ce3 | 213 | return; |
bcc6 | 0:3eeefb743ce3 | 214 | } |
bcc6 | 0:3eeefb743ce3 | 215 | |
mtmkimi | 2:7920d3227906 | 216 | ble.setDeviceName((const uint8_t *)DEVICE_NAME); |
bcc6 | 0:3eeefb743ce3 | 217 | ble.gap().onConnection(connectionCallback); |
bcc6 | 0:3eeefb743ce3 | 218 | ble.gap().onDisconnection(disconnectionCallback); |
bcc6 | 0:3eeefb743ce3 | 219 | ble.gattServer().onDataWritten(dataWrittenCallback); |
bcc6 | 0:3eeefb743ce3 | 220 | |
bcc6 | 0:3eeefb743ce3 | 221 | /* Setup primary service. */ |
bcc6 | 0:3eeefb743ce3 | 222 | ble.addService(syncServ); |
bcc6 | 0:3eeefb743ce3 | 223 | |
bcc6 | 0:3eeefb743ce3 | 224 | /* Setup advertising. */ |
bcc6 | 0:3eeefb743ce3 | 225 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
bcc6 | 0:3eeefb743ce3 | 226 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
bcc6 | 0:3eeefb743ce3 | 227 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG); |
bcc6 | 0:3eeefb743ce3 | 228 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
bcc6 | 0:3eeefb743ce3 | 229 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
bcc6 | 0:3eeefb743ce3 | 230 | ble.gap().setAdvertisingInterval(500); /* 500ms */ |
bcc6 | 0:3eeefb743ce3 | 231 | ble.gap().startAdvertising(); |
bcc6 | 0:3eeefb743ce3 | 232 | } |
bcc6 | 0:3eeefb743ce3 | 233 | |
bcc6 | 0:3eeefb743ce3 | 234 | int main(void) |
bcc6 | 0:3eeefb743ce3 | 235 | { |
bcc6 | 0:3eeefb743ce3 | 236 | /* Force to disable the hardware flow control of Serial */ |
bcc6 | 0:3eeefb743ce3 | 237 | *((uint32_t *)(0x40002000+0x56C)) = 0; |
bcc6 | 0:3eeefb743ce3 | 238 | pc.printf("~ Hell World ~\n"); |
bcc6 | 0:3eeefb743ce3 | 239 | |
bcc6 | 0:3eeefb743ce3 | 240 | /* Init BLE */ |
bcc6 | 0:3eeefb743ce3 | 241 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
bcc6 | 0:3eeefb743ce3 | 242 | ble.init(bleInitComplete); |
bcc6 | 0:3eeefb743ce3 | 243 | while (ble.hasInitialized() == false) { /* spin loop */ } |
bcc6 | 0:3eeefb743ce3 | 244 | |
bcc6 | 0:3eeefb743ce3 | 245 | /* Init I/O */ |
bcc6 | 0:3eeefb743ce3 | 246 | rl_off(); |
bcc6 | 0:3eeefb743ce3 | 247 | bz_off(); |
bcc6 | 0:3eeefb743ce3 | 248 | |
bcc6 | 0:3eeefb743ce3 | 249 | /* Config sensor */ |
mtmkimi | 2:7920d3227906 | 250 | acclerameter.ShakeDetection(&AcclerameterOnShaked); |
bcc6 | 0:3eeefb743ce3 | 251 | |
bcc6 | 0:3eeefb743ce3 | 252 | /* Main loop */ |
bcc6 | 0:3eeefb743ce3 | 253 | while (1) { |
bcc6 | 0:3eeefb743ce3 | 254 | /* low power wait for event */ |
bcc6 | 0:3eeefb743ce3 | 255 | pc.printf("sleep\n"); |
bcc6 | 0:3eeefb743ce3 | 256 | ble.waitForEvent(); |
bcc6 | 0:3eeefb743ce3 | 257 | pc.printf("wakeup\n"); |
bcc6 | 0:3eeefb743ce3 | 258 | } |
bcc6 | 0:3eeefb743ce3 | 259 | } |