FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.

Dependencies:   mbed FXOS8700CQ MODSERIAL

Committer:
jwhammel
Date:
Mon Apr 29 04:24:38 2019 +0000
Revision:
85:0cf65ceb4492
Parent:
84:fc8c9b39723a
We have added an alarm trigger that gets sent to AT&T Flow.

Who changed what in which revision?

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