Added IR support
This commit is contained in:
parent
1bd17e6993
commit
9b4662b8e4
1 changed files with 41 additions and 7 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
module HWiid ( BDAddr(..)
|
module HWiid ( BDAddr(..)
|
||||||
, Wiimote
|
, Wiimote
|
||||||
|
, WiimoteIRSource(..)
|
||||||
, WiimoteState(..)
|
, WiimoteState(..)
|
||||||
, WiimoteMesg(..)
|
, WiimoteMesg(..)
|
||||||
, nullWiimote
|
, nullWiimote
|
||||||
|
@ -10,6 +11,7 @@ module HWiid ( BDAddr(..)
|
||||||
, hwiidFlagNonblock
|
, hwiidFlagNonblock
|
||||||
, hwiidReportStatus
|
, hwiidReportStatus
|
||||||
, hwiidReportButtons
|
, hwiidReportButtons
|
||||||
|
, hwiidReportIR
|
||||||
, hwiidLed1
|
, hwiidLed1
|
||||||
, hwiidLed2
|
, hwiidLed2
|
||||||
, hwiidLed3
|
, hwiidLed3
|
||||||
|
@ -40,6 +42,7 @@ module HWiid ( BDAddr(..)
|
||||||
|
|
||||||
|
|
||||||
import Data.Bits
|
import Data.Bits
|
||||||
|
import Data.Int
|
||||||
import Data.Typeable
|
import Data.Typeable
|
||||||
import Data.Word
|
import Data.Word
|
||||||
import Foreign.C.Types
|
import Foreign.C.Types
|
||||||
|
@ -65,6 +68,9 @@ hwiidReportStatus = (#const CWIID_RPT_STATUS)
|
||||||
hwiidReportButtons :: Word8
|
hwiidReportButtons :: Word8
|
||||||
hwiidReportButtons = (#const CWIID_RPT_BTN)
|
hwiidReportButtons = (#const CWIID_RPT_BTN)
|
||||||
|
|
||||||
|
hwiidReportIR :: Word8
|
||||||
|
hwiidReportIR = (#const CWIID_RPT_IR)
|
||||||
|
|
||||||
|
|
||||||
hwiidLed1 :: Word8
|
hwiidLed1 :: Word8
|
||||||
hwiidLed1 = (#const CWIID_LED1_ON)
|
hwiidLed1 = (#const CWIID_LED1_ON)
|
||||||
|
@ -156,8 +162,32 @@ bdAddrAny :: BDAddr
|
||||||
bdAddrAny = BDAddr (0, 0, 0, 0, 0, 0)
|
bdAddrAny = BDAddr (0, 0, 0, 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
data WiimoteIRSource = WiimoteIRSource
|
||||||
|
{ irValid :: CChar
|
||||||
|
, irPosX :: Word16
|
||||||
|
, irPosY :: Word16
|
||||||
|
, irSize :: Int8
|
||||||
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Storable WiimoteIRSource where
|
||||||
|
sizeOf _ = (#size struct cwiid_ir_src)
|
||||||
|
alignment _ = alignment (undefined :: CInt)
|
||||||
|
|
||||||
|
peek src = do
|
||||||
|
valid <- (#peek struct cwiid_ir_src, valid) src
|
||||||
|
posx <- (#peek struct cwiid_ir_src, pos[0]) src
|
||||||
|
posy <- (#peek struct cwiid_ir_src, pos[1]) src
|
||||||
|
size <- (#peek struct cwiid_ir_src, size) src
|
||||||
|
return $ WiimoteIRSource valid posx posy size
|
||||||
|
|
||||||
|
poke _ _ = error "Can't write WiimoteIRSource"
|
||||||
|
|
||||||
data WiimoteState = WiimoteState
|
data WiimoteState = WiimoteState
|
||||||
{ stateButtons :: Word16
|
{ stateLed :: Word8
|
||||||
|
, stateRumble :: Word8
|
||||||
|
, stateBattery :: Word8
|
||||||
|
, stateButtons :: Word16
|
||||||
|
, stateIRSources :: [WiimoteIRSource]
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
instance Storable WiimoteState where
|
instance Storable WiimoteState where
|
||||||
|
@ -165,12 +195,14 @@ instance Storable WiimoteState where
|
||||||
alignment _ = alignment (undefined :: CInt)
|
alignment _ = alignment (undefined :: CInt)
|
||||||
|
|
||||||
peek state = do
|
peek state = do
|
||||||
|
led <- (#peek struct cwiid_state, led) state
|
||||||
|
rumble <- (#peek struct cwiid_state, rumble) state
|
||||||
|
battery <- (#peek struct cwiid_state, battery) state
|
||||||
buttons <- (#peek struct cwiid_state, buttons) state
|
buttons <- (#peek struct cwiid_state, buttons) state
|
||||||
return $ WiimoteState (buttons)
|
irSources <- peekArray (#const CWIID_IR_SRC_COUNT) $ (#ptr struct cwiid_state, ir_src) state
|
||||||
|
return $ WiimoteState led rumble battery buttons (filter (\src -> (irValid src) /= 0) irSources)
|
||||||
|
|
||||||
poke state (WiimoteState (buttons)) = do
|
poke _ _ = fail "Can't write WiimoteState"
|
||||||
(#poke struct cwiid_state, buttons) state buttons
|
|
||||||
|
|
||||||
|
|
||||||
data WiimoteMesg = WiimoteStatusMesg
|
data WiimoteMesg = WiimoteStatusMesg
|
||||||
{ mesgType :: (#type enum cwiid_mesg_type)
|
{ mesgType :: (#type enum cwiid_mesg_type)
|
||||||
|
@ -182,6 +214,8 @@ data WiimoteMesg = WiimoteStatusMesg
|
||||||
, mesgButtons :: Word16
|
, mesgButtons :: Word16
|
||||||
}
|
}
|
||||||
| WiimoteMesgUnknown
|
| WiimoteMesgUnknown
|
||||||
|
{ mesgType :: (#type enum cwiid_mesg_type)
|
||||||
|
}
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
instance Storable WiimoteMesg where
|
instance Storable WiimoteMesg where
|
||||||
|
@ -199,10 +233,10 @@ instance Storable WiimoteMesg where
|
||||||
| mesgtype == hwiidMesgTypeButton -> do
|
| mesgtype == hwiidMesgTypeButton -> do
|
||||||
buttons <- (#peek struct cwiid_btn_mesg, buttons) mesg
|
buttons <- (#peek struct cwiid_btn_mesg, buttons) mesg
|
||||||
return $ WiimoteButtonMesg mesgtype buttons
|
return $ WiimoteButtonMesg mesgtype buttons
|
||||||
| otherwise -> return WiimoteMesgUnknown
|
| otherwise -> return $ WiimoteMesgUnknown mesgtype
|
||||||
|
|
||||||
|
|
||||||
poke _ _ = error "Can't write WiimoteMesg"
|
poke _ _ = fail "Can't write WiimoteMesg"
|
||||||
|
|
||||||
|
|
||||||
newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable)
|
newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable)
|
||||||
|
|
Reference in a new issue