This is a repository for all my programs or modified programs.

Committer:
mturner5
Date:
Sun Sep 11 23:48:09 2016 +0000
Revision:
0:72480818e4a9
Made delays for the LEDS and button presses

Who changed what in which revision?

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