A Ton (Timer On) is used to provide an \'on delay\' system so as to avoid using wait()

Dependents:   NEWlcd_menu_v2 Garage_Control

Committer:
AjK
Date:
Thu Mar 03 15:30:13 2011 +0000
Revision:
0:748acff4e3c8
Child:
1:55c67a056031
1.0 See ChangeLog.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:748acff4e3c8 1 /*
AjK 0:748acff4e3c8 2 Copyright (c) 2011 Andy Kirkham
AjK 0:748acff4e3c8 3
AjK 0:748acff4e3c8 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:748acff4e3c8 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:748acff4e3c8 6 in the Software without restriction, including without limitation the rights
AjK 0:748acff4e3c8 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:748acff4e3c8 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:748acff4e3c8 9 furnished to do so, subject to the following conditions:
AjK 0:748acff4e3c8 10
AjK 0:748acff4e3c8 11 The above copyright notice and this permission notice shall be included in
AjK 0:748acff4e3c8 12 all copies or substantial portions of the Software.
AjK 0:748acff4e3c8 13
AjK 0:748acff4e3c8 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:748acff4e3c8 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:748acff4e3c8 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:748acff4e3c8 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:748acff4e3c8 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:748acff4e3c8 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:748acff4e3c8 20 THE SOFTWARE.
AjK 0:748acff4e3c8 21 */
AjK 0:748acff4e3c8 22
AjK 0:748acff4e3c8 23 #ifndef AJK_TON_H
AjK 0:748acff4e3c8 24 #define AJK_TON_H
AjK 0:748acff4e3c8 25
AjK 0:748acff4e3c8 26 #include "mbed.h"
AjK 0:748acff4e3c8 27
AjK 0:748acff4e3c8 28 namespace AjK {
AjK 0:748acff4e3c8 29
AjK 0:748acff4e3c8 30 /** @defgroup API The Ton API */
AjK 0:748acff4e3c8 31
AjK 0:748acff4e3c8 32 /** Ton module
AjK 0:748acff4e3c8 33 *
AjK 0:748acff4e3c8 34 * @author Andy Kirkham
AjK 0:748acff4e3c8 35 * @see API
AjK 0:748acff4e3c8 36 * @see example1.h
AjK 0:748acff4e3c8 37 * @see example2.h
AjK 0:748acff4e3c8 38 *
AjK 0:748acff4e3c8 39 */
AjK 0:748acff4e3c8 40 class Ton {
AjK 0:748acff4e3c8 41
AjK 0:748acff4e3c8 42 protected:
AjK 0:748acff4e3c8 43 bool _lastInput;
AjK 0:748acff4e3c8 44 int _counter;
AjK 0:748acff4e3c8 45 int _reload;
AjK 0:748acff4e3c8 46 int _mode;
AjK 0:748acff4e3c8 47 Ticker _tick;
AjK 0:748acff4e3c8 48
AjK 0:748acff4e3c8 49 /** init
AjK 0:748acff4e3c8 50 * Used to init the properties.
AjK 0:748acff4e3c8 51 */
AjK 0:748acff4e3c8 52 void init(void) {
AjK 0:748acff4e3c8 53 _lastInput = false;
AjK 0:748acff4e3c8 54 _mode = InputResets;
AjK 0:748acff4e3c8 55 _tick.attach_us(this, &Ton::ticktock, 1000);
AjK 0:748acff4e3c8 56 }
AjK 0:748acff4e3c8 57
AjK 0:748acff4e3c8 58 public:
AjK 0:748acff4e3c8 59 friend class Ticker;
AjK 0:748acff4e3c8 60
AjK 0:748acff4e3c8 61 /** Mode
AjK 0:748acff4e3c8 62 */
AjK 0:748acff4e3c8 63 enum Mode {
AjK 0:748acff4e3c8 64 InputResets = 0, /*!< Loss of input resets the timer. */
AjK 0:748acff4e3c8 65 InputPauses /*!< Loss of input pauses the timer. */
AjK 0:748acff4e3c8 66 };
AjK 0:748acff4e3c8 67
AjK 0:748acff4e3c8 68 /** ticktock
AjK 0:748acff4e3c8 69 * Used by the Ticker object to call to once per millisecond.
AjK 0:748acff4e3c8 70 */
AjK 0:748acff4e3c8 71 void ticktock(void) {
AjK 0:748acff4e3c8 72 switch (_mode) {
AjK 0:748acff4e3c8 73 case InputResets:
AjK 0:748acff4e3c8 74 if (_lastInput) {
AjK 0:748acff4e3c8 75 if (_counter > 0) _counter--;
AjK 0:748acff4e3c8 76 }
AjK 0:748acff4e3c8 77 else {
AjK 0:748acff4e3c8 78 _counter = _reload;
AjK 0:748acff4e3c8 79 }
AjK 0:748acff4e3c8 80 break;
AjK 0:748acff4e3c8 81 case InputPauses:
AjK 0:748acff4e3c8 82 if (_lastInput) {
AjK 0:748acff4e3c8 83 if (_counter > 0) _counter--;
AjK 0:748acff4e3c8 84 }
AjK 0:748acff4e3c8 85 break;
AjK 0:748acff4e3c8 86 }
AjK 0:748acff4e3c8 87 }
AjK 0:748acff4e3c8 88
AjK 0:748acff4e3c8 89 /** setTime
AjK 0:748acff4e3c8 90 * Used to set the timer value, in milliseconds.
AjK 0:748acff4e3c8 91 * @ingroup API
AjK 0:748acff4e3c8 92 * @param int i The value to time.
AjK 0:748acff4e3c8 93 */
AjK 0:748acff4e3c8 94 void setTime(int i) { _counter = _reload = i; }
AjK 0:748acff4e3c8 95
AjK 0:748acff4e3c8 96 /** getTime
AjK 0:748acff4e3c8 97 * Used to get the timer value, in milliseconds.
AjK 0:748acff4e3c8 98 * @ingroup API
AjK 0:748acff4e3c8 99 * @return int The value to time.
AjK 0:748acff4e3c8 100 */
AjK 0:748acff4e3c8 101 int getTime(void) { return _reload; }
AjK 0:748acff4e3c8 102
AjK 0:748acff4e3c8 103 /** getTimeLeft
AjK 0:748acff4e3c8 104 * Used to get the timer value remaining, in milliseconds.
AjK 0:748acff4e3c8 105 * @ingroup API
AjK 0:748acff4e3c8 106 * @return int The time remaining to zero (timeout).
AjK 0:748acff4e3c8 107 */
AjK 0:748acff4e3c8 108 int getTimeLeft(void) { return _reload - _counter; }
AjK 0:748acff4e3c8 109
AjK 0:748acff4e3c8 110 /** setMode
AjK 0:748acff4e3c8 111 * Set the Ton operational mode.
AjK 0:748acff4e3c8 112 * Valid values are Ton::InputResets or Ton::InputPauses.
AjK 0:748acff4e3c8 113 * The default mode is Ton::InputPauses
AjK 0:748acff4e3c8 114 * @ingroup API
AjK 0:748acff4e3c8 115 * @see Mode
AjK 0:748acff4e3c8 116 * @param enum Mode
AjK 0:748acff4e3c8 117 */
AjK 0:748acff4e3c8 118 void setMode(Mode i) { _mode = i; }
AjK 0:748acff4e3c8 119
AjK 0:748acff4e3c8 120 /** reset
AjK 0:748acff4e3c8 121 * Resets the timer to the default value.
AjK 0:748acff4e3c8 122 * @ingroup API
AjK 0:748acff4e3c8 123 */
AjK 0:748acff4e3c8 124 void reset(void) { _counter = _reload; }
AjK 0:748acff4e3c8 125
AjK 0:748acff4e3c8 126 /** reset
AjK 0:748acff4e3c8 127 * Overloaded method, reset the timer to the value supplied.
AjK 0:748acff4e3c8 128 * @ingroup API
AjK 0:748acff4e3c8 129 * @param int i The time to count.
AjK 0:748acff4e3c8 130 */
AjK 0:748acff4e3c8 131 void reset(int i) { _counter = _reload = i; }
AjK 0:748acff4e3c8 132
AjK 0:748acff4e3c8 133 /** Constructor
AjK 0:748acff4e3c8 134 */
AjK 0:748acff4e3c8 135 Ton(int to, int mode = InputResets) { init(); _counter = _reload = to; _mode = mode; }
AjK 0:748acff4e3c8 136
AjK 0:748acff4e3c8 137 /** Destructor
AjK 0:748acff4e3c8 138 */
AjK 0:748acff4e3c8 139 ~Ton() { _tick.detach(); }
AjK 0:748acff4e3c8 140
AjK 0:748acff4e3c8 141 /** operator=
AjK 0:748acff4e3c8 142 * Controls the starting of the timer.
AjK 0:748acff4e3c8 143 * @ingroup API
AjK 0:748acff4e3c8 144 */
AjK 0:748acff4e3c8 145 Ton& operator= (int value) {
AjK 0:748acff4e3c8 146 _lastInput = value ? true : false;
AjK 0:748acff4e3c8 147 if (_mode == InputResets && !_lastInput) _counter = _reload;
AjK 0:748acff4e3c8 148 return *this;
AjK 0:748acff4e3c8 149 }
AjK 0:748acff4e3c8 150
AjK 0:748acff4e3c8 151 /** operator int()
AjK 0:748acff4e3c8 152 * @ingroup API
AjK 0:748acff4e3c8 153 * @return zero if not timed out yet, non-zero otherwise.
AjK 0:748acff4e3c8 154 */
AjK 0:748acff4e3c8 155 operator int() {
AjK 0:748acff4e3c8 156 return (_counter == 0) ? 1 : 0;
AjK 0:748acff4e3c8 157 }
AjK 0:748acff4e3c8 158
AjK 0:748acff4e3c8 159 };
AjK 0:748acff4e3c8 160
AjK 0:748acff4e3c8 161 }; // namespace AjK ends.
AjK 0:748acff4e3c8 162
AjK 0:748acff4e3c8 163 using namespace AjK;
AjK 0:748acff4e3c8 164
AjK 0:748acff4e3c8 165 #endif