PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
mbed-pokitto/api/Ticker.h@5:ea7377f3d1af, 2017-10-11 (annotated)
- Committer:
- Pokitto
- Date:
- Wed Oct 11 20:35:27 2017 +0000
- Revision:
- 5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 5:ea7377f3d1af | 1 | /* mbed Microcontroller Library |
Pokitto | 5:ea7377f3d1af | 2 | * Copyright (c) 2006-2013 ARM Limited |
Pokitto | 5:ea7377f3d1af | 3 | * |
Pokitto | 5:ea7377f3d1af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Pokitto | 5:ea7377f3d1af | 5 | * you may not use this file except in compliance with the License. |
Pokitto | 5:ea7377f3d1af | 6 | * You may obtain a copy of the License at |
Pokitto | 5:ea7377f3d1af | 7 | * |
Pokitto | 5:ea7377f3d1af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pokitto | 5:ea7377f3d1af | 9 | * |
Pokitto | 5:ea7377f3d1af | 10 | * Unless required by applicable law or agreed to in writing, software |
Pokitto | 5:ea7377f3d1af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Pokitto | 5:ea7377f3d1af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pokitto | 5:ea7377f3d1af | 13 | * See the License for the specific language governing permissions and |
Pokitto | 5:ea7377f3d1af | 14 | * limitations under the License. |
Pokitto | 5:ea7377f3d1af | 15 | */ |
Pokitto | 5:ea7377f3d1af | 16 | #ifndef MBED_TICKER_H |
Pokitto | 5:ea7377f3d1af | 17 | #define MBED_TICKER_H |
Pokitto | 5:ea7377f3d1af | 18 | |
Pokitto | 5:ea7377f3d1af | 19 | #include "TimerEvent.h" |
Pokitto | 5:ea7377f3d1af | 20 | #include "FunctionPointer.h" |
Pokitto | 5:ea7377f3d1af | 21 | |
Pokitto | 5:ea7377f3d1af | 22 | namespace mbed { |
Pokitto | 5:ea7377f3d1af | 23 | |
Pokitto | 5:ea7377f3d1af | 24 | /** A Ticker is used to call a function at a recurring interval |
Pokitto | 5:ea7377f3d1af | 25 | * |
Pokitto | 5:ea7377f3d1af | 26 | * You can use as many seperate Ticker objects as you require. |
Pokitto | 5:ea7377f3d1af | 27 | * |
Pokitto | 5:ea7377f3d1af | 28 | * Example: |
Pokitto | 5:ea7377f3d1af | 29 | * @code |
Pokitto | 5:ea7377f3d1af | 30 | * // Toggle the blinking led after 5 seconds |
Pokitto | 5:ea7377f3d1af | 31 | * |
Pokitto | 5:ea7377f3d1af | 32 | * #include "mbed.h" |
Pokitto | 5:ea7377f3d1af | 33 | * |
Pokitto | 5:ea7377f3d1af | 34 | * Ticker timer; |
Pokitto | 5:ea7377f3d1af | 35 | * DigitalOut led1(LED1); |
Pokitto | 5:ea7377f3d1af | 36 | * DigitalOut led2(LED2); |
Pokitto | 5:ea7377f3d1af | 37 | * |
Pokitto | 5:ea7377f3d1af | 38 | * int flip = 0; |
Pokitto | 5:ea7377f3d1af | 39 | * |
Pokitto | 5:ea7377f3d1af | 40 | * void attime() { |
Pokitto | 5:ea7377f3d1af | 41 | * flip = !flip; |
Pokitto | 5:ea7377f3d1af | 42 | * } |
Pokitto | 5:ea7377f3d1af | 43 | * |
Pokitto | 5:ea7377f3d1af | 44 | * int main() { |
Pokitto | 5:ea7377f3d1af | 45 | * timer.attach(&attime, 5); |
Pokitto | 5:ea7377f3d1af | 46 | * while(1) { |
Pokitto | 5:ea7377f3d1af | 47 | * if(flip == 0) { |
Pokitto | 5:ea7377f3d1af | 48 | * led1 = !led1; |
Pokitto | 5:ea7377f3d1af | 49 | * } else { |
Pokitto | 5:ea7377f3d1af | 50 | * led2 = !led2; |
Pokitto | 5:ea7377f3d1af | 51 | * } |
Pokitto | 5:ea7377f3d1af | 52 | * wait(0.2); |
Pokitto | 5:ea7377f3d1af | 53 | * } |
Pokitto | 5:ea7377f3d1af | 54 | * } |
Pokitto | 5:ea7377f3d1af | 55 | * @endcode |
Pokitto | 5:ea7377f3d1af | 56 | */ |
Pokitto | 5:ea7377f3d1af | 57 | class Ticker : public TimerEvent { |
Pokitto | 5:ea7377f3d1af | 58 | |
Pokitto | 5:ea7377f3d1af | 59 | public: |
Pokitto | 5:ea7377f3d1af | 60 | Ticker() : TimerEvent() { |
Pokitto | 5:ea7377f3d1af | 61 | } |
Pokitto | 5:ea7377f3d1af | 62 | |
Pokitto | 5:ea7377f3d1af | 63 | Ticker(const ticker_data_t *data) : TimerEvent(data) { |
Pokitto | 5:ea7377f3d1af | 64 | } |
Pokitto | 5:ea7377f3d1af | 65 | |
Pokitto | 5:ea7377f3d1af | 66 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
Pokitto | 5:ea7377f3d1af | 67 | * |
Pokitto | 5:ea7377f3d1af | 68 | * @param fptr pointer to the function to be called |
Pokitto | 5:ea7377f3d1af | 69 | * @param t the time between calls in seconds |
Pokitto | 5:ea7377f3d1af | 70 | */ |
Pokitto | 5:ea7377f3d1af | 71 | void attach(void (*fptr)(void), float t) { |
Pokitto | 5:ea7377f3d1af | 72 | attach_us(fptr, t * 1000000.0f); |
Pokitto | 5:ea7377f3d1af | 73 | } |
Pokitto | 5:ea7377f3d1af | 74 | |
Pokitto | 5:ea7377f3d1af | 75 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
Pokitto | 5:ea7377f3d1af | 76 | * |
Pokitto | 5:ea7377f3d1af | 77 | * @param tptr pointer to the object to call the member function on |
Pokitto | 5:ea7377f3d1af | 78 | * @param mptr pointer to the member function to be called |
Pokitto | 5:ea7377f3d1af | 79 | * @param t the time between calls in seconds |
Pokitto | 5:ea7377f3d1af | 80 | */ |
Pokitto | 5:ea7377f3d1af | 81 | template<typename T> |
Pokitto | 5:ea7377f3d1af | 82 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
Pokitto | 5:ea7377f3d1af | 83 | attach_us(tptr, mptr, t * 1000000.0f); |
Pokitto | 5:ea7377f3d1af | 84 | } |
Pokitto | 5:ea7377f3d1af | 85 | |
Pokitto | 5:ea7377f3d1af | 86 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
Pokitto | 5:ea7377f3d1af | 87 | * |
Pokitto | 5:ea7377f3d1af | 88 | * @param fptr pointer to the function to be called |
Pokitto | 5:ea7377f3d1af | 89 | * @param t the time between calls in micro-seconds |
Pokitto | 5:ea7377f3d1af | 90 | */ |
Pokitto | 5:ea7377f3d1af | 91 | void attach_us(void (*fptr)(void), timestamp_t t) { |
Pokitto | 5:ea7377f3d1af | 92 | _function.attach(fptr); |
Pokitto | 5:ea7377f3d1af | 93 | setup(t); |
Pokitto | 5:ea7377f3d1af | 94 | } |
Pokitto | 5:ea7377f3d1af | 95 | |
Pokitto | 5:ea7377f3d1af | 96 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
Pokitto | 5:ea7377f3d1af | 97 | * |
Pokitto | 5:ea7377f3d1af | 98 | * @param tptr pointer to the object to call the member function on |
Pokitto | 5:ea7377f3d1af | 99 | * @param mptr pointer to the member function to be called |
Pokitto | 5:ea7377f3d1af | 100 | * @param t the time between calls in micro-seconds |
Pokitto | 5:ea7377f3d1af | 101 | */ |
Pokitto | 5:ea7377f3d1af | 102 | template<typename T> |
Pokitto | 5:ea7377f3d1af | 103 | void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { |
Pokitto | 5:ea7377f3d1af | 104 | _function.attach(tptr, mptr); |
Pokitto | 5:ea7377f3d1af | 105 | setup(t); |
Pokitto | 5:ea7377f3d1af | 106 | } |
Pokitto | 5:ea7377f3d1af | 107 | |
Pokitto | 5:ea7377f3d1af | 108 | virtual ~Ticker() { |
Pokitto | 5:ea7377f3d1af | 109 | detach(); |
Pokitto | 5:ea7377f3d1af | 110 | } |
Pokitto | 5:ea7377f3d1af | 111 | |
Pokitto | 5:ea7377f3d1af | 112 | /** Detach the function |
Pokitto | 5:ea7377f3d1af | 113 | */ |
Pokitto | 5:ea7377f3d1af | 114 | void detach(); |
Pokitto | 5:ea7377f3d1af | 115 | |
Pokitto | 5:ea7377f3d1af | 116 | protected: |
Pokitto | 5:ea7377f3d1af | 117 | void setup(timestamp_t t); |
Pokitto | 5:ea7377f3d1af | 118 | virtual void handler(); |
Pokitto | 5:ea7377f3d1af | 119 | |
Pokitto | 5:ea7377f3d1af | 120 | protected: |
Pokitto | 5:ea7377f3d1af | 121 | timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ |
Pokitto | 5:ea7377f3d1af | 122 | FunctionPointer _function; /**< Callback. */ |
Pokitto | 5:ea7377f3d1af | 123 | }; |
Pokitto | 5:ea7377f3d1af | 124 | |
Pokitto | 5:ea7377f3d1af | 125 | } // namespace mbed |
Pokitto | 5:ea7377f3d1af | 126 | |
Pokitto | 5:ea7377f3d1af | 127 | #endif |