-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystick.hs
More file actions
58 lines (45 loc) · 1.64 KB
/
Copy pathjoystick.hs
File metadata and controls
58 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Control.Monad.Identity (Identity)
import Control.Monad.IO.Class
import Control.Wire
-- use functions from Wire instead of Prelude
import Prelude hiding ((.), id)
import GHC.Float -- double2Float
import qualified Graphics.UI.GLFW as GLFW
control whenInhibited whenProduced wire = loop wire clockSession
where
loop w' session' = do
-- run a discrete time step of the wire
(mx, w, session) <- stepSession w' session' ()
case mx of
Left ex -> whenInhibited ex
Right x -> whenProduced x
loop w session
main :: IO ()
main = do
True <- GLFW.initialize
True <- GLFW.openWindow GLFW.defaultDisplayOptions
{ GLFW.displayOptions_numRedBits = 8
, GLFW.displayOptions_numGreenBits = 8
, GLFW.displayOptions_numBlueBits = 8
, GLFW.displayOptions_numDepthBits = 1 }
control return print $ positionWire . timeFrom 0
getJoystickDirections :: IO (Float, Float)
getJoystickDirections = do
GLFW.swapBuffers -- Process window messages
r <- take 2 `fmap` GLFW.getJoystickPosition GLFW.Joystick0 2
return $
case r of
[x, y] -> (x, y)
_ -> (0, 0)
positionWire :: Wire () IO Time (Float,Float)
positionWire = accum (+) (0, 0) . joystickWire
-- Scales input so small movements have little affect and large movements have
-- significant affect.
deadzone :: Float -> Float
deadzone x = if (abs x < deadzoneDefault) then 0 else x*x*x
deadzoneDefault = 0.16 -- best deadzone for my controller
joystickWire :: Wire () IO Time (Float,Float)
joystickWire = mkFixM $
\dt t -> do
(x, y) <- getJoystickDirections
return (Right (deadzone x * double2Float dt, deadzone y * double2Float dt))