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