mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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