| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package MyWii; |
|---|
| 5 | |
|---|
| 6 | use base qw/ Class::Accessor::Fast Mac::WiiRemote::Client /; |
|---|
| 7 | __PACKAGE__->mk_accessors(qw/ |
|---|
| 8 | path |
|---|
| 9 | rf_base rb_base lf_base lb_base |
|---|
| 10 | rf_last rb_last lf_last lb_last |
|---|
| 11 | x_last |
|---|
| 12 | last_scroll_time |
|---|
| 13 | /); |
|---|
| 14 | |
|---|
| 15 | sub send_event { |
|---|
| 16 | my($self, $msg) = @_; |
|---|
| 17 | return if $self->last_scroll_time + 1 > time; |
|---|
| 18 | |
|---|
| 19 | warn "GO TO: $msg"; |
|---|
| 20 | $self->last_scroll_time(time); |
|---|
| 21 | |
|---|
| 22 | open my $fh, '>', $self->path or return; |
|---|
| 23 | print $fh $msg; |
|---|
| 24 | close $fh; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | sub left { shift->send_event('left') } |
|---|
| 28 | sub right { shift->send_event('right') } |
|---|
| 29 | sub up { shift->send_event('left') } |
|---|
| 30 | sub down { shift->send_event('right') } |
|---|
| 31 | sub home { shift->send_event('home') } |
|---|
| 32 | sub end { shift->send_event('end') } |
|---|
| 33 | |
|---|
| 34 | sub connected { |
|---|
| 35 | my($self, $c, $remote) = @_; |
|---|
| 36 | |
|---|
| 37 | for my $bl qw( base last ) { |
|---|
| 38 | for my $rl qw( r l ) { |
|---|
| 39 | for my $fb qw( f b ) { |
|---|
| 40 | my $meth = "${rl}${fb}_${bl}"; |
|---|
| 41 | $self->$meth(0); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | $self->x_last(0); |
|---|
| 47 | $self->last_scroll_time(time); |
|---|
| 48 | warn "connect"; |
|---|
| 49 | |
|---|
| 50 | # $c->remote->setLEDEnabled1_enabled2_enabled3_enabled4(1,0,0,1); LED |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | sub expansion_port_changed { |
|---|
| 54 | my($self, $c, $remote) = @_; |
|---|
| 55 | return unless $remote->address eq $c->remote->address; |
|---|
| 56 | |
|---|
| 57 | if ($remote->isExpansionPortAttached) { |
|---|
| 58 | warn "Expansion ENABLE"; |
|---|
| 59 | $remote->setExpansionPortEnabled(1); |
|---|
| 60 | } else { |
|---|
| 61 | warn "Expansion DISABLE"; |
|---|
| 62 | $remote->setExpansionPortEnabled(0); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | sub button_changed { |
|---|
| 67 | my($self, $c, $button, $is_pressed) = @_; |
|---|
| 68 | |
|---|
| 69 | warn "$button, $is_pressed"; |
|---|
| 70 | |
|---|
| 71 | $self->rf_base($self->rf_last); |
|---|
| 72 | $self->rb_base($self->rb_last); |
|---|
| 73 | $self->lf_base($self->lf_last); |
|---|
| 74 | $self->lb_base($self->lb_last); |
|---|
| 75 | |
|---|
| 76 | if (!$is_pressed) { |
|---|
| 77 | $self->right if $button eq 10; |
|---|
| 78 | $self->left if $button eq 9; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sub acceleration_changed { |
|---|
| 83 | my($self, $c, $d, $x, $y, $z) = @_; |
|---|
| 84 | # warn $self->remote->expansionPortType; |
|---|
| 85 | # 右加速でx減少、左加速でx加算 |
|---|
| 86 | |
|---|
| 87 | $self->right if $x > $self->x_last + 100; |
|---|
| 88 | $self->left if $x < $self->x_last - 100; |
|---|
| 89 | |
|---|
| 90 | $self->x_last($x); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | sub barance_wii_board_changed { |
|---|
| 94 | my($self, $c, $zero, $rf, $rb, $lf, $lb) = @_; |
|---|
| 95 | # my $calib = $self->remote->calcWeightRf_rb_lf_lb($rf, $rb, $lf, $lb); |
|---|
| 96 | |
|---|
| 97 | $self->rf_last($rf); |
|---|
| 98 | $self->rb_last($rb); |
|---|
| 99 | $self->lf_last($lf); |
|---|
| 100 | $self->lb_last($lb); |
|---|
| 101 | |
|---|
| 102 | my $weight_rf = $rf - $self->rf_base; |
|---|
| 103 | my $weight_rb = $rb - $self->rb_base; |
|---|
| 104 | my $weight_lf = $lf - $self->lf_base; |
|---|
| 105 | my $weight_lb = $lb - $self->lb_base; |
|---|
| 106 | |
|---|
| 107 | if (($weight_lf * 5) < $weight_rf && ($weight_lb * 5) < $weight_rb && ($weight_rf + $weight_rb) > 1000 && !$self->{fit_is_pressed}) { |
|---|
| 108 | $self->{fit_is_pressed} = 1; |
|---|
| 109 | $self->right; |
|---|
| 110 | } elsif (($weight_rf * 5) < $weight_lf && ($weight_rb * 5) < $weight_lb && ($weight_lf + $weight_lb) > 1000 && !$self->{fit_is_pressed}) { |
|---|
| 111 | $self->{fit_is_pressed} = 1; |
|---|
| 112 | $self->left |
|---|
| 113 | } elsif (($weight_rf + $weight_rb + $weight_lf + $weight_lb) < 1000) { |
|---|
| 114 | $self->{fit_is_pressed} = 0; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | sub disconnected { |
|---|
| 119 | my($self, $c, $device) = @_; |
|---|
| 120 | $c->remote->release; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | package main; |
|---|
| 124 | |
|---|
| 125 | use Mac::WiiRemote; |
|---|
| 126 | |
|---|
| 127 | my $wii = Mac::WiiRemote->new( MyWii->new({ path => '/tmp/devsumi-ctl.txt' }) ); |
|---|
| 128 | NSRunLoop->currentRunLoop->run; |
|---|