summaryrefslogtreecommitdiffstats
path: root/src/HWiid.hsc
blob: 3699a05208bd3587d62c5d429dd7b6fccfb3d2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{-# LANGUAGE ForeignFunctionInterface, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}

module HWiid ( BDAddr(..)
             , Wiimote
             , WiimoteState(..)
             , nullWiimote
             , bdAddrAny
             , hwiidLed1
             , hwiidLed2
             , hwiidLed3
             , hwiidLed4
             , hwiidReportStatus
             , hwiidReportButtons
             , hwiidOpen
             , hwiidOpenTimeout
             , hwiidClose
             , hwiidGetState
             , hwiidSetReportMode
             , hwiidSetLed
             , hwiidButton2
             , hwiidButton1
             , hwiidButtonB
             , hwiidButtonA
             , hwiidButtonMinus
             , hwiidButtonHome
             , hwiidButtonLeft
             , hwiidButtonRight
             , hwiidButtonDown
             , hwiidButtonUp
             , hwiidButtonPlus
             , hwiidNunchukButtonZ
             , hwiidNunchukButtonC
             ) where


import Data.Word
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable


#include <cwiid.h>


data BDAddr = BDAddr (Word8, Word8, Word8, Word8, Word8, Word8)
              deriving (Eq, Ord, Show)

instance Storable BDAddr where
    sizeOf _ = (#size bdaddr_t)
    alignment _ = alignment (undefined :: CInt)
    
    peek addr = do
      b0 <- peekByteOff addr 0
      b1 <- peekByteOff addr 1
      b2 <- peekByteOff addr 2
      b3 <- peekByteOff addr 3
      b4 <- peekByteOff addr 4
      b5 <- peekByteOff addr 5
      return $ BDAddr (b0, b1, b2, b3, b4, b5)
    
    poke addr (BDAddr (b0, b1, b2, b3, b4, b5)) = do
                           pokeByteOff addr 0 b0
                           pokeByteOff addr 1 b1
                           pokeByteOff addr 2 b2
                           pokeByteOff addr 3 b3
                           pokeByteOff addr 4 b4
                           pokeByteOff addr 5 b5
                                                                                                   
bdAddrAny :: BDAddr
bdAddrAny = BDAddr (0, 0, 0, 0, 0, 0)


data WiimoteState = WiimoteState
                  { stateButtons :: Word16
                  } deriving (Eq, Show)

instance Storable WiimoteState where
    sizeOf _ = (#size struct cwiid_state)
    alignment _ = alignment (undefined :: CInt)
    
    peek state = do
      buttons <- (#peek struct cwiid_state, buttons) state
      return $ WiimoteState (buttons)
    
    poke state (WiimoteState (buttons)) = do
                                 (#poke struct cwiid_state, buttons) state buttons


newtype Wiimote = Wiimote (Ptr Wiimote) deriving (Eq, Ord, Show, Storable)

hwiidLed1 :: Word8
hwiidLed1 = (#const CWIID_LED1_ON)

hwiidLed2 :: Word8
hwiidLed2 = (#const CWIID_LED2_ON)

hwiidLed3 :: Word8
hwiidLed3 = (#const CWIID_LED3_ON)

hwiidLed4 :: Word8
hwiidLed4 = (#const CWIID_LED4_ON)

hwiidReportStatus :: Word8
hwiidReportStatus = (#const CWIID_RPT_STATUS)

hwiidReportButtons :: Word8
hwiidReportButtons = (#const CWIID_RPT_BTN)


hwiidButton2 :: Word16
hwiidButton2 = (#const CWIID_BTN_2)

hwiidButton1 :: Word16
hwiidButton1 = (#const CWIID_BTN_1)

hwiidButtonB :: Word16
hwiidButtonB = (#const CWIID_BTN_B)

hwiidButtonA :: Word16
hwiidButtonA = (#const CWIID_BTN_A)

hwiidButtonMinus :: Word16
hwiidButtonMinus = (#const CWIID_BTN_MINUS)

hwiidButtonHome :: Word16
hwiidButtonHome = (#const CWIID_BTN_HOME)

hwiidButtonLeft :: Word16
hwiidButtonLeft = (#const CWIID_BTN_LEFT)

hwiidButtonRight :: Word16
hwiidButtonRight = (#const CWIID_BTN_RIGHT)

hwiidButtonDown :: Word16
hwiidButtonDown = (#const CWIID_BTN_DOWN)

hwiidButtonUp :: Word16
hwiidButtonUp = (#const CWIID_BTN_UP)

hwiidButtonPlus :: Word16
hwiidButtonPlus = (#const CWIID_BTN_PLUS)


hwiidNunchukButtonZ :: Word16
hwiidNunchukButtonZ = (#const CWIID_NUNCHUK_BTN_Z)

hwiidNunchukButtonC :: Word16
hwiidNunchukButtonC = (#const CWIID_NUNCHUK_BTN_C)


nullWiimote :: Wiimote
nullWiimote = Wiimote nullPtr

foreign import ccall unsafe "cwiid.h cwiid_open_timeout"
        cwiid_open_timeout :: Ptr BDAddr -> CInt -> CInt -> IO Wiimote

hwiidOpenTimeout :: BDAddr -> CInt -> CInt -> IO Wiimote
hwiidOpenTimeout addr flags timeout = with addr $ \addrptr -> cwiid_open_timeout addrptr flags timeout

defaultTimeout :: CInt
defaultTimeout = 5

hwiidOpen :: BDAddr -> CInt -> IO Wiimote
hwiidOpen addr flags = hwiidOpenTimeout addr flags defaultTimeout

foreign import ccall unsafe "cwiid.h cwiid_close"
        hwiidClose :: Wiimote -> IO CInt

foreign import ccall unsafe "cwiid.h cwiid_get_state"
        cwiid_get_state :: Wiimote -> Ptr WiimoteState -> IO CInt

hwiidGetState :: Wiimote -> IO WiimoteState
hwiidGetState wiimote = alloca $ \state -> do
                          cwiid_get_state wiimote state
                          peek state

foreign import ccall unsafe "cwiid.h cwiid_set_rpt_mode"
        hwiidSetReportMode :: Wiimote -> Word8 -> IO CInt

foreign import ccall unsafe "cwiid.h cwiid_set_led"
        hwiidSetLed :: Wiimote -> Word8 -> IO CInt