{-# LANGUAGE RecordWildCards #-}
module Network.UI.Kafka.Joystick.Interpretation (
joystickLoop
) where
import Data.ByteString.Lazy.Char8 (hGet)
import Network.UI.Kafka (ExitAction, LoopAction)
import Network.UI.Kafka.Interpretation (Interpretation(..), interpretationLoop)
import System.Hardware.Linux.Joystick (Joystick(..), byteLength, interpretJoystick, maxValue)
import System.IO (IOMode(ReadMode), hClose, openFile)
joystickLoop :: Interpretation FilePath Double
-> IO (ExitAction, LoopAction)
joystickLoop interpretation@TrackInterpretation{..} =
do
let
analogHandler (Joystick _ setting number False True _) = Just (number, fromIntegral setting / fromIntegral maxValue)
analogHandler _ = Nothing
buttonHandler (Joystick _ pressed number True False _) = Just (number, pressed /= 0)
buttonHandler _ = Nothing
joystick <- openFile device ReadMode
(exit, loop) <-
interpretationLoop analogHandler buttonHandler interpretation
$ interpretJoystick
<$> hGet joystick byteLength
return
(
do
exit
hClose joystick
, loop
)