customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

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