TACHYON V4 IFDEF Wii_nunchuk @rest org FORGET Wii_nunchuk } pub Wii_nunchuk ." Wii_nunchuk 2017/07/18 21:38:15 " ; @org W@ == @rest --- remember { 2017/06/16 20:29:32 Translated Wii_nunchuk to Tachyon Wii nunchuk Propeller clk P28 dat P29 Diagram below is showing the pinout looking into the connector (which plugs into the Wii Remote) _______ | 1 2 3 | | | | 6 5 4 | |_-----_| Peopeller 1 - SDA --------- P29 2 - 3 - VCC --------- 3.3V 4 - SCL --------- P28 5 - 6 - GND --------- GND Cutted connecter. 2012/8/8 VCC(Red) --------- 3.3V GND (White) --------- GND SDA(Green) --------- P29 SCL(Yellow) --------- P28 } \ =========================================================================== \ Constants \ =========================================================================== \ Slave addres h52 for Wii-nunchaku \ $A4 == slaveAddr+wr #127 == cal_joy_x #135 == cal_joy_y #200 == cal_z #40000000 == 500msec \ =========================================================================== \ Variables \ =========================================================================== long sx long sy long az word ax word ay byte cb byte z \ =========================================================================== \ i2c command \ =========================================================================== : err_msg ." I2C error" ; : err? IF err_msg CR ENDIF ; \ Scan list for i2c-line \ ( -- ) : i2cScan 6 SPACES ." 0 1 2 3 4 5 6 7 8 9 A B C D E F" CR 0 #16 BY 8 FOR I .BYTE 3 SPACES #16 FOR SWAP $78 < AND \ Count up device (eliminate reserve number) IF 1+ ENDIF ENDIF SPACE NEXT CR NEXT I2C> ." I2C devices:" . CR ; \ =========================================================================== \ Main \ =========================================================================== \ Write data to Wii-nunchuk \ ( n1 n2 -- t/f ) n1:data n2:register : wr_nunchuk ; \ Initialize Wii-nunchuku \ ( -- ) : init_nunchuk $55 $F0 wr_nunchuk 100 ms 0 $FB wr_nunchuk OR err? ; \ Read data for Wii-nunchuk \ ( -- n1-n6 t/f ) n1-n6:data(6bytes) t/f: true if there are error : readNunchuk err? 1 ms ; \ Calculate data[joy_x,joy_y,ax,ay,az,c-button,z-button] \ ( n1..n6 -- ) n1..n6:6bytes : get_value DUP 1 AND z C! \ z-button DUP 2 AND cb C! \ c-button 2 SHR DUP 3 AND ax W! 2 SHR DUP 3 AND ay W! 2 SHR 3 AND az ! 2 SHL az @ + cal_z - az ! 2 SHL ay W@ + ay W! 2 SHL ax W@ + ax W! cal_joy_y - sy ! cal_joy_x - sx ! ; : test_Nunchuk I2C100 init_nunchuk #25 ms BEGIN readNunchuk get_value ." SX:" sx @ . TAB ." SY:" sy @ . TAB ." AX:" ax W@ . TAB ." AY:" ay W@ . TAB ." AZ:" az @ . TAB ." c:" cb C@ . TAB ." z:" z C@ . CR #100 ms KEY UNTIL ; pub tt ( -- ) test_Nunchuk ; END \ ?BACKUP