HRM1017による温度計のサンプルコード。ADT7410で使えるように修正しています。

Dependencies:   ADT7410 BLE_API TMP102 mbed nRF51822

Fork of BLE_HTM_HRM1017 by Yoshihiro TSUBOI

Committer:
nobukuma
Date:
Thu Dec 04 12:54:23 2014 +0000
Revision:
9:7b2f0da06ac3
Parent:
8:11d990f689fe
modified for ADT7410

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nobukuma 8:11d990f689fe 1 // original: http://developer.mbed.org/users/ytsuboi/code/BLE_HTM_HRM1017/shortlog
nobukuma 8:11d990f689fe 2 // modified for ADT7410
nobukuma 8:11d990f689fe 3
todotani 0:5e4210d108ac 4 #include "mbed.h"
todotani 0:5e4210d108ac 5 #include "BLEDevice.h"
todotani 0:5e4210d108ac 6 #include "ble_hts.h"
todotani 0:5e4210d108ac 7
todotani 0:5e4210d108ac 8 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
todotani 0:5e4210d108ac 9 * it will have an impact on code-size and power consumption. */
todotani 0:5e4210d108ac 10
todotani 0:5e4210d108ac 11 #if NEED_CONSOLE_OUTPUT
todotani 0:5e4210d108ac 12 Serial pc(USBTX, USBRX);
todotani 0:5e4210d108ac 13 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
todotani 0:5e4210d108ac 14 #else
todotani 0:5e4210d108ac 15 #define DEBUG(...) /* nothing */
todotani 0:5e4210d108ac 16 #endif /* #if NEED_CONSOLE_OUTPUT */
todotani 0:5e4210d108ac 17
todotani 0:5e4210d108ac 18 const static char DEVICE_NAME[] = "HRM1017_HTM";
todotani 2:daf2344afc28 19 static volatile bool triggerSensorPolling = false;
todotani 0:5e4210d108ac 20
todotani 0:5e4210d108ac 21 BLEDevice ble;
nobukuma 8:11d990f689fe 22
nobukuma 8:11d990f689fe 23 #define UseADT7410
nobukuma 8:11d990f689fe 24 #ifdef UseADT7410
nobukuma 8:11d990f689fe 25 #include "ADT7410.h"
nobukuma 8:11d990f689fe 26 //ADT7410 healthThemometer(p13, p15, 0x90, 100000);
nobukuma 8:11d990f689fe 27 ADT7410 healthThemometer(p22, p20, 0x90, 100000);
nobukuma 8:11d990f689fe 28 #else
nobukuma 8:11d990f689fe 29 #include "TMP102.h"
todotani 0:5e4210d108ac 30 TMP102 healthThemometer(p22, p20, 0x90); /* The TMP102 connected to our board */
nobukuma 8:11d990f689fe 31 #endif
todotani 0:5e4210d108ac 32
todotani 0:5e4210d108ac 33 /* LEDs for indication: */
todotani 0:5e4210d108ac 34 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */
todotani 0:5e4210d108ac 35 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
todotani 0:5e4210d108ac 36
todotani 0:5e4210d108ac 37
todotani 0:5e4210d108ac 38 /* Health Thermometer Service */
todotani 0:5e4210d108ac 39 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 };
todotani 0:5e4210d108ac 40 GattCharacteristic tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
todotani 0:5e4210d108ac 41 thermTempPayload, 5, 5,
todotani 0:5e4210d108ac 42 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
nobukuma 8:11d990f689fe 43 GattCharacteristic *htmChars[] = {&tempChar, };
nobukuma 8:11d990f689fe 44 GattService htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars,
nobukuma 8:11d990f689fe 45 sizeof(htmChars) / sizeof(GattCharacteristic *));
nobukuma 8:11d990f689fe 46
todotani 0:5e4210d108ac 47 /* Battery Level Service */
todotani 0:5e4210d108ac 48 uint8_t batt = 100; /* Battery level */
todotani 0:5e4210d108ac 49 uint8_t read_batt = 0; /* Variable to hold battery level reads */
todotani 0:5e4210d108ac 50 GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR,
todotani 0:5e4210d108ac 51 (uint8_t *)batt, 1, 1,
todotani 0:5e4210d108ac 52 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
todotani 0:5e4210d108ac 53 GattCharacteristic *battChars[] = {&battLevel, };
todotani 0:5e4210d108ac 54 GattService battService(GattService::UUID_BATTERY_SERVICE, battChars,
todotani 0:5e4210d108ac 55 sizeof(battChars) / sizeof(GattCharacteristic *));
todotani 0:5e4210d108ac 56
todotani 0:5e4210d108ac 57 uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE,
todotani 0:5e4210d108ac 58 GattService::UUID_BATTERY_SERVICE};
todotani 0:5e4210d108ac 59
todotani 0:5e4210d108ac 60 uint32_t quick_ieee11073_from_float(float temperature);
todotani 0:5e4210d108ac 61 void updateServiceValues(void);
todotani 0:5e4210d108ac 62
todotani 0:5e4210d108ac 63 static Gap::ConnectionParams_t connectionParams;
todotani 0:5e4210d108ac 64
todotani 2:daf2344afc28 65 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod
todotani 0:5e4210d108ac 66 {
todotani 0:5e4210d108ac 67 advertisingStateLed = 1;
todotani 0:5e4210d108ac 68
todotani 5:ab8889077be9 69 DEBUG("Disconnected handle %u, reason %u\n", handle, reason);
todotani 0:5e4210d108ac 70 DEBUG("Restarting the advertising process\n\r");
todotani 0:5e4210d108ac 71 ble.startAdvertising();
todotani 0:5e4210d108ac 72 }
todotani 0:5e4210d108ac 73
todotani 2:daf2344afc28 74 void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) //Mod
todotani 0:5e4210d108ac 75 {
todotani 0:5e4210d108ac 76 advertisingStateLed = 0;
todotani 0:5e4210d108ac 77
todotani 0:5e4210d108ac 78 DEBUG("connected. Got handle %u\r\n", handle);
todotani 0:5e4210d108ac 79
todotani 0:5e4210d108ac 80 connectionParams.slaveLatency = 1;
todotani 0:5e4210d108ac 81 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
todotani 0:5e4210d108ac 82 DEBUG("failed to update connection paramter\r\n");
todotani 0:5e4210d108ac 83 }
todotani 0:5e4210d108ac 84 }
todotani 0:5e4210d108ac 85
todotani 2:daf2344afc28 86 void periodicCallback(void)
todotani 2:daf2344afc28 87 {
todotani 2:daf2344afc28 88 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
todotani 2:daf2344afc28 89
todotani 2:daf2344afc28 90 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
todotani 2:daf2344afc28 91 * heavy-weight sensor polling from the main thread. */
todotani 2:daf2344afc28 92 triggerSensorPolling = true;
todotani 2:daf2344afc28 93 }
todotani 2:daf2344afc28 94
todotani 0:5e4210d108ac 95 /**************************************************************************/
todotani 0:5e4210d108ac 96 /*!
todotani 0:5e4210d108ac 97 @brief Program entry point
todotani 0:5e4210d108ac 98 */
todotani 0:5e4210d108ac 99 /**************************************************************************/
todotani 0:5e4210d108ac 100 int main(void)
todotani 0:5e4210d108ac 101 {
todotani 0:5e4210d108ac 102
todotani 0:5e4210d108ac 103 /* Setup blinky led */
todotani 0:5e4210d108ac 104 oneSecondLed = 1;
todotani 2:daf2344afc28 105 Ticker ticker;
todotani 2:daf2344afc28 106 ticker.attach(periodicCallback, 1);
todotani 2:daf2344afc28 107
todotani 2:daf2344afc28 108 DEBUG("Initialising the nRF51822\n");
todotani 0:5e4210d108ac 109 ble.init();
todotani 2:daf2344afc28 110 DEBUG("Init done\n");
todotani 0:5e4210d108ac 111 ble.onDisconnection(disconnectionCallback);
todotani 0:5e4210d108ac 112 ble.onConnection(onConnectionCallback);
todotani 0:5e4210d108ac 113
todotani 0:5e4210d108ac 114 ble.getPreferredConnectionParams(&connectionParams);
todotani 0:5e4210d108ac 115
todotani 0:5e4210d108ac 116 /* setup advertising */
todotani 0:5e4210d108ac 117 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
todotani 0:5e4210d108ac 118 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
todotani 0:5e4210d108ac 119 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
todotani 0:5e4210d108ac 120 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
todotani 0:5e4210d108ac 121 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
todotani 0:5e4210d108ac 122 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
todotani 0:5e4210d108ac 123 ble.startAdvertising();
todotani 0:5e4210d108ac 124 advertisingStateLed = 1;
todotani 2:daf2344afc28 125 DEBUG("Start Advertising\n");
todotani 0:5e4210d108ac 126
todotani 0:5e4210d108ac 127 ble.addService(htmService);
todotani 0:5e4210d108ac 128 ble.addService(battService);
todotani 2:daf2344afc28 129 DEBUG("Add Service\n");
nobukuma 8:11d990f689fe 130
nobukuma 8:11d990f689fe 131 healthThemometer.reset();
nobukuma 8:11d990f689fe 132 //healthThemometer.setConfig(ONE_SPS_MODE);
todotani 0:5e4210d108ac 133
todotani 2:daf2344afc28 134 while (true) {
todotani 2:daf2344afc28 135 if (triggerSensorPolling) {
todotani 2:daf2344afc28 136 triggerSensorPolling = false;
todotani 2:daf2344afc28 137 updateServiceValues();
todotani 2:daf2344afc28 138 } else {
todotani 2:daf2344afc28 139 ble.waitForEvent();
todotani 2:daf2344afc28 140 }
todotani 0:5e4210d108ac 141 }
todotani 0:5e4210d108ac 142 }
todotani 0:5e4210d108ac 143
todotani 0:5e4210d108ac 144 /**************************************************************************/
todotani 0:5e4210d108ac 145 /*!
todotani 0:5e4210d108ac 146 @brief Ticker callback to switch advertisingStateLed state
todotani 0:5e4210d108ac 147 */
todotani 0:5e4210d108ac 148 /**************************************************************************/
nobukuma 8:11d990f689fe 149 #include <stdlib.h>
todotani 0:5e4210d108ac 150 void updateServiceValues(void)
todotani 0:5e4210d108ac 151 {
todotani 0:5e4210d108ac 152 /* Decrement the battery level. */
todotani 0:5e4210d108ac 153 batt <=50 ? batt=100 : batt--;
todotani 0:5e4210d108ac 154
todotani 0:5e4210d108ac 155 /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
nobukuma 8:11d990f689fe 156 #ifdef UseADT7410
nobukuma 8:11d990f689fe 157 float temperature = healthThemometer.getTemp();
nobukuma 8:11d990f689fe 158 #else
todotani 0:5e4210d108ac 159 float temperature = healthThemometer.read();
nobukuma 8:11d990f689fe 160 #endif
todotani 0:5e4210d108ac 161 DEBUG("temp:%f\n", temperature);
todotani 0:5e4210d108ac 162 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
todotani 0:5e4210d108ac 163 memcpy(thermTempPayload+1, &temp_ieee11073, 4);
todotani 2:daf2344afc28 164 ble.updateCharacteristicValue(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload)); //Mod
todotani 2:daf2344afc28 165 ble.updateCharacteristicValue(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt)); //Mod
todotani 0:5e4210d108ac 166 }
todotani 0:5e4210d108ac 167
todotani 0:5e4210d108ac 168 /**
todotani 0:5e4210d108ac 169 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
todotani 0:5e4210d108ac 170 * @param temperature The temperature as a float.
todotani 0:5e4210d108ac 171 * @return The temperature in 11073-20601 FLOAT-Type format.
todotani 0:5e4210d108ac 172 */
todotani 0:5e4210d108ac 173 uint32_t quick_ieee11073_from_float(float temperature)
todotani 0:5e4210d108ac 174 {
todotani 0:5e4210d108ac 175 uint8_t exponent = 0xFF; //exponent is -1
todotani 0:5e4210d108ac 176 uint32_t mantissa = (uint32_t)(temperature*10);
todotani 0:5e4210d108ac 177
todotani 0:5e4210d108ac 178 return ( ((uint32_t)exponent) << 24) | mantissa;
todotani 0:5e4210d108ac 179 }
todotani 0:5e4210d108ac 180