{-# LANGUAGE RecordWildCards #-}
module Network.UI.Kafka.SpaceNav (
spacenavLoop
) where
import Data.ByteString.Lazy.Char8 (hGet)
import Network.UI.Kafka (ExitAction, LoopAction, TopicConnection, Sensor, producerLoop)
import Network.UI.Kafka.Types (Button(..), Event(..), Toggle(..))
import System.Hardware.Linux.SpaceNav (SpaceNav(..), byteLength, interpretSpaceNav)
import System.IO (IOMode(ReadMode), hClose, openFile)
spacenavLoop :: FilePath
-> TopicConnection
-> Sensor
-> IO (ExitAction, LoopAction)
spacenavLoop path topicConnection sensor =
do
spacenav <- openFile path ReadMode
(exit, loop) <-
producerLoop topicConnection sensor
$ translate
. interpretSpaceNav
<$> hGet spacenav byteLength
return
(
do
exit
hClose spacenav
, loop
)
translate :: SpaceNav
-> [Event]
translate SpaceNavButton{..} = [ButtonEvent (IndexButton number, if pressed then Down else Up)]
translate SpaceNavAnalog{..} = [AnalogEvent number setting]
translate SpaceNavNull = []