mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitThermometer.h Source File

MicroBitThermometer.h

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 #ifndef MICROBIT_THERMOMETER_H
00027 #define MICROBIT_THERMOMETER_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitComponent.h"
00032 #include "MicroBitStorage.h"
00033 
00034 #define MICROBIT_THERMOMETER_PERIOD             1000
00035 
00036 
00037 #define MAG3110_SAMPLE_RATES                    11
00038 
00039 /*
00040  * Temperature events
00041  */
00042 #define MICROBIT_THERMOMETER_EVT_UPDATE         1
00043 
00044 #define MICROBIT_THERMOMETER_ADDED_TO_IDLE      2
00045 
00046 /**
00047   * Class definition for MicroBit Thermometer.
00048   *
00049   * Infers and stores the ambient temoperature based on the surface temperature
00050   * of the various chips on the micro:bit.
00051   *
00052   */
00053 class MicroBitThermometer : public MicroBitComponent
00054 {
00055     unsigned long           sampleTime;
00056     uint32_t                samplePeriod;
00057     int16_t                 temperature;
00058     int16_t                 offset;
00059     MicroBitStorage*        storage;
00060 
00061     public:
00062 
00063     /**
00064       * Constructor.
00065       * Create new MicroBitThermometer that gives an indication of the current temperature.
00066       *
00067       * @param _storage an instance of MicroBitStorage used to persist temperature offset data
00068       *
00069       * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER.
00070       *
00071       * @code
00072       * MicroBitStorage storage;
00073       * MicroBitThermometer thermometer(storage);
00074       * @endcode
00075       */
00076     MicroBitThermometer(MicroBitStorage& _storage, uint16_t id = MICROBIT_ID_THERMOMETER);
00077 
00078     /**
00079       * Constructor.
00080       * Create new MicroBitThermometer that gives an indication of the current temperature.
00081       *
00082       * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER.
00083       *
00084       * @code
00085       * MicroBitThermometer thermometer;
00086       * @endcode
00087       */
00088     MicroBitThermometer(uint16_t id = MICROBIT_ID_THERMOMETER);
00089 
00090     /**
00091       * Set the sample rate at which the temperatureis read (in ms).
00092       *
00093       * The default sample period is 1 second.
00094       *
00095       * @param period the requested time between samples, in milliseconds.
00096       *
00097       * @note the temperature is always read in the background, and is only updated
00098       * when the processor is idle, or when the temperature is explicitly read.
00099       */
00100     void setPeriod(int period);
00101 
00102     /**
00103       * Reads the currently configured sample rate of the thermometer.
00104       *
00105       * @return The time between samples, in milliseconds.
00106       */
00107     int getPeriod();
00108 
00109     /**
00110       * Set the value that is used to offset the raw silicon temperature.
00111       *
00112       * @param offset the offset for the silicon temperature
00113       *
00114       * @return MICROBIT_OK on success
00115       */
00116     int setOffset(int offset);
00117 
00118     /**
00119       * Retreive the value that is used to offset the raw silicon temperature.
00120       *
00121       * @return the current offset.
00122       */
00123     int getOffset();
00124 
00125     /**
00126       * This member function fetches the raw silicon temperature, and calculates
00127       * the value used to offset the raw silicon temperature based on a given temperature.
00128       *
00129       * @param calibrationTemp the temperature used to calculate the raw silicon temperature
00130       * offset.
00131       *
00132       * @return MICROBIT_OK on success
00133       */
00134     int setCalibration(int calibrationTemp);
00135 
00136     /**
00137       * Gets the current temperature of the microbit.
00138       *
00139       * @return the current temperature, in degrees celsius.
00140       *
00141       * @code
00142       * thermometer.getTemperature();
00143       * @endcode
00144       */
00145     int getTemperature();
00146 
00147     /**
00148       * Updates the temperature sample of this instance of MicroBitThermometer
00149       * only if isSampleNeeded() indicates that an update is required.
00150       *
00151       * This call also will add the thermometer to fiber components to receive
00152       * periodic callbacks.
00153       *
00154       * @return MICROBIT_OK on success.
00155       */
00156     int updateSample();
00157 
00158     /**
00159       * Periodic callback from MicroBit idle thread.
00160       */
00161     virtual void idleTick();
00162 
00163     private:
00164 
00165     /**
00166       * Determines if we're due to take another temperature reading
00167       *
00168       * @return 1 if we're due to take a temperature reading, 0 otherwise.
00169       */
00170     int isSampleNeeded();
00171 };
00172 
00173 #endif