MtM+ / Mbed 2 deprecated MtConnect04S_Bike_Proximity

Dependencies:   BLE_API BMA250E mbed nRF51822

Committer:
bcc6
Date:
Thu Nov 24 07:55:46 2016 +0000
Revision:
1:9f5ecbaa7606
Parent:
0:3eeefb743ce3
Child:
2:7920d3227906
fix key tone

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcc6 0:3eeefb743ce3 1
bcc6 0:3eeefb743ce3 2 #include "mbed.h"
bcc6 0:3eeefb743ce3 3 #include "ble/BLE.h"
bcc6 0:3eeefb743ce3 4 #include "BMA250.h"
bcc6 0:3eeefb743ce3 5
bcc6 0:3eeefb743ce3 6
bcc6 0:3eeefb743ce3 7 /* UART printf */
bcc6 0:3eeefb743ce3 8 Serial pc(p5, p4);
bcc6 0:3eeefb743ce3 9
bcc6 0:3eeefb743ce3 10 /* Rear light */
bcc6 0:3eeefb743ce3 11 DigitalOut rl(p16, 0);
bcc6 0:3eeefb743ce3 12
bcc6 0:3eeefb743ce3 13 /* Buzzer */
bcc6 0:3eeefb743ce3 14 PwmOut bz(p1);
bcc6 0:3eeefb743ce3 15
bcc6 0:3eeefb743ce3 16 /* Sensor */
bcc6 0:3eeefb743ce3 17 BMA250 bma250(p14, p13, p0, NC);
bcc6 0:3eeefb743ce3 18
bcc6 0:3eeefb743ce3 19 /* UUID, Device name */
bcc6 0:3eeefb743ce3 20 uint16_t syncServUUID = 0x180A; /* Synchronize the status of Rear light with Head light */
bcc6 0:3eeefb743ce3 21 uint16_t syncCharUUID = 0x2B03;
bcc6 0:3eeefb743ce3 22 static const char DEVICE_NAME[] = "ASE_proximity"; //"mbed Proximity";
bcc6 0:3eeefb743ce3 23 static const uint16_t uuid16_list[] = { 0x180A };
bcc6 0:3eeefb743ce3 24
bcc6 0:3eeefb743ce3 25 /* Setup custom characteristics */
bcc6 0:3eeefb743ce3 26 uint8_t syncPayload[1];
bcc6 0:3eeefb743ce3 27 GattCharacteristic syncChar( syncCharUUID, syncPayload,
bcc6 0:3eeefb743ce3 28 sizeof(syncPayload), sizeof(syncPayload),
bcc6 0:3eeefb743ce3 29 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
bcc6 0:3eeefb743ce3 30
bcc6 0:3eeefb743ce3 31 /* Setup custom service */
bcc6 0:3eeefb743ce3 32 GattCharacteristic *characteristics[] = {&syncChar};
bcc6 0:3eeefb743ce3 33 GattService syncServ(syncServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
bcc6 0:3eeefb743ce3 34
bcc6 0:3eeefb743ce3 35
bcc6 0:3eeefb743ce3 36 void rl_on(void)
bcc6 0:3eeefb743ce3 37 {
bcc6 0:3eeefb743ce3 38 rl = 1;
bcc6 0:3eeefb743ce3 39 }
bcc6 0:3eeefb743ce3 40
bcc6 0:3eeefb743ce3 41 void rl_off(void)
bcc6 0:3eeefb743ce3 42 {
bcc6 0:3eeefb743ce3 43 rl = 0;
bcc6 0:3eeefb743ce3 44 }
bcc6 0:3eeefb743ce3 45
bcc6 1:9f5ecbaa7606 46 void bz_on_494hz(void)
bcc6 0:3eeefb743ce3 47 {
bcc6 1:9f5ecbaa7606 48 bz.period(1.0f / 494);
bcc6 1:9f5ecbaa7606 49 bz.write(0.5);
bcc6 1:9f5ecbaa7606 50 }
bcc6 1:9f5ecbaa7606 51
bcc6 1:9f5ecbaa7606 52 void bz_on_988hz(void)
bcc6 1:9f5ecbaa7606 53 {
bcc6 1:9f5ecbaa7606 54 bz.period(1.0f / 988);
bcc6 0:3eeefb743ce3 55 bz.write(0.5);
bcc6 0:3eeefb743ce3 56 }
bcc6 0:3eeefb743ce3 57
bcc6 0:3eeefb743ce3 58 void bz_off(void)
bcc6 0:3eeefb743ce3 59 {
bcc6 1:9f5ecbaa7606 60 bz.period(0);
bcc6 1:9f5ecbaa7606 61 bz.write(1); // output low
bcc6 0:3eeefb743ce3 62 }
bcc6 0:3eeefb743ce3 63
bcc6 0:3eeefb743ce3 64 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
bcc6 0:3eeefb743ce3 65 {
bcc6 0:3eeefb743ce3 66 pc.printf("connection\n");
bcc6 0:3eeefb743ce3 67
bcc6 0:3eeefb743ce3 68 bma250.EnterStandbyMode(); // sensor enters standby mode
bcc6 0:3eeefb743ce3 69
bcc6 0:3eeefb743ce3 70 /* flash and beep 1 times */
bcc6 0:3eeefb743ce3 71 rl_on();
bcc6 1:9f5ecbaa7606 72 bz_on_988hz();
bcc6 0:3eeefb743ce3 73 wait(0.5);
bcc6 0:3eeefb743ce3 74 rl_off();
bcc6 0:3eeefb743ce3 75 bz_off();
bcc6 0:3eeefb743ce3 76 wait(0.5);
bcc6 0:3eeefb743ce3 77 }
bcc6 0:3eeefb743ce3 78
bcc6 0:3eeefb743ce3 79 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
bcc6 0:3eeefb743ce3 80 {
bcc6 0:3eeefb743ce3 81 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
bcc6 0:3eeefb743ce3 82
bcc6 0:3eeefb743ce3 83 pc.printf("disconnection\n");
bcc6 0:3eeefb743ce3 84
bcc6 0:3eeefb743ce3 85 /* flash and beep 2 times */
bcc6 0:3eeefb743ce3 86 rl_off();
bcc6 0:3eeefb743ce3 87 bz_off();
bcc6 0:3eeefb743ce3 88 wait(0.5);
bcc6 0:3eeefb743ce3 89 rl_on();
bcc6 1:9f5ecbaa7606 90 bz_on_988hz();
bcc6 0:3eeefb743ce3 91 wait(0.5);
bcc6 0:3eeefb743ce3 92 rl_off();
bcc6 0:3eeefb743ce3 93 bz_off();
bcc6 0:3eeefb743ce3 94 wait(0.5);
bcc6 0:3eeefb743ce3 95 rl_on();
bcc6 1:9f5ecbaa7606 96 bz_on_988hz();
bcc6 0:3eeefb743ce3 97 wait(0.5);
bcc6 0:3eeefb743ce3 98 rl_off();
bcc6 0:3eeefb743ce3 99 bz_off();
bcc6 0:3eeefb743ce3 100
bcc6 0:3eeefb743ce3 101 bma250.LeaveStandbyMode(); // sensor leaves standby mode (enters normal mode)
bcc6 0:3eeefb743ce3 102 }
bcc6 0:3eeefb743ce3 103
bcc6 0:3eeefb743ce3 104 void dataWrittenCallback(const GattWriteCallbackParams *params)
bcc6 0:3eeefb743ce3 105 {
bcc6 0:3eeefb743ce3 106 pc.printf("dataWritten\n");
bcc6 0:3eeefb743ce3 107
bcc6 0:3eeefb743ce3 108 if ((params->handle == syncChar.getValueHandle()) && (params->len == 1)) {
bcc6 0:3eeefb743ce3 109 pc.printf("data(%d)\n", *(params->data));
bcc6 0:3eeefb743ce3 110
bcc6 0:3eeefb743ce3 111 uint8_t head_light_status = *(params->data);
bcc6 0:3eeefb743ce3 112 if (head_light_status) rl_on();
bcc6 0:3eeefb743ce3 113 else rl_off();
bcc6 0:3eeefb743ce3 114 }
bcc6 0:3eeefb743ce3 115 }
bcc6 0:3eeefb743ce3 116
bcc6 1:9f5ecbaa7606 117 void _alarm(void)
bcc6 1:9f5ecbaa7606 118 {
bcc6 1:9f5ecbaa7606 119 for(int i=0; i<3; i++){
bcc6 1:9f5ecbaa7606 120 rl_on();
bcc6 1:9f5ecbaa7606 121 bz_on_494hz();
bcc6 1:9f5ecbaa7606 122 wait(0.5);
bcc6 1:9f5ecbaa7606 123
bcc6 1:9f5ecbaa7606 124 rl_off();
bcc6 1:9f5ecbaa7606 125 bz_on_988hz();
bcc6 1:9f5ecbaa7606 126 wait(0.5);
bcc6 1:9f5ecbaa7606 127 }
bcc6 1:9f5ecbaa7606 128 }
bcc6 1:9f5ecbaa7606 129
bcc6 1:9f5ecbaa7606 130 void _two_tiger(void)
bcc6 1:9f5ecbaa7606 131 {
bcc6 1:9f5ecbaa7606 132 bz.period(1.0f / 262); //do
bcc6 1:9f5ecbaa7606 133 bz.write(0.5);
bcc6 1:9f5ecbaa7606 134 wait(0.4);
bcc6 1:9f5ecbaa7606 135 bz.period(1.0f / 294); //re
bcc6 1:9f5ecbaa7606 136 bz.write(0.5);
bcc6 1:9f5ecbaa7606 137 wait(0.4);
bcc6 1:9f5ecbaa7606 138 bz.period(1.0f / 330); //mi
bcc6 1:9f5ecbaa7606 139 bz.write(0.5);
bcc6 1:9f5ecbaa7606 140 wait(0.4);
bcc6 1:9f5ecbaa7606 141 bz.period(1.0f / 262); //do
bcc6 1:9f5ecbaa7606 142 bz.write(0.5);
bcc6 1:9f5ecbaa7606 143 wait(0.4);
bcc6 1:9f5ecbaa7606 144
bcc6 1:9f5ecbaa7606 145 bz.period(1.0f / 262); //do
bcc6 1:9f5ecbaa7606 146 bz.write(0.5);
bcc6 1:9f5ecbaa7606 147 wait(0.4);
bcc6 1:9f5ecbaa7606 148 bz.period(1.0f / 294); //re
bcc6 1:9f5ecbaa7606 149 bz.write(0.5);
bcc6 1:9f5ecbaa7606 150 wait(0.4);
bcc6 1:9f5ecbaa7606 151 bz.period(1.0f / 330); //mi
bcc6 1:9f5ecbaa7606 152 bz.write(0.5);
bcc6 1:9f5ecbaa7606 153 wait(0.4);
bcc6 1:9f5ecbaa7606 154 bz.period(1.0f / 262); //do
bcc6 1:9f5ecbaa7606 155 bz.write(0.5);
bcc6 1:9f5ecbaa7606 156 wait(0.4);
bcc6 1:9f5ecbaa7606 157
bcc6 1:9f5ecbaa7606 158
bcc6 1:9f5ecbaa7606 159 bz.period(1.0f / 330); //mi
bcc6 1:9f5ecbaa7606 160 bz.write(0.5);
bcc6 1:9f5ecbaa7606 161 wait(0.4);
bcc6 1:9f5ecbaa7606 162 bz.period(1.0f / 349); //fa
bcc6 1:9f5ecbaa7606 163 bz.write(0.5);
bcc6 1:9f5ecbaa7606 164 wait(0.4);
bcc6 1:9f5ecbaa7606 165 bz.period(1.0f / 392); //so
bcc6 1:9f5ecbaa7606 166 bz.write(0.5);
bcc6 1:9f5ecbaa7606 167 wait(0.8);
bcc6 1:9f5ecbaa7606 168
bcc6 1:9f5ecbaa7606 169 bz.period(1.0f / 330); //mi
bcc6 1:9f5ecbaa7606 170 bz.write(0.5);
bcc6 1:9f5ecbaa7606 171 wait(0.4);
bcc6 1:9f5ecbaa7606 172 bz.period(1.0f / 349); //fa
bcc6 1:9f5ecbaa7606 173 bz.write(0.5);
bcc6 1:9f5ecbaa7606 174 wait(0.4);
bcc6 1:9f5ecbaa7606 175 bz.period(1.0f / 392); //so
bcc6 1:9f5ecbaa7606 176 bz.write(0.5);
bcc6 1:9f5ecbaa7606 177 wait(0.8);
bcc6 1:9f5ecbaa7606 178 }
bcc6 1:9f5ecbaa7606 179
bcc6 0:3eeefb743ce3 180 void BMA250_int1Callback() {
bcc6 0:3eeefb743ce3 181 pc.printf("BMA250_int1\n");
bcc6 0:3eeefb743ce3 182
bcc6 1:9f5ecbaa7606 183 #if 1
bcc6 1:9f5ecbaa7606 184 _alarm();
bcc6 1:9f5ecbaa7606 185 #else
bcc6 1:9f5ecbaa7606 186 _two_tiger();
bcc6 1:9f5ecbaa7606 187 #endif
bcc6 1:9f5ecbaa7606 188
bcc6 0:3eeefb743ce3 189 rl_off();
bcc6 0:3eeefb743ce3 190 bz_off();
bcc6 0:3eeefb743ce3 191 }
bcc6 0:3eeefb743ce3 192
bcc6 0:3eeefb743ce3 193 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
bcc6 0:3eeefb743ce3 194 {
bcc6 0:3eeefb743ce3 195 BLE &ble = params->ble;
bcc6 0:3eeefb743ce3 196 ble_error_t error = params->error;
bcc6 0:3eeefb743ce3 197
bcc6 0:3eeefb743ce3 198 if (error != BLE_ERROR_NONE) {
bcc6 0:3eeefb743ce3 199 return;
bcc6 0:3eeefb743ce3 200 }
bcc6 0:3eeefb743ce3 201
bcc6 0:3eeefb743ce3 202 ble.gap().onConnection(connectionCallback);
bcc6 0:3eeefb743ce3 203 ble.gap().onDisconnection(disconnectionCallback);
bcc6 0:3eeefb743ce3 204 ble.gattServer().onDataWritten(dataWrittenCallback);
bcc6 0:3eeefb743ce3 205
bcc6 0:3eeefb743ce3 206 /* Setup primary service. */
bcc6 0:3eeefb743ce3 207 ble.addService(syncServ);
bcc6 0:3eeefb743ce3 208
bcc6 0:3eeefb743ce3 209 /* Setup advertising. */
bcc6 0:3eeefb743ce3 210 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
bcc6 0:3eeefb743ce3 211 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
bcc6 0:3eeefb743ce3 212 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG);
bcc6 0:3eeefb743ce3 213 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
bcc6 0:3eeefb743ce3 214 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
bcc6 0:3eeefb743ce3 215 ble.gap().setAdvertisingInterval(500); /* 500ms */
bcc6 0:3eeefb743ce3 216 ble.gap().startAdvertising();
bcc6 0:3eeefb743ce3 217 }
bcc6 0:3eeefb743ce3 218
bcc6 0:3eeefb743ce3 219 int main(void)
bcc6 0:3eeefb743ce3 220 {
bcc6 0:3eeefb743ce3 221 /* Force to disable the hardware flow control of Serial */
bcc6 0:3eeefb743ce3 222 *((uint32_t *)(0x40002000+0x56C)) = 0;
bcc6 0:3eeefb743ce3 223 pc.printf("~ Hell World ~\n");
bcc6 0:3eeefb743ce3 224
bcc6 0:3eeefb743ce3 225 /* Init BLE */
bcc6 0:3eeefb743ce3 226 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
bcc6 0:3eeefb743ce3 227 ble.init(bleInitComplete);
bcc6 0:3eeefb743ce3 228 while (ble.hasInitialized() == false) { /* spin loop */ }
bcc6 0:3eeefb743ce3 229
bcc6 0:3eeefb743ce3 230 /* Init I/O */
bcc6 0:3eeefb743ce3 231 rl_off();
bcc6 0:3eeefb743ce3 232 bz_off();
bcc6 0:3eeefb743ce3 233
bcc6 0:3eeefb743ce3 234 /* Config sensor */
bcc6 0:3eeefb743ce3 235 bma250.ShakeDetection(&BMA250_int1Callback, 1);
bcc6 0:3eeefb743ce3 236
bcc6 0:3eeefb743ce3 237 /* Main loop */
bcc6 0:3eeefb743ce3 238 while (1) {
bcc6 0:3eeefb743ce3 239 /* low power wait for event */
bcc6 0:3eeefb743ce3 240 pc.printf("sleep\n");
bcc6 0:3eeefb743ce3 241 ble.waitForEvent();
bcc6 0:3eeefb743ce3 242 pc.printf("wakeup\n");
bcc6 0:3eeefb743ce3 243 }
bcc6 0:3eeefb743ce3 244 }