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: BLE_API MMA8451Q_ss mbed
Fork of FRDM_MMA8451Q by
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 #include "BLEDevice.h" 00004 00005 #define MMA8451_I2C_ADDRESS (0x1C<<1) 00006 00007 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; 00008 * it will have an impact on code-size and power consumption. */ 00009 00010 #if NEED_CONSOLE_OUTPUT 00011 Serial pc(USBTX, USBRX); 00012 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00013 #else 00014 #define DEBUG(...) /* nothing */ 00015 #endif /* #if NEED_CONSOLE_OUTPUT */ 00016 00017 PinName const SDA = p22; 00018 PinName const SCL = p20; 00019 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00020 00021 //Serial pc(USBTX, USBRX); 00022 Ticker ticker; 00023 Ticker ticker2; 00024 00025 InterruptIn button(p1); 00026 00027 const static char DEVICE_NAME[] = "HRM1017_SPEED"; 00028 static volatile bool triggerSensorPolling = false; 00029 BLEDevice ble; 00030 00031 /* Health Thermometer Service */ 00032 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 }; 00033 GattCharacteristic tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, thermTempPayload, 5, 5, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); 00034 GattCharacteristic *htmChars[] = {&tempChar, }; 00035 GattService htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars, sizeof(htmChars) / sizeof(GattCharacteristic *)); 00036 uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; 00037 00038 static Gap::ConnectionParams_t connectionParams; 00039 00040 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod 00041 { 00042 DEBUG("Disconnected handle %u, reason %u\n", handle, reason); 00043 DEBUG("Restarting the advertising process\n\r"); 00044 ble.startAdvertising(); 00045 } 00046 00047 void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) //Mod 00048 { 00049 DEBUG("connected. Got handle %u\r\n", handle); 00050 connectionParams.slaveLatency = 1; 00051 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { 00052 DEBUG("failed to update connection paramter\r\n"); 00053 } 00054 } 00055 void GetAccelterData( void ); 00056 void periodicCallback(void) 00057 { 00058 GetAccelterData(); 00059 triggerSensorPolling = true; 00060 } 00061 00062 00063 void initBLE( void ) 00064 { 00065 ticker.attach_us(periodicCallback, 10000); //10msecの周期タイマー 00066 00067 ble.init(); 00068 DEBUG("Init done\n"); 00069 ble.onDisconnection(disconnectionCallback); 00070 ble.onConnection(onConnectionCallback); 00071 00072 ble.getPreferredConnectionParams(&connectionParams); 00073 00074 /* setup advertising */ 00075 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00076 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); 00077 00078 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00079 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00080 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ //Advertisingの周期時間 0.625msec単位 00081 ble.startAdvertising(); 00082 00083 DEBUG("Start Advertising\n"); 00084 00085 ble.addService(htmService); 00086 00087 } 00088 //------------------------------------- 00089 00090 float oxyz = 0; 00091 float dxyz = 0; 00092 00093 bool f=1; 00094 float velocity = 0; 00095 float maxvelocity = 0; 00096 void GetAccelterData( void ) 00097 { 00098 float x = acc.getAccX(); 00099 float y = acc.getAccY(); 00100 float z = acc.getAccZ(); 00101 float xyz = sqrt(x*x+y*y+z*z); 00102 00103 if(f==0){ 00104 dxyz += xyz - oxyz; 00105 } 00106 f=0; 00107 velocity = dxyz*9.8; 00108 velocity *= (3600.0f/1000.0f/10.0f); 00109 oxyz = xyz; 00110 00111 pc.printf("X: %7.2f, Y: %7.2f, Z: %7.2f v:%7.2f\r\n", x, y, z, velocity); 00112 00113 if (maxvelocity < velocity){ 00114 maxvelocity = velocity; 00115 } 00116 } 00117 void InitAccelterData( void ) 00118 { 00119 DEBUG("MMA8451 ID: %d\r\n", acc.getWhoAmI()); 00120 } 00121 void onButton( void ) 00122 { 00123 DEBUG("#####\r\n"); 00124 dxyz=0; 00125 maxvelocity = 0; 00126 } 00127 00128 void updateServiceValues(void); 00129 00130 int main(void) { 00131 #if NEED_CONSOLE_OUTPUT 00132 pc.baud(115200); 00133 #endif 00134 00135 button.rise(&onButton); 00136 00137 InitAccelterData(); 00138 initBLE(); 00139 00140 while (true) { 00141 if (triggerSensorPolling) { 00142 triggerSensorPolling = false; 00143 updateServiceValues(); 00144 } else { 00145 ble.waitForEvent(); 00146 } 00147 } 00148 } 00149 00150 uint32_t quick_ieee11073_from_float(float temperature) 00151 { 00152 uint8_t exponent = 0xFF; //exponent is -1 00153 uint32_t mantissa = (uint32_t)(temperature*10); 00154 00155 return ( ((uint32_t)exponent) << 24) | mantissa; 00156 } 00157 00158 void updateServiceValues(void) 00159 { 00160 uint32_t temp_ieee11073 = quick_ieee11073_from_float(maxvelocity); 00161 memcpy(thermTempPayload+1, &temp_ieee11073, 4); 00162 ble.updateCharacteristicValue(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload)); //Mod 00163 } 00164 00165 00166 00167
Generated on Thu Jul 14 2022 19:48:35 by
1.7.2
