Joysticks

Documentation. First post is always kind of manual page, being continuously updated by author. Following posts are discussion about the topic.
Post Reply
admin
Site Admin
Posts: 441
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Joysticks

Post by admin » 10 Jan 2017, 22:10

From version 0.1.0.313 game devices (joysticks or anything recognized by Direct Input) were extended and handling code was rewritten. Now it uses internally event system so no button press is lost even if it’s very short. It can also read POV (hat switch) value and it can read axis values.

Buttons are handled in the same way as keyboard keys. You have function to register callback for whole game device and in the function you receive button index (from 0) and direction (1 push, 0 release). Each POV switch behaves like 1 button but instead of simple 0/1 in direction value you can receive more values reflecting current POV position. The value is what Direct Input sends internally. -1 is center position, other directions send specific value.

Code: Select all

lmc_set_handler('JOY1',function(button, direction, ts)
  print('Callback for whole joystick: button ' .. button .. ', direction '..direction..', timestamp '..ts)
end)

The function is the same as for keyboard callback but game device callback can have additional attribute. In this attribute you receive timestamp (in milliseconds) when the button event happened. Events from game device are processed in some fixed interval by LuaMacros but this value come from Direct Input and it’s real timestamp of press/release – not delayed by LuaMacros handling code. This can be used to calculate time difference between to button presses – e.g. to accelerate rotary encoder value. The value is linked to computer startup, it’s not Unix epoch.

Axis return simply value between 0 and 65535 – the number you can see during calibration as numeric output. There’s callback function that is called on axis change.

Code: Select all

-- 1st param: device name
-- 2nd param: axis index
-- 3rd param: interval in ms
-- 4th param: minimum delta
lmc_set_axis_handler('LB2',0, 2000, 1000, function(val, ts)
  print('Callback for axis - value ' .. val..', timestamp '..ts)
end)

For performance reasons you can limit how often it is called (3rd argument) and what minimum value change is reported (4th argument). It means in the example above the handler won’t be called more often that 2000 miliseconds and axis value (callback argument) will be always bigger/smaller than last reported +- 1000.
Petr Medek
LUAmacros author

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest