Development:Tutorials:SetupInput

From DingooWiki

Jump to: navigation, search

Contents

[edit] Using SDL

As normal no need it any special setting, a simple example:

    [...]
    SDL_Event event;
    
    while(SDL_PollEvent(&event)){
               
	        switch (event.type) {
                   
	            case SDL_KEYDOWN:
	                  if(event.key.keysym.sym == SDLK_ESCAPE)
                            printf("Hello World from SDL!\n");
               }
    }
    [...]

[edit] SDL apps crashing

An app might crash due to SDL not finding a mouse. To solve this, you can run the command line

 export SDL_NOMOUSE=1

before launching any app. For instance, toddler's Local Pack does it after Dingux boots, and before launching gmenu2x (check file /local/sbin/main)

Since you cannot be certain that the user's configuration will include this, you can always force it in your own code by having

 putenv("SDL_NOMOUSE=1");

before SDL_Init.

[edit] Hiding the cursor

Sometimes you can see an annoying cursor in the top-left corner of an SDL app. The simple solution is to ask SDL to hide it. Call

 SDL_ShowCursor(SDL_DISABLE);

right after SDL_SetVideoMode.

[edit] Using Kernel Driver (GPIO)

And to setup the kernel event system, simple mode:

[...]
#include <fcntl.h>
#include <sys/mman.h>
#include <linux/kd.h>
#include <linux/input.h>
[...]

 int key_dev;
 
 if ((key_dev = open("/dev/event0", O_RDONLY | O_NONBLOCK)) == -1) return -1;

mmap mode, any help?

 int key_dev;
 
 if ((key_dev = open("/dev/event0", O_RDONLY | O_NONBLOCK)) == -1) return -1;
 mmap(...


Keyboard/Dingoo Pad relations:

           D-pad up            KEY_UP
           D-pad down          KEY_DOWN
           D-pad left          KEY_LEFT
           D-pad right         KEY_RIGHT
           A button            KEY_LEFTCTRL
           B button            KEY_LEFTATL
           X button            KEY_SPACE
           Y button            KEY_LEFTSHIFT
           Left shoulder       KEY_TAB
           Right shoulder      KEY_BACKSPACE
           START button        KEY_ENTER
           SELECT button       KEY_ESC

Using GPIO Kernel Driver:

       struct input_event event;
       if (key_dev == -1 || read(key_dev, &event, sizeof(event)) != sizeof(event)) return false;
       
       if (event.type == EV_KEY)
       {
	          switch(event.value)
	          {
                   case 1:
                           if(ev.code == KEY_ESC)
                               printf("ESC PRESSED!\n");
                           break;
                   case 0:
                           if(ev.code == KEY_ESC)
                               printf("ESC RELEASED!\n");
                           break;
                 }
       }

Freeing GPIO driver:

       close(key_dev);

[edit] Using Direct Memory Access

Setup memory direct access to pad:

(You need to disable console output too, see Development:Tutorials:SetupVideo#Disabling_Console_Output)

...
#include "jz4740.h" /* Get it from gp2xspectrum dingoo port */
...
  unsigned long * event_base;
  
  if ((jz_dev = open("/dev/mem", O_RDWR)) == -1) return false; 
  event_base = (unsigned long  *)mmap(0, 0x300, PROT_READ|PROT_WRITE, MAP_SHARED, jz_dev, 0x10010000); 

Close access:

  munmap((void *)event_base, 0x300); 
  close(jz_dev); 

Dingoo Pad definitions:

  enum  { DINGOO_BUTTON_UP=1<<6,      DINGOO_BUTTON_LEFT=1<<5,      DINGOO_BUTTON_DOWN=1<<27, DINGOO_BUTTON_RIGHT=1<<18, 
          DINGOO_BUTTON_START=1<<16,  DINGOO_BUTTON_SELECT=1<<17,   DINGOO_BUTTON_R=1<<14,    DINGOO_BUTTON_L=1<<15, 
          DINGOO_BUTTON_Y=1<<2,       DINGOO_BUTTON_B=1<<1,         DINGOO_BUTTON_A=1<<0,     DINGOO_BUTTON_X=1<<19, 
          DINGOO_BUTTON_VOL_UP=1<<20, DINGOO_BUTTON_VOL_DOWN=1<<21, DINGOO_BUTTON_PUSH=1<<29 };         

Using a simple example:

  unsigned long value; 
  value = ~((event_base[0x300>>2] & 0x280EC067) | ((event_base[0x200>>2] & 0x20000) >> 1) | 0x300000);
  
  if(value & DINGOO_BUTTON_SELECT) 
      printf("Hello World\n");
Personal tools