supernova: fix for small audio vector sizes
[supercollider.git] / lang / LangPrimSource / SC_Wii.cpp
blobd9bcea9f457640d0c725b0d96bf0ac242ef73a72
1 // problem: communication with the event loop
2 // pipe cannot be read
3 // on MacOSX, I could use the EventLoopTimer instead, like in HID
5 // to try: put discovery within thread (so extra command in manager)
7 /*
8 * SC_Wii.cpp
9 * SC3lang
11 * Created by Marije Baalman on Fri 18 May 2007.
12 * Copyright (c) 2007 Marije Baalman All rights reserved.
13 * part of ...
14 SuperCollider real time audio synthesis system
15 Copyright (c) 2002 James McCartney. All rights reserved.
16 http://www.audiosynth.com
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #ifdef SC_DARWIN
35 #include <Carbon/Carbon.h>
36 #endif
38 #include "SCBase.h"
39 #include "VMGlobals.h"
40 #include "PyrSymbolTable.h"
41 #include "PyrInterpreter.h"
42 #include "PyrKernel.h"
44 #include "PyrObjectProto.h"
45 #include "PyrPrimitiveProto.h"
46 #include "PyrKernelProto.h"
47 #include "SC_InlineUnaryOp.h"
48 #include "SC_InlineBinaryOp.h"
49 #include "PyrSched.h"
50 #include "GC.h"
53 #ifdef HAVE_WII
55 #ifdef SC_DARWIN
56 #include <mach/mach.h>
57 #include <mach/mach_error.h>
59 extern "C"{
60 #include "WiiMote_OSX/wiiremote.h"
62 #endif // SC_DARWIN
64 #ifdef SC_LINUX
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <math.h>
69 extern "C"{
70 #include <cwiid.h>
71 // #include <wiimote.h>
72 // #include <wiimote_api.h>
73 // #include <wiimote_event.h>
74 // #include <wiimote_link.h>
76 #endif // SC_LINUX
78 // common:
79 #include <string.h>
80 #include <errno.h>
83 //--------- PyrSymbols ------------
84 PyrSymbol * s_wiiDisconnected;
85 PyrSymbol * s_wiiConnected;
86 PyrSymbol * s_handleEvent;
88 PyrSymbol * s_handleBatteryEvent;
89 PyrSymbol * s_handleExtensionEvent;
90 PyrSymbol * s_handleNunchukEvent;
91 PyrSymbol * s_handleClassicEvent;
92 PyrSymbol * s_handleIREvent;
93 PyrSymbol * s_handleAccEvent;
94 PyrSymbol * s_handleButtonEvent;
96 static PyrSymbol* s_wii = 0;
97 static PyrSymbol* s_wiiCalibrationInfoClass = 0;
98 static PyrSymbol* s_wiiLEDStateClass = 0;
99 static PyrSymbol* s_readError = 0;
101 extern bool compiledOK;
102 //int gNumberOfWiiDevices = 0;
104 #ifdef SC_DARWIN
105 EventLoopTimerRef gWiiTimer = NULL; // timer for element data updates
106 #endif
109 // --------------- SC_WII structure --------------
110 struct SC_WII
112 SC_WII(PyrObject *obj);
113 ~SC_WII();
115 bool open();
116 bool close();
117 bool wii_connect();
118 bool wii_disconnect();
119 int enable( bool enab );
120 void disconnected();
121 void connected();
122 void get_address();
123 // void set_address( char * addr );
124 // void speaker_init( int format );
126 bool update();
128 #ifdef SC_LINUX
129 void handleBatteryEvent( uint8_t battery );
130 void handleButtonEvent( uint16_t buttons );
131 void handleAccEvent( uint8_t acc[3] );
132 void handleIREvent( int id, cwiid_ir_src ir );
133 void handleNunchukEvent( struct cwiid_nunchuk_mesg nunchuk );
134 void handleClassicEvent( struct cwiid_classic_mesg classic );
135 void handleExtensionEvent( int ext_type );
136 #endif
137 #ifdef SC_DARWIN
138 // void handleBatteryEvent( );
139 // void handleButtonEvent( );
140 // void handleAccEvent( );
141 // void handleIREvent( );
142 // void handleNunchukEvent( );
143 // void handleClassicEvent( );
144 void handleEvent();
145 #endif
147 void readError();
149 static PyrObject* getObject(PyrSlot* slot)
151 return isKindOfSlot(slot, s_wii->u.classobj) ? slotRawObject(slot) : 0;
154 static SC_WII* getDevice(PyrObject* obj)
156 return (SC_WII*)slotRawPtr(obj->slots);
159 #ifdef SC_DARWIN
160 WiiRemoteRef m_wiiremote;
161 char m_address[32];
162 #endif
163 #ifdef SC_LINUX
164 cwiid_wiimote_t *m_wiiremote;
165 int id;
166 unsigned char rpt_mode;
167 unsigned char led_state;
168 char m_address[19];
169 #endif
170 bool m_connected;
171 int m_searching;
173 SC_WII* m_next;
174 PyrObject* m_obj;
177 // ---------------- SC_WIIManager structure --------------
179 struct SC_WIIManager
181 public:
182 static SC_WIIManager& instance();
184 int start( float updtime );
185 int stop();
186 #ifdef SC_LINUX
187 cwiid_wiimote_t * discover();
188 #endif
189 #ifdef SC_DARWIN
190 WiiRemoteRef discover();
191 #endif
193 int add(SC_WII *dev);
194 int remove(SC_WII *dev);
196 /// kind of private, but used in callback function and getWiiData
197 SC_WII* m_devices;
199 private:
200 SC_WIIManager();
201 ~SC_WIIManager();
203 void loop();
205 bool m_running;
206 float m_updatetime;
210 #ifdef SC_LINUX
211 /// linux specific cwiid functions
212 #define toggle_bit(bf,b) \
213 (bf) = ((bf) & b) \
214 ? ((bf) & ~(b)) \
215 : ((bf) | (b))
217 void set_bit( unsigned char * bf, unsigned char b )
219 if (!(*bf & b) )
220 toggle_bit( *bf, b );
223 void clear_bit( unsigned char * bf, unsigned char b )
225 if ((*bf & b) )
226 toggle_bit( *bf, b );
229 cwiid_mesg_callback_t cwiid_callback;
230 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state);
231 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode);
233 cwiid_err_t err;
234 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
236 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
237 else printf("-1:");
238 vprintf(s, ap);
239 printf("\n");
242 void set_led_state(cwiid_wiimote_t *wiimotet, unsigned char led_state)
244 // printf( "set_led_state %i\n", led_state );
245 if (cwiid_set_led(wiimotet, led_state)) {
246 post( "WII Error: Unable to set led state\n");
250 void set_rpt_mode(cwiid_wiimote_t *wiimotet, unsigned char rpt_mode)
252 if (cwiid_set_rpt_mode(wiimotet, rpt_mode)) {
253 post( "WII Error: Unable to set report mode\n");
257 #endif
259 #ifdef SC_DARWIN
260 void GetWii_Events();
261 static pascal void IdleTimerWii (EventLoopTimerRef inTimer, void* userData);
262 static EventLoopTimerUPP GetTimerUPPWii (void);
264 static pascal void IdleTimerWii (EventLoopTimerRef inTimer, void* userData)
266 #pragma unused (inTimer, userData)
267 GetWii_Events ();
270 static EventLoopTimerUPP GetTimerUPPWii (void)
272 static EventLoopTimerUPP sTimerUPP = NULL;
274 if (sTimerUPP == NULL)
275 sTimerUPP = NewEventLoopTimerUPP (IdleTimerWii);
277 return sTimerUPP;
280 #endif
283 //------------ SC_WIIManager functions --------
284 SC_WIIManager& SC_WIIManager::instance()
286 static SC_WIIManager instance;
287 return instance;
290 SC_WIIManager::SC_WIIManager()
291 : m_running(false),
292 m_devices(0)
294 #ifdef SC_LINUX
295 cwiid_set_err(err);
296 #endif
298 #ifdef SC_DARWIN
299 NumVersion outSoftwareVersion;
300 BluetoothHCIVersionInfo outHardwareVersion;
302 // post("aka.wiiremote 1.0B6-UB by Masayuki Akamatsu");
304 if (IOBluetoothGetVersion(&outSoftwareVersion, &outHardwareVersion)==kIOReturnSuccess)
306 if (outSoftwareVersion.majorRev < 1 && outSoftwareVersion.minorAndBugRev < 0x63)
308 error("WII: requires Bluetooth version 1.6.3 or later.");
309 return;
312 else
314 error("WII: can't get Bluetooth version.");
315 return;
317 #endif
321 SC_WIIManager::~SC_WIIManager()
323 #ifdef SC_DARWIN
324 if (gWiiTimer)
326 RemoveEventLoopTimer(gWiiTimer);
327 gWiiTimer = NULL;
329 #endif
332 int SC_WIIManager::start( float updtime )
335 /// nothing to do for Linux
337 #ifdef SC_DARWIN
338 m_updatetime = (updtime / 1000); // convert to seconds
339 post( "WII: eventloop updatetime %f\n", m_updatetime );
340 //double eventtime = (double) updtime;
341 if(gWiiTimer)
343 RemoveEventLoopTimer(gWiiTimer);
344 gWiiTimer = NULL;
346 InstallEventLoopTimer (GetCurrentEventLoop(), 0, (EventTimerInterval) m_updatetime, GetTimerUPPWii (), 0, &gWiiTimer);
347 #endif
349 return errNone;
352 #ifdef SC_LINUX
353 cwiid_wiimote_t * SC_WIIManager::discover()
355 bdaddr_t bdaddr = {0, 0, 0, 0, 0, 0};
356 cwiid_wiimote_t* wiimotediscovered;
358 if ( (wiimotediscovered = cwiid_open(&bdaddr, 0)) == NULL ) {
359 return NULL;
361 if (cwiid_set_mesg_callback(wiimotediscovered, cwiid_callback)) {
362 post("ERROR: WII: Unable to set message callback\n");
363 if ( cwiid_close(wiimotediscovered) ){
364 post("ERROR: WII: Unable to close Wii\n");
366 return NULL;
369 /// adding these as devices in the primitive! this is the only way to be able to connect, as the connection uses much more info than I thought!
371 return( wiimotediscovered );
373 #endif
375 #ifdef SC_DARWIN
376 WiiRemoteRef SC_WIIManager::discover()
378 /* WiiRemoteRef newwii;
379 char address[32];
380 newwii = (WiiRemoteRef)malloc(sizeof(WiiRemoteRec));
381 if (newwii != NULL)
383 wiiremote_init(newwii);
384 bool result;
385 result = wiiremote_search( newwii, address); // start searching the device
386 // m_searching++;
387 post("WII: searching wiimote %i\n",result);
390 return( newwii );
393 #endif
396 int SC_WIIManager::stop()
398 /// nothing to do on LINUX
399 #ifdef SC_DARWIN
400 if (gWiiTimer)
402 RemoveEventLoopTimer(gWiiTimer);
403 gWiiTimer = NULL;
405 #endif
406 return errNone;
409 int SC_WIIManager::add(SC_WII* dev)
411 // post("WII: SC_WIIManager::add\n");
413 if (dev->m_next) return false;
414 if (m_devices == dev ) {
415 m_devices = dev;
417 else {
418 dev->m_next = m_devices;
419 m_devices = dev;
422 #ifdef SC_LINUX
423 dev->id = cwiid_get_id( dev->m_wiiremote );
425 if (cwiid_enable(dev->m_wiiremote, CWIID_FLAG_MESG_IFC)) {
426 post("ERROR: WII: Unable to enable Wii\n");
429 dev->rpt_mode = CWIID_RPT_STATUS;
430 dev->led_state = 0;
431 set_rpt_mode( dev->m_wiiremote, dev->rpt_mode );
432 set_led_state( dev->m_wiiremote, dev->led_state );
433 #endif
435 //#ifdef SC_DARWIN
436 // dev->wii_connect();
437 //#endif
439 return true;
443 int SC_WIIManager::remove(SC_WII* dev)
445 SC_WII *prev = 0, *cur = m_devices;
447 while (cur) {
448 if (cur == dev) {
449 if (prev) prev->m_next = dev->m_next;
450 else m_devices = dev->m_next;
451 dev->m_next = 0;
452 delete dev;
453 return errNone;
455 prev = cur;
456 cur = cur->m_next;
459 return errFailed;
462 #ifdef SC_LINUX
463 void cwiid_callback(cwiid_wiimote_t *wiimotet, int mesg_count,
464 union cwiid_mesg mesg[], struct timespec *timestamp)
466 int i, j;
467 int valid_source;
468 int thisid, id;
470 SC_WII *dev = SC_WIIManager::instance().m_devices;
472 id = 0;
473 thisid = cwiid_get_id( wiimotet );
474 while (dev) {
475 if ( thisid == dev->id )
477 break;
479 dev = dev->m_next;
482 for (i=0; i < mesg_count; i++)
484 switch (mesg[i].type) {
485 case CWIID_MESG_STATUS:
486 dev->handleBatteryEvent( mesg[i].status_mesg.battery );
487 dev->handleExtensionEvent( mesg[i].status_mesg.ext_type );
488 break;
489 case CWIID_MESG_BTN:
490 dev->handleButtonEvent( mesg[i].btn_mesg.buttons );
491 break;
492 case CWIID_MESG_ACC:
493 dev->handleAccEvent( mesg[i].acc_mesg.acc );
494 break;
495 case CWIID_MESG_IR:
496 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
497 dev->handleIREvent( j, mesg[i].ir_mesg.src[j] );
499 break;
500 case CWIID_MESG_NUNCHUK:
501 dev->handleNunchukEvent( mesg[i].nunchuk_mesg );
502 break;
503 case CWIID_MESG_CLASSIC:
504 dev->handleClassicEvent( mesg[i].classic_mesg );
505 break;
506 case CWIID_MESG_ERROR:
507 dev->disconnected();
508 dev->readError();
509 break;
510 default:
511 post("WII: Unknown Report");
512 break;
516 #endif
518 #ifdef SC_DARWIN
519 void GetWii_Events (){
520 SC_WII *dev = SC_WIIManager::instance().m_devices;
521 // m_devices;
522 int debugcnt = 0;
523 while (dev) {
524 bool connection;
525 // post( "WII: device %i, t %p, w %p, n %p\n", debugcnt, dev, dev->m_wiiremote, dev->m_next);
526 debugcnt++;
527 if ( dev->m_wiiremote != NULL )
529 connection = wiiremote_isconnected(dev->m_wiiremote);
530 if ( dev->m_connected == false && connection == true) // if the device is connected, but wasn't before
532 // post( "WII: wiimote got connected\n");
533 wiiremote_getstatus(dev->m_wiiremote);
534 dev->connected();
536 else if (dev->m_connected == true && connection == false) // if device was disconnected
538 // post( "WII: wiimote got disconnected\n");
539 dev->disconnected();
541 else if ( dev->m_searching > 0 )
543 dev->wii_connect();
545 if ( dev->m_connected ) {
546 // post( "WII: wiimote is connected\n");
547 wiiremote_getstatus(dev->m_wiiremote);
548 dev->handleEvent();
550 else {
551 // post("WII: wiimote not connected\n");
554 else
556 // post("WII: read error\n");
557 dev->readError();
559 dev = dev->m_next;
562 #endif
564 //---------------- SC_WII functions -----------------
566 SC_WII::SC_WII(PyrObject* obj)
567 : m_next(0),
568 m_obj(obj)
570 // #ifdef SC_LINUX
571 // m_report = nil;
572 // #endif
573 m_wiiremote = NULL;
574 m_searching = 0;
575 m_connected = false;
576 // m_address;
577 SetPtr(obj->slots+0, this);
580 SC_WII::~SC_WII()
582 close();
585 bool SC_WII::open()
587 #ifdef SC_DARWIN
588 m_wiiremote = (WiiRemoteRef)malloc(sizeof(WiiRemoteRec));
589 if (m_wiiremote != NULL)
591 wiiremote_init(m_wiiremote);
592 m_wiiremote->isMotionSensorEnabled = true;
593 m_wiiremote->isIRSensorEnabled = false;
594 m_wiiremote->isVibrationEnabled = false;
595 m_wiiremote->isExpansionPortEnabled = false;
596 m_wiiremote->isLED1Illuminated = false;
597 m_wiiremote->isLED2Illuminated = false;
598 m_wiiremote->isLED3Illuminated = false;
599 m_wiiremote->isLED4Illuminated = false;
601 m_connected = false;
602 // return SC_WIIManager::instance().add(this);
604 #endif
605 #ifdef SC_LINUX
606 // m_wiiremote = (wiimote_t*)malloc(sizeof(wiimote_t));
607 // m_report = (wiimote_report_t)malloc(sizeof(wiimote_report_t));
608 // if ( m_wiiremote != NULL )
609 // {
610 // *m_wiiremote = WIIMOTE_INIT;
611 // m_report = WIIMOTE_REPORT_INIT;
612 // m_connected = false;
613 // return SC_WIIManager::instance().add(this);
614 // }
615 #endif
616 return( false );
619 bool SC_WII::close()
621 if (m_wiiremote != NULL)
623 #ifdef SC_DARWIN
624 if (wiiremote_isconnected(m_wiiremote))
625 wiiremote_disconnect(m_wiiremote);
626 ; //m_wiiremote, sizeof(WiiRemoteRec));
627 m_wiiremote = NULL;
628 m_searching = 0;
629 #endif
630 #ifdef SC_LINUX
631 if ( cwiid_close( m_wiiremote ) ){
632 // m_wiiremote = NULL;
633 post( "error closing device\n" );
634 fflush( stdout );
635 return errFailed;
637 m_wiiremote = NULL;
638 // return errNone;
639 // if (wiimote_is_open(m_wiiremote))
640 // wiimote_disconnect(m_wiiremote);
641 #endif
644 // SetNil(m_obj->slots+0);
645 return SC_WIIManager::instance().remove(this);
648 bool SC_WII::wii_connect()
650 #ifdef SC_DARWIN
651 if (wiiremote_isconnected(m_wiiremote))
653 post("WII: wiimote is already connected\n");
654 m_connected = true;
656 else
658 bool result;
659 result = wiiremote_search( m_wiiremote, m_address); // start searching the device
660 m_searching++;
661 post("WII: searching wiimote %i\n",result);
662 if ( result )
663 m_connected = true;
664 else
665 m_connected = false;
667 #endif
668 #ifdef SC_LINUX
669 // if (wiimote_connect(m_wiiremote, m_address) < 0) {
670 // post("WII: unable to open wiimote: %s at address %s\n", wiimote_get_error(), m_address);
671 // post("WII: unable to open wiimote at address %s\n",m_address);
672 // m_connected = false;
673 // }
674 // else
675 m_connected = true;
676 #endif
677 return( m_connected );
680 void SC_WII::connected()
682 bool result;
683 m_connected = true;
684 m_searching = 0;
685 #ifdef SC_DARWIN
686 result = wiiremote_led( m_wiiremote, 0, 0, 0, 0);
687 // if ( !result )
688 // wii_disconnect();
689 #endif
690 // post("WII: wiiremote connected\n");
691 pthread_mutex_lock(&gLangMutex);
692 if (compiledOK) {
693 VMGlobals* g = gMainVMGlobals;
694 g->canCallOS = false;
695 ++g->sp; SetObject(g->sp, m_obj);
696 runInterpreter(g, s_wiiConnected, 1);
697 g->canCallOS = false;
699 pthread_mutex_unlock(&gLangMutex);
702 bool SC_WII::wii_disconnect()
704 bool result;
705 m_searching = 0;
706 /// TODO: remove from wii_manager
708 #ifdef SC_DARWIN
709 wiiremote_stopsearch( m_wiiremote );
710 result = wiiremote_disconnect( m_wiiremote);
711 #endif
712 #ifdef SC_LINUX
713 // result = wiimote_disconnect(m_wiiremote);
714 #endif
716 // call disconnect action
717 if ( result )
718 disconnected();
719 return( result );
722 int SC_WII::enable( bool enab )
724 #ifdef SC_LINUX
725 if ( enab ) {
726 if (cwiid_enable(m_wiiremote, CWIID_FLAG_MESG_IFC)) {
727 return errFailed;
729 } else {
730 if (cwiid_disable(m_wiiremote, CWIID_FLAG_MESG_IFC)) {
731 return errFailed;
734 return errNone;
735 #endif
736 return errNone;
739 void SC_WII::disconnected()
741 m_connected = false;
742 m_searching = 0;
743 pthread_mutex_lock(&gLangMutex);
744 if (compiledOK) {
745 VMGlobals* g = gMainVMGlobals;
746 g->canCallOS = false;
747 ++g->sp; SetObject(g->sp, m_obj);
748 runInterpreter(g, s_wiiDisconnected, 1);
749 g->canCallOS = false;
751 pthread_mutex_unlock(&gLangMutex);
754 // void SC_WII::speaker_init( int format )
755 // {
756 // if ( format == 4 )
757 // wiimote_speaker_init(m_wiiremote, WIIMOTE_FMT_S4, 0xff);
758 // else if ( format == 8 )
759 // wiimote_speaker_init(m_wiiremote, WIIMOTE_FMT_S8, 0xff);
760 // }
762 void SC_WII::get_address()
764 #ifdef SC_DARWIN
765 if (m_wiiremote->device == NULL)
767 return;
769 else
771 char str[32];
772 wiiremote_getaddress(m_wiiremote, str);
773 strcpy(m_address, str);
775 #endif
778 // bool SC_WII::update()
779 // {
780 // if (wiimote_update(m_wiiremote) < 0) {
781 // wiimote_disconnect(m_wiiremote);
782 // post( "WII: wiimote got disconnected\n");
783 // disconnected();
784 // return( false );
785 // }
786 // return( true );
787 // }
789 // void SC_WII::set_address( char * addr )
790 // {
791 // strcpy( m_address, addr );
792 // // post( "WII: addr %s, m_address %s\n", addr, m_address );
793 // }
795 #ifdef SC_DARWIN
796 void SC_WII::handleEvent()
798 if (m_wiiremote->device == NULL)
799 return; // do nothing
801 pthread_mutex_lock(&gLangMutex);
802 if (compiledOK) {
803 VMGlobals* g = gMainVMGlobals;
804 g->canCallOS = false;
805 ++g->sp; SetObject(g->sp, m_obj);
806 // buttons
807 // post( "buttondata %i\n", m_wiiremote->buttonData);
808 // ++g->sp; SetInt(g->sp, m_wiiremote->buttonData);
809 PyrObject *butArray = newPyrArray(g->gc, 11 * sizeof(int), 0, true);
810 PyrSlot *butArraySlots = butArray->slots;
811 SetInt(butArray->slots+butArray->size++, (int) ((0x0008 & m_wiiremote->buttonData) > 0) ); //A
812 SetInt(butArray->slots+butArray->size++, (int) ((0x0004 & m_wiiremote->buttonData) > 0) ); //B
813 SetInt(butArray->slots+butArray->size++, (int) ((0x0002 & m_wiiremote->buttonData) > 0) ); //1
814 SetInt(butArray->slots+butArray->size++, (int) ((0x0001 & m_wiiremote->buttonData) > 0) ); //2
815 SetInt(butArray->slots+butArray->size++, (int) ((0x0010 & m_wiiremote->buttonData) > 0) ); //minus
816 SetInt(butArray->slots+butArray->size++, (int) ((0x0080 & m_wiiremote->buttonData) > 0) ); //home
817 SetInt(butArray->slots+butArray->size++, (int) ((0x1000 & m_wiiremote->buttonData) > 0) ); // plus
818 SetInt(butArray->slots+butArray->size++, (int) ((0x0800 & m_wiiremote->buttonData) > 0) ); // up
819 SetInt(butArray->slots+butArray->size++, (int) ((0x0400 & m_wiiremote->buttonData) > 0) ); // down
820 SetInt(butArray->slots+butArray->size++, (int) ((0x0100 & m_wiiremote->buttonData) > 0) ); // left
821 SetInt(butArray->slots+butArray->size++, (int) ((0x0200 & m_wiiremote->buttonData) > 0) ); // right
822 butArray->size = 11;
823 ++g->sp; SetObject(g->sp, butArray);
826 if (m_wiiremote->isIRSensorEnabled)
827 {// IR sensor
828 ++g->sp; SetFloat(g->sp, m_wiiremote->posX);
829 ++g->sp; SetFloat(g->sp, m_wiiremote->posY);
830 ++g->sp; SetFloat(g->sp, m_wiiremote->angle);
831 ++g->sp; SetInt(g->sp, m_wiiremote->tracking);
832 } else {
833 ++g->sp; SetFloat(g->sp, 0);
834 ++g->sp; SetFloat(g->sp, 0);
835 ++g->sp; SetFloat(g->sp, 0);
836 ++g->sp; SetInt(g->sp, 0);
838 if (m_wiiremote->isMotionSensorEnabled)
839 { // motion sensor
840 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->accX / 256);
841 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->accY / 256);
842 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->accZ / 256);
843 ++g->sp; SetInt(g->sp, m_wiiremote->orientation);
844 } else {
845 ++g->sp; SetFloat(g->sp, 0);
846 ++g->sp; SetFloat(g->sp, 0);
847 ++g->sp; SetFloat(g->sp, 0);
848 ++g->sp; SetInt(g->sp, 0);
850 if (m_wiiremote->isExpansionPortAttached && m_wiiremote->isExpansionPortEnabled)
852 ++g->sp; SetInt(g->sp, m_wiiremote->expType);
853 // Classic Controller
854 if (m_wiiremote->expType == WiiClassicController)
856 // buttons
857 //++g->sp; SetInt(g->sp, m_wiiremote->cButtonData);
858 PyrObject *outArray = newPyrArray(g->gc, 15 * sizeof(char), 0, true);
859 PyrSlot *outArraySlots = outArray->slots;
860 SetInt(outArray->slots+outArray->size++, (int) ((0x0008 & m_wiiremote->cButtonData) > 0) ); //X
861 SetInt(outArray->slots+outArray->size++, (int) ((0x0020 & m_wiiremote->cButtonData) > 0) ); //Y
862 SetInt(outArray->slots+outArray->size++, (int) ((0x0010 & m_wiiremote->cButtonData) > 0) ); //A
863 SetInt(outArray->slots+outArray->size++, (int) ((0x0040 & m_wiiremote->cButtonData) > 0) ); //B
864 SetInt(outArray->slots+outArray->size++, (int) ((0x2000 & m_wiiremote->cButtonData) > 0) ); //L
866 SetInt(outArray->slots+outArray->size++, (int) ((0x0200 & m_wiiremote->cButtonData) > 0) ); //R
867 SetInt(outArray->slots+outArray->size++, (int) ((0x0080 & m_wiiremote->cButtonData) > 0) ); //ZL
868 SetInt(outArray->slots+outArray->size++, (int) ((0x0004 & m_wiiremote->cButtonData) > 0) ); //ZR
869 SetInt(outArray->slots+outArray->size++, (int) ((0x0001 & m_wiiremote->cButtonData) > 0) ); //Up
870 SetInt(outArray->slots+outArray->size++, (int) ((0x4000 & m_wiiremote->cButtonData) > 0) ); //Down
872 SetInt(outArray->slots+outArray->size++, (int) ((0x0002 & m_wiiremote->cButtonData) > 0) ); //Left
873 SetInt(outArray->slots+outArray->size++, (int) ((0x8000 & m_wiiremote->cButtonData) > 0) );//Right
874 SetInt(outArray->slots+outArray->size++, (int) ((0x1000 & m_wiiremote->cButtonData) > 0) );//Minus
875 SetInt(outArray->slots+outArray->size++, (int) ((0x0800 & m_wiiremote->cButtonData) > 0) );//Home
876 SetInt(outArray->slots+outArray->size++, (int) ((0x0400 & m_wiiremote->cButtonData) > 0) );//Plus
878 outArray->size = 15;
879 ++g->sp; SetObject(g->sp, outArray);
881 // Joystick 1
882 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cStickX1 / 0x3F);
883 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cStickY1 / 0x3F);
885 // Joystick 2
886 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cStickX2 / 0x1F);
887 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cStickY2 / 0x1F);
889 // Analog
890 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cAnalogL / 0x1F);
891 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->cAnalogR / 0x1F);
893 // Nunchuk
894 if (m_wiiremote->expType == WiiNunchuk)
896 // Buttons
897 //++g->sp; SetInt(g->sp, m_wiiremote->nButtonData);
898 PyrObject *butArrayN = newPyrArray(g->gc, 2 * sizeof(int), 0, true);
899 PyrSlot *butArraySlotsN = butArrayN->slots;
900 SetInt(butArrayN->slots+butArrayN->size++, (int) ((0x01 & m_wiiremote->nButtonData) < 1) );
901 SetInt(butArrayN->slots+butArrayN->size++, (int) ((0x02 & m_wiiremote->nButtonData) < 1) );
902 butArrayN->size = 2;
903 ++g->sp; SetObject(g->sp, butArrayN);
905 // Joystick
906 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->nStickX / 256);
907 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->nStickY / 256);
909 // Motion Sensor
910 if (m_wiiremote->isMotionSensorEnabled)
912 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->nAccX / 256);
913 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->nAccY / 256);
914 ++g->sp; SetFloat(g->sp, (float) m_wiiremote->nAccZ / 256);
915 ++g->sp; SetInt(g->sp, m_wiiremote->nOrientation);
916 } else {
917 ++g->sp; SetFloat(g->sp, 0);
918 ++g->sp; SetFloat(g->sp, 0);
919 ++g->sp; SetFloat(g->sp, 0);
920 ++g->sp; SetInt(g->sp, 0);
923 } else {
924 ++g->sp; SetInt(g->sp, 0);
925 ++g->sp; SetInt(g->sp, 0);
926 ++g->sp; SetInt(g->sp, 0);
927 ++g->sp; SetInt(g->sp, 0);
928 ++g->sp; SetInt(g->sp, 0);
929 ++g->sp; SetInt(g->sp, 0);
930 ++g->sp; SetInt(g->sp, 0);
931 ++g->sp; SetInt(g->sp, 0);
933 ++g->sp; SetFloat(g->sp, m_wiiremote->batteryLevel);
934 runInterpreter(g, s_handleEvent, 19);
935 g->canCallOS = false;
937 pthread_mutex_unlock(&gLangMutex);
939 #endif
941 #ifdef SC_LINUX
942 void SC_WII::handleBatteryEvent( uint8_t battery ){
943 pthread_mutex_lock(&gLangMutex);
944 if (compiledOK) {
945 VMGlobals* g = gMainVMGlobals;
946 g->canCallOS = false;
947 ++g->sp; SetObject(g->sp, m_obj);
948 ++g->sp; SetFloat(g->sp, (float) battery / CWIID_BATTERY_MAX );
949 runInterpreter(g, s_handleBatteryEvent, 2);
950 g->canCallOS = false;
952 pthread_mutex_unlock(&gLangMutex);
954 #endif
956 #ifdef SC_LINUX
957 void SC_WII::handleExtensionEvent( int ext_type ){
958 pthread_mutex_lock(&gLangMutex);
959 if (compiledOK) {
960 VMGlobals* g = gMainVMGlobals;
961 g->canCallOS = false;
962 ++g->sp; SetObject(g->sp, m_obj);
963 ++g->sp; SetInt(g->sp, ext_type );
964 runInterpreter(g, s_handleExtensionEvent, 2);
965 g->canCallOS = false;
967 pthread_mutex_unlock(&gLangMutex);
969 #endif
971 #ifdef SC_LINUX
972 void SC_WII::handleButtonEvent( uint16_t buttons ){
973 pthread_mutex_lock(&gLangMutex);
974 if (compiledOK) {
975 VMGlobals* g = gMainVMGlobals;
976 g->canCallOS = false;
977 ++g->sp; SetObject(g->sp, m_obj);
979 // post( "handle Button Event\n" );
980 // fflush( stdout );
982 // ++g->sp; SetInt(g->sp, (int) buttons );
983 PyrObject *outArray = newPyrArray(g->gc, 11 * sizeof(int), 0, true);
984 PyrSlot *outArraySlots = outArray->slots;
985 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_A & buttons) > 0) );
986 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_B & buttons) > 0) );
987 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_1 & buttons) > 0) );
988 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_2 & buttons) > 0) );
989 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_MINUS & buttons) > 0) );
990 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_HOME & buttons) > 0) );
991 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_PLUS & buttons) > 0) );
992 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_UP & buttons) > 0) );
993 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_DOWN & buttons) > 0) );
994 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_LEFT & buttons) > 0) );
995 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_BTN_RIGHT & buttons) > 0) );
996 outArray->size = 11;
997 ++g->sp; SetObject(g->sp, outArray);
999 runInterpreter(g, s_handleButtonEvent, 2);
1000 g->canCallOS = false;
1002 pthread_mutex_unlock(&gLangMutex);
1004 #endif
1006 #ifdef SC_LINUX
1007 void SC_WII::handleAccEvent( uint8_t acc[3] ){
1008 pthread_mutex_lock(&gLangMutex);
1009 if (compiledOK) {
1010 VMGlobals* g = gMainVMGlobals;
1011 g->canCallOS = false;
1012 ++g->sp; SetObject(g->sp, m_obj);
1013 ++g->sp; SetFloat(g->sp, (float) acc[CWIID_X]/CWIID_ACC_MAX );
1014 ++g->sp; SetFloat(g->sp, (float) acc[CWIID_Y]/CWIID_ACC_MAX );
1015 ++g->sp; SetFloat(g->sp, (float) acc[CWIID_Z]/CWIID_ACC_MAX );
1016 runInterpreter(g, s_handleAccEvent, 4);
1017 g->canCallOS = false;
1019 pthread_mutex_unlock(&gLangMutex);
1021 #endif
1023 #ifdef SC_LINUX
1024 void SC_WII::handleNunchukEvent( struct cwiid_nunchuk_mesg nunchuk ){
1025 pthread_mutex_lock(&gLangMutex);
1026 if (compiledOK) {
1027 VMGlobals* g = gMainVMGlobals;
1028 g->canCallOS = false;
1029 ++g->sp; SetObject(g->sp, m_obj);
1031 PyrObject *outArray = newPyrArray(g->gc, 2 * sizeof(int), 0, true);
1032 PyrSlot *outArraySlots = outArray->slots;
1033 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_NUNCHUK_BTN_Z & nunchuk.buttons) > 0) );
1034 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_NUNCHUK_BTN_C & nunchuk.buttons) > 0) );
1035 outArray->size = 2;
1036 ++g->sp; SetObject(g->sp, outArray);
1037 // ++g->sp; SetInt(g->sp, (int) nunchuk.buttons);
1038 ++g->sp; SetFloat(g->sp, (float) nunchuk.stick[CWIID_X]/256);
1039 ++g->sp; SetFloat(g->sp, (float) nunchuk.stick[CWIID_Y]/256);
1040 ++g->sp; SetFloat(g->sp, (float) nunchuk.acc[CWIID_X]/CWIID_ACC_MAX );
1041 ++g->sp; SetFloat(g->sp, (float) nunchuk.acc[CWIID_Y]/CWIID_ACC_MAX );
1042 ++g->sp; SetFloat(g->sp, (float) nunchuk.acc[CWIID_Z]/CWIID_ACC_MAX );
1043 runInterpreter(g, s_handleNunchukEvent, 7);
1044 g->canCallOS = false;
1046 pthread_mutex_unlock(&gLangMutex);
1048 #endif
1050 #ifdef SC_LINUX
1051 void SC_WII::handleClassicEvent( struct cwiid_classic_mesg classic ){
1052 pthread_mutex_lock(&gLangMutex);
1053 if (compiledOK) {
1054 VMGlobals* g = gMainVMGlobals;
1055 g->canCallOS = false;
1056 ++g->sp; SetObject(g->sp, m_obj);
1057 PyrObject *outArray = newPyrArray(g->gc, 15 * sizeof(char), 0, true);
1058 PyrSlot *outArraySlots = outArray->slots;
1059 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_X & classic.buttons) > 0) );
1060 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_Y & classic.buttons) > 0) );
1061 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_A & classic.buttons) > 0) );
1062 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_B & classic.buttons) > 0) );
1063 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_L & classic.buttons) > 0) );
1065 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_R & classic.buttons) > 0) );
1066 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_ZL & classic.buttons) > 0) );
1067 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_ZR & classic.buttons) > 0) );
1068 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_UP & classic.buttons) > 0) );
1069 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_DOWN & classic.buttons) > 0) );
1071 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_LEFT & classic.buttons) > 0) );
1072 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_RIGHT & classic.buttons) > 0) );
1073 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_MINUS & classic.buttons) > 0) );
1074 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_HOME & classic.buttons) > 0) );
1075 SetInt(outArray->slots+outArray->size++, (int) ((CWIID_CLASSIC_BTN_PLUS & classic.buttons) > 0) );
1077 outArray->size = 15;
1078 ++g->sp; SetObject(g->sp, outArray);
1080 // ++g->sp; SetInt(g->sp, (int) classic.buttons);
1081 ++g->sp; SetFloat(g->sp, (float) classic.l_stick[CWIID_X]/CWIID_CLASSIC_L_STICK_MAX );
1082 ++g->sp; SetFloat(g->sp, (float) classic.l_stick[CWIID_Y]/CWIID_CLASSIC_L_STICK_MAX );
1083 ++g->sp; SetFloat(g->sp, (float) classic.r_stick[CWIID_X]/CWIID_CLASSIC_R_STICK_MAX );
1084 ++g->sp; SetFloat(g->sp, (float) classic.r_stick[CWIID_Y]/CWIID_CLASSIC_R_STICK_MAX );
1085 ++g->sp; SetFloat(g->sp, (float) classic.l/CWIID_CLASSIC_LR_MAX );
1086 ++g->sp; SetFloat(g->sp, (float) classic.r/CWIID_CLASSIC_LR_MAX );
1087 runInterpreter(g, s_handleClassicEvent, 8);
1088 g->canCallOS = false;
1090 pthread_mutex_unlock(&gLangMutex);
1092 #endif
1094 #ifdef SC_LINUX
1095 void SC_WII::handleIREvent( int id, cwiid_ir_src ir ){
1096 pthread_mutex_lock(&gLangMutex);
1097 if (compiledOK) {
1098 VMGlobals* g = gMainVMGlobals;
1099 g->canCallOS = false;
1100 ++g->sp; SetObject(g->sp, m_obj);
1101 ++g->sp; SetInt(g->sp, id);
1102 ++g->sp; SetInt(g->sp, ir.valid);
1103 ++g->sp; SetFloat(g->sp, (float) ir.pos[CWIID_X]/CWIID_IR_X_MAX);
1104 ++g->sp; SetFloat(g->sp, (float) ir.pos[CWIID_Y]/CWIID_IR_Y_MAX);
1105 ++g->sp; SetFloat(g->sp, (float) ir.size/256);
1106 runInterpreter(g, s_handleIREvent, 6);
1107 g->canCallOS = false;
1109 pthread_mutex_unlock(&gLangMutex);
1111 #endif
1113 void SC_WII::readError()
1115 pthread_mutex_lock(&gLangMutex);
1116 if (compiledOK) {
1117 VMGlobals* g = gMainVMGlobals;
1118 g->canCallOS = false;
1119 ++g->sp; SetObject(g->sp, m_obj);
1120 runInterpreter(g, s_readError, 1);
1121 g->canCallOS = false;
1123 pthread_mutex_unlock(&gLangMutex);
1124 // SC_WIIManager::instance().remove( this );
1129 //------------ primitives ---------------
1131 int prWii_Start(VMGlobals* g, int numArgsPushed)
1133 float updtime;
1134 int err;
1135 PyrSlot* args = g->sp - 1;
1136 // PyrSlot* args = g->sp;
1138 err = slotFloatVal(args+1, &updtime);
1139 if (err) return err;
1141 post( "update time %f", updtime );
1143 // if (!g->canCallOS) return errCantCallOS;
1144 // return SC_WIIManager::instance().start();
1145 return SC_WIIManager::instance().start(updtime);
1148 int prWii_Stop(VMGlobals* g, int numArgsPushed)
1150 // if (!g->canCallOS) return errCantCallOS;
1151 return SC_WIIManager::instance().stop();
1154 int prWii_Discover(VMGlobals* g, int numArgsPushed)
1156 int curid, nmotes;
1157 int err;
1158 PyrSlot* args = g->sp - 1;
1160 err = slotIntVal(args, &curid);
1161 if (err) return err;
1163 if (!isKindOfSlot(args+1, class_array))
1164 return errWrongType;
1165 PyrObject* allDevsArray = slotRawObject(&args[1]);
1166 PyrSlot* slotsArray = allDevsArray->slots;
1168 #ifdef SC_LINUX
1169 cwiid_wiimote_t * thiswii;
1171 thiswii = SC_WIIManager::instance().discover();
1172 if ( thiswii == NULL ){
1173 SetFalse(g->sp-2);
1174 post( "no device found\n" );
1175 return errNone;
1177 #endif
1179 if ( !isKindOfSlot(slotsArray+curid, s_wii->u.classobj ) ) {
1180 SetFalse(g->sp-2);
1181 return errWrongType;
1183 PyrObject* obj = SC_WII::getObject(slotsArray+curid);
1184 SC_WII* dev = SC_WII::getDevice(obj);
1186 #ifdef SC_DARWIN
1187 dev->wii_connect();
1188 #endif
1190 // post( "dev %p, wii %p\n", dev, dev->m_wiiremote );
1192 // if (!dev) return errFailed;
1193 // free( dev->m_wiiremote );
1194 #ifdef SC_LINUX
1195 dev->m_wiiremote = thiswii;
1196 #endif
1197 if ( SC_WIIManager::instance().add( dev ) ) {
1198 post( "device added\n" );
1199 SetTrue(g->sp-2);
1200 } else {
1201 SetFalse(g->sp-2);
1202 post( "device was already added\n" );
1205 return errNone;
1208 int prWii_Open(VMGlobals *g, int numArgsPushed)
1210 PyrSlot* args = g->sp;
1211 int err;
1213 PyrObject* obj = SC_WII::getObject(args+0);
1214 if (!obj) return errWrongType;
1216 SC_WII* dev = new SC_WII(obj);
1217 err = dev->open();
1218 if (err) {
1219 delete dev;
1220 return err;
1223 return errNone;
1226 int prWii_Close(VMGlobals *g, int numArgsPushed)
1228 PyrSlot* args = g->sp;
1230 PyrObject* obj = SC_WII::getObject(args+0);
1231 if (!obj) return errWrongType;
1233 SC_WII* dev = SC_WII::getDevice(obj);
1234 if (!dev) return errFailed;
1236 return dev->close();
1239 int prWiiAddress(VMGlobals *g, int numArgsPushed)
1241 PyrSlot* args = g->sp;
1242 int err;
1244 PyrObject* obj = SC_WII::getObject(args+0);
1245 if (!obj) return errWrongType;
1247 SC_WII* dev = SC_WII::getDevice(obj);
1248 if (!dev) return errFailed;
1250 dev->get_address();
1252 SetSymbol(args, getsym(dev->m_address));
1254 return errNone;
1258 // int prWiiSetAddress(VMGlobals *g, int numArgsPushed)
1259 // {
1260 // PyrSlot* args = g->sp - 1;
1261 // int err;
1263 // PyrObject* obj = SC_WII::getObject(args+0);
1264 // if (!obj) return errWrongType;
1266 // SC_WII* dev = SC_WII::getDevice(obj);
1267 // if (!dev) return errFailed;
1269 // #ifdef SC_LINUX
1270 // char path[19];
1271 // #endif
1272 // #ifdef SC_DARWIN
1273 // char path[32];
1274 // #endif
1275 // err = slotStrVal(args+1, path, sizeof(path));
1276 // if (err) return err;
1277 // // post( "WII: address %s\n", path );
1279 // dev->set_address( path );
1281 // return errNone;
1282 // }
1284 int prWiiConnect(VMGlobals *g, int numArgsPushed)
1286 PyrSlot* args = g->sp;
1287 int err;
1289 PyrObject* obj = SC_WII::getObject(args+0);
1290 if (!obj) return errWrongType;
1292 SC_WII* dev = SC_WII::getDevice(obj);
1293 if (!dev) return errFailed;
1295 dev->wii_connect();
1297 return errNone;
1300 int prWiiDisconnect(VMGlobals *g, int numArgsPushed)
1302 PyrSlot* args = g->sp;
1303 int err;
1305 PyrObject* obj = SC_WII::getObject(args+0);
1306 if (!obj) return errWrongType;
1308 SC_WII* dev = SC_WII::getDevice(obj);
1309 if (!dev) return errFailed;
1311 dev->wii_disconnect();
1313 return errNone;
1316 int prWiiCalibration(VMGlobals *g, int numArgsPushed)
1318 PyrSlot* args = g->sp - 1;
1319 int err;
1321 PyrObject* obj = SC_WII::getObject(args+0);
1322 if (!obj) return errWrongType;
1324 if (!isKindOfSlot(args+1, s_wiiCalibrationInfoClass->u.classobj))
1325 return errWrongType;
1326 PyrObject* infoObj = slotRawObject(&args[1]);
1328 SC_WII* dev = SC_WII::getDevice(obj);
1329 if (!dev) return errFailed;
1331 if (dev->m_wiiremote == NULL)
1333 return errFailed;
1335 else
1337 #ifdef SC_DARWIN
1338 SetFloat(infoObj->slots+0, dev->m_wiiremote->wiiCalibData.accX_zero);
1339 SetFloat(infoObj->slots+1, dev->m_wiiremote->wiiCalibData.accY_zero);
1340 SetFloat(infoObj->slots+2, dev->m_wiiremote->wiiCalibData.accZ_zero);
1341 SetFloat(infoObj->slots+3, dev->m_wiiremote->wiiCalibData.accX_1g);
1342 SetFloat(infoObj->slots+4, dev->m_wiiremote->wiiCalibData.accY_1g);
1343 SetFloat(infoObj->slots+5, dev->m_wiiremote->wiiCalibData.accZ_1g);
1345 if (dev->m_wiiremote->isExpansionPortAttached)
1347 SetFloat(infoObj->slots+6, dev->m_wiiremote->nunchukCalibData.accX_zero);
1348 SetFloat(infoObj->slots+7, dev->m_wiiremote->nunchukCalibData.accY_zero);
1349 SetFloat(infoObj->slots+8, dev->m_wiiremote->nunchukCalibData.accZ_zero);
1350 SetFloat(infoObj->slots+9, dev->m_wiiremote->nunchukCalibData.accX_1g);
1351 SetFloat(infoObj->slots+10, dev->m_wiiremote->nunchukCalibData.accY_1g);
1352 SetFloat(infoObj->slots+11, dev->m_wiiremote->nunchukCalibData.accZ_1g);
1354 SetFloat(infoObj->slots+12, dev->m_wiiremote->nunchukJoyStickCalibData.x_max);
1355 SetFloat(infoObj->slots+13, dev->m_wiiremote->nunchukJoyStickCalibData.x_min);
1356 SetFloat(infoObj->slots+14, dev->m_wiiremote->nunchukJoyStickCalibData.x_center);
1357 SetFloat(infoObj->slots+15, dev->m_wiiremote->nunchukJoyStickCalibData.y_max);
1358 SetFloat(infoObj->slots+16, dev->m_wiiremote->nunchukJoyStickCalibData.y_min);
1359 SetFloat(infoObj->slots+17, dev->m_wiiremote->nunchukJoyStickCalibData.y_center);
1361 #endif
1362 #ifdef SC_LINUX
1363 /* SetInt(infoObj->slots+0, dev->m_wiiremote->cal.x_zero);
1364 SetInt(infoObj->slots+1, dev->m_wiiremote->cal.y_zero);
1365 SetInt(infoObj->slots+2, dev->m_wiiremote->cal.z_zero);
1366 SetInt(infoObj->slots+3, dev->m_wiiremote->cal.x_scale);
1367 SetInt(infoObj->slots+4, dev->m_wiiremote->cal.y_scale);
1368 SetInt(infoObj->slots+5, dev->m_wiiremote->cal.z_scale);
1370 if (dev->m_wiiremote->mode.ext == 1)
1372 SetInt(infoObj->slots+6, dev->m_wiiremote->ext.nunchuk.cal.x_zero);
1373 SetInt(infoObj->slots+7, dev->m_wiiremote->ext.nunchuk.cal.y_zero);
1374 SetInt(infoObj->slots+8, dev->m_wiiremote->ext.nunchuk.cal.z_zero);
1375 SetInt(infoObj->slots+9, dev->m_wiiremote->ext.nunchuk.cal.x_scale);
1376 SetInt(infoObj->slots+10, dev->m_wiiremote->ext.nunchuk.cal.y_scale);
1377 SetInt(infoObj->slots+11, dev->m_wiiremote->ext.nunchuk.cal.z_scale);
1379 SetInt(infoObj->slots+12, dev->m_wiiremote->ext.nunchuk.cal.joyx_max);
1380 SetInt(infoObj->slots+13, dev->m_wiiremote->ext.nunchuk.cal.joyx_min);
1381 SetInt(infoObj->slots+14, dev->m_wiiremote->ext.nunchuk.cal.joyx_center);
1382 SetInt(infoObj->slots+15, dev->m_wiiremote->ext.nunchuk.cal.joyy_max);
1383 SetInt(infoObj->slots+16, dev->m_wiiremote->ext.nunchuk.cal.joyy_min);
1384 SetInt(infoObj->slots+17, dev->m_wiiremote->ext.nunchuk.cal.joyy_center);
1386 #endif
1389 slotCopy(&args[0], &args[1]);
1390 return errNone;
1393 // int prWiiGetLED(VMGlobals *g, int numArgsPushed)
1394 // {
1395 // PyrSlot* args = g->sp - 1;
1396 // int err;
1398 // PyrObject* obj = SC_WII::getObject(args+0);
1399 // if (!obj) return errWrongType;
1401 // SC_WII* dev = SC_WII::getDevice(obj);
1402 // if (!dev) return errFailed;
1404 // if (!isKindOfSlot(args+1, class_array))
1405 // return errWrongType;
1406 // PyrObject* leds = slotRawObject(&args[1]);
1407 // PyrSlot* bslots = leds->slots;
1409 // if (dev->m_wiiremote == NULL)
1410 // {
1411 // return errFailed;
1412 // }
1413 // else
1414 // {
1415 // #ifdef SC_DARWIN
1416 // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1417 // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1418 // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1419 // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1420 // #endif
1421 // #ifdef SC_LINUX
1422 // SetInt(bslots+0, dev->m_wiiremote->led.one);
1423 // SetInt(bslots+1, dev->m_wiiremote->led.two);
1424 // SetInt(bslots+2, dev->m_wiiremote->led.three);
1425 // SetInt(bslots+3, dev->m_wiiremote->led.four);
1426 // #endif
1427 // }
1429 // return errNone;
1430 // }
1432 // int prWiiGetButtons(VMGlobals *g, int numArgsPushed)
1433 // {
1434 // PyrSlot* args = g->sp - 1;
1435 // int err;
1437 // PyrObject* obj = SC_WII::getObject(args+0);
1438 // if (!obj) return errWrongType;
1440 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1441 // // return errWrongType;
1442 // // PyrObject* infoObj = slotRawObject(&args[1]);
1444 // SC_WII* dev = SC_WII::getDevice(obj);
1445 // if (!dev) return errFailed;
1447 // if (!isKindOfSlot(args+1, class_array))
1448 // return errWrongType;
1449 // PyrObject* buttons = slotRawObject(&args[1]);
1450 // PyrSlot* bslots = buttons->slots;
1452 // if (dev->m_wiiremote == NULL)
1453 // {
1454 // return errFailed;
1455 // }
1456 // else
1457 // {
1458 // #ifdef SC_DARWIN
1459 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1460 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1461 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1462 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1463 // #endif
1464 // #ifdef SC_LINUX
1465 // SetInt(bslots+0, dev->m_wiiremote->keys.left);
1466 // SetInt(bslots+1, dev->m_wiiremote->keys.right);
1467 // SetInt(bslots+2, dev->m_wiiremote->keys.down);
1468 // SetInt(bslots+3, dev->m_wiiremote->keys.up);
1469 // SetInt(bslots+4, dev->m_wiiremote->keys.plus);
1470 // SetInt(bslots+5, dev->m_wiiremote->keys.two);
1471 // SetInt(bslots+6, dev->m_wiiremote->keys.one);
1472 // SetInt(bslots+7, dev->m_wiiremote->keys.b);
1473 // SetInt(bslots+8, dev->m_wiiremote->keys.a);
1474 // SetInt(bslots+9, dev->m_wiiremote->keys.minus);
1475 // SetInt(bslots+10, dev->m_wiiremote->keys.home);
1476 // #endif
1477 // }
1479 // return errNone;
1480 // }
1482 // int prWiiGetMotion(VMGlobals *g, int numArgsPushed)
1483 // {
1484 // PyrSlot* args = g->sp - 1;
1485 // int err;
1487 // PyrObject* obj = SC_WII::getObject(args+0);
1488 // if (!obj) return errWrongType;
1490 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1491 // // return errWrongType;
1492 // // PyrObject* infoObj = slotRawObject(&args[1]);
1494 // SC_WII* dev = SC_WII::getDevice(obj);
1495 // if (!dev) return errFailed;
1497 // if (!isKindOfSlot(args+1, class_array))
1498 // return errWrongType;
1499 // PyrObject* buttons = slotRawObject(&args[1]);
1500 // PyrSlot* bslots = buttons->slots;
1502 // if (dev->m_wiiremote == NULL)
1503 // {
1504 // return errFailed;
1505 // }
1506 // else
1507 // {
1508 // #ifdef SC_DARWIN
1509 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1510 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1511 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1512 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1513 // #endif
1514 // #ifdef SC_LINUX
1515 // SetInt(bslots+0, dev->m_wiiremote->axis.x);
1516 // SetInt(bslots+1, dev->m_wiiremote->axis.y);
1517 // SetInt(bslots+2, dev->m_wiiremote->axis.z);
1518 // #endif
1519 // }
1521 // return errNone;
1522 // }
1524 // int prWiiGetIR(VMGlobals *g, int numArgsPushed)
1525 // {
1526 // PyrSlot* args = g->sp - 1;
1527 // int err;
1529 // PyrObject* obj = SC_WII::getObject(args+0);
1530 // if (!obj) return errWrongType;
1532 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1533 // // return errWrongType;
1534 // // PyrObject* infoObj = slotRawObject(&args[1]);
1536 // SC_WII* dev = SC_WII::getDevice(obj);
1537 // if (!dev) return errFailed;
1539 // if (!isKindOfSlot(args+1, class_array))
1540 // return errWrongType;
1541 // PyrObject* buttons = slotRawObject(&args[1]);
1542 // PyrSlot* bslots = buttons->slots;
1544 // if (dev->m_wiiremote == NULL)
1545 // {
1546 // return errFailed;
1547 // }
1548 // else
1549 // {
1550 // #ifdef SC_DARWIN
1551 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1552 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1553 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1554 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1555 // #endif
1556 // #ifdef SC_LINUX
1557 // SetInt(bslots+0, dev->m_wiiremote->ir1.x);
1558 // SetInt(bslots+1, dev->m_wiiremote->ir1.y);
1559 // SetInt(bslots+2, dev->m_wiiremote->ir1.size);
1560 // #endif
1561 // }
1563 // return errNone;
1564 // }
1566 // int prWiiGetNunchukButtons(VMGlobals *g, int numArgsPushed)
1567 // {
1568 // PyrSlot* args = g->sp - 1;
1569 // int err;
1571 // PyrObject* obj = SC_WII::getObject(args+0);
1572 // if (!obj) return errWrongType;
1574 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1575 // // return errWrongType;
1576 // // PyrObject* infoObj = slotRawObject(&args[1]);
1578 // SC_WII* dev = SC_WII::getDevice(obj);
1579 // if (!dev) return errFailed;
1581 // if (!isKindOfSlot(args+1, class_array))
1582 // return errWrongType;
1583 // PyrObject* buttons = slotRawObject(&args[1]);
1584 // PyrSlot* bslots = buttons->slots;
1586 // if (dev->m_wiiremote == NULL)
1587 // {
1588 // return errFailed;
1589 // }
1590 // else
1591 // {
1592 // #ifdef SC_DARWIN
1593 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1594 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1595 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1596 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1597 // #endif
1598 // #ifdef SC_LINUX
1599 // SetInt(bslots+0, dev->m_wiiremote->ext.nunchuk.keys.z);
1600 // SetInt(bslots+1, dev->m_wiiremote->ext.nunchuk.keys.c);
1601 // #endif
1602 // }
1604 // return errNone;
1605 // }
1607 // int prWiiGetNunchukJoy(VMGlobals *g, int numArgsPushed)
1608 // {
1609 // PyrSlot* args = g->sp - 1;
1610 // int err;
1612 // PyrObject* obj = SC_WII::getObject(args+0);
1613 // if (!obj) return errWrongType;
1615 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1616 // // return errWrongType;
1617 // // PyrObject* infoObj = slotRawObject(&args[1]);
1619 // SC_WII* dev = SC_WII::getDevice(obj);
1620 // if (!dev) return errFailed;
1622 // if (!isKindOfSlot(args+1, class_array))
1623 // return errWrongType;
1624 // PyrObject* buttons = slotRawObject(&args[1]);
1625 // PyrSlot* bslots = buttons->slots;
1627 // if (dev->m_wiiremote == NULL)
1628 // {
1629 // return errFailed;
1630 // }
1631 // else
1632 // {
1633 // #ifdef SC_DARWIN
1634 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1635 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1636 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1637 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1638 // #endif
1639 // #ifdef SC_LINUX
1640 // SetInt(bslots+0, dev->m_wiiremote->ext.nunchuk.joyx);
1641 // SetInt(bslots+1, dev->m_wiiremote->ext.nunchuk.joyy);
1642 // #endif
1643 // }
1645 // return errNone;
1646 // }
1648 // int prWiiGetNunchukMotion(VMGlobals *g, int numArgsPushed)
1649 // {
1650 // PyrSlot* args = g->sp - 1;
1651 // int err;
1653 // PyrObject* obj = SC_WII::getObject(args+0);
1654 // if (!obj) return errWrongType;
1656 // // if (!isKindOfSlot(args+1, s_wiiLEDStateClass->u.classobj))
1657 // // return errWrongType;
1658 // // PyrObject* infoObj = slotRawObject(&args[1]);
1660 // SC_WII* dev = SC_WII::getDevice(obj);
1661 // if (!dev) return errFailed;
1663 // if (!isKindOfSlot(args+1, class_array))
1664 // return errWrongType;
1665 // PyrObject* buttons = slotRawObject(&args[1]);
1666 // PyrSlot* bslots = buttons->slots;
1668 // if (dev->m_wiiremote == NULL)
1669 // {
1670 // return errFailed;
1671 // }
1672 // else
1673 // {
1674 // #ifdef SC_DARWIN
1675 // // SetInt(bslots+0, dev->m_wiiremote->isLED1Illuminated);
1676 // // SetInt(bslots+1, dev->m_wiiremote->isLED2Illuminated);
1677 // // SetInt(bslots+2, dev->m_wiiremote->isLED3Illuminated);
1678 // // SetInt(bslots+3, dev->m_wiiremote->isLED4Illuminated);
1679 // #endif
1680 // #ifdef SC_LINUX
1681 // SetInt(bslots+0, dev->m_wiiremote->ext.nunchuk.axis.x);
1682 // SetInt(bslots+1, dev->m_wiiremote->ext.nunchuk.axis.y);
1683 // SetInt(bslots+2, dev->m_wiiremote->ext.nunchuk.axis.z);
1684 // #endif
1685 // }
1687 // return errNone;
1688 // }
1690 // int prWiiGetExpansion(VMGlobals *g, int numArgsPushed)
1691 // {
1692 // PyrSlot* args = g->sp;
1693 // int err;
1695 // PyrObject* obj = SC_WII::getObject(args+0);
1696 // if (!obj) return errWrongType;
1698 // SC_WII* dev = SC_WII::getDevice(obj);
1699 // if (!dev) return errFailed;
1701 // if (dev->m_wiiremote == NULL)
1702 // {
1703 // return errFailed;
1704 // }
1705 // else
1706 // {
1707 // #ifdef SC_DARWIN
1708 // if (dev->m_wiiremote->isExpansionPortAttached)
1709 // SetInt( args, dev->m_wiiremote->expType );
1710 // else
1711 // SetInt( args, 0 );
1712 // #endif
1713 // // #ifdef SC_LINUX
1714 // // if ( dev->m_wiiremote->mode.ext == 1 )
1715 // // SetInt( args, dev->m_wiiremote->ext.id );
1716 // // else
1717 // // SetInt( args, 0 );
1718 // // #endif
1719 // }
1721 // return errNone;
1722 // }
1724 // int prWiiGetBattery(VMGlobals *g, int numArgsPushed)
1725 // {
1726 // PyrSlot* args = g->sp;
1727 // int err;
1729 // PyrObject* obj = SC_WII::getObject(args+0);
1730 // if (!obj) return errWrongType;
1732 // SC_WII* dev = SC_WII::getDevice(obj);
1733 // if (!dev) return errFailed;
1735 // if (dev->m_wiiremote == NULL)
1736 // {
1737 // return errFailed;
1738 // }
1739 // else
1740 // {
1741 // #ifdef SC_DARWIN
1742 // if (dev->m_wiiremote->batteryLevel)
1743 // SetFloat( args, dev->m_wiiremote->batteryLevel );
1744 // else
1745 // SetFloat( args, 0 );
1746 // #endif
1747 // // #ifdef SC_LINUX
1748 // // SetFloat( args, dev->m_wiiremote->battery );
1749 // // #endif
1750 // }
1752 // return errNone;
1753 // }
1755 int prWiiSetLED(VMGlobals *g, int numArgsPushed)
1757 PyrSlot* args = g->sp - 1;
1758 int err;
1759 bool result;
1760 int enable1, enable2, enable3, enable4;
1762 PyrObject* obj = SC_WII::getObject(args+0);
1763 if (!obj) return errWrongType;
1765 SC_WII* dev = SC_WII::getDevice(obj);
1766 if (!dev) return errFailed;
1768 if (!isKindOfSlot(args+1, class_array))
1769 return errWrongType;
1770 PyrObject* leds = slotRawObject(&args[1]);
1771 PyrSlot* bslots = leds->slots;
1773 err = slotIntVal( bslots+0, &enable1 );
1774 if (err) return err;
1776 err = slotIntVal( bslots+1, &enable2 );
1777 if (err) return err;
1779 err = slotIntVal( bslots+2, &enable3 );
1780 if (err) return err;
1782 err = slotIntVal( bslots+3, &enable4 );
1783 if (err) return err;
1785 if (dev->m_wiiremote == NULL)
1787 return errFailed;
1789 else
1791 #ifdef SC_DARWIN
1792 result = wiiremote_led( dev->m_wiiremote, enable1, enable2, enable3, enable4);
1793 // if ( !result )
1794 // dev->wii_disconnect();
1795 #endif
1796 #ifdef SC_LINUX
1797 if ( enable1 )
1798 set_bit(&dev->led_state, CWIID_LED1_ON);
1799 else
1800 clear_bit(&dev->led_state, CWIID_LED1_ON);
1801 if ( enable2 )
1802 set_bit(&dev->led_state, CWIID_LED2_ON);
1803 else
1804 clear_bit(&dev->led_state, CWIID_LED2_ON);
1805 if ( enable3 )
1806 set_bit(&dev->led_state, CWIID_LED3_ON);
1807 else
1808 clear_bit(&dev->led_state, CWIID_LED3_ON);
1809 if ( enable4 )
1810 set_bit(&dev->led_state, CWIID_LED4_ON);
1811 else
1812 clear_bit(&dev->led_state, CWIID_LED4_ON);
1813 set_led_state(dev->m_wiiremote, dev->led_state);
1814 // post( "WII: led %i %i %i %i", dev->m_wiiremote->led.one, dev->m_wiiremote->led.two, dev->m_wiiremote->led.three, dev->m_wiiremote->led.four );
1815 // dev->update();
1816 #endif
1819 return errNone;
1822 int prWiiSetVibration(VMGlobals *g, int numArgsPushed)
1824 PyrSlot* args = g->sp - 1;
1825 int err;
1826 int enable1;
1827 bool result;
1829 PyrObject* obj = SC_WII::getObject(args+0);
1830 if (!obj) return errWrongType;
1832 SC_WII* dev = SC_WII::getDevice(obj);
1833 if (!dev) return errFailed;
1835 err = slotIntVal(args+1, &enable1);
1836 if (err) return err;
1838 if (dev->m_wiiremote == NULL)
1840 return errFailed;
1842 else
1844 #ifdef SC_DARWIN
1845 result = wiiremote_vibration( dev->m_wiiremote, enable1 );
1846 // if ( !result )
1847 // dev->wii_disconnect();
1848 // post( "WII: rumble %i %i", enable1, result );
1849 #endif
1850 #ifdef SC_LINUX
1851 if (cwiid_set_rumble(dev->m_wiiremote, (unsigned char) enable1)) {
1852 return errFailed;
1854 // post( "WII: rumble %i %i", dev->m_wiiremote->rumble, enable1 );
1855 #endif
1857 return errNone;
1860 int prWiiSetExpansion(VMGlobals *g, int numArgsPushed)
1862 PyrSlot* args = g->sp - 1;
1863 int err;
1864 int enable1;
1865 bool result;
1867 PyrObject* obj = SC_WII::getObject(args+0);
1868 if (!obj) return errWrongType;
1870 SC_WII* dev = SC_WII::getDevice(obj);
1871 if (!dev) return errFailed;
1873 err = slotIntVal(args+1, &enable1);
1874 if (err) return err;
1876 if (dev->m_wiiremote == NULL)
1878 return errFailed;
1880 else
1882 #ifdef SC_DARWIN
1883 result = wiiremote_expansion( dev->m_wiiremote, enable1 );
1884 // if ( !result )
1885 // dev->wii_disconnect();
1886 #endif
1887 #ifdef SC_LINUX
1888 if ( enable1 )
1889 set_bit(&dev->rpt_mode, CWIID_RPT_EXT);
1890 else
1891 clear_bit(&dev->rpt_mode, CWIID_RPT_EXT);
1892 set_rpt_mode(dev->m_wiiremote, dev->rpt_mode);
1893 // post( "WII: expansion %i %i", dev->m_wiiremote->mode.ext, enable1 );
1894 #endif
1897 return errNone;
1900 int prWiiSetIRSensor(VMGlobals *g, int numArgsPushed)
1902 PyrSlot* args = g->sp - 1;
1903 int err;
1904 int enable1;
1905 bool result;
1907 PyrObject* obj = SC_WII::getObject(args+0);
1908 if (!obj) return errWrongType;
1910 SC_WII* dev = SC_WII::getDevice(obj);
1911 if (!dev) return errFailed;
1913 err = slotIntVal(args+1, &enable1);
1914 if (err) return err;
1916 if (dev->m_wiiremote == NULL)
1918 return errFailed;
1920 else
1922 #ifdef SC_DARWIN
1923 result = wiiremote_irsensor( dev->m_wiiremote, enable1 );
1924 // if ( !result )
1925 // dev->wii_disconnect();
1927 #endif
1928 #ifdef SC_LINUX
1929 if ( enable1 )
1930 set_bit(&dev->rpt_mode, CWIID_RPT_IR);
1931 else
1932 clear_bit(&dev->rpt_mode, CWIID_RPT_IR);
1933 set_rpt_mode(dev->m_wiiremote, dev->rpt_mode);
1934 // post( "WII: ir sensor %i %i", dev->m_wiiremote->mode.ir, enable1 );
1935 #endif
1938 return errNone;
1941 int prWiiSetMotionSensor(VMGlobals *g, int numArgsPushed)
1943 PyrSlot* args = g->sp - 1;
1944 int err;
1945 int enable1;
1946 bool result;
1948 PyrObject* obj = SC_WII::getObject(args+0);
1949 if (!obj) return errWrongType;
1951 SC_WII* dev = SC_WII::getDevice(obj);
1952 if (!dev) return errFailed;
1954 err = slotIntVal(args+1, &enable1);
1955 if (err) return err;
1957 if (dev->m_wiiremote == NULL)
1959 return errFailed;
1961 else
1963 #ifdef SC_DARWIN
1964 result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
1965 // if ( !result )
1966 // dev->wii_disconnect();
1967 #endif
1968 #ifdef SC_LINUX
1969 if ( enable1 )
1970 set_bit(&dev->rpt_mode, CWIID_RPT_ACC);
1971 else
1972 clear_bit(&dev->rpt_mode, CWIID_RPT_ACC);
1973 set_rpt_mode(dev->m_wiiremote, dev->rpt_mode);
1974 #endif
1977 return errNone;
1980 int prWiiSetButtons(VMGlobals *g, int numArgsPushed)
1982 PyrSlot* args = g->sp - 1;
1983 int err;
1984 int enable1;
1985 bool result;
1987 PyrObject* obj = SC_WII::getObject(args+0);
1988 if (!obj) return errWrongType;
1990 SC_WII* dev = SC_WII::getDevice(obj);
1991 if (!dev) return errFailed;
1993 err = slotIntVal(args+1, &enable1);
1994 if (err) return err;
1996 if (dev->m_wiiremote == NULL)
1998 return errFailed;
2000 else
2002 #ifdef SC_DARWIN
2003 // buttons are always enabled
2004 // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2005 #endif
2006 #ifdef SC_LINUX
2007 if ( enable1 )
2008 set_bit(&dev->rpt_mode, CWIID_RPT_BTN);
2009 else
2010 clear_bit(&dev->rpt_mode, CWIID_RPT_BTN);
2011 set_rpt_mode(dev->m_wiiremote, dev->rpt_mode);
2012 #endif
2015 return errNone;
2018 int prWiiEnable(VMGlobals *g, int numArgsPushed)
2020 PyrSlot* args = g->sp - 1;
2021 int err;
2022 int enable1;
2023 bool result;
2025 PyrObject* obj = SC_WII::getObject(args+0);
2026 if (!obj) return errWrongType;
2028 SC_WII* dev = SC_WII::getDevice(obj);
2029 if (!dev) return errFailed;
2031 err = slotIntVal(args+1, &enable1);
2032 if (err) return err;
2034 if (dev->m_wiiremote == NULL)
2036 return errFailed;
2038 else
2040 #ifdef SC_DARWIN
2041 // is always enabled
2042 // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2043 #endif
2044 #ifdef SC_LINUX
2045 dev->enable( enable1 );
2046 #endif
2049 return errNone;
2052 // int prWiiPlaySpeaker(VMGlobals *g, int numArgsPushed)
2053 // {
2054 // PyrSlot* args = g->sp - 4;
2055 // int err;
2056 // int enable1;
2057 // int freq, vol;
2058 // bool result;
2060 // PyrObject* obj = SC_WII::getObject(args+0);
2061 // if (!obj) return errWrongType;
2063 // SC_WII* dev = SC_WII::getDevice(obj);
2064 // if (!dev) return errFailed;
2066 // err = slotIntVal(args+1, &enable1);
2067 // if (err) return err;
2069 // err = slotIntVal(args+2, &freq);
2070 // if (err) return err;
2072 // err = slotIntVal(args+3, &vol);
2073 // if (err) return err;
2075 // // err = slotIntVal(args+4, &sample);
2076 // // if (err) return err;
2078 // if (dev->m_wiiremote == NULL)
2079 // {
2080 // return errFailed;
2081 // }
2082 // else
2083 // {
2084 // // #ifdef SC_DARWIN
2085 // // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2086 // // #endif
2087 // #ifdef SC_LINUX
2088 // uint8_t sample[20] = {
2089 // 0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,
2090 // 0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c,0x3c
2091 // };
2092 // wiimote_speaker_freq( dev->m_wiiremote, (uint8_t) freq);
2093 // wiimote_speaker_volume( dev->m_wiiremote, (uint8_t) vol );
2094 // if ( enable1 )
2095 // wiimote_speaker_play( dev->m_wiiremote, sample, 20);
2097 // // post( "WII: speaker %i %i", freq, vol );
2098 // #endif
2099 // }
2101 // return errNone;
2102 // }
2104 // int prWiiEnableSpeaker(VMGlobals *g, int numArgsPushed)
2105 // {
2106 // PyrSlot* args = g->sp - 1;
2107 // int err;
2108 // int enable1;
2109 // bool result;
2111 // PyrObject* obj = SC_WII::getObject(args+0);
2112 // if (!obj) return errWrongType;
2114 // SC_WII* dev = SC_WII::getDevice(obj);
2115 // if (!dev) return errFailed;
2117 // err = slotIntVal(args+1, &enable1);
2118 // if (err) return err;
2120 // if (dev->m_wiiremote == NULL)
2121 // {
2122 // return errFailed;
2123 // }
2124 // else
2125 // {
2126 // // #ifdef SC_DARWIN
2127 // // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2128 // // #endif
2129 // #ifdef SC_LINUX
2130 // if ( enable1 )
2131 // wiimote_speaker_enable(dev->m_wiiremote);
2132 // else
2133 // wiimote_speaker_disable(dev->m_wiiremote);
2135 // // post( "WII: speaker enable %i", enable1 );
2137 // #endif
2138 // }
2140 // return errNone;
2141 // }
2143 // int prWiiInitSpeaker(VMGlobals *g, int numArgsPushed)
2144 // {
2145 // PyrSlot* args = g->sp - 1;
2146 // int err;
2147 // int enable1;
2148 // bool result;
2150 // PyrObject* obj = SC_WII::getObject(args+0);
2151 // if (!obj) return errWrongType;
2153 // SC_WII* dev = SC_WII::getDevice(obj);
2154 // if (!dev) return errFailed;
2156 // err = slotIntVal(args+1, &enable1);
2157 // if (err) return err;
2159 // if (dev->m_wiiremote == NULL)
2160 // {
2161 // return errFailed;
2162 // }
2163 // else
2164 // {
2165 // // #ifdef SC_DARWIN
2166 // // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2167 // // #endif
2168 // #ifdef SC_LINUX
2169 // if ( enable1 )
2170 // dev->speaker_init( 4 );
2171 // else
2172 // dev->speaker_init( 8 );
2174 // // post( "WII: speaker init %i", enable1 );
2176 // #endif
2177 // }
2179 // return errNone;
2180 // }
2182 // int prWiiMuteSpeaker(VMGlobals *g, int numArgsPushed)
2183 // {
2184 // PyrSlot* args = g->sp - 1;
2185 // int err;
2186 // int enable1;
2187 // bool result;
2189 // PyrObject* obj = SC_WII::getObject(args+0);
2190 // if (!obj) return errWrongType;
2192 // SC_WII* dev = SC_WII::getDevice(obj);
2193 // if (!dev) return errFailed;
2195 // err = slotIntVal(args+1, &enable1);
2196 // if (err) return err;
2198 // if (dev->m_wiiremote == NULL)
2199 // {
2200 // return errFailed;
2201 // }
2202 // else
2203 // {
2204 // // #ifdef SC_DARWIN
2205 // // result = wiiremote_motionsensor( dev->m_wiiremote, enable1 );
2206 // // #endif
2207 // #ifdef SC_LINUX
2208 // if ( enable1 )
2209 // wiimote_speaker_mute(dev->m_wiiremote);
2210 // else
2211 // wiimote_speaker_unmute(dev->m_wiiremote);
2213 // // post( "WII: mute %i", enable1 );
2214 // #endif
2215 // }
2217 // return errNone;
2218 // }
2220 void initWiiPrimitives()
2222 int base, index;
2224 s_wii = getsym("WiiMote");
2225 s_wiiCalibrationInfoClass = getsym("WiiCalibrationInfo"); // has calibration date for all axes
2226 // s_wiiLEDStateClass = getsym("WiiLEDState"); // has the four LED states
2228 // s_wiiRemoteClass = getsym("WiiRemote"); // Remote
2229 // s_wiiNunChuckClass = getsym("WiiNunChuck"); // NunChuck
2230 // s_wiiClassicClass = getsym("WiiClassic"); // Classic
2231 // s_wiiAction = getsym("prWiiMoteAction");
2233 s_wiiDisconnected = getsym("prDisconnectAction");
2234 s_wiiConnected = getsym("prConnectAction");
2235 s_readError = getsym("prReadError");
2237 /// general event on MacOSX
2238 s_handleEvent = getsym("prHandleEvent");
2240 /// separate events on Linux:
2241 s_handleBatteryEvent = getsym("prHandleBatteryEvent");
2242 s_handleExtensionEvent = getsym("prHandleExtensionEvent");
2243 s_handleButtonEvent = getsym("prHandleButtonEvent");
2244 s_handleNunchukEvent = getsym("prHandleNunchukEvent");
2245 s_handleClassicEvent = getsym("prHandleClassicEvent");
2246 s_handleIREvent = getsym("prHandleIREvent");
2247 s_handleAccEvent = getsym("prHandleAccEvent");
2249 base = nextPrimitiveIndex();
2250 index = 0;
2251 definePrimitive(base, index++, "_Wii_Start", prWii_Start, 2, 0); // starts the eventloop
2252 definePrimitive(base, index++, "_Wii_Discover", prWii_Discover, 3, 0); // discovers a new device
2253 definePrimitive(base, index++, "_Wii_Stop", prWii_Stop, 1, 0); // stops the eventloop
2255 definePrimitive(base, index++, "_Wii_Open", prWii_Open, 1, 0 );
2256 // definePrimitive(base, index++, "_Wii_Update", prWii_Update, 1, 0 );
2257 // definePrimitive(base, index++, "_Wii_UpdateData", prWii_UpdateData, 9, 0 );
2258 definePrimitive(base, index++, "_Wii_Close", prWii_Close, 1, 0 );
2260 definePrimitive(base, index++, "_Wii_Address", prWiiAddress, 1, 0);
2261 // definePrimitive(base, index++, "_Wii_SetAddress", prWiiSetAddress, 2, 0);
2263 definePrimitive(base, index++, "_Wii_Connect", prWiiConnect, 1, 0);
2264 definePrimitive(base, index++, "_Wii_Disconnect", prWiiDisconnect, 1, 0);
2266 definePrimitive(base, index++, "_Wii_Calibration", prWiiCalibration, 2, 0);
2268 // definePrimitive(base, index++, "_Wii_GetExpansion", prWiiGetExpansion, 1, 0);
2269 // definePrimitive(base, index++, "_Wii_GetBattery", prWiiGetBattery, 1, 0);
2271 // definePrimitive(base, index++, "_Wii_GetButtons", prWiiGetButtons, 2, 0);
2272 // definePrimitive(base, index++, "_Wii_GetMotion", prWiiGetMotion, 2, 0);
2273 // definePrimitive(base, index++, "_Wii_GetIR", prWiiGetIR, 2, 0);
2275 // definePrimitive(base, index++, "_Wii_GetNunchukButtons", prWiiGetNunchukButtons, 2, 0);
2276 // definePrimitive(base, index++, "_Wii_GetNunchukJoy", prWiiGetNunchukJoy, 2, 0);
2277 // definePrimitive(base, index++, "_Wii_GetNunchukMotion", prWiiGetNunchukMotion, 2, 0);
2279 // definePrimitive(base, index++, "_Wii_GetClassicButtons", prWiiGetClassicButtons, 2, 0);
2280 // definePrimitive(base, index++, "_Wii_GetClassicJoy", prWiiGetClassicJoy, 2, 0);
2281 // definePrimitive(base, index++, "_Wii_GetClassicAnalog", prWiiGetClassicAnalog, 2, 0);
2283 // definePrimitive(base, index++, "_Wii_GetLED", prWiiGetLED, 2, 0);
2285 definePrimitive(base, index++, "_Wii_SetLED", prWiiSetLED, 2, 0);
2287 definePrimitive(base, index++, "_Wii_SetVibration", prWiiSetVibration, 2, 0);
2289 // definePrimitive(base, index++, "_Wii_InitSpeaker", prWiiInitSpeaker, 2, 0);
2290 // definePrimitive(base, index++, "_Wii_PlaySpeaker", prWiiPlaySpeaker, 5, 0);
2291 // definePrimitive(base, index++, "_Wii_MuteSpeaker", prWiiMuteSpeaker, 2, 0);
2292 // definePrimitive(base, index++, "_Wii_EnableSpeaker", prWiiEnableSpeaker, 2, 0);
2294 definePrimitive(base, index++, "_Wii_Enable", prWiiEnable, 2, 0);
2295 definePrimitive(base, index++, "_Wii_EnableButtons", prWiiSetButtons, 2, 0);
2296 definePrimitive(base, index++, "_Wii_EnableIRSensor", prWiiSetIRSensor, 2, 0);
2297 definePrimitive(base, index++, "_Wii_EnableMotionSensor", prWiiSetMotionSensor, 2, 0);
2298 definePrimitive(base, index++, "_Wii_EnableExpansion", prWiiSetExpansion, 2, 0);
2301 #else // NOT HAVE_WII
2302 void initWiiPrimitives()
2304 //other platforms?
2306 #endif