touch screen handler for the microchip AR1020

Committer:
hlipka
Date:
Thu Feb 24 14:00:17 2011 +0000
Revision:
4:510ea5b28a05
Parent:
3:b7eb3b3fe79f
better documentation
added power handling (AR1020 is switched on by this library now)
added touch area handler for easier creation of menus and the like

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 4:510ea5b28a05 1 /*
hlipka 4:510ea5b28a05 2 * mbed AR1020 library
hlipka 4:510ea5b28a05 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 4:510ea5b28a05 4 *
hlipka 4:510ea5b28a05 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 4:510ea5b28a05 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 4:510ea5b28a05 7 * in the Software without restriction, including without limitation the rights
hlipka 4:510ea5b28a05 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 4:510ea5b28a05 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 4:510ea5b28a05 10 * furnished to do so, subject to the following conditions:
hlipka 4:510ea5b28a05 11 *
hlipka 4:510ea5b28a05 12 * The above copyright notice and this permission notice shall be included in
hlipka 4:510ea5b28a05 13 * all copies or substantial portions of the Software.
hlipka 4:510ea5b28a05 14 *
hlipka 4:510ea5b28a05 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 4:510ea5b28a05 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 4:510ea5b28a05 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 4:510ea5b28a05 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 4:510ea5b28a05 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 4:510ea5b28a05 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 4:510ea5b28a05 21 * THE SOFTWARE.
hlipka 4:510ea5b28a05 22 */
hlipka 4:510ea5b28a05 23
hlipka 4:510ea5b28a05 24 #include "AreaTouchHandler.h"
hlipka 4:510ea5b28a05 25 #include "touchevent.h"
hlipka 4:510ea5b28a05 26
hlipka 4:510ea5b28a05 27 using namespace std;
hlipka 4:510ea5b28a05 28
hlipka 4:510ea5b28a05 29 class area
hlipka 4:510ea5b28a05 30 {
hlipka 4:510ea5b28a05 31 public:
hlipka 4:510ea5b28a05 32 int top,bottom,left,right,command;
hlipka 4:510ea5b28a05 33 area(int top, int bottom, int left, int right, int command)
hlipka 4:510ea5b28a05 34 {
hlipka 4:510ea5b28a05 35 this->top=top;
hlipka 4:510ea5b28a05 36 this->bottom=bottom;
hlipka 4:510ea5b28a05 37 this->left=left;
hlipka 4:510ea5b28a05 38 this->right=right;
hlipka 4:510ea5b28a05 39 this->command=command;
hlipka 4:510ea5b28a05 40 }
hlipka 4:510ea5b28a05 41 };
hlipka 4:510ea5b28a05 42
hlipka 4:510ea5b28a05 43 AreaTouchHandler::AreaTouchHandler(TouchPanel* panel)
hlipka 4:510ea5b28a05 44 {
hlipka 4:510ea5b28a05 45 panel->attachPenDown(this,&AreaTouchHandler::down);
hlipka 4:510ea5b28a05 46 panel->attachPenMove(this,&AreaTouchHandler::move);
hlipka 4:510ea5b28a05 47 panel->attachPenUp(this,&AreaTouchHandler::up);
hlipka 4:510ea5b28a05 48 }
hlipka 4:510ea5b28a05 49
hlipka 4:510ea5b28a05 50 void AreaTouchHandler::addArea(int top, int bottom, int left, int right, int commandCode)
hlipka 4:510ea5b28a05 51 {
hlipka 4:510ea5b28a05 52 _areas.push_back(new area(top,bottom,left,right,commandCode));
hlipka 4:510ea5b28a05 53 }
hlipka 4:510ea5b28a05 54
hlipka 4:510ea5b28a05 55 uint32_t AreaTouchHandler::down(uint32_t arg)
hlipka 4:510ea5b28a05 56 {
hlipka 4:510ea5b28a05 57 TouchEvent *te=(TouchEvent*)arg;
hlipka 4:510ea5b28a05 58 // printf("d %i/%i\n",te->x,te->y);
hlipka 4:510ea5b28a05 59 _x=te->x;
hlipka 4:510ea5b28a05 60 _y=te->y;
hlipka 4:510ea5b28a05 61 _samples=1;
hlipka 4:510ea5b28a05 62 int c=findCommand();
hlipka 4:510ea5b28a05 63 _called=false;
hlipka 4:510ea5b28a05 64 if (0!=c)
hlipka 4:510ea5b28a05 65 {
hlipka 4:510ea5b28a05 66 _called=true;
hlipka 4:510ea5b28a05 67 _callback.call(c);
hlipka 4:510ea5b28a05 68 }
hlipka 4:510ea5b28a05 69 return 0;
hlipka 4:510ea5b28a05 70 }
hlipka 4:510ea5b28a05 71 uint32_t AreaTouchHandler::move(uint32_t arg)
hlipka 4:510ea5b28a05 72 {
hlipka 4:510ea5b28a05 73 if (_called)
hlipka 4:510ea5b28a05 74 return 0;
hlipka 4:510ea5b28a05 75 TouchEvent *te=(TouchEvent*)arg;
hlipka 4:510ea5b28a05 76 // printf("m %i/%i\n",te->x,te->y);
hlipka 4:510ea5b28a05 77 _x=(te->x+_x*_samples)/(_samples+1);
hlipka 4:510ea5b28a05 78 _y=(te->y+_y*_samples)/(_samples+1);
hlipka 4:510ea5b28a05 79 _samples++;
hlipka 4:510ea5b28a05 80 int c=findCommand();
hlipka 4:510ea5b28a05 81 if (0!=c)
hlipka 4:510ea5b28a05 82 {
hlipka 4:510ea5b28a05 83 _called=true;
hlipka 4:510ea5b28a05 84 _callback.call(c);
hlipka 4:510ea5b28a05 85 }
hlipka 4:510ea5b28a05 86 return 0;
hlipka 4:510ea5b28a05 87 }
hlipka 4:510ea5b28a05 88 uint32_t AreaTouchHandler::up(uint32_t arg)
hlipka 4:510ea5b28a05 89 {
hlipka 4:510ea5b28a05 90 _x=0;
hlipka 4:510ea5b28a05 91 _y=0;
hlipka 4:510ea5b28a05 92 _samples=0;
hlipka 4:510ea5b28a05 93 _called=false;
hlipka 4:510ea5b28a05 94 return 0;
hlipka 4:510ea5b28a05 95 }
hlipka 4:510ea5b28a05 96
hlipka 4:510ea5b28a05 97 int AreaTouchHandler::findCommand()
hlipka 4:510ea5b28a05 98 {
hlipka 4:510ea5b28a05 99 if (_called)
hlipka 4:510ea5b28a05 100 return 0;
hlipka 4:510ea5b28a05 101 if (_samples<5)
hlipka 4:510ea5b28a05 102 return 0;
hlipka 4:510ea5b28a05 103 for (list<area*>::iterator it = _areas.begin(); it != _areas.end(); it++) {
hlipka 4:510ea5b28a05 104 area* a=*it;
hlipka 4:510ea5b28a05 105 if (_x>=a->left && _x<=a->right && _y>=a->top && _y<=a->bottom)
hlipka 4:510ea5b28a05 106 return a->command;
hlipka 4:510ea5b28a05 107 }
hlipka 4:510ea5b28a05 108 return 0;
hlipka 4:510ea5b28a05 109 }