Skip to content

Commit

Permalink
add basic gsensor support
Browse files Browse the repository at this point in the history
  • Loading branch information
0x64c committed Sep 30, 2016
1 parent 53e9712 commit 199aa23
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
1 change: 1 addition & 0 deletions vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ u8 playeraim=0,portraitmode=0,fpsdisplay=0,frameskip=1,thisframenice=1;

s32 vibro=127,gsensor[6]= {0,0,0, 0,0,0};
u8 vibrogcw=3;
u8 gsensor_recentre=1;

s32 lcuber[3],lflare[8];

Expand Down
23 changes: 22 additions & 1 deletion zcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ EGLint attrib_list[]= {EGL_SURFACE_TYPE,EGL_WINDOW_BIT,EGL_BUFFER_SIZE,16,EGL_DE
SDL_Surface *screen = NULL;
#endif

SDL_Joystick *gamepad=NULL, *pissdoranub2=NULL;;
SDL_Joystick *gamepad=NULL, *pissdoranub2=NULL;

int audio_channels=2,audio_rate=22050,audio_buffers=1024;
Uint16 audio_format= AUDIO_S16;
Expand Down Expand Up @@ -684,8 +684,26 @@ void zcore_input_init(void)
#ifdef PANDORA
gamepad=SDL_JoystickOpen(1);
pissdoranub2=SDL_JoystickOpen(2);
#else
#if defined(GCW) && !defined(PC)
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
printf("Joystick %u: \"%s\"\n", i, SDL_JoystickName(i));
if (strcmp(SDL_JoystickName(i), "linkdev device (Analog 2-axis 8-button 2-hat)") == 0)
{
gamepad = SDL_JoystickOpen(i);
if (gamepad)
printf("Recognized GCW Zero's built-in analog stick..\n");
else
printf("ERROR: Failed to recognize GCW Zero's built-in analog stick..\n");
} else if (strcmp(SDL_JoystickName(i), "mxc6225") == 0)
{
printf("GCW Zero's built-in g-sensor..\n");
}
}
#else
gamepad=SDL_JoystickOpen(0);
#endif
#endif
}
}
Expand Down Expand Up @@ -758,6 +776,9 @@ void zcore_input_frame(void)
break;
case SDL_KEYUP:
for (i=0; i<20; i++) if (event.key.keysym.sym==code_keyb[i]) i_keyb[i]=0;
#ifdef GCW
if(event.key.keysym.sym==SDLK_HOME) gsensor_recentre=1;
#endif
break;
#endif
case SDL_QUIT:
Expand Down
85 changes: 83 additions & 2 deletions zlext.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void zlInitVibe(void){
effect.delay = 0;
id = Shake_UploadEffect(device, &effect);

Shake_InitEffect(&effect2, SHAKE_EFFECT_PERIODIC);
Shake_InitEffect(&effect2, SHAKE_EFFECT_PERIODIC);
effect2.u.periodic.waveform = SHAKE_PERIODIC_SINE;
effect2.u.periodic.period = 0.1*0x100;
effect2.u.periodic.magnitude = 0x6000;
Expand All @@ -37,7 +37,7 @@ void zlInitVibe(void){
effect2.delay = 0;
id2 = Shake_UploadEffect(device, &effect2);

Shake_InitEffect(&effect3, SHAKE_EFFECT_PERIODIC);
Shake_InitEffect(&effect3, SHAKE_EFFECT_PERIODIC);
effect3.u.periodic.waveform = SHAKE_PERIODIC_SINE;
effect3.u.periodic.period = 0.1*0x100;
effect3.u.periodic.magnitude = 0x5000;
Expand Down Expand Up @@ -67,6 +67,63 @@ void zlShutDownVibe(void){
Shake_Close(device);
Shake_Quit();
}
#ifndef PC
#include "SDL/SDL.h"
#include <stdio.h>
SDL_Joystick *gamepad_sensor=NULL;
void zlInitGSensor(){
printf ("zlext Initializing joystick driver\n");
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
printf("zlext Joystick %u: \"%s\"\n", i, SDL_JoystickName(i));
if (strcmp(SDL_JoystickName(i), "linkdev device (Analog 2-axis 8-button 2-hat)") == 0)
{
printf("zlext GCW Zero's built-in analog stick..\n");
} else if (strcmp(SDL_JoystickName(i), "mxc6225") == 0)
{
gamepad_sensor = SDL_JoystickOpen(i);
if (gamepad_sensor)
printf("zlext Recognized GCW Zero's built-in g-sensor..\n");
else
printf("zlext ERROR: Failed to recognize GCW Zero's built-in g-sensor..\n");
}
}
}
int x_correct=0;
int y_correct=0;
void zlProcGSensor(){
int x,y,ix,iy;
x=SDL_JoystickGetAxis(gamepad_sensor,0)/32;
y=SDL_JoystickGetAxis(gamepad_sensor,1)/32;

if(gsensor_recentre==1){
x_correct=0-x;
y_correct=0-y;
gsensor_recentre=0;
}

printf("%d\t%d\t%d\t%d\n",x,y,x+x_correct,y+y_correct);

ix=x+x_correct-gsensor[0];
iy=y+y_correct-gsensor[1];

int gsensor_filter=40;

if (abs(ix)<gsensor_filter) ix=0;
if (abs(iy)<gsensor_filter) iy=0;

gsensor[3]=gsensor[3]+(ix-gsensor[3])/4;
gsensor[4]=gsensor[4]+(iy-gsensor[4])/4;

int gsensor_filter0=5;

if (abs(gsensor[3])<gsensor_filter0) gsensor[3]=0;
if (abs(gsensor[4])<gsensor_filter0) gsensor[4]=0;
}
void zlShutDownGSensor(){
SDL_JoystickClose(gamepad_sensor);
}
#endif
#endif

#ifdef GP2XCAANOO
Expand Down Expand Up @@ -282,6 +339,7 @@ void zlextinit(void)
{
#ifdef GCW
zlInitVibe();
zlInitGSensor();
#endif
#ifdef GP2XCAANOO
zlInitVibe();
Expand Down Expand Up @@ -323,6 +381,26 @@ void zlextframe(void)
#endif

#ifdef GCW
#ifndef PC
if (configdata[11])
{
zlProcGSensor();
consoleturn[1]+=((-gsensor[0]-consoleturn[1])>>4);
consoleturn[0]+=(((1024-gsensor[1])-consoleturn[0])>>4);
}
else
{
gsensor[0]=0;
gsensor[1]=0;
gsensor[2]=0;
gsensor[3]=0;
gsensor[4]=0;
gsensor[5]=0;
consoleturn[0]=0;
consoleturn[1]=0;
}
#endif

if (configdata[10]) zlProcVibe();
else vibrogcw=0;
#endif
Expand All @@ -337,5 +415,8 @@ void zlextshutdown(void)
#endif
#ifdef GCW
zlShutDownVibe();
#ifndef PC
zlShutDownGSensor();
#endif
#endif
}

0 comments on commit 199aa23

Please sign in to comment.