Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ADXL345_I2C BLE_API mbed nRF51822
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 #include "mbed.h" 00021 #include "ble/BLE.h" 00022 #include "ADXL345_I2C/ADXL345_I2C.h" 00023 00024 #define TXRX_BUF_LEN 20 00025 #define LOCAL_NAME "4WD" 00026 #define NOTIFY_INTERVAL 200 00027 #define MEASURE_INTERVAL 10 00028 00029 ADXL345_I2C accelerometer(P0_10, P0_8); 00030 BLE ble; 00031 DigitalOut led(P0_19); 00032 00033 static const uint8_t uart_base_uuid[] = {0xc8, 0xd6, 0xea, 0x62, 0x40, 0x76, 0x45, 0x61, 0xbf, 0xc8, 0x90, 0xaf, 0x4e, 0xd7, 0x8f, 0x4a}; 00034 static const uint8_t uart_tx_uuid[] = {0xd5, 0x31, 0x16, 0xfc, 0x4b, 0x96, 0x44, 0x46, 0xb8, 0x9f, 0xd9, 0x60, 0x00, 0x1c, 0xea, 0x91}; 00035 static const uint8_t uart_rx_uuid[] = {0xd9, 0x46, 0x28, 0x6e, 0xb6, 0xae, 0x4e, 0x75, 0x8c, 0x8d, 0xcb, 0x18, 0x43, 0x4a, 0x13, 0x98}; 00036 static uint8_t uart_base_uuid_rev[] = {0x4a, 0x8f, 0xd7, 0x4e, 0xaf, 0x90, 0xc8, 0xbf, 0x61, 0x45, 0x76, 0x40, 0x62, 0xea, 0xd6, 0xc8}; 00037 00038 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00039 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00040 00041 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00042 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00043 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00044 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00045 00046 struct Vector3 { 00047 float x; 00048 float y; 00049 float z; 00050 Vector3():x(0),y(0),z(0){} 00051 void clear(){x=y=z=0;} 00052 }; 00053 00054 struct NotifyData { 00055 float x; 00056 float y; 00057 float z; 00058 float speed; 00059 }; 00060 00061 NotifyData notifyData; 00062 00063 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t* disconnectionCallback) { 00064 ble.startAdvertising(); 00065 } 00066 00067 void WrittenHandler(const GattWriteCallbackParams *Handler) { 00068 } 00069 00070 void notifyValues() { 00071 uint8_t buf[20]; 00072 memcpy(buf, ¬ifyData, sizeof(NotifyData)); 00073 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, sizeof(NotifyData)); 00074 } 00075 00076 Vector3 rawValue; 00077 int rawCount = 0; 00078 00079 void measure() { 00080 int readings[3] = {0, 0, 0}; 00081 accelerometer.getOutput(readings); 00082 // accelerometerの値は16bitの2の補数表現なので、自力で負数に変換する 00083 for (int i = 0; i < 3; i++) { 00084 if ((readings[i] & 0x8000) != 0) { 00085 readings[i] = -((~(readings[i] & 0x7FFF)) & 0x7FFF); 00086 } 00087 } 00088 00089 rawValue.x += readings[0]; 00090 rawValue.y += readings[1]; 00091 rawValue.z += readings[2]; 00092 rawCount++; 00093 00094 if (rawCount >= NOTIFY_INTERVAL/MEASURE_INTERVAL) { 00095 notifyData.x = rawValue.x / 255 / rawCount; // G 00096 notifyData.y = rawValue.y / 255 / rawCount; // G 00097 notifyData.z = rawValue.z / 255 / rawCount; // G 00098 notifyData.speed += (rawValue.x / 255.0 * 9.8 * 3.6 / 2 / MEASURE_INTERVAL) - notifyData.speed; // km/h 00099 00100 rawValue.clear(); 00101 rawCount = 0; 00102 } 00103 } 00104 00105 int main(void) { 00106 00107 memset(¬ifyData, 0, sizeof(NotifyData)); 00108 00109 Ticker ticker; 00110 ticker.attach_us(notifyValues, NOTIFY_INTERVAL * 1000); 00111 00112 Ticker ticker2; 00113 ticker2.attach_us(measure, MEASURE_INTERVAL * 1000); 00114 00115 // Go into standby mode to configure the device. 00116 accelerometer.setPowerControl(0x00); 00117 // Full resolution, +/-16g, 4mg/LSB. 00118 accelerometer.setDataFormatControl(0x0B); 00119 // 100Hz data rate.(10msec) 00120 accelerometer.setDataRate(ADXL345_100HZ); 00121 // Measurement mode. 00122 accelerometer.setPowerControl(0x08); 00123 00124 ble.init(); 00125 ble.onDisconnection(disconnectionCallback); 00126 ble.onDataWritten(WrittenHandler); 00127 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00128 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00129 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00130 (const uint8_t *)LOCAL_NAME, sizeof(LOCAL_NAME) - 1); 00131 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00132 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00133 ble.setDeviceName(LOCAL_NAME); 00134 ble.setAdvertisingInterval(1000/0.625); 00135 ble.addService(uartService); 00136 ble.startAdvertising(); 00137 led = 0; 00138 00139 while(1) { 00140 ble.waitForEvent(); 00141 } 00142 }
Generated on Sat Jul 23 2022 21:50:39 by
1.7.2