A Ricker is a simple Ticker but using the RIT rather than Timer3

Committer:
AjK
Date:
Fri Mar 11 18:28:13 2011 +0000
Revision:
0:5684eed14bda
0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:5684eed14bda 1 /*
AjK 0:5684eed14bda 2 Copyright (c) 2011 Andy Kirkham
AjK 0:5684eed14bda 3
AjK 0:5684eed14bda 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:5684eed14bda 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:5684eed14bda 6 in the Software without restriction, including without limitation the rights
AjK 0:5684eed14bda 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:5684eed14bda 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:5684eed14bda 9 furnished to do so, subject to the following conditions:
AjK 0:5684eed14bda 10
AjK 0:5684eed14bda 11 The above copyright notice and this permission notice shall be included in
AjK 0:5684eed14bda 12 all copies or substantial portions of the Software.
AjK 0:5684eed14bda 13
AjK 0:5684eed14bda 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:5684eed14bda 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:5684eed14bda 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:5684eed14bda 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:5684eed14bda 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:5684eed14bda 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:5684eed14bda 20 THE SOFTWARE.
AjK 0:5684eed14bda 21 */
AjK 0:5684eed14bda 22
AjK 0:5684eed14bda 23 #ifndef AJK_RICKER_H
AjK 0:5684eed14bda 24 #define AJK_RICKER_H
AjK 0:5684eed14bda 25
AjK 0:5684eed14bda 26 #include "mbed.h"
AjK 0:5684eed14bda 27 #include <list>
AjK 0:5684eed14bda 28
AjK 0:5684eed14bda 29 namespace AjK {
AjK 0:5684eed14bda 30
AjK 0:5684eed14bda 31 class Ricker; // forward ref.
AjK 0:5684eed14bda 32
AjK 0:5684eed14bda 33 // Ricker controller.
AjK 0:5684eed14bda 34 /** RickerSys
AjK 0:5684eed14bda 35 *
AjK 0:5684eed14bda 36 * Base system, only a single instance of this is allowed. It's used to manage
AjK 0:5684eed14bda 37 * the RIT IRQ and the chain of "Rickers" attached.
AjK 0:5684eed14bda 38 *
AjK 0:5684eed14bda 39 * Note, unlike an Mbed Ticker the Ricker uses a 1ms resolution for it's timer.
AjK 0:5684eed14bda 40 * Therefore teh smallest unit of time that a Ricker can measure is 1ms. If
AjK 0:5684eed14bda 41 * you need sub-millisecond then use an Mbed Ticker.
AjK 0:5684eed14bda 42 */
AjK 0:5684eed14bda 43 class RickerSys {
AjK 0:5684eed14bda 44 protected:
AjK 0:5684eed14bda 45 list<Ricker *> rickers;
AjK 0:5684eed14bda 46 public:
AjK 0:5684eed14bda 47 void init(void);
AjK 0:5684eed14bda 48 RickerSys();
AjK 0:5684eed14bda 49 void isr(void);
AjK 0:5684eed14bda 50 void addTicker(Ricker *t);
AjK 0:5684eed14bda 51 void delTicker(Ricker *t);
AjK 0:5684eed14bda 52 };
AjK 0:5684eed14bda 53
AjK 0:5684eed14bda 54 extern RickerSys _rickerSys;
AjK 0:5684eed14bda 55
AjK 0:5684eed14bda 56 // Ricker instance object.
AjK 0:5684eed14bda 57 /** Ricker
AjK 0:5684eed14bda 58 *
AjK 0:5684eed14bda 59 * A class used to create multiple Rickers. These attach themselves to the RickerSys
AjK 0:5684eed14bda 60 * main controller.
AjK 0:5684eed14bda 61 *
AjK 0:5684eed14bda 62 * <b>Note</b>, unlike an Mbed Ticker, a Ricker can only make callbacks down to 1ms.
AjK 0:5684eed14bda 63 *
AjK 0:5684eed14bda 64 * @code
AjK 0:5684eed14bda 65 * #include "mbed.h"
AjK 0:5684eed14bda 66 * #include "Ricker.h"
AjK 0:5684eed14bda 67 *
AjK 0:5684eed14bda 68 * DigitalOut led1(LED1);
AjK 0:5684eed14bda 69 *
AjK 0:5684eed14bda 70 * Ricker r1;
AjK 0:5684eed14bda 71 *
AjK 0:5684eed14bda 72 * void cb1(void) { led1 = !led1; }
AjK 0:5684eed14bda 73 *
AjK 0:5684eed14bda 74 * int main() {
AjK 0:5684eed14bda 75 *
AjK 0:5684eed14bda 76 * r1.attach(&cb1, 0.25);
AjK 0:5684eed14bda 77 *
AjK 0:5684eed14bda 78 * while(1) { }
AjK 0:5684eed14bda 79 * }
AjK 0:5684eed14bda 80 * @endcode
AjK 0:5684eed14bda 81 */
AjK 0:5684eed14bda 82 class Ricker {
AjK 0:5684eed14bda 83 protected:
AjK 0:5684eed14bda 84 FunctionPointer callback;
AjK 0:5684eed14bda 85 uint32_t counter;
AjK 0:5684eed14bda 86 uint32_t reload;
AjK 0:5684eed14bda 87
AjK 0:5684eed14bda 88 /** tick
AjK 0:5684eed14bda 89 * @internal
AjK 0:5684eed14bda 90 * Called by the RickerSys controller every 1ms.
AjK 0:5684eed14bda 91 */
AjK 0:5684eed14bda 92 void tick(void) {
AjK 0:5684eed14bda 93 if (counter) {
AjK 0:5684eed14bda 94 counter--;
AjK 0:5684eed14bda 95 if (counter == 0) {
AjK 0:5684eed14bda 96 counter = reload;
AjK 0:5684eed14bda 97 callback.call();
AjK 0:5684eed14bda 98 }
AjK 0:5684eed14bda 99 }
AjK 0:5684eed14bda 100 }
AjK 0:5684eed14bda 101
AjK 0:5684eed14bda 102 public:
AjK 0:5684eed14bda 103 friend class RickerSys;
AjK 0:5684eed14bda 104
AjK 0:5684eed14bda 105 /** Constructor
AjK 0:5684eed14bda 106 */
AjK 0:5684eed14bda 107 Ricker() {
AjK 0:5684eed14bda 108 counter = 0;
AjK 0:5684eed14bda 109 _rickerSys.addTicker(this);
AjK 0:5684eed14bda 110 }
AjK 0:5684eed14bda 111
AjK 0:5684eed14bda 112 /** Destructor
AjK 0:5684eed14bda 113 */
AjK 0:5684eed14bda 114 ~Ricker() {
AjK 0:5684eed14bda 115 _rickerSys.delTicker(this);
AjK 0:5684eed14bda 116 }
AjK 0:5684eed14bda 117
AjK 0:5684eed14bda 118 /** detach
AjK 0:5684eed14bda 119 *
AjK 0:5684eed14bda 120 * Remove the callback from the Ricker.
AjK 0:5684eed14bda 121 */
AjK 0:5684eed14bda 122 void detach(void) {
AjK 0:5684eed14bda 123 callback.attach();
AjK 0:5684eed14bda 124 }
AjK 0:5684eed14bda 125
AjK 0:5684eed14bda 126 /** attach_ms
AjK 0:5684eed14bda 127 *
AjK 0:5684eed14bda 128 * Attach a C style function pointer callback.
AjK 0:5684eed14bda 129 *
AjK 0:5684eed14bda 130 * @param fptr A C style function pointer.
AjK 0:5684eed14bda 131 * @param uint32_t u The number of milliseconds to call at.
AjK 0:5684eed14bda 132 */
AjK 0:5684eed14bda 133 void attach_ms(void (*fptr)(void), uint32_t u) {
AjK 0:5684eed14bda 134 counter = reload = u;
AjK 0:5684eed14bda 135 callback.attach(fptr);
AjK 0:5684eed14bda 136 }
AjK 0:5684eed14bda 137
AjK 0:5684eed14bda 138 /** attach_ms
AjK 0:5684eed14bda 139 *
AjK 0:5684eed14bda 140 * Attach a C++ style functor object/method callback.
AjK 0:5684eed14bda 141 *
AjK 0:5684eed14bda 142 * @param tptr A C++ style object pointer.
AjK 0:5684eed14bda 143 * @param mptr A C++ style method pointer.
AjK 0:5684eed14bda 144 * @param uint32_t u The number of milliseconds to call at.
AjK 0:5684eed14bda 145 */
AjK 0:5684eed14bda 146 template<typename T>
AjK 0:5684eed14bda 147 void attach_ms(T* tptr, void (T::*mptr)(void), uint32_t u) {
AjK 0:5684eed14bda 148 if((mptr != NULL) && (tptr != NULL)) {
AjK 0:5684eed14bda 149 counter = reload = u;
AjK 0:5684eed14bda 150 callback.attach(tptr, mptr);
AjK 0:5684eed14bda 151 }
AjK 0:5684eed14bda 152 }
AjK 0:5684eed14bda 153
AjK 0:5684eed14bda 154 /** attach
AjK 0:5684eed14bda 155 *
AjK 0:5684eed14bda 156 * Attach a C style function pointer callback.
AjK 0:5684eed14bda 157 *
AjK 0:5684eed14bda 158 * @param fptr A C style function pointer.
AjK 0:5684eed14bda 159 * @param double d The number of seconds to call at.
AjK 0:5684eed14bda 160 */
AjK 0:5684eed14bda 161 void attach(void (*fptr)(void), double d) {
AjK 0:5684eed14bda 162 counter = reload = (uint32_t)( d * 1000.0 );
AjK 0:5684eed14bda 163 callback.attach(fptr);
AjK 0:5684eed14bda 164 }
AjK 0:5684eed14bda 165
AjK 0:5684eed14bda 166 /** attach
AjK 0:5684eed14bda 167 *
AjK 0:5684eed14bda 168 * Attach a C++ style functor object/method callback.
AjK 0:5684eed14bda 169 *
AjK 0:5684eed14bda 170 * @param tptr A C++ style object pointer.
AjK 0:5684eed14bda 171 * @param mptr A C++ style method pointer.
AjK 0:5684eed14bda 172 * @param double d The number of seconds to call at.
AjK 0:5684eed14bda 173 */
AjK 0:5684eed14bda 174 template<typename T>
AjK 0:5684eed14bda 175 void attach(T* tptr, void (T::*mptr)(void), double d) {
AjK 0:5684eed14bda 176 if((mptr != NULL) && (tptr != NULL)) {
AjK 0:5684eed14bda 177 counter = reload = (uint32_t)( d * 1000.0 );
AjK 0:5684eed14bda 178 callback.attach(tptr, mptr);
AjK 0:5684eed14bda 179 }
AjK 0:5684eed14bda 180 }
AjK 0:5684eed14bda 181
AjK 0:5684eed14bda 182 };
AjK 0:5684eed14bda 183
AjK 0:5684eed14bda 184 }; // namespace AjK ends.
AjK 0:5684eed14bda 185
AjK 0:5684eed14bda 186 using namespace AjK;
AjK 0:5684eed14bda 187
AjK 0:5684eed14bda 188 #endif