Added nunchuk support

This commit is contained in:
Matthias Schiffer 2010-04-08 15:59:19 +02:00
parent 9b4662b8e4
commit 6e96cdf9ea

View file

@ -4,6 +4,7 @@ module HWiid ( BDAddr(..)
, Wiimote , Wiimote
, WiimoteIRSource(..) , WiimoteIRSource(..)
, WiimoteState(..) , WiimoteState(..)
, WiimoteExtState(..)
, WiimoteMesg(..) , WiimoteMesg(..)
, nullWiimote , nullWiimote
, bdAddrAny , bdAddrAny
@ -12,6 +13,8 @@ module HWiid ( BDAddr(..)
, hwiidReportStatus , hwiidReportStatus
, hwiidReportButtons , hwiidReportButtons
, hwiidReportIR , hwiidReportIR
, hwiidReportNunchuk
, hwiidReportExt
, hwiidLed1 , hwiidLed1
, hwiidLed2 , hwiidLed2
, hwiidLed3 , hwiidLed3
@ -31,6 +34,8 @@ module HWiid ( BDAddr(..)
, hwiidNunchukButtonC , hwiidNunchukButtonC
, hwiidMesgTypeStatus , hwiidMesgTypeStatus
, hwiidMesgTypeButton , hwiidMesgTypeButton
, hwiidExtNone
, hwiidExtNunchuk
, hwiidOpen , hwiidOpen
, hwiidOpenTimeout , hwiidOpenTimeout
, hwiidClose , hwiidClose
@ -71,6 +76,12 @@ hwiidReportButtons = (#const CWIID_RPT_BTN)
hwiidReportIR :: Word8 hwiidReportIR :: Word8
hwiidReportIR = (#const CWIID_RPT_IR) hwiidReportIR = (#const CWIID_RPT_IR)
hwiidReportNunchuk :: Word8
hwiidReportNunchuk = (#const CWIID_RPT_NUNCHUK)
hwiidReportExt :: Word8
hwiidReportExt = hwiidReportNunchuk
hwiidLed1 :: Word8 hwiidLed1 :: Word8
hwiidLed1 = (#const CWIID_LED1_ON) hwiidLed1 = (#const CWIID_LED1_ON)
@ -133,6 +144,12 @@ hwiidMesgTypeButton :: (#type enum cwiid_mesg_type)
hwiidMesgTypeButton = (#const CWIID_MESG_BTN) hwiidMesgTypeButton = (#const CWIID_MESG_BTN)
hwiidExtNone :: (#type enum cwiid_ext_type)
hwiidExtNone = (#const CWIID_EXT_NONE)
hwiidExtNunchuk :: (#type enum cwiid_ext_type)
hwiidExtNunchuk = (#const CWIID_EXT_NUNCHUK)
data BDAddr = BDAddr (Word8, Word8, Word8, Word8, Word8, Word8) data BDAddr = BDAddr (Word8, Word8, Word8, Word8, Word8, Word8)
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
@ -188,6 +205,7 @@ data WiimoteState = WiimoteState
, stateBattery :: Word8 , stateBattery :: Word8
, stateButtons :: Word16 , stateButtons :: Word16
, stateIRSources :: [WiimoteIRSource] , stateIRSources :: [WiimoteIRSource]
, stateExt :: WiimoteExtState
} deriving (Eq, Show) } deriving (Eq, Show)
instance Storable WiimoteState where instance Storable WiimoteState where
@ -200,20 +218,21 @@ instance Storable WiimoteState where
battery <- (#peek struct cwiid_state, battery) state battery <- (#peek struct cwiid_state, battery) state
buttons <- (#peek struct cwiid_state, buttons) state buttons <- (#peek struct cwiid_state, buttons) state
irSources <- peekArray (#const CWIID_IR_SRC_COUNT) $ (#ptr struct cwiid_state, ir_src) state 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) exttype <- (#peek struct cwiid_state, ext_type) state
extstate <- peekExtState exttype $ (#ptr struct cwiid_state, ext) state
return $ WiimoteState led rumble battery buttons (filter (\src -> (irValid src) /= 0) irSources) extstate
poke _ _ = fail "Can't write WiimoteState" poke _ _ = fail "Can't write WiimoteState"
data WiimoteMesg = WiimoteStatusMesg data WiimoteMesg = WiimoteStatusMesg
{ mesgType :: (#type enum cwiid_mesg_type) { mesgType :: (#type enum cwiid_mesg_type)
, mesgBattery :: Word8 , mesgBattery :: Word8
, mesgExtensionType :: (#type enum cwiid_ext_type)
} }
| WiimoteButtonMesg | WiimoteButtonMesg
{ mesgType :: (#type enum cwiid_mesg_type) { mesgType :: (#type enum cwiid_mesg_type)
, mesgButtons :: Word16 , mesgButtons :: Word16
} }
| WiimoteMesgUnknown | WiimoteMesgOther
{ mesgType :: (#type enum cwiid_mesg_type) { mesgType :: (#type enum cwiid_mesg_type)
} }
deriving (Eq, Show) deriving (Eq, Show)
@ -228,17 +247,37 @@ instance Storable WiimoteMesg where
case () of case () of
_ | mesgtype == hwiidMesgTypeStatus -> do _ | mesgtype == hwiidMesgTypeStatus -> do
battery <- (#peek struct cwiid_status_mesg, battery) mesg battery <- (#peek struct cwiid_status_mesg, battery) mesg
exttype <- (#peek struct cwiid_status_mesg, ext_type) mesg return $ WiimoteStatusMesg mesgtype battery
return $ WiimoteStatusMesg mesgtype battery exttype
| 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 mesgtype | otherwise -> return $ WiimoteMesgOther mesgtype
poke _ _ = fail "Can't write WiimoteMesg" poke _ _ = fail "Can't write WiimoteMesg"
data WiimoteExtState = WiimoteNunchukState
{ extType :: (#type enum cwiid_ext_type)
, extNunchukStickX :: Word8
, extNunchukStickY :: Word8
, extNunchukButtons :: Word8
}
| WiimoteOtherState
{ extType :: (#type enum cwiid_ext_type)
}
deriving (Eq, Show)
peekExtState :: (#type enum cwiid_ext_type) -> Ptr WiimoteExtState -> IO WiimoteExtState
peekExtState exttype state
| exttype == hwiidExtNunchuk = do
posx <- (#peek struct nunchuk_state, stick[0]) state
posy <- (#peek struct nunchuk_state, stick[1]) state
buttons <- (#peek struct nunchuk_state, buttons) state
return $ WiimoteNunchukState exttype posx posy buttons
| otherwise = return $ WiimoteOtherState exttype
newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable) newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable)