brmscope/Joystick.class
2011-08-30 22:22:08 +02:00

62 lines
1.6 KiB
Text

' Gambas class file
' Joystick.class - represents a HID game device containing
' one or more joysticks and buttons
'
' Copyright 2008 Rob Kudla, Binara, Inc. <rpm@kudla.org>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License version 2 as
' published by the Free Software Foundation.
PRIVATE jsdev AS Process
PUBLIC SUB _new(OPTIONAL device AS String)
IF device = "" THEN device = "/dev/input/js0"
' the interpreter dies when we try to read from /dev/input/js0 as a file
' saying "invalid argument", so we have to run cat
jsdev = EXEC ["cat", device] FOR READ AS "Device"
END
PUBLIC SUB Close()
' kill the cat process if it's still going
TRY jsdev.Kill
END
PUBLIC SUB Device_Read()
DIM stamp AS Integer
DIM value AS Short
DIM myevent AS Byte
DIM number AS Byte
DIM test AS String
' each joystick event produces 8 bytes
READ #jsdev, stamp, 4 ' 4 bytes
READ #jsdev, value, 2 ' 2 bytes
READ #jsdev, myevent, 1 ' 1 byte
READ #jsdev, number, 1 ' 1 byte
IF myevent AND 1 THEN ' button
IF value THEN ' pressed
PRINT "Got button press"
RAISE ButtonPress(number)
ELSE ' released
PRINT "Got button release"
RAISE ButtonRelease(number)
ENDIF
ELSE IF myevent AND 2 THEN ' joystick axis
RAISE StickMove(number, value)
ELSE
PRINT "Unhandled event: myevent is " & myevent
ENDIF
END
EVENT ButtonPress(ButtonNum AS Integer)
EVENT ButtonRelease(ButtonNum AS Integer)
EVENT StickMove(Axis AS Integer, Position AS Integer)