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 mbed nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #define TARGET_NRF_LFCLK_RC 1 00018 00019 #include "mbed.h" 00020 #include "BLE.h" 00021 #include "UberVestService.h" 00022 #include <math.h> 00023 00024 // BEGIN IO 00025 // Inputs and outputs need to be changed between the devkit and prospeckz 00026 00027 // Outputs 00028 DigitalOut connectionLed(LED1); 00029 00030 // Inputs 00031 AnalogIn ecg(p6); 00032 AnalogIn temp(p2); 00033 00034 // N.B. The devkit and prospeckz are different - DK is active low, prospeckz is active high. 00035 static const bool LED_ON = 1; 00036 static const bool LED_OFF = 0; 00037 00038 // END IO 00039 00040 Serial pc(USBTX, USBRX); 00041 BLE ble; 00042 00043 const static char DEVICE_NAME[] = "UberVest"; 00044 static const uint16_t uuid16_list[] = {UberVestService::UBER_VEST_SERVICE_UUID}; 00045 00046 static int8_t ecgValue; 00047 static int8_t tempValue; 00048 00049 static volatile bool sampleEcg = false; 00050 static volatile bool sampleTemp = false; 00051 00052 UberVestService *uberVestServicePtr; 00053 00054 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00055 { 00056 connectionLed = LED_OFF; 00057 } 00058 00059 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00060 { 00061 ble.gap().startAdvertising(); 00062 connectionLed = LED_ON; 00063 } 00064 00065 void sampleEcgCallback(void) 00066 { 00067 sampleEcg = true; 00068 } 00069 00070 void sampleTempCallback(void) 00071 { 00072 sampleTemp = true; 00073 } 00074 00075 void flasherTimer(void) 00076 { 00077 if (!ble.getGapState().connected) { 00078 connectionLed = !connectionLed; 00079 } 00080 } 00081 00082 float getTemp(void) 00083 { 00084 const int TEMP_RESISTOR_VALUE = 10900; 00085 float rawValue; 00086 float r; // Thermistor resistance 00087 double convertedTemp; 00088 00089 rawValue = temp.read(); 00090 // The thermistor is set up as a simple voltage divider. We can get the resistance as follows: 00091 r = ((TEMP_RESISTOR_VALUE * 5.0f) / (rawValue * 4.3f)) - TEMP_RESISTOR_VALUE; 00092 00093 // Now calculate the temperature from the resistance 00094 // From the thermistor data sheet (http://www.farnell.com/datasheets/1784420.pdf): 00095 const float R_REF = 10000.0f; 00096 const double A = 3.354016e-03; 00097 const double B = 2.569850e-04; 00098 const double C = 2.620131e-06; 00099 const double D = 6.383091e-08; 00100 00101 convertedTemp = A + B * (log(r / R_REF)) + C * pow(log(r / R_REF), 2.0f) + D * pow(log(r / R_REF), 3.0f); 00102 convertedTemp = 1.0f / convertedTemp; 00103 00104 // convert to degrees celsius: 00105 convertedTemp = convertedTemp - 273.0f; 00106 00107 pc.printf("Raw: %f, Resisance: %f, Temp: %f\r\n", rawValue, r, convertedTemp); 00108 00109 return (float) convertedTemp; 00110 } 00111 00112 int main(void) 00113 { 00114 ble.init(); 00115 ble.gap().onConnection(connectionCallback); 00116 ble.gap().onDisconnection(disconnectionCallback); 00117 00118 ecgValue = 0; 00119 tempValue = 0; 00120 00121 UberVestService uberVestService(ble, ecgValue, tempValue); 00122 uberVestServicePtr = &uberVestService; 00123 00124 Ticker ecgSampler; 00125 ecgSampler.attach(sampleEcgCallback, 0.02); 00126 00127 Ticker tempSampler; 00128 tempSampler.attach(sampleTempCallback, 1); 00129 00130 Ticker flasher; 00131 flasher.attach(flasherTimer, 0.5); 00132 00133 /* setup advertising */ 00134 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00135 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00136 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00137 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00138 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ 00139 ble.gap().startAdvertising(); 00140 00141 while (true) { 00142 if (ble.getGapState().connected) { 00143 if (sampleEcg) { 00144 sampleEcg = false; 00145 00146 ecgValue = (ecg.read() * 512); 00147 uberVestServicePtr->updateEcg(ecgValue); 00148 } 00149 00150 if (sampleTemp) { 00151 sampleTemp = false; 00152 00153 tempValue = getTemp(); 00154 uberVestServicePtr->updateTemp(tempValue); 00155 } 00156 } else { 00157 ble.waitForEvent(); 00158 } 00159 } 00160 }
Generated on Thu Jul 21 2022 03:22:43 by
1.7.2