From 1a31573eb8b6a6c02536279d77f6232981155958 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 10 Apr 2010 15:17:23 +0200 Subject: Added support for calibration --- src/HWiid.hsc | 62 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/HWiid.hsc b/src/HWiid.hsc index 91b4ec8..46340b3 100644 --- a/src/HWiid.hsc +++ b/src/HWiid.hsc @@ -4,8 +4,11 @@ module HWiid ( BDAddr(..) , Wiimote , WiimoteIRSource(..) , WiimoteState(..) + , WiimoteExtType , WiimoteExtState(..) + , WiimoteMesgType , WiimoteMesg(..) + , WiimoteAccCal(..) , nullWiimote , bdAddrAny , hwiidFlagMesgInterface @@ -46,6 +49,7 @@ module HWiid ( BDAddr(..) , hwiidSetReportMode , hwiidSetLed , hwiidGetMesg + , hwiidGetAccCal ) where @@ -150,17 +154,21 @@ hwiidIRMaxY :: CInt hwiidIRMaxY = (#const CWIID_IR_Y_MAX) -hwiidMesgTypeStatus :: (#type enum cwiid_mesg_type) +type WiimoteMesgType = (#type enum cwiid_mesg_type) + +hwiidMesgTypeStatus :: WiimoteMesgType hwiidMesgTypeStatus = (#const CWIID_MESG_STATUS) -hwiidMesgTypeButton :: (#type enum cwiid_mesg_type) +hwiidMesgTypeButton :: WiimoteMesgType hwiidMesgTypeButton = (#const CWIID_MESG_BTN) -hwiidExtNone :: (#type enum cwiid_ext_type) +type WiimoteExtType = (#type enum cwiid_ext_type) + +hwiidExtNone :: WiimoteExtType hwiidExtNone = (#const CWIID_EXT_NONE) -hwiidExtNunchuk :: (#type enum cwiid_ext_type) +hwiidExtNunchuk :: WiimoteExtType hwiidExtNunchuk = (#const CWIID_EXT_NUNCHUK) @@ -244,15 +252,15 @@ instance Storable WiimoteState where poke _ _ = fail "Can't write WiimoteState" data WiimoteMesg = WiimoteStatusMesg - { mesgType :: (#type enum cwiid_mesg_type) + { mesgType :: WiimoteMesgType , mesgBattery :: Word8 } | WiimoteButtonMesg - { mesgType :: (#type enum cwiid_mesg_type) + { mesgType :: WiimoteMesgType , mesgButtons :: Word16 } | WiimoteMesgOther - { mesgType :: (#type enum cwiid_mesg_type) + { mesgType :: WiimoteMesgType } deriving (Eq, Show) @@ -277,17 +285,17 @@ instance Storable WiimoteMesg where data WiimoteExtState = WiimoteNunchukState - { extType :: (#type enum cwiid_ext_type) + { extType :: WiimoteExtType , extNunchukStickX :: Word8 , extNunchukStickY :: Word8 , extNunchukButtons :: Word8 } | WiimoteOtherState - { extType :: (#type enum cwiid_ext_type) + { extType :: WiimoteExtType } deriving (Eq, Show) -peekExtState :: (#type enum cwiid_ext_type) -> Ptr WiimoteExtState -> IO WiimoteExtState +peekExtState :: WiimoteExtType -> Ptr WiimoteExtState -> IO WiimoteExtState peekExtState exttype state | exttype == hwiidExtNunchuk = do posx <- (#peek struct nunchuk_state, stick[0]) state @@ -295,6 +303,33 @@ peekExtState exttype state buttons <- (#peek struct nunchuk_state, buttons) state return $ WiimoteNunchukState exttype posx posy buttons | otherwise = return $ WiimoteOtherState exttype + + +data WiimoteAccCal = WiimoteAccCal + { accCalZeroX :: Word8 + , accCalZeroY :: Word8 + , accCalZeroZ :: Word8 + , accCalOneX :: Word8 + , accCalOneY :: Word8 + , accCalOneZ :: Word8 + } + deriving (Eq, Show) + +instance Storable WiimoteAccCal where + sizeOf _ = (#size struct acc_cal) + alignment _ = alignment (undefined :: CInt) + + peek cal = do + zerox <- (#peek struct acc_cal, zero[0]) cal + zeroy <- (#peek struct acc_cal, zero[1]) cal + zeroz <- (#peek struct acc_cal, zero[2]) cal + onex <- (#peek struct acc_cal, one[0]) cal + oney <- (#peek struct acc_cal, one[1]) cal + onez <- (#peek struct acc_cal, one[2]) cal + + return $ WiimoteAccCal zerox zeroy zeroz onex oney onez + + poke _ _ = fail "Can't write WiimoteAccCal" newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable) @@ -346,3 +381,10 @@ hwiidGetMesg wiimote = alloca $ \countptr -> alloca $ \arrayptr -> alloca $ \tim free array return list else return [] +foreign import ccall unsafe "cwiid.h cwiid_get_acc_cal" + cwiid_get_acc_cal :: Wiimote -> WiimoteExtType -> Ptr WiimoteAccCal -> IO CInt + +hwiidGetAccCal wiimote exttype = alloca $ \calptr -> do + cwiid_get_acc_cal wiimote exttype calptr + cal <- peek calptr + return cal -- cgit v1.2.3