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.
Dependents: TouchScreenGUIDemo
TouchScreenEventSource.cpp@1:6fb3545bddf7, 2016-03-25 (annotated)
- Committer:
- duncanFrance
- Date:
- Fri Mar 25 16:48:32 2016 +0000
- Revision:
- 1:6fb3545bddf7
- Parent:
- 0:b250e56f3514
- Child:
- 3:45777fe81448
Full TouchScreenEventSource implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 0:b250e56f3514 | 1 | #include "TouchScreenEventSource.h" |
duncanFrance | 0:b250e56f3514 | 2 | /** |
duncanFrance | 0:b250e56f3514 | 3 | * Implementation of TouchScreenEventSource which adds timers to detect and dispatch |
duncanFrance | 0:b250e56f3514 | 4 | * tap and double-tap events |
duncanFrance | 0:b250e56f3514 | 5 | **/ |
duncanFrance | 0:b250e56f3514 | 6 | |
duncanFrance | 1:6fb3545bddf7 | 7 | TouchScreenEventSource::TouchScreenEventSource(TouchScreen* touchScreen, EventDispatcher* dispatcher) |
duncanFrance | 1:6fb3545bddf7 | 8 | : EventSource(dispatcher), _touchScreen(touchScreen), _state(Idle) |
duncanFrance | 0:b250e56f3514 | 9 | { |
duncanFrance | 0:b250e56f3514 | 10 | } |
duncanFrance | 0:b250e56f3514 | 11 | |
duncanFrance | 0:b250e56f3514 | 12 | void TouchScreenEventSource::touchStartHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 13 | { |
duncanFrance | 1:6fb3545bddf7 | 14 | if(_state == Idle) { |
duncanFrance | 1:6fb3545bddf7 | 15 | // see if we get the touchEnd event before the timeout |
duncanFrance | 1:6fb3545bddf7 | 16 | _state = SingleTimer; |
duncanFrance | 1:6fb3545bddf7 | 17 | _timeout.attach_us(this, &TouchScreenEventSource::_timeoutHandler, TAP_TIMEOUT_MICROS); |
duncanFrance | 1:6fb3545bddf7 | 18 | } |
duncanFrance | 1:6fb3545bddf7 | 19 | |
duncanFrance | 1:6fb3545bddf7 | 20 | Event e; |
duncanFrance | 1:6fb3545bddf7 | 21 | e.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 22 | e.screenY = p.screenY; |
duncanFrance | 1:6fb3545bddf7 | 23 | e.type = touchStartEvent; |
duncanFrance | 1:6fb3545bddf7 | 24 | _dispatcher->dispatchEvent(e); |
duncanFrance | 0:b250e56f3514 | 25 | } |
duncanFrance | 0:b250e56f3514 | 26 | |
duncanFrance | 0:b250e56f3514 | 27 | void TouchScreenEventSource::touchMoveHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 28 | { |
duncanFrance | 1:6fb3545bddf7 | 29 | |
duncanFrance | 1:6fb3545bddf7 | 30 | Event e; |
duncanFrance | 1:6fb3545bddf7 | 31 | e.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 32 | e.screenY = p.screenY; |
duncanFrance | 1:6fb3545bddf7 | 33 | e.type = touchMoveEvent; |
duncanFrance | 1:6fb3545bddf7 | 34 | _dispatcher->dispatchEvent(e); |
duncanFrance | 0:b250e56f3514 | 35 | } |
duncanFrance | 0:b250e56f3514 | 36 | |
duncanFrance | 0:b250e56f3514 | 37 | void TouchScreenEventSource::touchEndHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 38 | { |
duncanFrance | 1:6fb3545bddf7 | 39 | // Work out what events we need before we dispatch them, so the handler doesn't interfere with any timers |
duncanFrance | 1:6fb3545bddf7 | 40 | Event endEvent; |
duncanFrance | 1:6fb3545bddf7 | 41 | Event tapEvent; |
duncanFrance | 1:6fb3545bddf7 | 42 | bool tapped = false; |
duncanFrance | 1:6fb3545bddf7 | 43 | |
duncanFrance | 1:6fb3545bddf7 | 44 | // We always want to dispatch a touchEndEvent |
duncanFrance | 1:6fb3545bddf7 | 45 | endEvent.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 46 | endEvent.screenY = p.screenY; |
duncanFrance | 1:6fb3545bddf7 | 47 | endEvent.type = touchEndEvent; |
duncanFrance | 1:6fb3545bddf7 | 48 | |
duncanFrance | 1:6fb3545bddf7 | 49 | tapEvent.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 50 | tapEvent.screenY = p.screenY; |
duncanFrance | 1:6fb3545bddf7 | 51 | |
duncanFrance | 1:6fb3545bddf7 | 52 | // We'll want a touchTapEvent if we haven't timed out and we're looking for it |
duncanFrance | 1:6fb3545bddf7 | 53 | if(_state == SingleTimer) { |
duncanFrance | 1:6fb3545bddf7 | 54 | // Reset the timer to look for a double-tap |
duncanFrance | 1:6fb3545bddf7 | 55 | _timeout.attach_us(this, &TouchScreenEventSource::_timeoutHandler, DOUBLE_TAP_TIMEOUT_MICROS); |
duncanFrance | 1:6fb3545bddf7 | 56 | _state = DoubleTimer; |
duncanFrance | 1:6fb3545bddf7 | 57 | |
duncanFrance | 1:6fb3545bddf7 | 58 | tapEvent.type = touchTapEvent; |
duncanFrance | 1:6fb3545bddf7 | 59 | tapped = true; |
duncanFrance | 1:6fb3545bddf7 | 60 | } else if(_state == DoubleTimer) { |
duncanFrance | 1:6fb3545bddf7 | 61 | |
duncanFrance | 1:6fb3545bddf7 | 62 | _timeout.detach(); |
duncanFrance | 1:6fb3545bddf7 | 63 | _state = Idle; |
duncanFrance | 1:6fb3545bddf7 | 64 | |
duncanFrance | 1:6fb3545bddf7 | 65 | tapEvent.type = touchDoubleTapEvent; |
duncanFrance | 1:6fb3545bddf7 | 66 | tapped = true; |
duncanFrance | 1:6fb3545bddf7 | 67 | } |
duncanFrance | 1:6fb3545bddf7 | 68 | |
duncanFrance | 1:6fb3545bddf7 | 69 | _dispatcher->dispatchEvent(endEvent); |
duncanFrance | 1:6fb3545bddf7 | 70 | |
duncanFrance | 1:6fb3545bddf7 | 71 | if(tapped) { |
duncanFrance | 1:6fb3545bddf7 | 72 | _dispatcher->dispatchEvent(tapEvent); |
duncanFrance | 1:6fb3545bddf7 | 73 | } |
duncanFrance | 1:6fb3545bddf7 | 74 | } |
duncanFrance | 1:6fb3545bddf7 | 75 | |
duncanFrance | 1:6fb3545bddf7 | 76 | void TouchScreenEventSource::_timeoutHandler() { |
duncanFrance | 1:6fb3545bddf7 | 77 | // Timed out waiting for whatever, so clear the state |
duncanFrance | 1:6fb3545bddf7 | 78 | _state = Idle; |
duncanFrance | 0:b250e56f3514 | 79 | } |