NXT/Simple.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 114 CC 14 SCAR 27.0
CC
14
Cov
0%
init_sensor
30.0
motor_on
20.0
precise_motor_on
20.0
CRAP 14.9
1 package LEGO::NXT::Simple;
2  
3 use LEGO::NXT;
4 use LEGO::NXT::Constants qw(:DEFAULT);
5  
6 my %motormap = {
7 'A' => $NXT_MOTOR_A,
8 'B' => $NXT_MOTOR_B,
9 'C' => $NXT_MOTOR_C
10 };
11  
12 my %sensormap = {
13 1 => $NXT_SENSOR_1,
14 2 => $NXT_SENSOR_2,
15 3 => $NXT_SENSOR_3,
16 4 => $NXT_SENSOR_4
17 };
18  
19 my %sensmap = {
20 'ultrasound' => \&sensor_value_ultrasound,
21 'light' => \&sensor_value_light,
22 'touch' => \&sensor_value_touch,
23 'sound' => \&sensor_value_sound
24 };
25  
26 sub new
27 {
28 my ($pkg,$comm) = @_;
29
30 my $this = {
31 'nxt' => new LEGO::NXT($comm),
32 'motors' => {'A'=>undef,'B'=>undef,'C'=>undef},
33 'sensors' => {1=>undef,2=>undef,3=>undef,4=>undef}
34 };
35
36 bless $this, $pkg;
37 return $this;
38 }
39  
40  
41 sub motor_on
42 {
43 my ($this,$motor,$speed,$ticks) = @_;
44  
45 return if !exists $motormap{$motor};
46 return if ($speed<-100 or speed>100);
47  
48 $ticks=0 if undef $ticks;
49  
50 $this->{nxt}->set_output_state($NXT_NORET, $motormap{$motor}, $speed, $NXT_MOTOR_ON|$NXT_REGULATED, $NXT_REGULATION_MODE_MOTOR_SPEED, 0, $NXT_MOTOR_RUN_STATE_RUNNING, $ticks );
51 }
52  
53 sub motor_off
54 {
55 my ($this,$motor) = @_;
56  
57 return if !exists $motormap{$motor};
58  
59 $this->{nxt}->set_output_state($NXT_NORET, $motormap{$motor}, 0, $NXT_MOTOR_ON|$NXT_REGULATED, $NXT_REGULATION_MODE_IDLE, 0, $NXT_MOTOR_RUN_STATE_IDLE, 0 );
60 }
61  
62 sub precise_motor_on
63 {
64 my ($this,$motor,$speed,$ticks) = @_;
65  
66 return if !exists $motormap{$motor};
67 return if ($speed<-100 or speed>100);
68  
69 $ticks=0 if undef $ticks;
70 $this->{nxt}->set_output_state($NXT_NORET, $motormap{$motor}, $speed, $NXT_MOTOR_ON|$NXT_REGULATED|$NXT_BRAKE, $NXT_REGULATION_MODE_MOTOR_SPEED, 0, $NXT_MOTOR_RUN_STATE_RUNNING, $ticks );
71 }
72  
73 sub init_sensor
74 {
75 my ($this,$port,$sensor_type,%opts) = @_;
76  
77 $this->{sensors}->{$port} = {'type'=>$sensor_type};
78  
79 if ($sensor_type eq 'ultrasound')
80 {
81 $bt->initialize_ultrasound_port($port);
82 }
83  
84 elsif ($sensor_type eq 'light')
85 {
86 $bt->set_input_state($NXT_NORET,$NXT_LIGHT_ACTIVE,$NXT_RAW_MODE);
87 }
88  
89 elsif ($sensor_type eq 'touch')
90 {
91 $bt->set_input_state($NXT_NORET,$NXT_SWITCH,$NXT_BOOLEAN_MODE);
92 }
93  
94 elsif ($sensor_type eq 'sound' )
95 {
96 $bt->set_input_state($NXT_NORET,$NXT_SOUND_DBA,$NXT_BOOLEAN_MODE);
97 }
98 }
99  
100 sub sensor_value
101 {
102 my ($this,$port) = @_;
103  
104 return if $this->{sensors}->{$port} == undef;
105  
106 my $type = $this->{sensors}->{$port}->{type};
107  
108 return if !$sensmap{$type};
109  
110 $sensmap{$type}->($this,$port);
111 }
112  
113 sub sensor_value_ultrasound
114 {
115 my ($this,$port) = @_;
116  
117 $res = $this->{nxt}->get_ultrasound_measurement_byte($portmap{$port},0);
118  
119 $res->{data};
120 }
121  
122 sub sensor_value_light
123 {
124 my ($this,$port) = @_;
125  
126 $res = $this->{nxt}->get_input_values($NXT_RET,$port);
127  
128 $res->{data};
129 }
130  
131 sub sensor_value_touch
132 {
133 my ($this,$port) = @_;
134  
135 $res = $this->{nxt}->get_input_values($NXT_RET,$port);
136  
137 $res->{data};
138 }
139  
140 sub get_nxt
141 {
142 my $this = shift;
143 $this->{nxt};
144 }
145  
146  
147 1;