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@3:45777fe81448, 2016-05-08 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun May 08 14:42:59 2016 +0000
- Revision:
- 3:45777fe81448
- Parent:
- 1:6fb3545bddf7
- Child:
- 4:0be0d6a61e90
Work with new SimpleGUI event mechanism
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 | 0:b250e56f3514 | 12 | } |
duncanFrance | 0:b250e56f3514 | 13 | |
duncanFrance | 0:b250e56f3514 | 14 | void TouchScreenEventSource::touchStartHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 15 | { |
duncanFrance | 1:6fb3545bddf7 | 16 | if(_state == Idle) { |
duncanFrance | 1:6fb3545bddf7 | 17 | // see if we get the touchEnd event before the timeout |
duncanFrance | 1:6fb3545bddf7 | 18 | _state = SingleTimer; |
duncanFrance | 1:6fb3545bddf7 | 19 | _timeout.attach_us(this, &TouchScreenEventSource::_timeoutHandler, TAP_TIMEOUT_MICROS); |
duncanFrance | 1:6fb3545bddf7 | 20 | } |
duncanFrance | 1:6fb3545bddf7 | 21 | |
duncanFrance | 1:6fb3545bddf7 | 22 | Event e; |
duncanFrance | 1:6fb3545bddf7 | 23 | e.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 24 | e.screenY = p.screenY; |
duncanFrance | 3:45777fe81448 | 25 | e.type = TOUCH_START; |
duncanFrance | 3:45777fe81448 | 26 | _gui->queueEvent(e); |
duncanFrance | 0:b250e56f3514 | 27 | } |
duncanFrance | 0:b250e56f3514 | 28 | |
duncanFrance | 0:b250e56f3514 | 29 | void TouchScreenEventSource::touchMoveHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 30 | { |
duncanFrance | 1:6fb3545bddf7 | 31 | |
duncanFrance | 1:6fb3545bddf7 | 32 | Event e; |
duncanFrance | 1:6fb3545bddf7 | 33 | e.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 34 | e.screenY = p.screenY; |
duncanFrance | 3:45777fe81448 | 35 | e.type = TOUCH_MOVE; |
duncanFrance | 3:45777fe81448 | 36 | _gui->queueEvent(e); |
duncanFrance | 0:b250e56f3514 | 37 | } |
duncanFrance | 0:b250e56f3514 | 38 | |
duncanFrance | 0:b250e56f3514 | 39 | void TouchScreenEventSource::touchEndHandler(TouchPosition p) |
duncanFrance | 0:b250e56f3514 | 40 | { |
duncanFrance | 1:6fb3545bddf7 | 41 | // Work out what events we need before we dispatch them, so the handler doesn't interfere with any timers |
duncanFrance | 1:6fb3545bddf7 | 42 | Event endEvent; |
duncanFrance | 1:6fb3545bddf7 | 43 | Event tapEvent; |
duncanFrance | 1:6fb3545bddf7 | 44 | bool tapped = false; |
duncanFrance | 1:6fb3545bddf7 | 45 | |
duncanFrance | 1:6fb3545bddf7 | 46 | // We always want to dispatch a touchEndEvent |
duncanFrance | 1:6fb3545bddf7 | 47 | endEvent.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 48 | endEvent.screenY = p.screenY; |
duncanFrance | 3:45777fe81448 | 49 | endEvent.type = TOUCH_END; |
duncanFrance | 1:6fb3545bddf7 | 50 | |
duncanFrance | 1:6fb3545bddf7 | 51 | tapEvent.screenX = p.screenX; |
duncanFrance | 1:6fb3545bddf7 | 52 | tapEvent.screenY = p.screenY; |
duncanFrance | 1:6fb3545bddf7 | 53 | |
duncanFrance | 1:6fb3545bddf7 | 54 | // We'll want a touchTapEvent if we haven't timed out and we're looking for it |
duncanFrance | 1:6fb3545bddf7 | 55 | if(_state == SingleTimer) { |
duncanFrance | 1:6fb3545bddf7 | 56 | // Reset the timer to look for a double-tap |
duncanFrance | 1:6fb3545bddf7 | 57 | _timeout.attach_us(this, &TouchScreenEventSource::_timeoutHandler, DOUBLE_TAP_TIMEOUT_MICROS); |
duncanFrance | 1:6fb3545bddf7 | 58 | _state = DoubleTimer; |
duncanFrance | 1:6fb3545bddf7 | 59 | |
duncanFrance | 3:45777fe81448 | 60 | tapEvent.type = TOUCH_TAP; |
duncanFrance | 1:6fb3545bddf7 | 61 | tapped = true; |
duncanFrance | 1:6fb3545bddf7 | 62 | } else if(_state == DoubleTimer) { |
duncanFrance | 1:6fb3545bddf7 | 63 | |
duncanFrance | 1:6fb3545bddf7 | 64 | _timeout.detach(); |
duncanFrance | 1:6fb3545bddf7 | 65 | _state = Idle; |
duncanFrance | 1:6fb3545bddf7 | 66 | |
duncanFrance | 3:45777fe81448 | 67 | tapEvent.type = TOUCH_DOUBLE_TAP; |
duncanFrance | 1:6fb3545bddf7 | 68 | tapped = true; |
duncanFrance | 1:6fb3545bddf7 | 69 | } |
duncanFrance | 1:6fb3545bddf7 | 70 | |
duncanFrance | 3:45777fe81448 | 71 | _gui->queueEvent(endEvent); |
duncanFrance | 1:6fb3545bddf7 | 72 | |
duncanFrance | 1:6fb3545bddf7 | 73 | if(tapped) { |
duncanFrance | 3:45777fe81448 | 74 | _gui->queueEvent(tapEvent); |
duncanFrance | 1:6fb3545bddf7 | 75 | } |
duncanFrance | 1:6fb3545bddf7 | 76 | } |
duncanFrance | 1:6fb3545bddf7 | 77 | |
duncanFrance | 1:6fb3545bddf7 | 78 | void TouchScreenEventSource::_timeoutHandler() { |
duncanFrance | 1:6fb3545bddf7 | 79 | // Timed out waiting for whatever, so clear the state |
duncanFrance | 1:6fb3545bddf7 | 80 | _state = Idle; |
duncanFrance | 0:b250e56f3514 | 81 | } |