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