Skip to content

Commit

Permalink
touch screen input in DSi mode, temp hack to flag DSi mode to arm7
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Murphy committed Sep 22, 2010
1 parent 4256310 commit 59a63f7
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 43 deletions.
1 change: 1 addition & 0 deletions include/nds/arm7/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extern "C" {
void touchReadXY(touchPosition *touchPos);
uint16 touchRead(uint32 command);
uint32 touchReadTemperature(int * t1, int * t2);
bool touchPenDown();

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion include/nds/fifocommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ typedef enum {
PM_REQ_SLEEP = (4<<16),
PM_REQ_SLEEP_DISABLE = (5<<16),
PM_REQ_SLEEP_ENABLE = (6<<16),
PM_REQ_BATTERY = (7<<16)
PM_REQ_BATTERY = (7<<16),
PM_DSI_HACK = (8<<16)
}FifoPMCommands;

//! Enum values for the fifo wifi commands.
Expand Down
2 changes: 1 addition & 1 deletion include/nds/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typedef enum
*/
#define REG_POWERCNT *(vu16*)0x4000304


#define REG_DSIMODE *(vu32*)0x4004000
static inline
/*!
\brief sets the Y trigger(?)
Expand Down
28 changes: 13 additions & 15 deletions source/arm7/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,30 @@ void inputGetAndSend(void){

u16 keys= REG_KEYXY;


if(!touchPenDown()) {
keys |= KEY_TOUCH;
} else {
keys &= ~KEY_TOUCH;
}

msg.SystemInput.keys = keys;

if(keys & KEY_TOUCH)
{
if(keys & KEY_TOUCH) {
penDown = false;
}
else
{
} else {
msg.SystemInput.keys |= KEY_TOUCH;

if(penDown)
{
if(penDown) {
touchReadXY(&tempPos);

if(tempPos.rawx && tempPos.rawy)
{
if(tempPos.rawx && tempPos.rawy) {
msg.SystemInput.keys &= ~KEY_TOUCH;
msg.SystemInput.touch = tempPos;
}
else
{
} else {
penDown = false;
}
}
else
{
} else {
penDown = true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions source/arm7/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <nds/arm7/clock.h>

bool sleepIsEnabled = true;
extern bool __dsimode;

//---------------------------------------------------------------------------------
void powerValueHandler(u32 value, void* user_data) {
Expand Down Expand Up @@ -100,6 +101,9 @@ void powerValueHandler(u32 value, void* user_data) {
if (backlight & (1<<6)) battery += backlight & (1<<3)<<12;
fifoSendValue32(FIFO_SYSTEM, battery);
break;
case PM_DSI_HACK:
__dsimode = true;
break;
}
}

Expand Down
171 changes: 145 additions & 26 deletions source/arm7/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Touch screen control for the ARM7
Copyright (C) 2005
Copyright (C) 2005 - 2010
Michael Noland (joat)
Jason Rogers (dovoto)
Dave Murphy (WinterMute)
Expand All @@ -29,6 +29,7 @@
#include <nds/ndstypes.h>
#include <nds/system.h>
#include <nds/arm7/touch.h>
#include <nds/arm7/input.h>
#include <nds/interrupts.h>

#include <stdlib.h>
Expand All @@ -40,6 +41,69 @@ static u8 range_counter_2 = 0;
static u8 range = 20;
static u8 min_range = 20;

bool __dsimode = false;

//---------------------------------------------------------------------------------
u32 readTSCReg(u32 reg) {
//---------------------------------------------------------------------------------

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = ((reg<<1) | 1) & 0xFF;

while(REG_SPICNT & 0x80);

REG_SPIDATA = 0;

while(REG_SPICNT & 0x80);

REG_SPICNT = 0;

return REG_SPIDATA;
}

//---------------------------------------------------------------------------------
void readTSCRegArray(u32 reg, void *buffer, int size) {
//---------------------------------------------------------------------------------

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = ((reg<<1) | 1) & 0xFF;

char *buf = (char*)buffer;
while(REG_SPICNT & 0x80);
int count = 0;
while(count<size) {
REG_SPIDATA = 0;

while(REG_SPICNT & 0x80);


buf[count++] = REG_SPIDATA;

}
REG_SPICNT = 0;

}


//---------------------------------------------------------------------------------
u32 writeTSCReg(u32 reg, u32 value) {
//---------------------------------------------------------------------------------

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = ((reg<<1)) & 0xFF;

while(REG_SPICNT & 0x80);

REG_SPIDATA = value;

while(REG_SPICNT & 0x80);

REG_SPICNT = 0;

return REG_SPIDATA;
}


//---------------------------------------------------------------------------------
u8 CheckStylus(){
//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -271,24 +335,12 @@ void UpdateRange(uint8 *this_range, int16 last_dist_max, u8 data_error, u8 tsc_t
}

//---------------------------------------------------------------------------------
// reading pixel position:
//---------------------------------------------------------------------------------
void touchReadXY(touchPosition *touchPos) {
void touchReadDSMode(touchPosition *touchPos) {
//---------------------------------------------------------------------------------

int16 dist_max_y, dist_max_x, dist_max;
u8 error, error_where, first_check, i;

if ( !touchInit ) {

xscale = ((PersonalData->calX2px - PersonalData->calX1px) << 19) / ((PersonalData->calX2) - (PersonalData->calX1));
yscale = ((PersonalData->calY2px - PersonalData->calY1px) << 19) / ((PersonalData->calY2) - (PersonalData->calY1));

xoffset = ((PersonalData->calX1 + PersonalData->calX2) * xscale - ((PersonalData->calX1px + PersonalData->calX2px) << 19) ) / 2;
yoffset = ((PersonalData->calY1 + PersonalData->calY2) * yscale - ((PersonalData->calY1px + PersonalData->calY2px) << 19) ) / 2;
touchInit = true;
}

uint32 oldIME = REG_IME;

REG_IME = 0;
Expand Down Expand Up @@ -340,18 +392,6 @@ void touchReadXY(touchPosition *touchPos) {
break;
}

s16 px = ( touchPos->rawx * xscale - xoffset + xscale/2 ) >>19;
s16 py = ( touchPos->rawy * yscale - yoffset + yscale/2 ) >>19;

if ( px < 0) px = 0;
if ( py < 0) py = 0;
if ( px > (SCREEN_WIDTH -1)) px = SCREEN_WIDTH -1;
if ( py > (SCREEN_HEIGHT -1)) py = SCREEN_HEIGHT -1;

touchPos->px = px;
touchPos->py = py;


}else{
error_where = 3;
touchPos->rawx = 0;
Expand All @@ -365,3 +405,82 @@ void touchReadXY(touchPosition *touchPos) {

}

//---------------------------------------------------------------------------------
bool touchPenDown() {
//---------------------------------------------------------------------------------
bool down;
if (__dsimode) {

int oldIME = enterCriticalSection();
writeTSCReg(0,3);

down = (!(readTSCReg(9)&0x40) && !(readTSCReg(14)&3));
leaveCriticalSection(oldIME);

} else {
down = !(REG_KEYXY & (1<<6));
}
return down;
}

//---------------------------------------------------------------------------------
void touchReadDSiMode(touchPosition *touchPos) {
//---------------------------------------------------------------------------------
u8 touchdata[20];
int i, rawx = 0, rawy = 0, x, y;

int oldIME = enterCriticalSection();

writeTSCReg(0,252);
readTSCRegArray(1,touchdata,20);

for (i=0;i<10;i+=2) {
x = (touchdata[i]<<8) + touchdata[i+1];
y = (touchdata[i+10]<<8) + touchdata[i+11];
if ((x & 0xf000) || (y & 0xf000)) break;
rawx += x;
rawy += y;
}

if (i!=10) {
rawx = 0; rawy= 0;
} else {
touchPos->rawx = rawx/5;
touchPos->rawy = rawy/5;
}

leaveCriticalSection(oldIME);
}

//---------------------------------------------------------------------------------
// reading pixel position:
//---------------------------------------------------------------------------------
void touchReadXY(touchPosition *touchPos) {
//---------------------------------------------------------------------------------
if ( !touchInit ) {

xscale = ((PersonalData->calX2px - PersonalData->calX1px) << 19) / ((PersonalData->calX2) - (PersonalData->calX1));
yscale = ((PersonalData->calY2px - PersonalData->calY1px) << 19) / ((PersonalData->calY2) - (PersonalData->calY1));

xoffset = ((PersonalData->calX1 + PersonalData->calX2) * xscale - ((PersonalData->calX1px + PersonalData->calX2px) << 19) ) / 2;
yoffset = ((PersonalData->calY1 + PersonalData->calY2) * yscale - ((PersonalData->calY1px + PersonalData->calY2px) << 19) ) / 2;
touchInit = true;
}

if (__dsimode) {
touchReadDSiMode(touchPos);
} else {
touchReadDSMode(touchPos);
}

s16 px = ( touchPos->rawx * xscale - xoffset + xscale/2 ) >>19;
s16 py = ( touchPos->rawy * yscale - yoffset + yscale/2 ) >>19;

if ( px < 0) px = 0;
if ( py < 0) py = 0;
if ( px > (SCREEN_WIDTH -1)) px = SCREEN_WIDTH -1;
if ( py > (SCREEN_HEIGHT -1)) py = SCREEN_HEIGHT -1;

touchPos->px = px;
touchPos->py = py;
}
3 changes: 3 additions & 0 deletions source/arm9/system/initSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void __attribute__((weak)) initSystem(void) {
fifoSetValue32Handler(FIFO_PM, powerValueHandler, 0);
fifoSetDatamsgHandler(FIFO_SYSTEM, systemMsgHandler, 0);

if(REG_DSIMODE) {
fifoSendValue32(FIFO_PM,PM_DSI_HACK);
}
__transferRegion()->buttons = 0xffff;

punixTime = (time_t*)memUncached((void *)&__transferRegion()->unixTime);
Expand Down

0 comments on commit 59a63f7

Please sign in to comment.