mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Mon Aug 15 14:57:08 2016 +1000
Revision:
16:076cbe3e55be
Parent:
1:71204b8406f2
Added tag v1.0 for changeset 8123a070ade6

Who changed what in which revision?

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