Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/mbed-dev-bin/Timer.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 | /* mbed Microcontroller Library |
MrBedfordVan | 0:b9164b348919 | 2 | * Copyright (c) 2006-2013 ARM Limited |
MrBedfordVan | 0:b9164b348919 | 3 | * |
MrBedfordVan | 0:b9164b348919 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MrBedfordVan | 0:b9164b348919 | 5 | * you may not use this file except in compliance with the License. |
MrBedfordVan | 0:b9164b348919 | 6 | * You may obtain a copy of the License at |
MrBedfordVan | 0:b9164b348919 | 7 | * |
MrBedfordVan | 0:b9164b348919 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MrBedfordVan | 0:b9164b348919 | 9 | * |
MrBedfordVan | 0:b9164b348919 | 10 | * Unless required by applicable law or agreed to in writing, software |
MrBedfordVan | 0:b9164b348919 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MrBedfordVan | 0:b9164b348919 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MrBedfordVan | 0:b9164b348919 | 13 | * See the License for the specific language governing permissions and |
MrBedfordVan | 0:b9164b348919 | 14 | * limitations under the License. |
MrBedfordVan | 0:b9164b348919 | 15 | */ |
MrBedfordVan | 0:b9164b348919 | 16 | #ifndef MBED_TIMER_H |
MrBedfordVan | 0:b9164b348919 | 17 | #define MBED_TIMER_H |
MrBedfordVan | 0:b9164b348919 | 18 | |
MrBedfordVan | 0:b9164b348919 | 19 | #include "platform.h" |
MrBedfordVan | 0:b9164b348919 | 20 | #include "ticker_api.h" |
MrBedfordVan | 0:b9164b348919 | 21 | |
MrBedfordVan | 0:b9164b348919 | 22 | namespace mbed { |
MrBedfordVan | 0:b9164b348919 | 23 | |
MrBedfordVan | 0:b9164b348919 | 24 | /** A general purpose timer |
MrBedfordVan | 0:b9164b348919 | 25 | * |
MrBedfordVan | 0:b9164b348919 | 26 | * Example: |
MrBedfordVan | 0:b9164b348919 | 27 | * @code |
MrBedfordVan | 0:b9164b348919 | 28 | * // Count the time to toggle a LED |
MrBedfordVan | 0:b9164b348919 | 29 | * |
MrBedfordVan | 0:b9164b348919 | 30 | * #include "mbed.h" |
MrBedfordVan | 0:b9164b348919 | 31 | * |
MrBedfordVan | 0:b9164b348919 | 32 | * Timer timer; |
MrBedfordVan | 0:b9164b348919 | 33 | * DigitalOut led(LED1); |
MrBedfordVan | 0:b9164b348919 | 34 | * int begin, end; |
MrBedfordVan | 0:b9164b348919 | 35 | * |
MrBedfordVan | 0:b9164b348919 | 36 | * int main() { |
MrBedfordVan | 0:b9164b348919 | 37 | * timer.start(); |
MrBedfordVan | 0:b9164b348919 | 38 | * begin = timer.read_us(); |
MrBedfordVan | 0:b9164b348919 | 39 | * led = !led; |
MrBedfordVan | 0:b9164b348919 | 40 | * end = timer.read_us(); |
MrBedfordVan | 0:b9164b348919 | 41 | * printf("Toggle the led takes %d us", end - begin); |
MrBedfordVan | 0:b9164b348919 | 42 | * } |
MrBedfordVan | 0:b9164b348919 | 43 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 44 | */ |
MrBedfordVan | 0:b9164b348919 | 45 | class Timer { |
MrBedfordVan | 0:b9164b348919 | 46 | |
MrBedfordVan | 0:b9164b348919 | 47 | public: |
MrBedfordVan | 0:b9164b348919 | 48 | Timer(); |
MrBedfordVan | 0:b9164b348919 | 49 | Timer(const ticker_data_t *data); |
MrBedfordVan | 0:b9164b348919 | 50 | |
MrBedfordVan | 0:b9164b348919 | 51 | /** Start the timer |
MrBedfordVan | 0:b9164b348919 | 52 | */ |
MrBedfordVan | 0:b9164b348919 | 53 | void start(); |
MrBedfordVan | 0:b9164b348919 | 54 | |
MrBedfordVan | 0:b9164b348919 | 55 | /** Stop the timer |
MrBedfordVan | 0:b9164b348919 | 56 | */ |
MrBedfordVan | 0:b9164b348919 | 57 | void stop(); |
MrBedfordVan | 0:b9164b348919 | 58 | |
MrBedfordVan | 0:b9164b348919 | 59 | /** Reset the timer to 0. |
MrBedfordVan | 0:b9164b348919 | 60 | * |
MrBedfordVan | 0:b9164b348919 | 61 | * If it was already counting, it will continue |
MrBedfordVan | 0:b9164b348919 | 62 | */ |
MrBedfordVan | 0:b9164b348919 | 63 | void reset(); |
MrBedfordVan | 0:b9164b348919 | 64 | |
MrBedfordVan | 0:b9164b348919 | 65 | /** Get the time passed in seconds |
MrBedfordVan | 0:b9164b348919 | 66 | */ |
MrBedfordVan | 0:b9164b348919 | 67 | float read(); |
MrBedfordVan | 0:b9164b348919 | 68 | |
MrBedfordVan | 0:b9164b348919 | 69 | /** Get the time passed in mili-seconds |
MrBedfordVan | 0:b9164b348919 | 70 | */ |
MrBedfordVan | 0:b9164b348919 | 71 | int read_ms(); |
MrBedfordVan | 0:b9164b348919 | 72 | |
MrBedfordVan | 0:b9164b348919 | 73 | /** Get the time passed in micro-seconds |
MrBedfordVan | 0:b9164b348919 | 74 | */ |
MrBedfordVan | 0:b9164b348919 | 75 | int read_us(); |
MrBedfordVan | 0:b9164b348919 | 76 | |
MrBedfordVan | 0:b9164b348919 | 77 | #ifdef MBED_OPERATORS |
MrBedfordVan | 0:b9164b348919 | 78 | operator float(); |
MrBedfordVan | 0:b9164b348919 | 79 | #endif |
MrBedfordVan | 0:b9164b348919 | 80 | |
MrBedfordVan | 0:b9164b348919 | 81 | protected: |
MrBedfordVan | 0:b9164b348919 | 82 | int slicetime(); |
MrBedfordVan | 0:b9164b348919 | 83 | int _running; // whether the timer is running |
MrBedfordVan | 0:b9164b348919 | 84 | unsigned int _start; // the start time of the latest slice |
MrBedfordVan | 0:b9164b348919 | 85 | int _time; // any accumulated time from previous slices |
MrBedfordVan | 0:b9164b348919 | 86 | const ticker_data_t *_ticker_data; |
MrBedfordVan | 0:b9164b348919 | 87 | }; |
MrBedfordVan | 0:b9164b348919 | 88 | |
MrBedfordVan | 0:b9164b348919 | 89 | } // namespace mbed |
MrBedfordVan | 0:b9164b348919 | 90 | |
MrBedfordVan | 0:b9164b348919 | 91 | #endif |