Limitation of analogic speed

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
kuat
Posts: 5
Joined: Mon Mar 09, 2009 7:06 am

Limitation of analogic speed

Post by kuat »

Hi there!
Is there anyway to limit speed of analogic incrementation of pad.Lx and pad.Ly?
Thx
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Well there are easy software ways to achieve the same thing,
ie. time the increment of another var till it reaches the lx/y value.
If not actually, then potentially.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

It just reports the x,y coordinates at that moment in time. If you want acceleration/deceleration effects then that is up to you to implement in your software.
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

You can use a simple follower derived from PID controller: for each axis,

Code: Select all

ca += (ra - ca)*k;
where:
ca = computed axis (the one you will use)
ra = real axis (the position you readed from hw)
k = a constant between 0 and 1 (the lower, the slower)
each of the members need to be float or more precise floating point types.

A fast fixed point implementation is possible if you plan to make heavy use of this (like I do :P )
kuat
Posts: 5
Joined: Mon Mar 09, 2009 7:06 am

Post by kuat »

Well!
Thankyou guys, that was a lot of help!!
sauron_le_noir
Posts: 203
Joined: Sat Jul 05, 2008 8:03 am

Post by sauron_le_noir »

look at this it is taken from the mouse part of the allegro library for psp
the function static void psp_mouse_timer_poll(void) do all the job
you have auto accelration in all the position thx to the real good code of Nekuz0r

Code: Select all

/*         ______   ___    ___
 *        /\  _  \ /\_ \  /\_ \
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      PSP mouse driver.
 *      TODO:.
 *
 *      By sauron_le_noir some part of psp_mouse_timer_poll based on
 *                        the sdl code of Nekuz0r
 *
 *      See readme.txt for copyright information.
 */

#include "allegro.h"
#include "allegro/internal/aintern.h"
#include "allegro/platform/aintpsp.h"

#include "math.h"
#include <pspctrl.h>

#ifndef ALLEGRO_PSP
#error something is wrong with the makefile
#endif

static int  psp_mouse_init&#40;void&#41;;
static void psp_mouse_exit&#40;void&#41;;
static void psp_mouse_position&#40;int, int&#41;;
static void psp_mouse_set_range&#40;int, int, int, int&#41;;
static void psp_mouse_get_mickeys&#40;int *, int *&#41;;
static void psp_mouse_timer_poll&#40;void&#41;;


MOUSE_DRIVER mouse_psp =
&#123;
   MOUSE_PSP,
   empty_string,
   empty_string,
   "psp mouse",
   psp_mouse_init,
   psp_mouse_exit,
   NULL,       // AL_METHOD&#40;void, poll, &#40;void&#41;&#41;;
   psp_mouse_timer_poll,                 // AL_METHOD&#40;void, timer_poll, &#40;void&#41;&#41;;
   psp_mouse_position,
   psp_mouse_set_range,
   NULL,       // AL_METHOD&#40;void, set_speed, &#40;int xspeed, int yspeed&#41;&#41;;
   psp_mouse_get_mickeys,
   NULL,       // AL_METHOD&#40;int,  analyse_data, &#40;AL_CONST char *buffer, int size&#41;&#41;;
   NULL,       // AL_METHOD&#40;void,  enable_hardware_cursor, &#40;AL_CONST int mode&#41;&#41;;
   NULL        // AL_METHOD&#40;int,  select_system_cursor, &#40;AL_CONST int cursor&#41;&#41;;
&#125;;

static int mouse_minx = 0;
static int mouse_miny = 0;
static int mouse_maxx = 479;  /* 0 to 479 = 480 */
static int mouse_maxy = 271;  /* 0 to 272 = 272 */   /* 480x272 is the resolution of the psp display */

static int mymickey_x = 0;
static int mymickey_y = 0;

static SceCtrlData pad;
static int aStickX = 0;
static int aStickY = 0;
static float aStickR;
static float aStickA;
static int x,y;
static float Precision = 360.0f;

static int getSqrRadius&#40;int X, int Y&#41; 
&#123;
   return sqrt&#40;&#40;X * X&#41; + &#40;Y * Y&#41;&#41;;
&#125;
static int psp_mouse_init&#40;void&#41;
&#123;
  sceCtrlSetSamplingCycle&#40;0&#41;;
  sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
&#125;

static void psp_mouse_timer_poll&#40;void&#41;
&#123;
   sceCtrlPeekBufferPositive&#40;&pad, 1&#41;;

   if &#40;pad.Buttons != 0&#41;
   &#123;
     sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
     _mouse_b = 0;
     if &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41; _mouse_b = 1;
     if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41; _mouse_b = 2;
     if &#40; &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41; && &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;&#41; _mouse_b = 4;
   &#125;
   aStickX = pad.Ly - 128;
   aStickY = pad.Lx - 128;
   
   aStickR = getSqrRadius&#40;aStickX, aStickY&#41;;
   if &#40;aStickR > 100&#41; &#123;
      if &#40;getSqrRadius&#40;aStickX, aStickY&#41; > 30&#41; &#123;
         aStickA = &#40;&#40;atan2f&#40;aStickX, -aStickY&#41; + M_PI&#41; / &#40;M_PI * 2&#41;&#41; * Precision;
      &#125; else &#123;
         aStickA = -1;     
      &#125;
   
      //1° a 89°
      if &#40;aStickA >= 1 && aStickA <= &#40;89 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         x += &#40;10 * &#40;1 - &#40;aStickA / &#40;Precision / 360.0f&#41; / 90&#41;&#41;&#41;;
         y -= &#40;10 * &#40;0 + &#40;aStickA / &#40;Precision / 360.0f&#41; / 90&#41;&#41;&#41;;
      &#125;
      else if &#40;aStickA >= &#40;91 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;179 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         x -= &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 89&#41; / 90&#41;&#41;&#41;;
         y -= &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 89&#41; / 90&#41;&#41;&#41;;
      &#125;
      else if &#40;aStickA >= &#40;181 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;269 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         x -= &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 179&#41; / 90&#41;&#41;&#41;;
         y += &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 179&#41; / 90&#41;&#41;&#41;;
      &#125;
      else if &#40;aStickA >= &#40;271 * &#40;Precision / 360.0f&#41;&#41; && aStickA <= &#40;359 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         x += &#40;10 * &#40;0 + &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 269&#41; / 90&#41;&#41;&#41;;
         y += &#40;10 * &#40;1 - &#40;&#40;&#40;aStickA / &#40;Precision / 360.0f&#41;&#41; - 269&#41; / 90&#41;&#41;&#41;;
      &#125;
      else if &#40;aStickA == 0&#41; &#123;
         x += 10;
      &#125;
      else if &#40;aStickA == &#40;90 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         y -= 10;
      &#125;
      else if &#40;aStickA == &#40;180 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         x -= 10;
      &#125;
      else if &#40;aStickR == &#40;270 * &#40;Precision / 360.0f&#41;&#41;&#41; &#123;
         y += 10;
      &#125;
   &#125;
   _mouse_x = x;
   if &#40;_mouse_x <  mouse_minx&#41; _mouse_x = 0;
   if &#40;_mouse_x > mouse_maxx&#41;  _mouse_x = mouse_maxx;
   _mouse_y = y; 
   if &#40;_mouse_y < mouse_miny&#41; _mouse_y = 0;
   if &#40;_mouse_y > mouse_maxy&#41; _mouse_y = mouse_maxy;
&#125;

static void psp_mouse_position&#40;int x, int y&#41;
&#123;
  _mouse_x = x;
  if &#40;_mouse_x <  mouse_minx&#41; _mouse_x = 0;
  if &#40;_mouse_x > mouse_maxx&#41;  _mouse_x = mouse_maxx;
  _mouse_y = y;
  if &#40;_mouse_y < mouse_miny&#41; _mouse_y = 0;
  if &#40;_mouse_y > mouse_maxy&#41; _mouse_y = mouse_maxy;
&#125;

static void psp_mouse_set_range&#40;int x1, int y1, int x2, int y2&#41;
&#123;
&#125;

static void psp_mouse_get_mickeys&#40;int *mickeyx, int *mickeyy&#41;
&#123;
  *mickeyx = 0;
  *mickeyy = 0;
&#125;

static void psp_mouse_exit&#40;void&#41;
&#123;
&#125;
Post Reply