dev01 Brautlecht / mbed-STM32F030F4

Dependents:   STM32F031_Blink_Aug17

Fork of mbed-STM32F030F4 by Nothing Special

Committer:
mega64
Date:
Sat Oct 18 02:40:17 2014 +0000
Revision:
0:38ccae254a29
only for STM32F030F4

Who changed what in which revision?

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