summaryrefslogtreecommitdiffstats
path: root/src/HWiid.hsc
blob: 1d9d4c65c0d5ff632fd8f0aa4dd9eea781324e61 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
{-# LANGUAGE ForeignFunctionInterface, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}

module HWiid ( BDAddr(..)
             , Wiimote
             , WiimoteIRSource(..)
             , WiimoteState(..)
             , WiimoteExtType
             , WiimoteExtState(..)
             , WiimoteMesgType
             , WiimoteMesg(..)
             , WiimoteAccCal(..)
             , nullWiimote
             , bdAddrAny
             , flagMesgInterface
             , flagNonblock
             , reportStatus
             , reportButtons
             , reportAcc
             , reportIR
             , reportNunchuk
             , reportExt
             , led1
             , led2
             , led3
             , led4
             , button2
             , button1
             , buttonB
             , buttonA
             , buttonMinus
             , buttonHome
             , buttonLeft
             , buttonRight
             , buttonDown
             , buttonUp
             , buttonPlus
             , nunchukButtonZ
             , nunchukButtonC
             , irMaxX
             , irMaxY
             , mesgTypeStatus
             , mesgTypeButton
             , extNone
             , extNunchuk
             , open
             , openTimeout
             , close
             , getState
             , setReportMode
             , setLed
             , getMesg
             , getAccCal
             ) where


import Data.Bits
import Data.Int
import Data.Typeable
import Data.Word
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca, free)
import Foreign.Marshal.Array (peekArray)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable
import System.Posix.Clock (TimeSpec)

#include <cwiid.h>

flagMesgInterface :: CInt
flagMesgInterface = (#const CWIID_FLAG_MESG_IFC)

flagNonblock :: CInt
flagNonblock = (#const CWIID_FLAG_NONBLOCK)


reportStatus :: Word8
reportStatus = (#const CWIID_RPT_STATUS)

reportButtons :: Word8
reportButtons = (#const CWIID_RPT_BTN)

reportAcc :: Word8
reportAcc = (#const CWIID_RPT_ACC)

reportIR :: Word8
reportIR = (#const CWIID_RPT_IR)

reportNunchuk :: Word8
reportNunchuk = (#const CWIID_RPT_NUNCHUK)

reportExt :: Word8
reportExt = reportNunchuk


led1 :: Word8
led1 = (#const CWIID_LED1_ON)

led2 :: Word8
led2 = (#const CWIID_LED2_ON)

led3 :: Word8
led3 = (#const CWIID_LED3_ON)

led4 :: Word8
led4 = (#const CWIID_LED4_ON)


button2 :: Word16
button2 = (#const CWIID_BTN_2)

button1 :: Word16
button1 = (#const CWIID_BTN_1)

buttonB :: Word16
buttonB = (#const CWIID_BTN_B)

buttonA :: Word16
buttonA = (#const CWIID_BTN_A)

buttonMinus :: Word16
buttonMinus = (#const CWIID_BTN_MINUS)

buttonHome :: Word16
buttonHome = (#const CWIID_BTN_HOME)

buttonLeft :: Word16
buttonLeft = (#const CWIID_BTN_LEFT)

buttonRight :: Word16
buttonRight = (#const CWIID_BTN_RIGHT)

buttonDown :: Word16
buttonDown = (#const CWIID_BTN_DOWN)

buttonUp :: Word16
buttonUp = (#const CWIID_BTN_UP)

buttonPlus :: Word16
buttonPlus = (#const CWIID_BTN_PLUS)


nunchukButtonZ :: Word16
nunchukButtonZ = (#const CWIID_NUNCHUK_BTN_Z)

nunchukButtonC :: Word16
nunchukButtonC = (#const CWIID_NUNCHUK_BTN_C)


irMaxX :: CInt
irMaxX = (#const CWIID_IR_X_MAX)

irMaxY :: CInt
irMaxY = (#const CWIID_IR_Y_MAX)


type WiimoteMesgType = (#type enum cwiid_mesg_type)

mesgTypeStatus :: WiimoteMesgType
mesgTypeStatus = (#const CWIID_MESG_STATUS)

mesgTypeButton :: WiimoteMesgType
mesgTypeButton = (#const CWIID_MESG_BTN)


type WiimoteExtType = (#type enum cwiid_ext_type)

extNone :: WiimoteExtType
extNone = (#const CWIID_EXT_NONE)

extNunchuk :: WiimoteExtType
extNunchuk = (#const CWIID_EXT_NUNCHUK)


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 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
    { stateLed       :: Word8
    , stateRumble    :: Word8
    , stateBattery   :: Word8
    , stateButtons   :: Word16
    , stateAccX      :: Word8
    , stateAccY      :: Word8
    , stateAccZ      :: Word8
    , stateIRSources :: [WiimoteIRSource]
    , stateExt       :: WiimoteExtState
    } deriving (Eq, Show)

instance Storable WiimoteState where
    sizeOf _ = (#size struct cwiid_state)
    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
      accX      <- (#peek struct cwiid_state, acc[0]) state
      accY      <- (#peek struct cwiid_state, acc[1]) state
      accZ      <- (#peek struct cwiid_state, acc[2]) state
      irSources <- peekArray (#const CWIID_IR_SRC_COUNT) $ (#ptr struct cwiid_state, ir_src) state
      exttype   <- (#peek struct cwiid_state, ext_type) state
      extstate  <- peekExtState exttype $ (#ptr struct cwiid_state, ext) state
      return $ WiimoteState led rumble battery buttons accX accY accZ (filter (\src -> (irValid src) /= 0) irSources) extstate
    
    poke _ _ = fail "Can't write WiimoteState"

data WiimoteMesg = WiimoteStatusMesg
    { mesgType          :: WiimoteMesgType
    , mesgBattery       :: Word8
    }
                 | WiimoteButtonMesg
    { mesgType          :: WiimoteMesgType
    , mesgButtons       :: Word16
    }
                 | WiimoteMesgOther
    { mesgType          :: WiimoteMesgType
    }
                   deriving (Eq, Show)

instance Storable WiimoteMesg where
    sizeOf _ = (#size union cwiid_mesg)
    alignment _ = alignment (undefined :: CInt)
    
    peek mesg = do
      mesgtype <- (#peek union cwiid_mesg, type) mesg
      
      case () of
         _ | mesgtype == mesgTypeStatus -> do
                                battery <- (#peek struct cwiid_status_mesg, battery) mesg
                                return $ WiimoteStatusMesg mesgtype battery
           | mesgtype == mesgTypeButton -> do
                                buttons <- (#peek struct cwiid_btn_mesg, buttons) mesg
                                return $ WiimoteButtonMesg mesgtype buttons
           | otherwise -> return $ WiimoteMesgOther mesgtype
                                    
    
    poke _ _ = fail "Can't write WiimoteMesg"


data WiimoteExtState = WiimoteNunchukState
    { extType           :: WiimoteExtType
    , extNunchukStickX  :: Word8
    , extNunchukStickY  :: Word8
    , extNunchukButtons :: Word8
    }
                     | WiimoteOtherState
    { extType           :: WiimoteExtType
    }
                       deriving (Eq, Show)

peekExtState :: WiimoteExtType -> Ptr WiimoteExtState -> IO WiimoteExtState
peekExtState exttype state
    | exttype == extNunchuk = 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


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)


nullWiimote :: Wiimote
nullWiimote = Wiimote nullPtr

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

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

defaultTimeout :: CInt
defaultTimeout = 5

open :: BDAddr -> CInt -> IO Wiimote
open addr flags = openTimeout addr flags defaultTimeout

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

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

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

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

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

foreign import ccall unsafe "cwiid.h cwiid_get_mesg"
        cwiid_get_mesg :: Wiimote -> Ptr CInt -> Ptr (Ptr WiimoteMesg) -> Ptr TimeSpec -> IO CInt

getMesg :: Wiimote -> IO [WiimoteMesg]
getMesg wiimote = alloca $ \countptr -> alloca $ \arrayptr -> alloca $ \timestamp -> do
                    ret <- cwiid_get_mesg wiimote countptr arrayptr timestamp
                    count <- peek countptr
                    array <- peek arrayptr
                    
                    if (ret == 0) then do
                                    list <- peekArray (fromIntegral count) array
                                    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

getAccCal wiimote exttype = alloca $ \calptr -> do
                              cwiid_get_acc_cal wiimote exttype calptr
                              cal <- peek calptr
                              return cal