Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

UserRevisionLine numberNew 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_TICKER_H
ethaderu 3:78f223d34f36 17 #define MBED_TICKER_H
ethaderu 3:78f223d34f36 18
ethaderu 3:78f223d34f36 19 #include "TimerEvent.h"
ethaderu 3:78f223d34f36 20 #include "FunctionPointer.h"
ethaderu 3:78f223d34f36 21
ethaderu 3:78f223d34f36 22 namespace mbed {
ethaderu 3:78f223d34f36 23
ethaderu 3:78f223d34f36 24 /** A Ticker is used to call a function at a recurring interval
ethaderu 3:78f223d34f36 25 *
ethaderu 3:78f223d34f36 26 * You can use as many seperate Ticker objects as you require.
ethaderu 3:78f223d34f36 27 *
ethaderu 3:78f223d34f36 28 * Example:
ethaderu 3:78f223d34f36 29 * @code
ethaderu 3:78f223d34f36 30 * // Toggle the blinking led after 5 seconds
ethaderu 3:78f223d34f36 31 *
ethaderu 3:78f223d34f36 32 * #include "mbed.h"
ethaderu 3:78f223d34f36 33 *
ethaderu 3:78f223d34f36 34 * Ticker timer;
ethaderu 3:78f223d34f36 35 * DigitalOut led1(LED1);
ethaderu 3:78f223d34f36 36 * DigitalOut led2(LED2);
ethaderu 3:78f223d34f36 37 *
ethaderu 3:78f223d34f36 38 * int flip = 0;
ethaderu 3:78f223d34f36 39 *
ethaderu 3:78f223d34f36 40 * void attime() {
ethaderu 3:78f223d34f36 41 * flip = !flip;
ethaderu 3:78f223d34f36 42 * }
ethaderu 3:78f223d34f36 43 *
ethaderu 3:78f223d34f36 44 * int main() {
ethaderu 3:78f223d34f36 45 * timer.attach(&attime, 5);
ethaderu 3:78f223d34f36 46 * while(1) {
ethaderu 3:78f223d34f36 47 * if(flip == 0) {
ethaderu 3:78f223d34f36 48 * led1 = !led1;
ethaderu 3:78f223d34f36 49 * } else {
ethaderu 3:78f223d34f36 50 * led2 = !led2;
ethaderu 3:78f223d34f36 51 * }
ethaderu 3:78f223d34f36 52 * wait(0.2);
ethaderu 3:78f223d34f36 53 * }
ethaderu 3:78f223d34f36 54 * }
ethaderu 3:78f223d34f36 55 * @endcode
ethaderu 3:78f223d34f36 56 */
ethaderu 3:78f223d34f36 57 class Ticker : public TimerEvent {
ethaderu 3:78f223d34f36 58
ethaderu 3:78f223d34f36 59 public:
ethaderu 3:78f223d34f36 60 Ticker() : TimerEvent() {
ethaderu 3:78f223d34f36 61 }
ethaderu 3:78f223d34f36 62
ethaderu 3:78f223d34f36 63 Ticker(const ticker_data_t *data) : TimerEvent(data) {
ethaderu 3:78f223d34f36 64 }
ethaderu 3:78f223d34f36 65
ethaderu 3:78f223d34f36 66 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
ethaderu 3:78f223d34f36 67 *
ethaderu 3:78f223d34f36 68 * @param fptr pointer to the function to be called
ethaderu 3:78f223d34f36 69 * @param t the time between calls in seconds
ethaderu 3:78f223d34f36 70 */
ethaderu 3:78f223d34f36 71 void attach(void (*fptr)(void), float t) {
ethaderu 3:78f223d34f36 72 attach_us(fptr, t * 1000000.0f);
ethaderu 3:78f223d34f36 73 }
ethaderu 3:78f223d34f36 74
ethaderu 3:78f223d34f36 75 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
ethaderu 3:78f223d34f36 76 *
ethaderu 3:78f223d34f36 77 * @param tptr pointer to the object to call the member function on
ethaderu 3:78f223d34f36 78 * @param mptr pointer to the member function to be called
ethaderu 3:78f223d34f36 79 * @param t the time between calls in seconds
ethaderu 3:78f223d34f36 80 */
ethaderu 3:78f223d34f36 81 template<typename T>
ethaderu 3:78f223d34f36 82 void attach(T* tptr, void (T::*mptr)(void), float t) {
ethaderu 3:78f223d34f36 83 attach_us(tptr, mptr, t * 1000000.0f);
ethaderu 3:78f223d34f36 84 }
ethaderu 3:78f223d34f36 85
ethaderu 3:78f223d34f36 86 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
ethaderu 3:78f223d34f36 87 *
ethaderu 3:78f223d34f36 88 * @param fptr pointer to the function to be called
ethaderu 3:78f223d34f36 89 * @param t the time between calls in micro-seconds
ethaderu 3:78f223d34f36 90 */
ethaderu 3:78f223d34f36 91 void attach_us(void (*fptr)(void), timestamp_t t) {
ethaderu 3:78f223d34f36 92 _function.attach(fptr);
ethaderu 3:78f223d34f36 93 setup(t);
ethaderu 3:78f223d34f36 94 }
ethaderu 3:78f223d34f36 95
ethaderu 3:78f223d34f36 96 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
ethaderu 3:78f223d34f36 97 *
ethaderu 3:78f223d34f36 98 * @param tptr pointer to the object to call the member function on
ethaderu 3:78f223d34f36 99 * @param mptr pointer to the member function to be called
ethaderu 3:78f223d34f36 100 * @param t the time between calls in micro-seconds
ethaderu 3:78f223d34f36 101 */
ethaderu 3:78f223d34f36 102 template<typename T>
ethaderu 3:78f223d34f36 103 void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
ethaderu 3:78f223d34f36 104 _function.attach(tptr, mptr);
ethaderu 3:78f223d34f36 105 setup(t);
ethaderu 3:78f223d34f36 106 }
ethaderu 3:78f223d34f36 107
ethaderu 3:78f223d34f36 108 virtual ~Ticker() {
ethaderu 3:78f223d34f36 109 detach();
ethaderu 3:78f223d34f36 110 }
ethaderu 3:78f223d34f36 111
ethaderu 3:78f223d34f36 112 /** Detach the function
ethaderu 3:78f223d34f36 113 */
ethaderu 3:78f223d34f36 114 void detach();
ethaderu 3:78f223d34f36 115
ethaderu 3:78f223d34f36 116 protected:
ethaderu 3:78f223d34f36 117 void setup(timestamp_t t);
ethaderu 3:78f223d34f36 118 virtual void handler();
ethaderu 3:78f223d34f36 119
ethaderu 3:78f223d34f36 120 protected:
ethaderu 3:78f223d34f36 121 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
ethaderu 3:78f223d34f36 122 FunctionPointer _function; /**< Callback. */
ethaderu 3:78f223d34f36 123 };
ethaderu 3:78f223d34f36 124
ethaderu 3:78f223d34f36 125 } // namespace mbed
ethaderu 3:78f223d34f36 126
ethaderu 3:78f223d34f36 127 #endif