From 6e96cdf9ea2f87b54bcf77bef2c2d10b7453361d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 8 Apr 2010 15:59:19 +0200 Subject: Added nunchuk support --- src/HWiid.hsc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/HWiid.hsc b/src/HWiid.hsc index 9a4bb2f..07a3a48 100644 --- a/src/HWiid.hsc +++ b/src/HWiid.hsc @@ -4,6 +4,7 @@ module HWiid ( BDAddr(..) , Wiimote , WiimoteIRSource(..) , WiimoteState(..) + , WiimoteExtState(..) , WiimoteMesg(..) , nullWiimote , bdAddrAny @@ -12,6 +13,8 @@ module HWiid ( BDAddr(..) , hwiidReportStatus , hwiidReportButtons , hwiidReportIR + , hwiidReportNunchuk + , hwiidReportExt , hwiidLed1 , hwiidLed2 , hwiidLed3 @@ -31,6 +34,8 @@ module HWiid ( BDAddr(..) , hwiidNunchukButtonC , hwiidMesgTypeStatus , hwiidMesgTypeButton + , hwiidExtNone + , hwiidExtNunchuk , hwiidOpen , hwiidOpenTimeout , hwiidClose @@ -71,6 +76,12 @@ hwiidReportButtons = (#const CWIID_RPT_BTN) hwiidReportIR :: Word8 hwiidReportIR = (#const CWIID_RPT_IR) +hwiidReportNunchuk :: Word8 +hwiidReportNunchuk = (#const CWIID_RPT_NUNCHUK) + +hwiidReportExt :: Word8 +hwiidReportExt = hwiidReportNunchuk + hwiidLed1 :: Word8 hwiidLed1 = (#const CWIID_LED1_ON) @@ -133,6 +144,12 @@ hwiidMesgTypeButton :: (#type enum cwiid_mesg_type) 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) deriving (Eq, Ord, Show) @@ -188,6 +205,7 @@ data WiimoteState = WiimoteState , stateBattery :: Word8 , stateButtons :: Word16 , stateIRSources :: [WiimoteIRSource] + , stateExt :: WiimoteExtState } deriving (Eq, Show) instance Storable WiimoteState where @@ -195,25 +213,26 @@ instance Storable WiimoteState where alignment _ = alignment (undefined :: CInt) 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 + 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 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" data WiimoteMesg = WiimoteStatusMesg { mesgType :: (#type enum cwiid_mesg_type) , mesgBattery :: Word8 - , mesgExtensionType :: (#type enum cwiid_ext_type) } | WiimoteButtonMesg { mesgType :: (#type enum cwiid_mesg_type) , mesgButtons :: Word16 } - | WiimoteMesgUnknown + | WiimoteMesgOther { mesgType :: (#type enum cwiid_mesg_type) } deriving (Eq, Show) @@ -228,17 +247,37 @@ instance Storable WiimoteMesg where case () of _ | mesgtype == hwiidMesgTypeStatus -> do battery <- (#peek struct cwiid_status_mesg, battery) mesg - exttype <- (#peek struct cwiid_status_mesg, ext_type) mesg - return $ WiimoteStatusMesg mesgtype battery exttype + return $ WiimoteStatusMesg mesgtype battery | mesgtype == hwiidMesgTypeButton -> do buttons <- (#peek struct cwiid_btn_mesg, buttons) mesg return $ WiimoteButtonMesg mesgtype buttons - | otherwise -> return $ WiimoteMesgUnknown mesgtype + | otherwise -> return $ WiimoteMesgOther mesgtype 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) -- cgit v1.2.3