Health Thermometer example for Switch Science mbed TY51822r3
Dependencies: BLE_API TMP102 mbed nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "TMP102.h" 00003 #include "BLE.h" 00004 00005 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; 00006 * it will have an impact on code-size and power consumption. */ 00007 00008 #if NEED_CONSOLE_OUTPUT 00009 Serial pc(USBTX, USBRX); 00010 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00011 #else 00012 #define DEBUG(...) /* nothing */ 00013 #endif /* #if NEED_CONSOLE_OUTPUT */ 00014 00015 const static char DEVICE_NAME[] = "TY51822R3_HTM"; 00016 static volatile bool triggerSensorPolling = false; 00017 00018 BLEDevice ble; 00019 TMP102 healthThemometer(I2C_SDA0, I2C_SCL0, 0x90); /* The TMP102 connected to our board */ 00020 //TMP102 healthThemometer(I2C_SDA1, I2C_SCL1, 0x90); /* The TMP102 connected to our board */ 00021 00022 /* LEDs for indication: */ 00023 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */ 00024 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */ 00025 00026 00027 /* Health Thermometer Service */ 00028 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 }; 00029 00030 GattCharacteristic tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, 00031 thermTempPayload, 5, 5, 00032 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); 00033 /* Battery Level Service */ 00034 uint8_t batt = 100; /* Battery level */ 00035 uint8_t read_batt = 0; /* Variable to hold battery level reads */ 00036 GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 00037 (uint8_t *)batt, 1, 1, 00038 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00039 GattCharacteristic *htmChars[] = {&tempChar, }; 00040 GattCharacteristic *battChars[] = {&battLevel, }; 00041 GattService htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars, 00042 sizeof(htmChars) / sizeof(GattCharacteristic *)); 00043 GattService battService(GattService::UUID_BATTERY_SERVICE, battChars, 00044 sizeof(battChars) / sizeof(GattCharacteristic *)); 00045 00046 uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE, 00047 GattService::UUID_BATTERY_SERVICE}; 00048 00049 uint32_t quick_ieee11073_from_float(float temperature); 00050 void updateServiceValues(void); 00051 00052 static Gap::ConnectionParams_t connectionParams; 00053 00054 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) // Mod 00055 { 00056 advertisingStateLed = 1; 00057 00058 DEBUG("Disconnected handle %u, reason %u\r\n", params->handle, params->reason); 00059 DEBUG("Restarting the advertising process\r\n"); 00060 ble.gap().startAdvertising(); 00061 } 00062 00063 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) //Mod 00064 { 00065 advertisingStateLed = 0; 00066 00067 DEBUG("connected. Got handle %u\r\n", params->handle); 00068 00069 connectionParams.slaveLatency = 1; 00070 if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { 00071 DEBUG("failed to update connection paramter\r\n"); 00072 } 00073 } 00074 00075 void periodicCallback(void) 00076 { 00077 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ 00078 00079 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00080 * heavy-weight sensor polling from the main thread. */ 00081 triggerSensorPolling = true; 00082 } 00083 00084 /**************************************************************************/ 00085 /*! 00086 @brief Program entry point 00087 */ 00088 /**************************************************************************/ 00089 int main(void) 00090 { 00091 00092 /* Setup blinky led */ 00093 oneSecondLed = 1; 00094 Ticker ticker; 00095 ticker.attach(periodicCallback, 1); 00096 00097 DEBUG("Initialising the nRF51822\r\n"); 00098 ble.init(); 00099 DEBUG("Init done\r\n"); 00100 ble.gap().onDisconnection(disconnectionCallback); 00101 ble.gap().onConnection(onConnectionCallback); 00102 00103 ble.gap().getPreferredConnectionParams(&connectionParams); 00104 00105 /* setup advertising */ 00106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); 00108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); 00109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00110 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00111 ble.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00112 ble.gap().startAdvertising(); 00113 advertisingStateLed = 1; 00114 DEBUG("Start Advertising\r\n"); 00115 00116 ble.gattServer().addService(htmService); 00117 ble.gattServer().addService(battService); 00118 DEBUG("Add Service\r\n"); 00119 00120 while (true) { 00121 if (triggerSensorPolling) { 00122 triggerSensorPolling = false; 00123 updateServiceValues(); 00124 } else { 00125 ble.waitForEvent(); 00126 } 00127 } 00128 } 00129 00130 /**************************************************************************/ 00131 /*! 00132 @brief Ticker callback to switch advertisingStateLed state 00133 */ 00134 /**************************************************************************/ 00135 void updateServiceValues(void) 00136 { 00137 /* Decrement the battery level. */ 00138 batt <=50 ? batt=100 : batt--; 00139 00140 /* Update the temperature. Note that we need to convert to an ieee11073 format float. */ 00141 float temperature = healthThemometer.read(); 00142 DEBUG("temp:%f\r\n", temperature); 00143 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature); 00144 memcpy(thermTempPayload+1, &temp_ieee11073, 4); 00145 ble.gattServer().write(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload)); //Mod 00146 ble.gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt)); //Mod 00147 } 00148 00149 /** 00150 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type. 00151 * @param temperature The temperature as a float. 00152 * @return The temperature in 11073-20601 FLOAT-Type format. 00153 */ 00154 uint32_t quick_ieee11073_from_float(float temperature) 00155 { 00156 uint8_t exponent = 0xFF; //exponent is -1 00157 uint32_t mantissa = (uint32_t)(temperature*10); 00158 00159 return ( ((uint32_t)exponent) << 24) | mantissa; 00160 }
Generated on Tue Jul 12 2022 17:32:57 by 1.7.2