Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: XBeeLib mbed HvZAlphaNumLib HvZServerLib
lib/Stun.hpp@0:9cdba0589ba2, 2010-12-12 (annotated)
- Committer:
- etherealflaim
- Date:
- Sun Dec 12 19:35:00 2010 +0000
- Revision:
- 0:9cdba0589ba2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| etherealflaim | 0:9cdba0589ba2 | 1 | #ifndef _Stun |
| etherealflaim | 0:9cdba0589ba2 | 2 | #define _Stun |
| etherealflaim | 0:9cdba0589ba2 | 3 | |
| etherealflaim | 0:9cdba0589ba2 | 4 | #include "mbed.h" |
| etherealflaim | 0:9cdba0589ba2 | 5 | |
| etherealflaim | 0:9cdba0589ba2 | 6 | class Stun { |
| etherealflaim | 0:9cdba0589ba2 | 7 | private: |
| etherealflaim | 0:9cdba0589ba2 | 8 | |
| etherealflaim | 0:9cdba0589ba2 | 9 | // Tag status and timing |
| etherealflaim | 0:9cdba0589ba2 | 10 | bool m_is_stunned; //< Whether currently stunned or not |
| etherealflaim | 0:9cdba0589ba2 | 11 | // Game options |
| etherealflaim | 0:9cdba0589ba2 | 12 | unsigned m_stun_time; //< Total duration of current stun |
| etherealflaim | 0:9cdba0589ba2 | 13 | Timer m_stun_timer; //< Current amount of stun |
| etherealflaim | 0:9cdba0589ba2 | 14 | Timeout m_stun_timeout; //< Triggers when the stun is over |
| etherealflaim | 0:9cdba0589ba2 | 15 | Ticker m_stun_ticker; //< Ticks for the duration of the stun |
| etherealflaim | 0:9cdba0589ba2 | 16 | |
| etherealflaim | 0:9cdba0589ba2 | 17 | // Tag LED |
| etherealflaim | 0:9cdba0589ba2 | 18 | DigitalOut m_stun_led; |
| etherealflaim | 0:9cdba0589ba2 | 19 | |
| etherealflaim | 0:9cdba0589ba2 | 20 | public: |
| etherealflaim | 0:9cdba0589ba2 | 21 | inline Stun(PinName stun_led) |
| etherealflaim | 0:9cdba0589ba2 | 22 | : m_is_stunned(false), m_stun_time(0), m_stun_led(stun_led) |
| etherealflaim | 0:9cdba0589ba2 | 23 | { |
| etherealflaim | 0:9cdba0589ba2 | 24 | } |
| etherealflaim | 0:9cdba0589ba2 | 25 | |
| etherealflaim | 0:9cdba0589ba2 | 26 | /// Get the stun status |
| etherealflaim | 0:9cdba0589ba2 | 27 | inline bool stunned() { return m_is_stunned; } |
| etherealflaim | 0:9cdba0589ba2 | 28 | |
| etherealflaim | 0:9cdba0589ba2 | 29 | /// Get the stun time remaining |
| etherealflaim | 0:9cdba0589ba2 | 30 | inline int stunleft() |
| etherealflaim | 0:9cdba0589ba2 | 31 | { |
| etherealflaim | 0:9cdba0589ba2 | 32 | if (!m_is_stunned) return 0; |
| etherealflaim | 0:9cdba0589ba2 | 33 | return m_stun_time - m_stun_timer.read(); |
| etherealflaim | 0:9cdba0589ba2 | 34 | } |
| etherealflaim | 0:9cdba0589ba2 | 35 | |
| etherealflaim | 0:9cdba0589ba2 | 36 | /* Actions */ |
| etherealflaim | 0:9cdba0589ba2 | 37 | inline void stun(unsigned duration) |
| etherealflaim | 0:9cdba0589ba2 | 38 | { |
| etherealflaim | 0:9cdba0589ba2 | 39 | m_is_stunned = true; |
| etherealflaim | 0:9cdba0589ba2 | 40 | m_stun_time = duration; |
| etherealflaim | 0:9cdba0589ba2 | 41 | m_stun_timeout.attach(this, &Stun::stun_expire, m_stun_time); |
| etherealflaim | 0:9cdba0589ba2 | 42 | m_stun_ticker.attach(this, &Stun::stun_tick, 1); |
| etherealflaim | 0:9cdba0589ba2 | 43 | m_stun_timer.start(); |
| etherealflaim | 0:9cdba0589ba2 | 44 | m_stun_led = 1; |
| etherealflaim | 0:9cdba0589ba2 | 45 | } |
| etherealflaim | 0:9cdba0589ba2 | 46 | |
| etherealflaim | 0:9cdba0589ba2 | 47 | private: |
| etherealflaim | 0:9cdba0589ba2 | 48 | inline void stun_tick() |
| etherealflaim | 0:9cdba0589ba2 | 49 | { |
| etherealflaim | 0:9cdba0589ba2 | 50 | |
| etherealflaim | 0:9cdba0589ba2 | 51 | } |
| etherealflaim | 0:9cdba0589ba2 | 52 | |
| etherealflaim | 0:9cdba0589ba2 | 53 | inline void stun_expire() |
| etherealflaim | 0:9cdba0589ba2 | 54 | { |
| etherealflaim | 0:9cdba0589ba2 | 55 | m_is_stunned = false; |
| etherealflaim | 0:9cdba0589ba2 | 56 | m_stun_ticker.detach(); |
| etherealflaim | 0:9cdba0589ba2 | 57 | m_stun_timer.stop(); |
| etherealflaim | 0:9cdba0589ba2 | 58 | m_stun_timer.reset(); |
| etherealflaim | 0:9cdba0589ba2 | 59 | m_stun_led = 0; |
| etherealflaim | 0:9cdba0589ba2 | 60 | m_stun_time = 0; |
| etherealflaim | 0:9cdba0589ba2 | 61 | |
| etherealflaim | 0:9cdba0589ba2 | 62 | } |
| etherealflaim | 0:9cdba0589ba2 | 63 | }; |
| etherealflaim | 0:9cdba0589ba2 | 64 | |
| etherealflaim | 0:9cdba0589ba2 | 65 | |
| etherealflaim | 0:9cdba0589ba2 | 66 | #endif |