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 X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A2 mbed
Fork of BLE_HeartRate_IDB0XA1 by
Revision 22:ef03ce1c6c99, committed 2018-08-13
- Comitter:
- tomotakaw
- Date:
- Mon Aug 13 07:09:13 2018 +0000
- Parent:
- 21:0e7c08f5386f
- Commit message:
- BLE test with temp sensor
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDService.h Mon Aug 13 07:09:13 2018 +0000
@@ -0,0 +1,43 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __BLE_LED_SERVICE_H__
+#define __BLE_LED_SERVICE_H__
+
+class LEDService {
+public:
+ const static uint16_t LED_SERVICE_UUID = 0xA000;
+ const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
+
+ LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
+ ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
+ {
+ GattCharacteristic *charTable[] = {&ledState};
+ GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.addService(ledService);
+ }
+
+ GattAttribute::Handle_t getValueHandle() const
+ {
+ return ledState.getValueHandle();
+ }
+
+private:
+ BLEDevice &ble;
+ ReadWriteGattCharacteristic<bool> ledState;
+};
+
+#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X_NUCLEO_IKS01A2.lib Mon Aug 13 07:09:13 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/X_NUCLEO_IKS01A2/#138a7a28bd21
--- a/main.cpp Wed Oct 05 09:16:58 2016 +0000
+++ b/main.cpp Mon Aug 13 07:09:13 2018 +0000
@@ -1,108 +1,128 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
#include "mbed.h"
-#include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
-
-DigitalOut led1(LED1, 1);
+#include "BLE.h"
+#include "XNucleoIKS01A2.h"
-const static char DEVICE_NAME[] = "HRM1";
-static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
-
-static volatile bool triggerSensorPolling = false;
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
- (void)params;
- BLE::Instance().gap().startAdvertising(); // restart advertising
-}
+#if 1 //1:シリアルモニタ表示, 0:非表示
+Serial pc(USBTX, USBRX);
+#define DEBUG(...) { pc.printf(__VA_ARGS__); }
+#else
+#define DEBUG(...)
+#endif
-void periodicCallback(void)
-{
- led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
- /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
- * heavy-weight sensor polling from the main thread. */
- triggerSensorPolling = true;
-}
+/* Instantiate the expansion board */
+static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
-void onBleInitError(BLE &ble, ble_error_t error)
-{
- (void)ble;
- (void)error;
- /* Initialization error handling should go here */
-}
+float value1, value2;
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
-{
- BLE& ble = params->ble;
- ble_error_t error = params->error;
-
- if (error != BLE_ERROR_NONE) {
- onBleInitError(ble, error);
- return;
- }
-
- if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
- return;
- }
-
- ble.gap().onDisconnection(disconnectionCallback);
-
- /* Setup primary service. */
- uint8_t hrmCounter = 60; // init HRM to 60bps
- HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+DigitalOut led(LED1);
+Ticker ticker;
+BLE ble;
+const static char DEVICE_NAME[] = "CD1";
+static Gap::ConnectionParams_t connectionParams;
+uint16_t uuid_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE};
+uint8_t msg = 0;
+uint16_t msg16 = 0;
+uint8_t msg_low = 0;
+uint8_t msg_high = 0;
+uint8_t error_flag = 0;
- /* Setup advertising. */
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
- ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.gap().setAdvertisingInterval(1000); /* 1000ms */
- ble.gap().startAdvertising();
-
- // infinite loop
- while (true) {
- // check for trigger from periodicCallback()
- if (triggerSensorPolling && ble.getGapState().connected) {
- triggerSensorPolling = false;
+//ここからサービスとキャラクタリスティックの用意
+static const uint8_t UUID_CHAR_DATA[] = {0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC};//なんでもいい
+GattCharacteristic customCharastic(UUID_CHAR_DATA, (uint8_t *)&msg16, sizeof(msg16), sizeof(msg16), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+GattCharacteristic *customChars[] = {&customCharastic};
+GattService customService(GattService::UUID_DEVICE_INFORMATION_SERVICE, customChars, sizeof(customChars) / sizeof(GattCharacteristic *));
- // Do blocking calls or whatever is necessary for sensor polling.
- // In our case, we simply update the HRM measurement.
- hrmCounter++;
-
- // 60 <= HRM bps <= 100
- if (hrmCounter == 100) {
- hrmCounter = 60;
- }
-
- // update bps
- hrService.updateHeartRate(hrmCounter);
- } else {
- ble.waitForEvent(); // low power wait for event
- }
+//BLE接続したら呼ばれるやつ
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+ DEBUG("Connected!\n\r");
+ ble.getPreferredConnectionParams(&connectionParams);
+ connectionParams.minConnectionInterval = 7.5;
+ connectionParams.maxConnectionInterval = 10;
+ connectionParams.slaveLatency = 0;
+ if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) {
+ DEBUG("Failed to update\n\r");
}
}
-int main(void)
-{
- Ticker ticker;
- ticker.attach(periodicCallback, 1); // blink LED every second
+//BLE切断したら呼ばれるやつ
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
+ DEBUG("Disconnected!\n\r");
+ ticker.detach();
+ ble.gap().startAdvertising();
+}
- BLE::Instance().init(bleInitComplete);
+//周期的に呼ばれるやつ
+void tickerCallback() {
+ DEBUG("update\n\r");
+ hum_temp->get_temperature(&value1);
+ msg_high = (uint8_t)value1;
+ msg_low = (uint8_t) ((value1 - msg_high) *255);
+ msg16 = msg_high << 8 | msg_low ;
+ DEBUG("msg = 0x%x\n\r", msg16);
+ error_flag = ble.updateCharacteristicValue(customCharastic.getValueAttribute().getHandle(), (uint8_t *)&msg16 , sizeof(msg16));
+ DEBUG("error = %d\n\r", error_flag);
+}
+
+//通知を開始するやつ
+void updatesEnabledCallback(Gap::Handle_t handle) {
+ led = 1;
+ ticker.attach(&tickerCallback, 10);
+ DEBUG("Notification is enabled\n\r");
+}
+
+//通知を停止するやつ
+void updatesDisabledCallback(Gap::Handle_t handle) {
+ led = 0;
+ ticker.detach();
+ DEBUG("Notification is disabled\n\r");
}
+void printMacAddress()
+{
+ /* Print out device MAC address to the console*/
+ Gap::AddressType_t addr_type;
+ Gap::Address_t address;
+ BLE::Instance().gap().getAddress(&addr_type, address);
+ printf("DEVICE MAC ADDRESS: ");
+ for (int i = 5; i >= 1; i--){
+ printf("%02x:", address[i]);
+ }
+ printf("%02x\r\n", address[0]);
+}
+
+
+int main(void) {
+ uint8_t id;
+
+ DEBUG("start\n\r");
+
+ hum_temp->enable();
+ hum_temp->read_id(&id);
+ printf("HTS221 humidity & temperature = 0x%X\r\n", id);
+ DEBUG("Initialize\n\r");
+ ble.init();
+
+ DEBUG("Setup the event handlers\n\r");
+ ble.gap().onConnection(connectionCallback);
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.onUpdatesEnabled(updatesEnabledCallback);
+ ble.onUpdatesDisabled(updatesDisabledCallback);
+
+ DEBUG("Advertising payload\n\r");
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid_list, sizeof(uuid_list));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof((const uint8_t *)DEVICE_NAME));
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().setAdvertisingInterval(160); //100ms;
+ ble.gap().startAdvertising();
+
+ DEBUG("Add service\n\r");
+ ble.gattServer().addService(customService);
+
+ printMacAddress();
+
+ while (true) {
+ ble.waitForEvent();
+ }
+}
\ No newline at end of file
