Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wiimote/src/external/wiicpp/wiicpp/wiicpp.cpp @ 9780

Last change on this file since 9780 was 9780, checked in by georgr, 10 years ago

WiiCpp library successfully (?) added - won't work without libbluetooth-dev

  • Property svn:executable set to *
File size: 23.1 KB
Line 
1/*
2 *    wiicpp.cpp
3 *
4 *              This file is part of WiiC, written by:
5 *                      Gabriele Randelli
6 *                      Email: randelli@dis.uniroma1.it
7 *
8 *              Copyright 2010
9 *             
10 *              This file is based on WiiuseCpp, written By:
11 *        James Thomas
12 *        Email: jt@missioncognition.org
13 *
14 *    Copyright 2009
15 *
16 *    This program is free software; you can redistribute it and/or modify
17 *    it under the terms of the GNU General Public License as published by
18 *    the Free Software Foundation; either version 3 of the License, or
19 *    (at your option) any later version.
20 *
21 *    This program is distributed in the hope that it will be useful,
22 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
23 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 *    GNU General Public License for more details.
25 *
26 *    You should have received a copy of the GNU General Public License
27 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31/*
32 * Note:  This C++ library is a (very) thin wrapper of the the WiiC library.
33 */
34
35#include <iostream>
36#include "wiicpp.h"
37#include <unistd.h>
38
39using namespace std;
40using namespace WiiC;
41
42/*
43 * CButtonBase class methods.
44 */
45CButtonBase::CButtonBase(void *ButtonsPtr, void *ButtonsHeldPtr, void *ButtonsReleasedPtr)
46{
47    mpBtnsPtr = ButtonsPtr;
48    mpBtnsHeldPtr = ButtonsHeldPtr;
49    mpBtnsReleasedPtr = ButtonsReleasedPtr;
50}
51
52int CButtonBase::isPressed(int Button)
53{
54    return (Cast(mpBtnsPtr) & Button) == Button;
55}
56
57int CButtonBase::isHeld(int Button)
58{
59    return (Cast(mpBtnsHeldPtr) & Button) == Button;
60}
61
62int CButtonBase::isReleased(int Button)
63{
64    return (Cast(mpBtnsReleasedPtr) & Button) == Button;
65}
66
67int CButtonBase::isJustPressed(int Button)
68{
69    return ((Cast(mpBtnsPtr) & Button) == Button) && ((Cast(mpBtnsHeldPtr) & Button) != Button);
70}
71
72/*
73 * Initializers for classes derrived from CButtonBase.
74 */
75CButtons::CButtons(void *ButtonsPtr, void *ButtonsHeldPtr, void *ButtonsReleasedPtr) :
76    CButtonBase(ButtonsPtr, ButtonsHeldPtr, ButtonsReleasedPtr)
77{
78}
79
80CNunchukButtons::CNunchukButtons(void *ButtonsPtr, void *ButtonsHeldPtr, void *ButtonsReleasedPtr) :
81    CButtonBase(ButtonsPtr, ButtonsHeldPtr, ButtonsReleasedPtr)
82{
83}
84
85CClassicButtons::CClassicButtons(void *ButtonsPtr, void *ButtonsHeldPtr, void *ButtonsReleasedPtr) :
86    CButtonBase(ButtonsPtr, ButtonsHeldPtr, ButtonsReleasedPtr)
87{
88}
89
90CGH3Buttons::CGH3Buttons(void *ButtonsPtr, void *ButtonsHeldPtr, void *ButtonsReleasedPtr) :
91    CButtonBase(ButtonsPtr, ButtonsHeldPtr, ButtonsReleasedPtr)
92{
93}
94
95/*
96 * CJoystick class methods.
97 */
98
99CJoystick::CJoystick(struct joystick_t *JSPtr)
100{
101    mpJoystickPtr = JSPtr;
102}
103
104void CJoystick::GetMaxCal(int &X, int &Y)
105{
106    X = mpJoystickPtr->max.x;
107    Y = mpJoystickPtr->max.y;
108}
109
110void CJoystick::SetMaxCal(int X, int Y)
111{
112    mpJoystickPtr->max.x = X;
113    mpJoystickPtr->max.y = Y;
114}
115
116void CJoystick::GetMinCal(int &X, int &Y)
117{
118    X = mpJoystickPtr->min.x;
119    Y = mpJoystickPtr->min.y;
120}
121
122void CJoystick::SetMinCal(int X, int Y)
123{
124    mpJoystickPtr->min.x = X;
125    mpJoystickPtr->min.y = Y;
126}
127
128void CJoystick::GetCenterCal(int &X, int &Y)
129{
130    X = mpJoystickPtr->center.x;
131    Y = mpJoystickPtr->center.y;
132}
133
134void CJoystick::SetCenterCal(int X, int Y)
135{
136    mpJoystickPtr->center.x = X;
137    mpJoystickPtr->center.y = Y;
138}
139
140void CJoystick::GetPosition(float &Angle, float &Magnitude)
141{
142    Angle = mpJoystickPtr->ang;
143    Magnitude = mpJoystickPtr->mag;
144}
145
146/*
147 * CAccelerometer class methods.
148 */
149
150CAccelerometer::CAccelerometer(struct accel_t *AccelCalPtr, struct vec3b_t *AccelerationPtr, int *AccelThresholdPtr,
151                               struct orient_t *OrientationPtr, float *OrientationThresholdPtr,
152                               struct gforce_t *GForcePtr)
153{
154    mpAccelCalibPtr = AccelCalPtr;
155    mpAccelPtr = AccelerationPtr;
156    mpOrientPtr = OrientationPtr;
157    mpGForcePtr = GForcePtr;
158    mpAccelThresholdPtr = AccelThresholdPtr;
159    mpOrientThresholdPtr = OrientationThresholdPtr;
160}
161
162float CAccelerometer::SetSmoothAlpha(float Alpha)
163{
164    float old_value;
165
166    old_value = mpAccelCalibPtr->st_alpha;
167
168    mpAccelCalibPtr->st_alpha = Alpha;
169
170    return old_value;
171}
172
173float CAccelerometer::GetOrientThreshold()
174{
175    return *mpOrientThresholdPtr;
176}
177
178void CAccelerometer::SetOrientThreshold(float Threshold)
179{
180    *mpOrientThresholdPtr = Threshold;
181}
182
183int CAccelerometer::GetAccelThreshold()
184{
185    return *mpAccelThresholdPtr;
186}
187
188void CAccelerometer::SetAccelThreshold(int Threshold)
189{
190    *mpAccelThresholdPtr = Threshold;
191}
192
193/**
194 *
195 * @brief Retrieves the smoothed device attitude (pitch, roll, and yaw) computed with an
196 * exponential moving average.
197 *
198 * @param Pitch         [out] Reference variable where the smooth device pitch will be set.
199 * @param Roll          [out] Reference variable where the smooth device roll will be set.
200 * @param Yaw           [out] Reference variable where the smooth device yaw will be set. Please,
201 *                                                note that without IR enabled, yaw cannot be retrieved.
202 */
203void CAccelerometer::GetOrientation(float &Pitch, float &Roll, float &Yaw)
204{
205    Pitch = mpOrientPtr->angle.pitch;
206    Roll = mpOrientPtr->angle.roll;
207    Yaw = mpOrientPtr->angle.yaw;
208}
209
210void CAccelerometer::GetGravityCalVector(float &X, float &Y, float &Z)
211{
212    X = mpAccelCalibPtr->cal_g.x;
213    Y = mpAccelCalibPtr->cal_g.y;
214    Z = mpAccelCalibPtr->cal_g.z;
215}
216
217void CAccelerometer::SetGravityCalVector(float X, float Y, float Z)
218{
219    mpAccelCalibPtr->cal_g.x = X;
220    mpAccelCalibPtr->cal_g.y = Y;
221    mpAccelCalibPtr->cal_g.z = Z;
222}
223
224void CAccelerometer::GetGravityVector(float &X, float &Y, float &Z)
225{
226    X = mpGForcePtr->vec.x;
227    Y = mpGForcePtr->vec.y;
228    Z = mpGForcePtr->vec.z;
229}
230
231void CAccelerometer::GetRawGravityVector(float &X, float &Y, float &Z)
232{
233    X = mpGForcePtr->a_vec.x;
234    Y = mpGForcePtr->a_vec.y;
235    Z = mpGForcePtr->a_vec.z;
236}
237
238/*
239 * CGyroscope class methods.
240 */
241CGyroscope::CGyroscope(struct ang3s_t* RawGyro, struct ang3s_t* CalGyro, struct ang3f_t* AngleRate, unsigned char* Mode, struct motion_plus_t* MPPtr, 
242        int* GyroThresholdPtr)
243{
244        mpRawGyro = RawGyro;
245        mpCalGyro = CalGyro;
246        mpAngleRate = AngleRate;
247        mpMode = Mode;
248        mpMPPtr = MPPtr;
249        mpGyroThresholdPtr = GyroThresholdPtr;
250}
251
252void CGyroscope::GetRawRates(int& Roll, int& Pitch, int& Yaw)
253{
254        Roll = mpRawGyro->roll;
255        Pitch = mpRawGyro->pitch;
256        Yaw = mpRawGyro->yaw;
257}
258
259void CGyroscope::GetRates(float& Roll, float& Pitch, float& Yaw)
260{
261        Roll = mpAngleRate->roll;
262        Pitch = mpAngleRate->pitch;
263        Yaw = mpAngleRate->yaw;
264}
265
266void CGyroscope::Calibrate()
267{
268        wiic_calibrate_motion_plus(mpMPPtr);   
269}
270
271int CGyroscope::GetGyroThreshold()
272{
273    return *mpGyroThresholdPtr;
274}
275
276void CGyroscope::SetGyroThreshold(int Threshold)
277{
278    *mpGyroThresholdPtr = Threshold;
279}
280
281
282/*
283 * CWeightSensor class methods.
284 */
285CWeightSensor::CWeightSensor(struct pressure_t* RawWeight, struct pressure_t* LowCalWeight, struct pressure_t* MediumCalWeight, struct pressure_t* HighCalWeight, struct pressure_weight_t* Weight, struct balance_board_t* BBPtr)
286{
287        mpRawWeight = RawWeight;
288        mpLowCalWeight = LowCalWeight;
289        mpMediumCalWeight = MediumCalWeight;
290        mpHighCalWeight = HighCalWeight;
291        mpWeight = Weight;
292        mpBBPtr = BBPtr;
293}
294
295void CWeightSensor::GetRawWeight(int& TopLeft, int& TopRight, int& BottomLeft, int& BottomRight)
296{
297        TopLeft = mpRawWeight->top_left;
298        TopRight = mpRawWeight->top_right;
299        BottomLeft = mpRawWeight->bottom_left;
300        BottomRight = mpRawWeight->bottom_right;
301}
302
303void CWeightSensor::GetLowCalWeight(int& TopLeft, int& TopRight, int& BottomLeft, int& BottomRight)
304{
305        TopLeft = mpLowCalWeight->top_left;
306        TopRight = mpLowCalWeight->top_right;
307        BottomLeft = mpLowCalWeight->bottom_left;
308        BottomRight = mpLowCalWeight->bottom_right;
309}
310
311void CWeightSensor::GetMediumCalWeight(int& TopLeft, int& TopRight, int& BottomLeft, int& BottomRight)
312{
313        TopLeft = mpMediumCalWeight->top_left;
314        TopRight = mpMediumCalWeight->top_right;
315        BottomLeft = mpMediumCalWeight->bottom_left;
316        BottomRight = mpMediumCalWeight->bottom_right;
317}
318
319void CWeightSensor::GetHighCalWeight(int& TopLeft, int& TopRight, int& BottomLeft, int& BottomRight)
320{
321        TopLeft = mpHighCalWeight->top_left;
322        TopRight = mpHighCalWeight->top_right;
323        BottomLeft = mpHighCalWeight->bottom_left;
324        BottomRight = mpHighCalWeight->bottom_right;
325}
326
327void CWeightSensor::GetWeight(float& TotalWeight, float& TopLeft, float& TopRight, float& BottomLeft, float& BottomRight)
328{
329        TotalWeight = mpWeight->weight;
330        TopLeft = mpWeight->top_left;
331        TopRight = mpWeight->top_right;
332        BottomLeft = mpWeight->bottom_left;
333        BottomRight = mpWeight->bottom_right;
334}
335
336
337/*
338 * CIRDot class methods.
339 */
340
341CIRDot::CIRDot()
342{
343    mpDotPtr = NULL;
344}
345
346CIRDot::CIRDot(struct ir_dot_t *DotPtr)
347{
348    mpDotPtr = DotPtr;
349}
350
351CIRDot::CIRDot(const CIRDot &copyin) // Copy constructor to handle pass by value.
352{
353    mpDotPtr = copyin.mpDotPtr;
354}
355
356int CIRDot::isVisible()
357{
358    return mpDotPtr->visible;
359}
360
361int CIRDot::GetSize()
362{
363    return mpDotPtr->size;
364}
365
366int CIRDot::GetOrder()
367{
368    return mpDotPtr->order;
369}
370
371void CIRDot::GetCoordinate(int &X, int &Y)
372{
373    X = mpDotPtr->x;
374    Y = mpDotPtr->y;
375}
376
377void CIRDot::GetRawCoordinate(int &X, int &Y)
378{
379    X = mpDotPtr->rx;
380    Y = mpDotPtr->ry;
381}
382
383/*
384 * CIR class methods.
385 */
386
387CIR::CIR(struct wiimote_t *wmPtr)
388{
389    mpWiimotePtr = wmPtr;
390}
391
392void CIR::SetMode(CIR::OnOffSelection State)
393{
394    wiic_set_ir(mpWiimotePtr, State);
395}
396
397void CIR::SetVres(unsigned int x, unsigned int y)
398{
399    wiic_set_ir_vres(mpWiimotePtr, x, y);
400}
401
402CIR::BarPositions CIR::GetBarPositionSetting()
403{
404    return (CIR::BarPositions) mpWiimotePtr->ir.pos;
405}
406
407void CIR::SetBarPosition(CIR::BarPositions PositionSelection)
408{
409    wiic_set_ir_position(mpWiimotePtr, (ir_position_t) PositionSelection);
410}
411
412CIR::AspectRatioSelections CIR::GetAspectRatioSetting()
413{
414    return (CIR::AspectRatioSelections) mpWiimotePtr->ir.aspect;
415}
416
417void CIR::SetAspectRatio(CIR::AspectRatioSelections AspectRatioSelection)
418{
419    wiic_set_aspect_ratio(mpWiimotePtr, (enum aspect_t) AspectRatioSelection);
420}
421
422void CIR::SetSensitivity(int Level)
423{
424    wiic_set_ir_sensitivity(mpWiimotePtr, Level);
425}
426
427int CIR::GetSensitivity()
428{
429    int level = 0;
430
431         WIIC_GET_IR_SENSITIVITY(mpWiimotePtr, &level);
432
433    return level;
434}
435
436int CIR::GetNumDots()
437{
438    return mpWiimotePtr->ir.num_dots;
439}
440
441std::vector<CIRDot>& CIR::GetDots()
442{
443    int index;
444
445    // Empty the array of irdots before reloading
446    mpIRDotsVector.clear();
447
448    for(index = 0 ; index < 4 ; index++)
449    {
450        CIRDot dot((struct ir_dot_t *) (&(mpWiimotePtr->ir.dot[index])));
451                if(dot.isVisible())
452                mpIRDotsVector.push_back(dot);
453    }
454
455    return mpIRDotsVector;
456}
457
458void CIR::GetOffset(int &X, int &Y)
459{
460    X = mpWiimotePtr->ir.offset[0];
461    Y = mpWiimotePtr->ir.offset[1];
462}
463
464int CIR::GetState()
465{
466    return mpWiimotePtr->ir.state;
467}
468
469void CIR::GetCursorPositionAbsolute(int &X, int &Y)
470{
471    X = mpWiimotePtr->ir.ax;
472    Y = mpWiimotePtr->ir.ay;
473}
474
475void CIR::GetCursorPosition(int &X, int &Y)
476{
477    X = mpWiimotePtr->ir.x;
478    Y = mpWiimotePtr->ir.y;
479}
480
481float CIR::GetPixelDistance()
482{
483    return mpWiimotePtr->ir.distance;
484}
485
486float CIR::GetDistance()
487{
488    return mpWiimotePtr->ir.z;
489}
490
491/*
492 * CExpansionDevice class methods.  This is a container class so there is not much.
493 */
494
495CExpansionDevice::CExpansionDevice(struct expansion_t * ExpPtr) :
496    Nunchuk(ExpPtr),Classic(ExpPtr),GuitarHero3(ExpPtr),MotionPlus(ExpPtr),BalanceBoard(ExpPtr)
497{
498    mpExpansionPtr = ExpPtr;
499}
500
501CExpansionDevice::ExpTypes CExpansionDevice::GetType()
502{
503    return (CExpansionDevice::ExpTypes) (mpExpansionPtr->type);
504}
505
506/*
507 * CNunchuk class methods.
508 */
509
510CNunchuk::CNunchuk(struct expansion_t *ExpPtr):
511    Buttons((void *) &(ExpPtr->nunchuk.btns), (void *) &(ExpPtr->nunchuk.btns_held),
512            (void *) &(ExpPtr->nunchuk.btns_released)), Joystick(&(ExpPtr->nunchuk.js)),
513            Accelerometer(&(ExpPtr->nunchuk.accel_calib), &(ExpPtr->nunchuk.accel), &(ExpPtr->nunchuk.accel_threshold),
514                          &(ExpPtr->nunchuk.orient), &(ExpPtr->nunchuk.orient_threshold), &(ExpPtr->nunchuk.gforce))
515{
516}
517
518/*
519 * CClassic class methods.
520 */
521
522CClassic::CClassic(struct expansion_t *ExpPtr):
523    Buttons((void *) &(ExpPtr->classic.btns), (void *) &(ExpPtr->classic.btns_held), (void *) &(ExpPtr->classic.btns_released)),LeftJoystick(&(ExpPtr->classic.ljs)), RightJoystick(&(ExpPtr->classic.rjs))
524{
525    // Initialize the expansion pointer.
526    mpClassicPtr = &(ExpPtr->classic);
527}
528
529float CClassic::GetLShoulderButton()
530{
531    return mpClassicPtr->l_shoulder;
532}
533
534float CClassic::GetRShoulderButton()
535{
536    return mpClassicPtr->r_shoulder;
537}
538
539/*
540 * CGuitarHero3 class methods.
541 */
542
543CGuitarHero3::CGuitarHero3(struct expansion_t *ExpPtr):
544    Buttons((void *) &(ExpPtr->gh3.btns), (void *) &(ExpPtr->gh3.btns_held), (void *) &(ExpPtr->gh3.btns_released)),
545            Joystick(&(ExpPtr->gh3.js))
546{
547    // Initialize the expansion pointer.
548    mpGH3Ptr = &(ExpPtr->gh3);
549}
550
551float CGuitarHero3::GetWhammyBar()
552{
553    return mpGH3Ptr->whammy_bar;
554}
555
556
557/*
558 * CMotionPlus class methods.
559 */
560CMotionPlus::CMotionPlus(struct expansion_t *ExpPtr):
561        Gyroscope(&(ExpPtr->mp.raw_gyro),&(ExpPtr->mp.cal_gyro),&(ExpPtr->mp.gyro_rate),(unsigned char*)&(ExpPtr->mp.acc_mode),&(ExpPtr->mp),
562        &(ExpPtr->mp.raw_gyro_threshold))
563{
564        mpMPPtr = &(ExpPtr->mp);
565}
566
567void CMotionPlus::Connect(struct wiimote_t* WiimotePtr)
568{
569        wiic_set_motion_plus(WiimotePtr,1);
570}
571
572void CMotionPlus::Disconnect(struct wiimote_t* WiimotePtr)
573{
574        wiic_set_motion_plus(WiimotePtr,0);
575}
576
577
578/*
579 * CBalanceBoard class methods.
580 */
581CBalanceBoard::CBalanceBoard(struct expansion_t *ExpPtr):
582        WeightSensor(&(ExpPtr->bb.pressure_raw_data),&(ExpPtr->bb.cal_low_weight),&(ExpPtr->bb.cal_medium_weight),&(ExpPtr->bb.cal_high_weight),&(ExpPtr->bb.pressure_weight),&(ExpPtr->bb))
583{
584    // Initialize the expansion pointer.
585         mpBBPtr = &(ExpPtr->bb);
586} 
587
588
589/*
590 * CWiimote class methods.
591 */
592
593CWiimote::CWiimote() : // SWIG insisted it exist for the vectors. Hopefully it will only be used for copy.
594    IR(NULL), Buttons(NULL, NULL, NULL), Accelerometer((accel_t*) NULL, (vec3b_t*) NULL, (int*) &(mpTempInt),
595                                                       (orient_t*) NULL, (float*) &(mpTempFloat), (gforce_t*) NULL),
596            ExpansionDevice(NULL)
597{
598    mpWiimotePtr = NULL;
599}
600
601CWiimote::CWiimote(struct wiimote_t *wmPtr):
602    IR(wmPtr), Buttons((void *) &(wmPtr->btns), (void *) &(wmPtr->btns_held), (void *) &(wmPtr->btns_released)),
603            Accelerometer((accel_t*) &(wmPtr->accel_calib), (vec3b_t*) &(wmPtr->accel),
604                          (int*) &(wmPtr->accel_threshold), (orient_t*) &(wmPtr->orient),
605                          (float*) &(wmPtr->orient_threshold), (gforce_t*) &(wmPtr->gforce)), 
606            ExpansionDevice((struct expansion_t*) &(wmPtr->exp))
607{
608    mpWiimotePtr = wmPtr;
609    logger.SetDeviceAddress(string(GetAddress()));
610}
611
612CWiimote::CWiimote(const CWiimote &copyin) : // Copy constructor to handle pass by value.
613            IR(copyin.mpWiimotePtr),
614            Buttons((void *) &(copyin.mpWiimotePtr->btns), (void *) &(copyin.mpWiimotePtr->btns_held),
615                    (void *) &(copyin.mpWiimotePtr->btns_released)),
616            Accelerometer((accel_t*) &(copyin.mpWiimotePtr->accel_calib), (vec3b_t*) &(copyin.mpWiimotePtr->accel),
617                          (int*) &(copyin.mpWiimotePtr->accel_threshold), (orient_t*) &(copyin.mpWiimotePtr->orient),
618                          (float*) &(copyin.mpWiimotePtr->orient_threshold), (gforce_t*) &(copyin.mpWiimotePtr->gforce)),
619            ExpansionDevice((struct expansion_t*) &(copyin.mpWiimotePtr->exp)), logger(copyin.logger)
620{
621    mpWiimotePtr = copyin.mpWiimotePtr;
622    logger = copyin.logger;
623}
624
625void CWiimote::Disconnected()
626{
627    wiic_disconnected(mpWiimotePtr);
628}
629
630void CWiimote::SetRumbleMode(CWiimote::OnOffSelection State)
631{
632    wiic_rumble(mpWiimotePtr, State);
633}
634
635void CWiimote::ToggleRumble()
636{
637    wiic_toggle_rumble(mpWiimotePtr);
638}
639
640bool CWiimote::isRumbleEnabled()
641{
642    return WIIMOTE_IS_SET(mpWiimotePtr,WIIMOTE_STATE_RUMBLE);
643}
644
645int CWiimote::GetLEDs()
646{
647    return mpWiimotePtr->leds;
648}
649
650void CWiimote::SetLEDs(int LEDs)
651{
652    wiic_set_leds(mpWiimotePtr, LEDs);
653}
654
655float CWiimote::GetBatteryLevel()
656{
657    return mpWiimotePtr->battery_level;
658}
659
660int CWiimote::GetHandshakeState()
661{
662    return mpWiimotePtr->handshake_state;
663}
664
665CWiimote::EventTypes CWiimote::GetEvent()
666{
667    return (CWiimote::EventTypes) mpWiimotePtr->event;
668}
669
670const unsigned char *CWiimote::GetEventBuffer()
671{
672    return mpWiimotePtr->event_buf;
673}
674
675void CWiimote::Log()
676{
677        logger.InitLog();
678       
679        if(logType & WIIC_LOG_ACC) {
680                float x, y, z;
681                Accelerometer.GetGravityVector(x,y,z);
682                logger.LogAcc(x,y,z);
683        }
684       
685        if(logType & WIIC_LOG_GYRO) {                   
686                float roll, pitch, yaw;
687                ExpansionDevice.MotionPlus.Gyroscope.GetRates(roll,pitch,yaw);
688                logger.LogGyro(roll,pitch,yaw);
689        }                       
690}
691
692void CWiimote::SetMotionSensingMode(CWiimote::OnOffSelection State)
693{
694    wiic_motion_sensing(mpWiimotePtr, State);
695}
696
697void CWiimote::EnableMotionPlus(CWiimote::OnOffSelection State)
698{
699    if(State == CWiimote::ON)
700        ExpansionDevice.MotionPlus.Connect(mpWiimotePtr);
701    else
702        ExpansionDevice.MotionPlus.Disconnect(mpWiimotePtr);
703}
704
705void CWiimote::EnableSpeaker(CWiimote::OnOffSelection State)
706{
707    wiic_set_speaker(mpWiimotePtr, State);
708}
709
710void CWiimote::MuteSpeaker(CWiimote::OnOffSelection State)
711{
712    wiic_mute_speaker(mpWiimotePtr, State);
713}
714
715void CWiimote::PlaySound()
716{
717        wiic_sound(mpWiimotePtr);
718}
719
720void CWiimote::ReadData(unsigned char *Buffer, unsigned int Offset, unsigned int Length)
721{
722    wiic_read_data(mpWiimotePtr, Buffer, Offset, Length);
723}
724
725void CWiimote::WriteData(unsigned int Address, unsigned char *Data, unsigned int Length)
726{
727    wiic_write_data(mpWiimotePtr, Address, Data, Length);
728}
729
730void CWiimote::UpdateStatus()
731{
732    wiic_status(mpWiimotePtr);
733}
734
735int CWiimote::GetID()
736{
737    return mpWiimotePtr->unid;
738}
739
740const char* CWiimote::GetAddress()
741{
742    return mpWiimotePtr->bdaddr_str;
743}
744
745int CWiimote::GetState()
746{
747    return mpWiimotePtr->state;
748}
749
750int CWiimote::GetFlags()
751{
752    return mpWiimotePtr->flags;
753}
754
755int CWiimote::SetFlags(int Enable, int Disable)
756{
757    return wiic_set_flags(mpWiimotePtr, Enable, Disable);
758}
759
760void CWiimote::SetSmoothing(bool Smooth)
761{
762        if(Smooth)
763                wiic_set_flags(mpWiimotePtr, WIIC_SMOOTHING, 0);
764        else
765                wiic_set_flags(mpWiimotePtr, 0, WIIC_SMOOTHING);
766}
767
768void CWiimote::Resync()
769{
770    wiic_resync(mpWiimotePtr);
771}
772
773void CWiimote::Disconnect()
774{
775    wiic_disconnect(mpWiimotePtr);
776}
777
778int CWiimote::isUsingACC()
779{
780    return WIIC_USING_ACC(mpWiimotePtr) != 0;
781}
782
783int CWiimote::isUsingEXP()
784{
785    return WIIC_USING_EXP(mpWiimotePtr) != 0;
786}
787
788int CWiimote::isUsingIR()
789{
790    return WIIC_USING_IR(mpWiimotePtr) != 0;
791}
792
793int CWiimote::isUsingMotionPlus()
794{
795        return WIIC_USING_MOTION_PLUS(mpWiimotePtr) != 0;
796}
797
798int CWiimote::isUsingSpeaker()
799{
800    return WIIC_USING_SPEAKER(mpWiimotePtr) != 0;
801}
802
803int CWiimote::isSpeakerMuted()
804{
805    return WIIC_SPEAKER_MUTED(mpWiimotePtr) != 0;
806}
807
808int CWiimote::isLEDSet(int LEDNum)
809{
810    int result = 0;
811
812    switch(LEDNum)
813    {
814        case 1:
815            result = (mpWiimotePtr->leds & LED_1) != 0;
816            break;
817        case 2:
818            result = (mpWiimotePtr->leds & LED_2) != 0;
819            break;
820        case 3:
821            result = (mpWiimotePtr->leds & LED_3) != 0;
822            break;
823        case 4:
824            result = (mpWiimotePtr->leds & LED_4) != 0;
825        default:
826            result = 0;
827    }
828
829    return result;
830}
831
832/*
833 * Wii Class Methods
834 */
835
836CWii::CWii()
837{
838    mpWiimoteArraySize = 4;
839    mpWiimoteArray = wiic_init(mpWiimoteArraySize);
840}
841
842CWii::CWii(int MaxNumWiimotes)
843{
844    mpWiimoteArraySize = MaxNumWiimotes;
845    mpWiimoteArray = wiic_init(mpWiimoteArraySize);
846}
847
848CWii::~CWii()
849{
850    wiic_cleanup((struct wiimote_t**) mpWiimoteArray, mpWiimoteArraySize);
851}
852
853void CWii::RefreshWiimotes()
854{
855    int index;
856
857    // This approach is a bit wasteful but it will work.  The other
858    // option is to force the user to handle disconnect events to remove
859    // wiimotes from the array.
860    mpWiimotesVector.clear();
861
862    for(index = 0; index < mpWiimoteArraySize; index++)
863    {
864        if((mpWiimoteArray[index]->state & WIIMOTE_STATE_CONNECTED) != 0)
865        {
866            CWiimote wm(mpWiimoteArray[index]);
867            mpWiimotesVector.push_back(wm);
868        }
869    }
870}
871
872int CWii::GetNumConnectedWiimotes()
873{
874    int index;
875    int count = 0;
876
877    for(index = 0; index < mpWiimoteArraySize; index++)
878    {
879        if((mpWiimoteArray[index]->state & WIIMOTE_STATE_CONNECTED) != 0)
880            count++;
881    }
882
883    return count;
884}
885
886CWiimote& CWii::GetByID(int UnID, int Refresh)
887{
888    std::vector<CWiimote>::iterator i;
889
890    if(Refresh)
891        RefreshWiimotes();
892
893    for(i = mpWiimotesVector.begin(); i != mpWiimotesVector.end(); ++i)
894    {
895        if((*i).GetID() == UnID)
896            return *i;
897    }
898
899    return *mpWiimotesVector.begin(); // Return the first one if it was not found.
900}
901
902std::vector<CWiimote>& CWii::GetWiimotes(int Refresh)
903{
904    if(Refresh)
905        RefreshWiimotes();
906
907    return mpWiimotesVector;
908}
909
910int CWii::Find(int Timeout)
911{
912    return wiic_find((struct wiimote_t**) mpWiimoteArray, mpWiimoteArraySize, Timeout);
913}
914
915int CWii::LoadRegisteredWiimotes()
916{
917        return wiic_load((struct wiimote_t**) mpWiimoteArray);
918}
919
920/**
921 *
922 * @brief Connects to the found Wii devices.
923 *
924 * @param autoreconnect [in] Automatically attempt to re-connect a Wii device in case of unexpected disconnection (default is disabled)
925 */
926std::vector<CWiimote>& CWii::Connect(bool autoreconnect)
927{
928    int numConnected = 0;
929    int index;
930
931    mpWiimotesVector.clear();
932
933    numConnected = wiic_connect((struct wiimote_t**) mpWiimoteArray, mpWiimoteArraySize, autoreconnect);
934
935    for(index = 0; index < numConnected; index++)
936    {
937        CWiimote wm(mpWiimoteArray[index]);
938        mpWiimotesVector.push_back(wm);
939    }
940
941    return mpWiimotesVector;
942}
943
944/**
945 *
946 * @brief Finds up to five devices and automatically connect to all of them.
947 *
948 * @param timeout       [in] Timeout for the discovery step (default is 5 seconds)
949 * @param rumbleAck     [in] Each found and connected device will receive a small rumble ack (deafult is enabled)
950 * @param autoreconnect [in] Automatically attempt to re-connect a Wii device in case of unexpected disconnection (default is disabled)
951 */
952std::vector<CWiimote>& CWii::FindAndConnect(int timeout, bool rumbleAck, bool autoreconnect)
953{
954    std::vector<CWiimote>::iterator i;
955    int numFound = 0;
956    int index;
957
958    //Find the wiimote
959    numFound = Find(timeout);
960
961    // Search for up to five seconds;
962    cout << "Found " << numFound << " wiimotes" << endl;
963    cout << "Connecting to wiimotes..." << endl;
964
965    // Connect to the wiimote
966    Connect(autoreconnect);
967
968        cout << "Connected to " << (unsigned int)mpWiimotesVector.size() << " wiimotes" << endl;
969
970    // Setup the wiimotes
971    for(index = 0, i = mpWiimotesVector.begin(); i != mpWiimotesVector.end(); ++i, ++index)
972    {
973        // Use a reference to make working with the iterator handy.
974        CWiimote & wiimote = *i;
975
976        //Rumble for 0.2 seconds as a connection ack
977                if(rumbleAck) {
978                wiimote.SetRumbleMode(CWiimote::ON);
979                usleep(200000);
980                wiimote.SetRumbleMode(CWiimote::OFF);
981                }
982    }
983
984        return mpWiimotesVector;
985}
986
987int CWii::Poll()
988{
989        int events = wiic_poll((struct wiimote_t**) mpWiimoteArray, mpWiimoteArraySize);
990
991        // Logging
992        for(std::vector<CWiimote>::iterator i = mpWiimotesVector.begin() ; i != mpWiimotesVector.end() ; ++i)
993                if((*i).isLogEnabled())
994                        (*i).Log();
995               
996        return events;
997}
Note: See TracBrowser for help on using the repository browser.