Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/inc/drivers/MicroBitThermometer.h@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* |
MrBedfordVan | 0:b9164b348919 | 2 | The MIT License (MIT) |
MrBedfordVan | 0:b9164b348919 | 3 | |
MrBedfordVan | 0:b9164b348919 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
MrBedfordVan | 0:b9164b348919 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
MrBedfordVan | 0:b9164b348919 | 6 | |
MrBedfordVan | 0:b9164b348919 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
MrBedfordVan | 0:b9164b348919 | 8 | copy of this software and associated documentation files (the "Software"), |
MrBedfordVan | 0:b9164b348919 | 9 | to deal in the Software without restriction, including without limitation |
MrBedfordVan | 0:b9164b348919 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MrBedfordVan | 0:b9164b348919 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
MrBedfordVan | 0:b9164b348919 | 12 | Software is furnished to do so, subject to the following conditions: |
MrBedfordVan | 0:b9164b348919 | 13 | |
MrBedfordVan | 0:b9164b348919 | 14 | The above copyright notice and this permission notice shall be included in |
MrBedfordVan | 0:b9164b348919 | 15 | all copies or substantial portions of the Software. |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MrBedfordVan | 0:b9164b348919 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MrBedfordVan | 0:b9164b348919 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
MrBedfordVan | 0:b9164b348919 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MrBedfordVan | 0:b9164b348919 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
MrBedfordVan | 0:b9164b348919 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
MrBedfordVan | 0:b9164b348919 | 23 | DEALINGS IN THE SOFTWARE. |
MrBedfordVan | 0:b9164b348919 | 24 | */ |
MrBedfordVan | 0:b9164b348919 | 25 | |
MrBedfordVan | 0:b9164b348919 | 26 | #ifndef MICROBIT_THERMOMETER_H |
MrBedfordVan | 0:b9164b348919 | 27 | #define MICROBIT_THERMOMETER_H |
MrBedfordVan | 0:b9164b348919 | 28 | |
MrBedfordVan | 0:b9164b348919 | 29 | #include "mbed.h" |
MrBedfordVan | 0:b9164b348919 | 30 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 31 | #include "MicroBitComponent.h" |
MrBedfordVan | 0:b9164b348919 | 32 | #include "MicroBitStorage.h" |
MrBedfordVan | 0:b9164b348919 | 33 | |
MrBedfordVan | 0:b9164b348919 | 34 | #define MICROBIT_THERMOMETER_PERIOD 1000 |
MrBedfordVan | 0:b9164b348919 | 35 | |
MrBedfordVan | 0:b9164b348919 | 36 | |
MrBedfordVan | 0:b9164b348919 | 37 | #define MAG3110_SAMPLE_RATES 11 |
MrBedfordVan | 0:b9164b348919 | 38 | |
MrBedfordVan | 0:b9164b348919 | 39 | /* |
MrBedfordVan | 0:b9164b348919 | 40 | * Temperature events |
MrBedfordVan | 0:b9164b348919 | 41 | */ |
MrBedfordVan | 0:b9164b348919 | 42 | #define MICROBIT_THERMOMETER_EVT_UPDATE 1 |
MrBedfordVan | 0:b9164b348919 | 43 | |
MrBedfordVan | 0:b9164b348919 | 44 | #define MICROBIT_THERMOMETER_ADDED_TO_IDLE 2 |
MrBedfordVan | 0:b9164b348919 | 45 | |
MrBedfordVan | 0:b9164b348919 | 46 | /** |
MrBedfordVan | 0:b9164b348919 | 47 | * Class definition for MicroBit Thermometer. |
MrBedfordVan | 0:b9164b348919 | 48 | * |
MrBedfordVan | 0:b9164b348919 | 49 | * Infers and stores the ambient temoperature based on the surface temperature |
MrBedfordVan | 0:b9164b348919 | 50 | * of the various chips on the micro:bit. |
MrBedfordVan | 0:b9164b348919 | 51 | * |
MrBedfordVan | 0:b9164b348919 | 52 | */ |
MrBedfordVan | 0:b9164b348919 | 53 | class MicroBitThermometer : public MicroBitComponent |
MrBedfordVan | 0:b9164b348919 | 54 | { |
MrBedfordVan | 0:b9164b348919 | 55 | unsigned long sampleTime; |
MrBedfordVan | 0:b9164b348919 | 56 | uint32_t samplePeriod; |
MrBedfordVan | 0:b9164b348919 | 57 | int16_t temperature; |
MrBedfordVan | 0:b9164b348919 | 58 | int16_t offset; |
MrBedfordVan | 0:b9164b348919 | 59 | MicroBitStorage* storage; |
MrBedfordVan | 0:b9164b348919 | 60 | |
MrBedfordVan | 0:b9164b348919 | 61 | public: |
MrBedfordVan | 0:b9164b348919 | 62 | |
MrBedfordVan | 0:b9164b348919 | 63 | /** |
MrBedfordVan | 0:b9164b348919 | 64 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 65 | * Create new MicroBitThermometer that gives an indication of the current temperature. |
MrBedfordVan | 0:b9164b348919 | 66 | * |
MrBedfordVan | 0:b9164b348919 | 67 | * @param _storage an instance of MicroBitStorage used to persist temperature offset data |
MrBedfordVan | 0:b9164b348919 | 68 | * |
MrBedfordVan | 0:b9164b348919 | 69 | * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER. |
MrBedfordVan | 0:b9164b348919 | 70 | * |
MrBedfordVan | 0:b9164b348919 | 71 | * @code |
MrBedfordVan | 0:b9164b348919 | 72 | * MicroBitStorage storage; |
MrBedfordVan | 0:b9164b348919 | 73 | * MicroBitThermometer thermometer(storage); |
MrBedfordVan | 0:b9164b348919 | 74 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 75 | */ |
MrBedfordVan | 0:b9164b348919 | 76 | MicroBitThermometer(MicroBitStorage& _storage, uint16_t id = MICROBIT_ID_THERMOMETER); |
MrBedfordVan | 0:b9164b348919 | 77 | |
MrBedfordVan | 0:b9164b348919 | 78 | /** |
MrBedfordVan | 0:b9164b348919 | 79 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 80 | * Create new MicroBitThermometer that gives an indication of the current temperature. |
MrBedfordVan | 0:b9164b348919 | 81 | * |
MrBedfordVan | 0:b9164b348919 | 82 | * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_THERMOMETER. |
MrBedfordVan | 0:b9164b348919 | 83 | * |
MrBedfordVan | 0:b9164b348919 | 84 | * @code |
MrBedfordVan | 0:b9164b348919 | 85 | * MicroBitThermometer thermometer; |
MrBedfordVan | 0:b9164b348919 | 86 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 87 | */ |
MrBedfordVan | 0:b9164b348919 | 88 | MicroBitThermometer(uint16_t id = MICROBIT_ID_THERMOMETER); |
MrBedfordVan | 0:b9164b348919 | 89 | |
MrBedfordVan | 0:b9164b348919 | 90 | /** |
MrBedfordVan | 0:b9164b348919 | 91 | * Set the sample rate at which the temperatureis read (in ms). |
MrBedfordVan | 0:b9164b348919 | 92 | * |
MrBedfordVan | 0:b9164b348919 | 93 | * The default sample period is 1 second. |
MrBedfordVan | 0:b9164b348919 | 94 | * |
MrBedfordVan | 0:b9164b348919 | 95 | * @param period the requested time between samples, in milliseconds. |
MrBedfordVan | 0:b9164b348919 | 96 | * |
MrBedfordVan | 0:b9164b348919 | 97 | * @note the temperature is always read in the background, and is only updated |
MrBedfordVan | 0:b9164b348919 | 98 | * when the processor is idle, or when the temperature is explicitly read. |
MrBedfordVan | 0:b9164b348919 | 99 | */ |
MrBedfordVan | 0:b9164b348919 | 100 | void setPeriod(int period); |
MrBedfordVan | 0:b9164b348919 | 101 | |
MrBedfordVan | 0:b9164b348919 | 102 | /** |
MrBedfordVan | 0:b9164b348919 | 103 | * Reads the currently configured sample rate of the thermometer. |
MrBedfordVan | 0:b9164b348919 | 104 | * |
MrBedfordVan | 0:b9164b348919 | 105 | * @return The time between samples, in milliseconds. |
MrBedfordVan | 0:b9164b348919 | 106 | */ |
MrBedfordVan | 0:b9164b348919 | 107 | int getPeriod(); |
MrBedfordVan | 0:b9164b348919 | 108 | |
MrBedfordVan | 0:b9164b348919 | 109 | /** |
MrBedfordVan | 0:b9164b348919 | 110 | * Set the value that is used to offset the raw silicon temperature. |
MrBedfordVan | 0:b9164b348919 | 111 | * |
MrBedfordVan | 0:b9164b348919 | 112 | * @param offset the offset for the silicon temperature |
MrBedfordVan | 0:b9164b348919 | 113 | * |
MrBedfordVan | 0:b9164b348919 | 114 | * @return MICROBIT_OK on success |
MrBedfordVan | 0:b9164b348919 | 115 | */ |
MrBedfordVan | 0:b9164b348919 | 116 | int setOffset(int offset); |
MrBedfordVan | 0:b9164b348919 | 117 | |
MrBedfordVan | 0:b9164b348919 | 118 | /** |
MrBedfordVan | 0:b9164b348919 | 119 | * Retreive the value that is used to offset the raw silicon temperature. |
MrBedfordVan | 0:b9164b348919 | 120 | * |
MrBedfordVan | 0:b9164b348919 | 121 | * @return the current offset. |
MrBedfordVan | 0:b9164b348919 | 122 | */ |
MrBedfordVan | 0:b9164b348919 | 123 | int getOffset(); |
MrBedfordVan | 0:b9164b348919 | 124 | |
MrBedfordVan | 0:b9164b348919 | 125 | /** |
MrBedfordVan | 0:b9164b348919 | 126 | * This member function fetches the raw silicon temperature, and calculates |
MrBedfordVan | 0:b9164b348919 | 127 | * the value used to offset the raw silicon temperature based on a given temperature. |
MrBedfordVan | 0:b9164b348919 | 128 | * |
MrBedfordVan | 0:b9164b348919 | 129 | * @param calibrationTemp the temperature used to calculate the raw silicon temperature |
MrBedfordVan | 0:b9164b348919 | 130 | * offset. |
MrBedfordVan | 0:b9164b348919 | 131 | * |
MrBedfordVan | 0:b9164b348919 | 132 | * @return MICROBIT_OK on success |
MrBedfordVan | 0:b9164b348919 | 133 | */ |
MrBedfordVan | 0:b9164b348919 | 134 | int setCalibration(int calibrationTemp); |
MrBedfordVan | 0:b9164b348919 | 135 | |
MrBedfordVan | 0:b9164b348919 | 136 | /** |
MrBedfordVan | 0:b9164b348919 | 137 | * Gets the current temperature of the microbit. |
MrBedfordVan | 0:b9164b348919 | 138 | * |
MrBedfordVan | 0:b9164b348919 | 139 | * @return the current temperature, in degrees celsius. |
MrBedfordVan | 0:b9164b348919 | 140 | * |
MrBedfordVan | 0:b9164b348919 | 141 | * @code |
MrBedfordVan | 0:b9164b348919 | 142 | * thermometer.getTemperature(); |
MrBedfordVan | 0:b9164b348919 | 143 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 144 | */ |
MrBedfordVan | 0:b9164b348919 | 145 | int getTemperature(); |
MrBedfordVan | 0:b9164b348919 | 146 | |
MrBedfordVan | 0:b9164b348919 | 147 | /** |
MrBedfordVan | 0:b9164b348919 | 148 | * Updates the temperature sample of this instance of MicroBitThermometer |
MrBedfordVan | 0:b9164b348919 | 149 | * only if isSampleNeeded() indicates that an update is required. |
MrBedfordVan | 0:b9164b348919 | 150 | * |
MrBedfordVan | 0:b9164b348919 | 151 | * This call also will add the thermometer to fiber components to receive |
MrBedfordVan | 0:b9164b348919 | 152 | * periodic callbacks. |
MrBedfordVan | 0:b9164b348919 | 153 | * |
MrBedfordVan | 0:b9164b348919 | 154 | * @return MICROBIT_OK on success. |
MrBedfordVan | 0:b9164b348919 | 155 | */ |
MrBedfordVan | 0:b9164b348919 | 156 | int updateSample(); |
MrBedfordVan | 0:b9164b348919 | 157 | |
MrBedfordVan | 0:b9164b348919 | 158 | /** |
MrBedfordVan | 0:b9164b348919 | 159 | * Periodic callback from MicroBit idle thread. |
MrBedfordVan | 0:b9164b348919 | 160 | */ |
MrBedfordVan | 0:b9164b348919 | 161 | virtual void idleTick(); |
MrBedfordVan | 0:b9164b348919 | 162 | |
MrBedfordVan | 0:b9164b348919 | 163 | private: |
MrBedfordVan | 0:b9164b348919 | 164 | |
MrBedfordVan | 0:b9164b348919 | 165 | /** |
MrBedfordVan | 0:b9164b348919 | 166 | * Determines if we're due to take another temperature reading |
MrBedfordVan | 0:b9164b348919 | 167 | * |
MrBedfordVan | 0:b9164b348919 | 168 | * @return 1 if we're due to take a temperature reading, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 169 | */ |
MrBedfordVan | 0:b9164b348919 | 170 | int isSampleNeeded(); |
MrBedfordVan | 0:b9164b348919 | 171 | }; |
MrBedfordVan | 0:b9164b348919 | 172 | |
MrBedfordVan | 0:b9164b348919 | 173 | #endif |