Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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