| 1 |
/* |
| 2 |
Read touchscreen positions. |
| 3 |
|
| 4 |
Returns 1 if touched, and the average position in xpos and ypos. Otherwise, if not touched, returns 0. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <nds.h> |
| 8 |
|
| 9 |
void CDC_ReadRegistersEx(u32 page, u32 reg, u8* buf, u32 size); |
| 10 |
|
| 11 |
u32 TouchRead(u32* xpos, u32* ypos) |
| 12 |
{ |
| 13 |
int i; |
| 14 |
u8 buf[20]; |
| 15 |
u16 x[5]; |
| 16 |
u16 y[5]; |
| 17 |
u32 xavg; |
| 18 |
u32 yavg; |
| 19 |
u8 state; |
| 20 |
|
| 21 |
|
| 22 |
state = CDC_ReadRegisterEx(3,9); |
| 23 |
if ( (state & 0xC0) == 0x40 ) |
| 24 |
return 0; |
| 25 |
|
| 26 |
state = CDC_ReadRegisterEx(3, 14); |
| 27 |
if (state & 2) |
| 28 |
return 2; |
| 29 |
|
| 30 |
|
| 31 |
CDC_ReadRegistersEx(252, 1, buf, 20); |
| 32 |
|
| 33 |
xavg = 0; |
| 34 |
yavg = 0; |
| 35 |
|
| 36 |
for(i=0; i<5; i++) |
| 37 |
{ |
| 38 |
x[i] = buf[i*2+1] | (buf[i*2+0]<<8); |
| 39 |
y[i] = buf[10+i*2+1] | (buf[10+i*2+0]<<8); |
| 40 |
|
| 41 |
if (x[i] & 0xF000) |
| 42 |
return 3; |
| 43 |
|
| 44 |
if (y[i] & 0xF000) |
| 45 |
return 4; |
| 46 |
|
| 47 |
xavg += x[i]; |
| 48 |
yavg += y[i]; |
| 49 |
} |
| 50 |
|
| 51 |
xavg /= 5; |
| 52 |
yavg /= 5; |
| 53 |
|
| 54 |
*xpos = xavg; |
| 55 |
*ypos = yavg; |
| 56 |
|
| 57 |
return 1; |
| 58 |
} |
| 59 |
|