Microduino

Dependencies:   mbed

Fork of Io_moon by Li Weiyi

Committer:
lixianyu
Date:
Fri Jun 24 02:06:43 2016 +0000
Revision:
1:e34100dd6532
Parent:
0:740c1eb2df13
?Arduino??????????0~255??????LPC824????????????????

Who changed what in which revision?

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