Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

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