mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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