Skip to content

3.4. What about the touchscreen?

N•I•L edited this page Nov 24, 2021 · 2 revisions

Let's use the libnds examples "hello_world" code in the following discussion. I have rewritten the main block in order to highlight the most important parts related to touch screen:

touchPosition touchXY;
consoleDemoInit();

while(1) 
{	
    swiWaitForVBlank();
    scanKeys();    
    touchRead(&touchXY);

    goto_xy(0,16);
    iprintf("Touch x = %04X, %04X\n", touchXY.rawx, touchXY.px);
    iprintf("Touch y = %04X, %04X\n", touchXY.rawy, touchXY.py);			
}

touchPosition touchXY

touchPosition is a struct that stores touch data. The points of interest are the members px and py, which represent the touch point coordinates. There are also some rawx/rawy values. The relation between the raw and point coordinates seems to be the following:

touchXY.rawx == (touchXY.px - (touchXY.px==0 || touchXY.px==255)) << 4;
touchXY.rawy == (touchXY.py - (touchXY.py==0 || touchXY.py==191)) << 4;

touchPosition also contains some "raw cross panel resistance" values probably related to the touch pressure (needs to be tested).

scanKeys()

As explained in 3.3. Handling key input, the touch also counts as a key, so you must call scanKeys() before attempting to read touch data.

touchRead(&touchXY);

This is where fetching touch data actually happens. By passing a touchPosition reference to touchRead, its properties are updated with the latest values of touch positions. Now we can freely use touchXY.px and touchXY.py in any way we want.