work.

Dependencies:   Blynk mbed

Committer:
lixianyu
Date:
Wed Jun 15 03:08:40 2016 +0000
Revision:
2:6cd3b0947188
PM2.5????????5V??????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 2:6cd3b0947188 1 /*
lixianyu 2:6cd3b0947188 2 * SimpleTimer.h
lixianyu 2:6cd3b0947188 3 *
lixianyu 2:6cd3b0947188 4 * SimpleTimer - A timer library for Arduino.
lixianyu 2:6cd3b0947188 5 * Author: mromani@ottotecnica.com
lixianyu 2:6cd3b0947188 6 * Copyright (c) 2010 OTTOTECNICA Italy
lixianyu 2:6cd3b0947188 7 *
lixianyu 2:6cd3b0947188 8 * This library is free software; you can redistribute it
lixianyu 2:6cd3b0947188 9 * and/or modify it under the terms of the GNU Lesser
lixianyu 2:6cd3b0947188 10 * General Public License as published by the Free Software
lixianyu 2:6cd3b0947188 11 * Foundation; either version 2.1 of the License, or (at
lixianyu 2:6cd3b0947188 12 * your option) any later version.
lixianyu 2:6cd3b0947188 13 *
lixianyu 2:6cd3b0947188 14 * This library is distributed in the hope that it will
lixianyu 2:6cd3b0947188 15 * be useful, but WITHOUT ANY WARRANTY; without even the
lixianyu 2:6cd3b0947188 16 * implied warranty of MERCHANTABILITY or FITNESS FOR A
lixianyu 2:6cd3b0947188 17 * PARTICULAR PURPOSE. See the GNU Lesser General Public
lixianyu 2:6cd3b0947188 18 * License for more details.
lixianyu 2:6cd3b0947188 19 *
lixianyu 2:6cd3b0947188 20 * You should have received a copy of the GNU Lesser
lixianyu 2:6cd3b0947188 21 * General Public License along with this library; if not,
lixianyu 2:6cd3b0947188 22 * write to the Free Software Foundation, Inc.,
lixianyu 2:6cd3b0947188 23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lixianyu 2:6cd3b0947188 24 *
lixianyu 2:6cd3b0947188 25 */
lixianyu 2:6cd3b0947188 26
lixianyu 2:6cd3b0947188 27
lixianyu 2:6cd3b0947188 28 #ifndef SIMPLETIMER_H
lixianyu 2:6cd3b0947188 29 #define SIMPLETIMER_H
lixianyu 2:6cd3b0947188 30
lixianyu 2:6cd3b0947188 31 //#include <functional>
lixianyu 2:6cd3b0947188 32
lixianyu 2:6cd3b0947188 33 //typedef std::function<void(void)> timer_callback;
lixianyu 2:6cd3b0947188 34 typedef void (*timer_callback)(void);
lixianyu 2:6cd3b0947188 35
lixianyu 2:6cd3b0947188 36 class SimpleTimer {
lixianyu 2:6cd3b0947188 37
lixianyu 2:6cd3b0947188 38 public:
lixianyu 2:6cd3b0947188 39 // maximum number of timers
lixianyu 2:6cd3b0947188 40 const static int MAX_TIMERS = 10;
lixianyu 2:6cd3b0947188 41
lixianyu 2:6cd3b0947188 42 // setTimer() constants
lixianyu 2:6cd3b0947188 43 const static int RUN_FOREVER = 0;
lixianyu 2:6cd3b0947188 44 const static int RUN_ONCE = 1;
lixianyu 2:6cd3b0947188 45
lixianyu 2:6cd3b0947188 46 // constructor
lixianyu 2:6cd3b0947188 47 SimpleTimer(Timer &timer);
lixianyu 2:6cd3b0947188 48
lixianyu 2:6cd3b0947188 49 // this function must be called inside loop()
lixianyu 2:6cd3b0947188 50 void run();
lixianyu 2:6cd3b0947188 51
lixianyu 2:6cd3b0947188 52 // call function f every d milliseconds
lixianyu 2:6cd3b0947188 53 int setInterval(int d, timer_callback f);
lixianyu 2:6cd3b0947188 54
lixianyu 2:6cd3b0947188 55 // call function f once after d milliseconds
lixianyu 2:6cd3b0947188 56 int setTimeout(int d, timer_callback f);
lixianyu 2:6cd3b0947188 57
lixianyu 2:6cd3b0947188 58 // call function f every d milliseconds for n times
lixianyu 2:6cd3b0947188 59 int setTimer(int d, timer_callback f, int n);
lixianyu 2:6cd3b0947188 60
lixianyu 2:6cd3b0947188 61 // destroy the specified timer
lixianyu 2:6cd3b0947188 62 void deleteTimer(int numTimer);
lixianyu 2:6cd3b0947188 63
lixianyu 2:6cd3b0947188 64 // restart the specified timer
lixianyu 2:6cd3b0947188 65 void restartTimer(int numTimer);
lixianyu 2:6cd3b0947188 66
lixianyu 2:6cd3b0947188 67 // returns true if the specified timer is enabled
lixianyu 2:6cd3b0947188 68 bool isEnabled(int numTimer);
lixianyu 2:6cd3b0947188 69
lixianyu 2:6cd3b0947188 70 // enables the specified timer
lixianyu 2:6cd3b0947188 71 void enable(int numTimer);
lixianyu 2:6cd3b0947188 72
lixianyu 2:6cd3b0947188 73 // disables the specified timer
lixianyu 2:6cd3b0947188 74 void disable(int numTimer);
lixianyu 2:6cd3b0947188 75
lixianyu 2:6cd3b0947188 76 // enables the specified timer if it's currently disabled,
lixianyu 2:6cd3b0947188 77 // and vice-versa
lixianyu 2:6cd3b0947188 78 void toggle(int numTimer);
lixianyu 2:6cd3b0947188 79
lixianyu 2:6cd3b0947188 80 // returns the number of used timers
lixianyu 2:6cd3b0947188 81 int getNumTimers();
lixianyu 2:6cd3b0947188 82
lixianyu 2:6cd3b0947188 83 // returns the number of available timers
lixianyu 2:6cd3b0947188 84 int getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
lixianyu 2:6cd3b0947188 85
lixianyu 2:6cd3b0947188 86 private:
lixianyu 2:6cd3b0947188 87 int elapsed();
lixianyu 2:6cd3b0947188 88
lixianyu 2:6cd3b0947188 89 // deferred call constants
lixianyu 2:6cd3b0947188 90 const static int DEFCALL_DONTRUN = 0; // don't call the callback function
lixianyu 2:6cd3b0947188 91 const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer
lixianyu 2:6cd3b0947188 92 const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer
lixianyu 2:6cd3b0947188 93
lixianyu 2:6cd3b0947188 94 // find the first available slot
lixianyu 2:6cd3b0947188 95 int findFirstFreeSlot();
lixianyu 2:6cd3b0947188 96
lixianyu 2:6cd3b0947188 97 // value returned by the millis() function
lixianyu 2:6cd3b0947188 98 // in the previous run() call
lixianyu 2:6cd3b0947188 99 int prev_millis[MAX_TIMERS];
lixianyu 2:6cd3b0947188 100
lixianyu 2:6cd3b0947188 101 // pointers to the callback functions
lixianyu 2:6cd3b0947188 102 timer_callback callbacks[MAX_TIMERS];
lixianyu 2:6cd3b0947188 103
lixianyu 2:6cd3b0947188 104 // delay values
lixianyu 2:6cd3b0947188 105 int delays[MAX_TIMERS];
lixianyu 2:6cd3b0947188 106
lixianyu 2:6cd3b0947188 107 // number of runs to be executed for each timer
lixianyu 2:6cd3b0947188 108 int maxNumRuns[MAX_TIMERS];
lixianyu 2:6cd3b0947188 109
lixianyu 2:6cd3b0947188 110 // number of executed runs for each timer
lixianyu 2:6cd3b0947188 111 int numRuns[MAX_TIMERS];
lixianyu 2:6cd3b0947188 112
lixianyu 2:6cd3b0947188 113 // which timers are enabled
lixianyu 2:6cd3b0947188 114 bool enabled[MAX_TIMERS];
lixianyu 2:6cd3b0947188 115
lixianyu 2:6cd3b0947188 116 // deferred function call (sort of) - N.B.: this array is only used in run()
lixianyu 2:6cd3b0947188 117 int toBeCalled[MAX_TIMERS];
lixianyu 2:6cd3b0947188 118
lixianyu 2:6cd3b0947188 119 // actual number of timers in use
lixianyu 2:6cd3b0947188 120 int numTimers;
lixianyu 2:6cd3b0947188 121
lixianyu 2:6cd3b0947188 122 Timer &_timer;
lixianyu 2:6cd3b0947188 123 };
lixianyu 2:6cd3b0947188 124
lixianyu 2:6cd3b0947188 125 #endif
lixianyu 2:6cd3b0947188 126