GP2X Regarding Touchscreen Use


flower_of_doom

Still Fresh
Joined
Feb 4, 2009
Messages
2
me again..
one thing i've noticed.. many games tend to depend on the homebrew enabler for touchscreen support.. i'm sorry for i didn't have time to scan through your code and see how you do it, but there is a touchscreen sample program somewhere on the archives, which works by opening the touchscreen device as a file and then reading data from it. the touchscreen sample did have a flaw, as i found out when i tried drawing a square on the coordinates provided by the device. the x and y coordinates were read in wrong order.
also, i tried to reduce the jitter in the location by buffering the last 5 positions and then calculating the mean position. it's not perfect, but it seems to be better than before.
so, here's the code

touchscreen_manager.hpp
CODE
#ifndef TOUCHSCREEN_MANAGER_HPP_INCLUDED

#define TOUCHSCREEN_MANAGER_HPP_INCLUDED

#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <fcntl.h>



// touchscreen manager mainly based on a touchscreen testing program source from gp2x archives...


typedef struct {
uint16_t pressure;
uint16_t y;
uint16_t x;
uint16_t pad;
struct timeval stamp;
} ts_event_t;

class TouchscreenManager {
private:
int fd;
ts_event_t ts_event;
int valuesx[5],valuesy[5];
int valind,xcur,ycur;
public:
TouchscreenManager();
void getTouchData(int *x, int *y);
};

#endif // TOUCHSCREEN_MANAGER_HPP_INCLUDED



and
touchscreen_manager.cpp
CODE
#include "touchscreen_manager.hpp"

TouchscreenManager::TouchscreenManager()
{
fd = open( "/dev/touchscreen/wm97xx", O_RDONLY | O_NOCTTY );
valind=0;
read(fd, &ts_event, sizeof(ts_event_t));
for (int i=0; i<5; i++) {
valuesx=ts_event.x;
valuesy=ts_event.y;
}
}


void TouchscreenManager::getTouchData(int *x, int *y)
{
read(fd, &ts_event, sizeof(ts_event_t));

valuesx[valind]=ts_event.x;
valuesy[valind]=ts_event.y;
valind==4?valind=0:valind++;

xcur = (valuesx[0] + valuesx[1] + valuesx[2] + valuesx[3] + valuesx[4]) /5; //hardcoded for speed;)
ycur = (valuesy[0] + valuesy[1] + valuesy[2] + valuesy[3] + valuesy[4]) /5;

*x = (int)((xcur-200)*320/3750);
*y = (int)(240 - ((ycur-200)*240/3750));
}



i didn't implement getting data about whether the touchscreen is pressed or not.. will do that soon..

hope it helps someone..
if you try it out, please notify me of the results :)
 
Top