touch screen handler for the microchip AR1020

Files at this revision

API Documentation at this revision

Comitter:
hlipka
Date:
Tue Feb 22 21:43:39 2011 +0000
Parent:
0:cf4dd04052e3
Child:
2:1a436d154c84
Commit message:

Changed in this revision

AreaTouchHandler.cpp Show annotated file Show diff for this revision Revisions of this file
AreaTouchHandler.h Show annotated file Show diff for this revision Revisions of this file
ar1020.cpp Show annotated file Show diff for this revision Revisions of this file
ar1020.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AreaTouchHandler.cpp	Tue Feb 22 21:43:39 2011 +0000
@@ -0,0 +1,85 @@
+/*
+* mbed AR1020 library
+* Copyright (c) 2010 Hendrik Lipka
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+#include "AreaTouchHandler.h"
+#include "touchevent.h"
+
+using namespace std;
+
+uint32_t AreaTouchHandler::down(uint32_t arg)
+{
+    TouchEvent *te=(TouchEvent*)arg;
+    _x=te->x;
+    _y=te->y;
+    _samples=1;
+    int c=findCommand();
+    _called=false;
+    if (0!=c)
+    {
+        _called=true;
+        _callback.call(c);
+    }
+    return 0;
+}
+uint32_t AreaTouchHandler::move(uint32_t arg)
+{
+    if (_called)
+        return 0;
+    TouchEvent *te=(TouchEvent*)arg;
+    printf("%i\n",te->y);
+    _x=(te->x+_x*_samples)/(_samples+1);
+    _y=(te->y+_y*_samples)/(_samples+1);
+    _samples++;
+    int c=findCommand();
+    if (0!=c)
+    {
+        _called=true;
+        _callback.call(c);
+    }
+    return 0;
+}
+uint32_t AreaTouchHandler::up(uint32_t arg)
+{
+    printf("up\n");
+    _x=0;
+    _y=0;
+    _samples=0;
+    _called=false;
+    return 0;
+}
+
+int AreaTouchHandler::findCommand()
+{
+    if (_called)
+        return 0;
+    if (_samples<3)
+        return 0;
+    printf("%f\n",_y);
+    for (list<area*>::iterator it = _areas.begin(); it != _areas.end(); it++) {
+        area* a=*it;
+        if (_x>=a->left && _x<=a->right && _y>=a->top && _y<=a->bottom)
+            return a->command;
+        }
+    printf(".\n");
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AreaTouchHandler.h	Tue Feb 22 21:43:39 2011 +0000
@@ -0,0 +1,79 @@
+/*
+* mbed AR1020 library
+* Copyright (c) 2010 Hendrik Lipka
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+#ifndef __AREATOUCHHANDLER_H__
+#define __AREATOUCHHANDLER_H__
+
+#include "mbed.h"
+#include "FPointer.h"
+#include "touchpanel.h"
+#include <list>
+
+class area
+{
+    public:
+    int top,bottom,left,right,command;
+    area(int top, int bottom, int left, int right, int command)
+    {
+        this->top=top;
+        this->bottom=bottom;
+        this->left=left;
+        this->right=right;
+        this->command=command;
+    }
+};
+
+class AreaTouchHandler
+{
+    public:
+        AreaTouchHandler(TouchPanel* panel)
+        {
+            panel->attachPenDown(this,&AreaTouchHandler::down);
+            panel->attachPenMove(this,&AreaTouchHandler::move);
+            panel->attachPenUp(this,&AreaTouchHandler::up);
+        }
+        class T;
+        template<class T> 
+        void attach(T* item, uint32_t (T::*method)(uint32_t)) { _callback.attach(item, method); }   
+        void attach(uint32_t (*function)(uint32_t)) { _callback.attach(function); }   
+        
+        void addArea(int top, int bottom, int left, int right, int commandCode)
+        {
+            _areas.push_back(new area(top,bottom,left,right,commandCode));
+        }
+    private:
+        FPointer      _callback;
+        std::list<area*> _areas;
+        uint32_t down(uint32_t arg);
+        uint32_t move(uint32_t arg);
+        uint32_t up(uint32_t arg);
+        
+        int findCommand();
+        
+        float _x, _y;
+        int _samples;
+        bool _called;
+};
+
+
+#endif
--- a/ar1020.cpp	Mon Feb 21 22:29:40 2011 +0000
+++ b/ar1020.cpp	Tue Feb 22 21:43:39 2011 +0000
@@ -24,7 +24,7 @@
 #include "ar1020.h"
 #include "wait_api.h"
 
-AR1020::AR1020(SPI *spi, PinName enable, PinName sqi, bool swapX, bool swapY, bool swapXY)
+AR1020::AR1020(SPI *spi, PinName enable, PinName sqi, PinName power, bool swapX, bool swapY, bool swapXY)
 {
     _spi=spi;
     _enable=new DigitalOut(enable);
@@ -35,9 +35,11 @@
     _pen=0;
     _oldPen=false;
     _led=new DigitalOut(LED1);
+    _power=new DigitalOut(power);
+    _power->write(0);
 }
 
-AR1020::AR1020(PinName mosi, PinName miso, PinName clk, PinName enable, PinName sqi, bool swapX, bool swapY, bool swapXY)
+AR1020::AR1020(PinName mosi, PinName miso, PinName clk, PinName enable, PinName sqi, PinName power, bool swapX, bool swapY, bool swapXY)
 {
     _mosi=new DigitalOut(mosi);
     _miso=new DigitalIn(miso);
@@ -53,6 +55,8 @@
     _pen=0;
     _oldPen=false;
     _led=new DigitalOut(LED1);
+    _power=new DigitalOut(power);
+    _power->write(0);
 }
 
 AR1020::~AR1020()
@@ -63,6 +67,8 @@
 
 void AR1020::init()
 {
+    _power->write(1);
+    wait_ms(100);
     int r=cmd(0x13,NULL,0);    
     printf("disable touch=%i\n",r);
 
@@ -172,7 +178,7 @@
 int AR1020::cmd(char cmd,char* data, int len)
 {
     _enable->write(1);
-    wait_us(100);
+    wait_us(1000);
     _enable->write(0);
     
     wait_us(10);
--- a/ar1020.h	Mon Feb 21 22:29:40 2011 +0000
+++ b/ar1020.h	Tue Feb 22 21:43:39 2011 +0000
@@ -39,8 +39,8 @@
             @param spi the SPI object where the AR1020 is connected
             @params enable the pin name where /CS is connected
         */
-        AR1020(SPI *spi, PinName enable, PinName sqi, bool swapX=false, bool swapY=false, bool swapXY=false);
-        AR1020(PinName mosi, PinName miso, PinName clk, PinName enable, PinName sqi, bool swapX=false, bool swapY=false, bool swapXY=false);
+        AR1020(SPI *spi, PinName enable, PinName sqi, PinName power, bool swapX=false, bool swapY=false, bool swapXY=false);
+        AR1020(PinName mosi, PinName miso, PinName clk, PinName enable, PinName sqi, PinName power, bool swapX=false, bool swapY=false, bool swapXY=false);
         ~AR1020();
         virtual void init();
         virtual int x();
@@ -61,6 +61,7 @@
         int _x, _y, _pen; 
         
         DigitalOut *_led;
+        DigitalOut* _power;
     
         DigitalOut *_mosi;
         DigitalIn *_miso;