IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbrahmbhatt6 0:2f388b030837 1 /* mbed Microcontroller Library
kbrahmbhatt6 0:2f388b030837 2 * Copyright (c) 2006-2013 ARM Limited
kbrahmbhatt6 0:2f388b030837 3 *
kbrahmbhatt6 0:2f388b030837 4 * Licensed under the Apache License, Version 2.0 (the "License");
kbrahmbhatt6 0:2f388b030837 5 * you may not use this file except in compliance with the License.
kbrahmbhatt6 0:2f388b030837 6 * You may obtain a copy of the License at
kbrahmbhatt6 0:2f388b030837 7 *
kbrahmbhatt6 0:2f388b030837 8 * http://www.apache.org/licenses/LICENSE-2.0
kbrahmbhatt6 0:2f388b030837 9 *
kbrahmbhatt6 0:2f388b030837 10 * Unless required by applicable law or agreed to in writing, software
kbrahmbhatt6 0:2f388b030837 11 * distributed under the License is distributed on an "AS IS" BASIS,
kbrahmbhatt6 0:2f388b030837 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kbrahmbhatt6 0:2f388b030837 13 * See the License for the specific language governing permissions and
kbrahmbhatt6 0:2f388b030837 14 * limitations under the License.
kbrahmbhatt6 0:2f388b030837 15 */
kbrahmbhatt6 0:2f388b030837 16 #ifndef MBED_TICKER_H
kbrahmbhatt6 0:2f388b030837 17 #define MBED_TICKER_H
kbrahmbhatt6 0:2f388b030837 18
kbrahmbhatt6 0:2f388b030837 19 #include "TimerEvent.h"
kbrahmbhatt6 0:2f388b030837 20 #include "FunctionPointer.h"
kbrahmbhatt6 0:2f388b030837 21
kbrahmbhatt6 0:2f388b030837 22 namespace mbed {
kbrahmbhatt6 0:2f388b030837 23
kbrahmbhatt6 0:2f388b030837 24 /** A Ticker is used to call a function at a recurring interval
kbrahmbhatt6 0:2f388b030837 25 *
kbrahmbhatt6 0:2f388b030837 26 * You can use as many seperate Ticker objects as you require.
kbrahmbhatt6 0:2f388b030837 27 *
kbrahmbhatt6 0:2f388b030837 28 * Example:
kbrahmbhatt6 0:2f388b030837 29 * @code
kbrahmbhatt6 0:2f388b030837 30 * // Toggle the blinking led after 5 seconds
kbrahmbhatt6 0:2f388b030837 31 *
kbrahmbhatt6 0:2f388b030837 32 * #include "mbed.h"
kbrahmbhatt6 0:2f388b030837 33 *
kbrahmbhatt6 0:2f388b030837 34 * Ticker timer;
kbrahmbhatt6 0:2f388b030837 35 * DigitalOut led1(LED1);
kbrahmbhatt6 0:2f388b030837 36 * DigitalOut led2(LED2);
kbrahmbhatt6 0:2f388b030837 37 *
kbrahmbhatt6 0:2f388b030837 38 * int flip = 0;
kbrahmbhatt6 0:2f388b030837 39 *
kbrahmbhatt6 0:2f388b030837 40 * void attime() {
kbrahmbhatt6 0:2f388b030837 41 * flip = !flip;
kbrahmbhatt6 0:2f388b030837 42 * }
kbrahmbhatt6 0:2f388b030837 43 *
kbrahmbhatt6 0:2f388b030837 44 * int main() {
kbrahmbhatt6 0:2f388b030837 45 * timer.attach(&attime, 5);
kbrahmbhatt6 0:2f388b030837 46 * while(1) {
kbrahmbhatt6 0:2f388b030837 47 * if(flip == 0) {
kbrahmbhatt6 0:2f388b030837 48 * led1 = !led1;
kbrahmbhatt6 0:2f388b030837 49 * } else {
kbrahmbhatt6 0:2f388b030837 50 * led2 = !led2;
kbrahmbhatt6 0:2f388b030837 51 * }
kbrahmbhatt6 0:2f388b030837 52 * wait(0.2);
kbrahmbhatt6 0:2f388b030837 53 * }
kbrahmbhatt6 0:2f388b030837 54 * }
kbrahmbhatt6 0:2f388b030837 55 * @endcode
kbrahmbhatt6 0:2f388b030837 56 */
kbrahmbhatt6 0:2f388b030837 57 class Ticker : public TimerEvent {
kbrahmbhatt6 0:2f388b030837 58
kbrahmbhatt6 0:2f388b030837 59 public:
kbrahmbhatt6 0:2f388b030837 60
kbrahmbhatt6 0:2f388b030837 61 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
kbrahmbhatt6 0:2f388b030837 62 *
kbrahmbhatt6 0:2f388b030837 63 * @param fptr pointer to the function to be called
kbrahmbhatt6 0:2f388b030837 64 * @param t the time between calls in seconds
kbrahmbhatt6 0:2f388b030837 65 */
kbrahmbhatt6 0:2f388b030837 66 void attach(void (*fptr)(void), float t) {
kbrahmbhatt6 0:2f388b030837 67 attach_us(fptr, t * 1000000.0f);
kbrahmbhatt6 0:2f388b030837 68 }
kbrahmbhatt6 0:2f388b030837 69
kbrahmbhatt6 0:2f388b030837 70 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
kbrahmbhatt6 0:2f388b030837 71 *
kbrahmbhatt6 0:2f388b030837 72 * @param tptr pointer to the object to call the member function on
kbrahmbhatt6 0:2f388b030837 73 * @param mptr pointer to the member function to be called
kbrahmbhatt6 0:2f388b030837 74 * @param t the time between calls in seconds
kbrahmbhatt6 0:2f388b030837 75 */
kbrahmbhatt6 0:2f388b030837 76 template<typename T>
kbrahmbhatt6 0:2f388b030837 77 void attach(T* tptr, void (T::*mptr)(void), float t) {
kbrahmbhatt6 0:2f388b030837 78 attach_us(tptr, mptr, t * 1000000.0f);
kbrahmbhatt6 0:2f388b030837 79 }
kbrahmbhatt6 0:2f388b030837 80
kbrahmbhatt6 0:2f388b030837 81 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
kbrahmbhatt6 0:2f388b030837 82 *
kbrahmbhatt6 0:2f388b030837 83 * @param fptr pointer to the function to be called
kbrahmbhatt6 0:2f388b030837 84 * @param t the time between calls in micro-seconds
kbrahmbhatt6 0:2f388b030837 85 */
kbrahmbhatt6 0:2f388b030837 86 void attach_us(void (*fptr)(void), unsigned int t) {
kbrahmbhatt6 0:2f388b030837 87 _function.attach(fptr);
kbrahmbhatt6 0:2f388b030837 88 setup(t);
kbrahmbhatt6 0:2f388b030837 89 }
kbrahmbhatt6 0:2f388b030837 90
kbrahmbhatt6 0:2f388b030837 91 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
kbrahmbhatt6 0:2f388b030837 92 *
kbrahmbhatt6 0:2f388b030837 93 * @param tptr pointer to the object to call the member function on
kbrahmbhatt6 0:2f388b030837 94 * @param mptr pointer to the member function to be called
kbrahmbhatt6 0:2f388b030837 95 * @param t the time between calls in micro-seconds
kbrahmbhatt6 0:2f388b030837 96 */
kbrahmbhatt6 0:2f388b030837 97 template<typename T>
kbrahmbhatt6 0:2f388b030837 98 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
kbrahmbhatt6 0:2f388b030837 99 _function.attach(tptr, mptr);
kbrahmbhatt6 0:2f388b030837 100 setup(t);
kbrahmbhatt6 0:2f388b030837 101 }
kbrahmbhatt6 0:2f388b030837 102
kbrahmbhatt6 0:2f388b030837 103 /** Detach the function
kbrahmbhatt6 0:2f388b030837 104 */
kbrahmbhatt6 0:2f388b030837 105 void detach();
kbrahmbhatt6 0:2f388b030837 106
kbrahmbhatt6 0:2f388b030837 107 protected:
kbrahmbhatt6 0:2f388b030837 108 void setup(unsigned int t);
kbrahmbhatt6 0:2f388b030837 109 virtual void handler();
kbrahmbhatt6 0:2f388b030837 110
kbrahmbhatt6 0:2f388b030837 111 unsigned int _delay;
kbrahmbhatt6 0:2f388b030837 112 FunctionPointer _function;
kbrahmbhatt6 0:2f388b030837 113 };
kbrahmbhatt6 0:2f388b030837 114
kbrahmbhatt6 0:2f388b030837 115 } // namespace mbed
kbrahmbhatt6 0:2f388b030837 116
kbrahmbhatt6 0:2f388b030837 117 #endif