Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
MicroBitThermometer.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #include "MicroBitConfig.h" 00027 #include "MicroBitThermometer.h" 00028 #include "MicroBitSystemTimer.h" 00029 #include "MicroBitFiber.h" 00030 00031 /* 00032 * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ 00033 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) 00034 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this 00035 * as a compatability option, but does not support the options used... 00036 */ 00037 #if !defined(__arm) 00038 #pragma GCC diagnostic ignored "-Wunused-function" 00039 #pragma GCC diagnostic push 00040 #pragma GCC diagnostic ignored "-Wunused-parameter" 00041 #endif 00042 00043 #include "nrf_soc.h" 00044 #include "nrf_sdm.h" 00045 00046 /* 00047 * Return to our predefined compiler settings. 00048 */ 00049 #if !defined(__arm) 00050 #pragma GCC diagnostic pop 00051 #endif 00052 00053 /** 00054 * Constructor. 00055 * Create new MicroBitThermometer that gives an indication of the current temperature. 00056 * 00057 * @param _storage an instance of MicroBitStorage used to persist temperature offset data 00058 * 00059 * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER. 00060 * 00061 * @code 00062 * MicroBitStorage storage; 00063 * MicroBitThermometer thermometer(storage); 00064 * @endcode 00065 */ 00066 MicroBitThermometer::MicroBitThermometer(MicroBitStorage& _storage, uint16_t id) : 00067 storage(&_storage) 00068 { 00069 this->id = id; 00070 this->samplePeriod = MICROBIT_THERMOMETER_PERIOD; 00071 this->sampleTime = 0; 00072 this->offset = 0; 00073 00074 KeyValuePair *tempCalibration = storage->get("tempCal"); 00075 00076 if(tempCalibration != NULL) 00077 { 00078 memcpy(&offset, tempCalibration->value, sizeof(int16_t)); 00079 delete tempCalibration; 00080 } 00081 } 00082 00083 /** 00084 * Constructor. 00085 * Create new MicroBitThermometer that gives an indication of the current temperature. 00086 * 00087 * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER. 00088 * 00089 * @code 00090 * MicroBitThermometer thermometer; 00091 * @endcode 00092 */ 00093 MicroBitThermometer::MicroBitThermometer(uint16_t id) : 00094 storage(NULL) 00095 { 00096 this->id = id; 00097 this->samplePeriod = MICROBIT_THERMOMETER_PERIOD; 00098 this->sampleTime = 0; 00099 this->offset = 0; 00100 } 00101 00102 /** 00103 * Gets the current temperature of the microbit. 00104 * 00105 * @return the current temperature, in degrees celsius. 00106 * 00107 * @code 00108 * thermometer.getTemperature(); 00109 * @endcode 00110 */ 00111 int MicroBitThermometer::getTemperature() 00112 { 00113 updateSample(); 00114 return temperature - offset; 00115 } 00116 00117 00118 /** 00119 * Updates the temperature sample of this instance of MicroBitThermometer 00120 * only if isSampleNeeded() indicates that an update is required. 00121 * 00122 * This call also will add the thermometer to fiber components to receive 00123 * periodic callbacks. 00124 * 00125 * @return MICROBIT_OK on success. 00126 */ 00127 int MicroBitThermometer::updateSample() 00128 { 00129 if(!(status & MICROBIT_THERMOMETER_ADDED_TO_IDLE)) 00130 { 00131 // If we're running under a fiber scheduer, register ourselves for a periodic callback to keep our data up to date. 00132 // Otherwise, we do just do this on demand, when polled through our read() interface. 00133 fiber_add_idle_component(this); 00134 status |= MICROBIT_THERMOMETER_ADDED_TO_IDLE; 00135 } 00136 00137 // check if we need to update our sample... 00138 if(isSampleNeeded()) 00139 { 00140 int32_t processorTemperature; 00141 uint8_t sd_enabled; 00142 00143 // For now, we just rely on the nrf senesor to be the most accurate. 00144 // The compass module also has a temperature sensor, and has the lowest power consumption, so will run the cooler... 00145 // ...however it isn't trimmed for accuracy during manufacture, so requires calibration. 00146 00147 sd_softdevice_is_enabled(&sd_enabled); 00148 00149 if (sd_enabled) 00150 { 00151 // If Bluetooth is enabled, we need to go through the Nordic software to safely do this 00152 sd_temp_get(&processorTemperature); 00153 } 00154 else 00155 { 00156 // Othwerwise, we access the information directly... 00157 uint32_t *TEMP = (uint32_t *)0x4000C508; 00158 00159 NRF_TEMP->TASKS_START = 1; 00160 00161 while (NRF_TEMP->EVENTS_DATARDY == 0); 00162 00163 NRF_TEMP->EVENTS_DATARDY = 0; 00164 00165 processorTemperature = *TEMP; 00166 00167 NRF_TEMP->TASKS_STOP = 1; 00168 } 00169 00170 00171 // Record our reading... 00172 temperature = processorTemperature / 4; 00173 00174 // Schedule our next sample. 00175 sampleTime = system_timer_current_time() + samplePeriod; 00176 00177 // Send an event to indicate that we'e updated our temperature. 00178 MicroBitEvent e(id, MICROBIT_THERMOMETER_EVT_UPDATE); 00179 } 00180 00181 return MICROBIT_OK; 00182 }; 00183 00184 /** 00185 * Periodic callback from MicroBit idle thread. 00186 */ 00187 void MicroBitThermometer::idleTick() 00188 { 00189 updateSample(); 00190 } 00191 00192 /** 00193 * Determines if we're due to take another temperature reading 00194 * 00195 * @return 1 if we're due to take a temperature reading, 0 otherwise. 00196 */ 00197 int MicroBitThermometer::isSampleNeeded() 00198 { 00199 return system_timer_current_time() >= sampleTime; 00200 } 00201 00202 /** 00203 * Set the sample rate at which the temperatureis read (in ms). 00204 * 00205 * The default sample period is 1 second. 00206 * 00207 * @param period the requested time between samples, in milliseconds. 00208 * 00209 * @note the temperature is always read in the background, and is only updated 00210 * when the processor is idle, or when the temperature is explicitly read. 00211 */ 00212 void MicroBitThermometer::setPeriod(int period) 00213 { 00214 updateSample(); 00215 samplePeriod = period; 00216 } 00217 00218 /** 00219 * Reads the currently configured sample rate of the thermometer. 00220 * 00221 * @return The time between samples, in milliseconds. 00222 */ 00223 int MicroBitThermometer::getPeriod() 00224 { 00225 return samplePeriod; 00226 } 00227 00228 /** 00229 * Set the value that is used to offset the raw silicon temperature. 00230 * 00231 * @param offset the offset for the silicon temperature 00232 * 00233 * @return MICROBIT_OK on success 00234 */ 00235 int MicroBitThermometer::setOffset(int offset) 00236 { 00237 if(this->storage != NULL) 00238 this->storage->put(ManagedString("tempCal"), (uint8_t *)&offset, sizeof(int)); 00239 00240 this->offset = offset; 00241 00242 return MICROBIT_OK; 00243 } 00244 00245 /** 00246 * Retreive the value that is used to offset the raw silicon temperature. 00247 * 00248 * @return the current offset. 00249 */ 00250 int MicroBitThermometer::getOffset() 00251 { 00252 return offset; 00253 } 00254 00255 /** 00256 * This member function fetches the raw silicon temperature, and calculates 00257 * the value used to offset the raw silicon temperature based on a given temperature. 00258 * 00259 * @param calibrationTemp the temperature used to calculate the raw silicon temperature 00260 * offset. 00261 * 00262 * @return MICROBIT_OK on success 00263 */ 00264 int MicroBitThermometer::setCalibration(int calibrationTemp) 00265 { 00266 updateSample(); 00267 return setOffset(temperature - calibrationTemp); 00268 }
Generated on Tue Jul 12 2022 20:39:13 by 1.7.2