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