Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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