mi100 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/mi100/version.rb +3 -3
  2. data/lib/mi100.rb +367 -315
  3. data/spec/mi100_spec.rb +408 -301
  4. metadata +8 -8
data/spec/mi100_spec.rb CHANGED
@@ -1,301 +1,408 @@
1
- require 'spec_helper'
2
-
3
- describe Mi100 do
4
-
5
- it 'should have a version number' do
6
- expect(Mi100::VERSION).not_to be nil
7
- end
8
-
9
- before :each do
10
- Mi100.any_instance.stub(:initialize_serialport)
11
- end
12
-
13
- it 'should create the instance' do
14
- mi100 = Mi100.new "COM5"
15
- expect(mi100).to be_a Mi100
16
- end
17
-
18
- it 'should send "H" on :ping' do
19
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_PING,1234])
20
- mi100 = Mi100.new "COM5"
21
- expect(mi100.ping).to eq(["H",1234])
22
- expect(mi100).to have_received(:send_command_get_response).with("H")
23
- end
24
-
25
- it 'should send "H" and return rounded power level on :power' do
26
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_GET_POWER_LEVEL,1188])
27
- mi100 = Mi100.new "COM5"
28
- expect(mi100.power).to eq(1.19)
29
- expect(mi100).to have_received(:send_command_get_response).with("H")
30
- end
31
-
32
- it 'should send "P" and get light level on :light' do
33
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_GET_LIGHT,1188])
34
- mi100 = Mi100.new "COM5"
35
- expect(mi100.light).to eq(1188)
36
- expect(mi100).to have_received(:send_command_get_response).with("P")
37
- end
38
-
39
- it 'should send "F,duration" on :move with "FORWARD"' do
40
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_FORWARD,1234])
41
- mi100 = Mi100.new "COM5"
42
- expect(mi100.move "FORWARD").to eq(["F",1234])
43
- expect(mi100).to have_received(:send_command_get_response).with("F,300")
44
- end
45
-
46
- it 'should send "B,duration" on move "BACKWARD"' do
47
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_BACKWARD,1234])
48
- mi100 = Mi100.new "COM5"
49
- expect(mi100.move "BACKWARD").to eq(["B",1234])
50
- expect(mi100).to have_received(:send_command_get_response).with("B,300")
51
- end
52
-
53
- it 'should send "R,140" on spin "RIGHT"' do
54
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_RIGHT,1234])
55
- mi100 = Mi100.new "COM5"
56
- expect(mi100.spin "RIGHT").to eq(["R",1234])
57
- expect(mi100).to have_received(:send_command_get_response).with("R,140")
58
- end
59
-
60
- it 'should send "L,140" on spin "LEFT"' do
61
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_LEFT,1234])
62
- mi100 = Mi100.new "COM5"
63
- expect(mi100.spin "LEFT").to eq(["L",1234])
64
- expect(mi100).to have_received(:send_command_get_response).with("L,140")
65
- end
66
-
67
- it 'should send "F,duration" on move_forward duration' do
68
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_FORWARD,1234])
69
- mi100 = Mi100.new "COM5"
70
- expect(mi100.move_forward 500).to eq(["F",1234])
71
- expect(mi100).to have_received(:send_command_get_response).with("F,500")
72
- end
73
-
74
- it 'should send "B,duration" move_backward duration' do
75
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_BACKWARD,1234])
76
- mi100 = Mi100.new "COM5"
77
- expect(mi100.move_backward 500).to eq(["B",1234])
78
- expect(mi100).to have_received(:send_command_get_response).with("B,500")
79
- end
80
-
81
- it 'should send "R,duration" spin_right duration' do
82
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_RIGHT,1234])
83
- mi100 = Mi100.new "COM5"
84
- expect(mi100.spin_right 500).to eq(["R",1234])
85
- expect(mi100).to have_received(:send_command_get_response).with("R,500")
86
- end
87
-
88
- it 'should send "L,duration" on spin_left duration' do
89
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_LEFT,1234])
90
- mi100 = Mi100.new "COM5"
91
- expect(mi100.spin_left 500).to eq(["L",1234])
92
- expect(mi100).to have_received(:send_command_get_response).with("L,500")
93
- end
94
-
95
- it 'should send "U,duration" on turn_right duration' do
96
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TURN_RIGHT,1234])
97
- mi100 = Mi100.new "COM5"
98
- expect(mi100.turn_right 500).to eq(["U",1234])
99
- expect(mi100).to have_received(:send_command_get_response).with("U,500")
100
- end
101
-
102
- it 'should send "A,duration" on turn_left duration' do
103
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TURN_LEFT,1234])
104
- mi100 = Mi100.new "COM5"
105
- expect(mi100.turn_left 500).to eq(["A",1234])
106
- expect(mi100).to have_received(:send_command_get_response).with("A,500")
107
- end
108
-
109
- it 'should send "F, 300" on movef' do
110
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_FORWARD,1234])
111
- mi100 = Mi100.new "COM5"
112
- expect(mi100.movef).to eq(["F",1234])
113
- expect(mi100).to have_received(:send_command_get_response).with("F,300")
114
- end
115
-
116
- it 'should send "B,300" on moveb' do
117
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_MOVE_BACKWARD,1234])
118
- mi100 = Mi100.new "COM5"
119
- expect(mi100.moveb).to eq(["B",1234])
120
- expect(mi100).to have_received(:send_command_get_response).with("B,300")
121
- end
122
-
123
- it 'should send "R,140" spinr' do
124
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_RIGHT,1234])
125
- mi100 = Mi100.new "COM5"
126
- expect(mi100.spinr).to eq(["R",1234])
127
- expect(mi100).to have_received(:send_command_get_response).with("R,140")
128
- end
129
-
130
- it 'should send "L,140" on spinl' do
131
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SPIN_LEFT,1234])
132
- mi100 = Mi100.new "COM5"
133
- expect(mi100.spinl).to eq(["L",1234])
134
- expect(mi100).to have_received(:send_command_get_response).with("L,140")
135
- end
136
-
137
- it 'should send "F,duration" on move_forward! duration in async (via sendln)' do
138
- Mi100.any_instance.stub(:sendln => 6)
139
- mi100 = Mi100.new "COM5"
140
- expect(mi100.move_forward! 500).to eq(6)
141
- expect(mi100).to have_received(:sendln).with("F,500")
142
- end
143
-
144
- it 'should send "B,duration" on move_backward! duration in async' do
145
- Mi100.any_instance.stub(:sendln => 6)
146
- mi100 = Mi100.new "COM5"
147
- expect(mi100.move_backward! 500).to eq(6)
148
- expect(mi100).to have_received(:sendln).with("B,500")
149
- end
150
-
151
- it 'should send "R,duration" on spin_right! duration in async' do
152
- Mi100.any_instance.stub(:sendln => 6)
153
- mi100 = Mi100.new "COM5"
154
- expect(mi100.spin_right! 500).to eq(6)
155
- expect(mi100).to have_received(:sendln).with("R,500")
156
- end
157
-
158
- it 'should send "L,duration" on spin_left! duration in async' do
159
- Mi100.any_instance.stub(:sendln => 6)
160
- mi100 = Mi100.new "COM5"
161
- expect(mi100.spin_left! 500).to eq(6)
162
- expect(mi100).to have_received(:sendln).with("L,500")
163
- end
164
-
165
- it 'should send "U,duration" on turn_right! duration in async' do
166
- Mi100.any_instance.stub(:sendln => 6)
167
- mi100 = Mi100.new "COM5"
168
- expect(mi100.turn_right! 500).to eq(6)
169
- expect(mi100).to have_received(:sendln).with("U,500")
170
- end
171
-
172
- it 'should send "A,duration" on turn_left! duration in async' do
173
- Mi100.any_instance.stub(:sendln => 6)
174
- mi100 = Mi100.new "COM5"
175
- expect(mi100.turn_left! 500).to eq(6)
176
- expect(mi100).to have_received(:sendln).with("A,500")
177
- end
178
-
179
- it 'should send "W,pwm_value" on speed pwm_value' do
180
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SET_SPEED,1234])
181
- mi100 = Mi100.new "COM5"
182
- expect(mi100.speed 500).to eq(["W",1234])
183
- expect(mi100).to have_received(:send_command_get_response).with("W,500")
184
- end
185
-
186
- it 'should reset default speed' do
187
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_SET_SPEED,1234])
188
- mi100 = Mi100.new "COM5"
189
- expect(mi100.speed).to eq(["W",1234])
190
- expect(mi100).to have_received(:send_command_get_response).with("W,1023")
191
- end
192
-
193
- it 'should send "M" and get free ram memory area on :free_ram' do
194
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_FREE_RAM,1234])
195
- mi100 = Mi100.new "COM5"
196
- expect(mi100.free_ram).to eq(["M",1234])
197
- expect(mi100).to have_received(:send_command_get_response).with("M")
198
- end
199
-
200
- it 'should send "D,100, 50, 50,duration on blink 100,50,50,duration' do
201
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_BLINK_LED,1234])
202
- mi100 = Mi100.new "COM5"
203
- expect(mi100.blink 100,50,50,100).to eq(["D",1234])
204
- expect(mi100).to have_received(:send_command_get_response).with("D,100,50,50,100")
205
- end
206
-
207
- it 'should send "S" on stop' do
208
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_STOP,1234])
209
- mi100 = Mi100.new "COM5"
210
- expect(mi100.stop).to eq(["S",1234])
211
- expect(mi100).to have_received(:send_command_get_response).with("S")
212
- end
213
-
214
- it 'should send "T,random frequency, 300" on tone' do
215
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TONE,1234])
216
- mi100 = Mi100.new "COM5"
217
- expect(mi100.tone).to eq(["T",1234])
218
- expect(mi100).to have_received(:send_command_get_response).with(/T,\d{2,4},300/)
219
- end
220
-
221
- it 'should limit minimum tone frequency to 28 on tome' do
222
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TONE,1234])
223
- mi100 = Mi100.new "COM5"
224
- mi100.tone 20, 500
225
- expect(mi100).to have_received(:send_command_get_response).with("T,28,500")
226
- end
227
-
228
- it 'should send "T,440,1000" on sound "la", 1000' do
229
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TONE,1234])
230
- mi100 = Mi100.new "COM5"
231
- mi100.sound "la",1000
232
- expect(mi100).to have_received(:send_command_get_response).with("T,440,1000")
233
- end
234
-
235
- it 'sound should not call tone with false frequency' do
236
- mi100 = Mi100.new "COM5"
237
- mi100.stub(:tone)
238
- mi100.sound false, 300
239
- expect(mi100).not_to have_received(:tone)
240
- end
241
-
242
- it 'should send three good tones on good' do
243
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TONE,1234])
244
- mi100 = Mi100.new "COM5"
245
- mi100.good
246
- expect(mi100).to have_received(:send_command_get_response).exactly(3).times
247
- end
248
-
249
- it 'should send one bad tone on bad' do
250
- Mi100.any_instance.stub(:send_command_get_response => [Mi100::CMD_TONE,1234])
251
- mi100 = Mi100.new "COM5"
252
- mi100.bad
253
- expect(mi100).to have_received(:send_command_get_response).once
254
- end
255
-
256
- it 'should talk morse code' do
257
- mi100 = Mi100.new "COM5"
258
- mi100.stub(:tone)
259
- mi100.talk "sos"
260
- expect(mi100).to have_received(:tone).exactly(6).times.with(4000,50)
261
- expect(mi100).to have_received(:tone).exactly(3).times.with(4000,150)
262
- end
263
-
264
- it 'should access to morse_frequency' do
265
- mi100 = Mi100.new "COM5"
266
- expect(mi100.morse_frequency).to eq(4000)
267
- expect(mi100.morse_frequency = 440).to eq(440)
268
- expect(mi100.morse_frequency).to eq(440)
269
- end
270
-
271
- it 'should access to morse_unit' do
272
- mi100 = Mi100.new "COM5"
273
- expect(mi100.morse_unit).to eq(50)
274
- expect(mi100.morse_unit = 100).to eq(100)
275
- expect(mi100.morse_unit).to eq(100)
276
- end
277
-
278
- it 'should talk with new default frequency and unit' do
279
- mi100 = Mi100.new "COM5"
280
- mi100.stub(:tone)
281
- mi100.talk "sos"
282
- expect(mi100).to have_received(:tone).exactly(6).times.with(440,100)
283
- expect(mi100).to have_received(:tone).exactly(3).times.with(440,300)
284
- end
285
-
286
- it 'should reset default frequency and unit' do
287
- Mi100::Morsecoder.reset
288
- expect(Mi100::Morsecoder.default_frequency).to eq(4000)
289
- expect(Mi100::Morsecoder.default_unit).to eq(50)
290
- mi100 = Mi100.new "COM5"
291
- expect(mi100.morse_frequency).to eq(4000)
292
- expect(mi100.morse_unit).to eq(50)
293
- end
294
-
295
- it 'should provide Morsecode.to_morse_from class method converting the string to the morse code array' do
296
- morsecode = Mi100::Morsecoder.to_morse_from "sos"
297
- expect(morsecode.size).to eq(21)
298
- expect(morsecode[0]).to eq ({:frequency=>4000, :duration=>50})
299
- end
300
-
301
- end
1
+ require 'spec_helper'
2
+
3
+ describe Mi100 do
4
+
5
+ it 'should have a version number' do
6
+ expect(Mi100::VERSION).not_to be nil
7
+ end
8
+
9
+ before :each do
10
+ allow_any_instance_of(Mi100).to receive(:initialize_serialport)
11
+ allow_any_instance_of(Mi100).to receive(:initialize_robo)
12
+ end
13
+
14
+ it 'should create the instance' do
15
+ mi100 = Mi100.new "COM5"
16
+ expect(mi100).to be_a Mi100
17
+ end
18
+
19
+ it 'should send "H" on :ping' do
20
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_PING,1234])
21
+ mi100 = Mi100.new "COM5"
22
+ expect(mi100.ping).to eq(["H",1234])
23
+ expect(mi100).to have_received(:send_command_get_response).with("H")
24
+ end
25
+
26
+ it 'should send "H" and return rounded power level on :power' do
27
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_GET_POWER_LEVEL,1188])
28
+ mi100 = Mi100.new "COM5"
29
+ expect(mi100.power).to eq(1.19)
30
+ expect(mi100).to have_received(:send_command_get_response).with("H")
31
+ end
32
+
33
+ it 'should send "P" and get light level on :light' do
34
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_GET_LIGHT,1188])
35
+ mi100 = Mi100.new "COM5"
36
+ expect(mi100.light).to eq(1188)
37
+ expect(mi100).to have_received(:send_command_get_response).with("P")
38
+ end
39
+
40
+ it 'should send "F,duration" on move "FORWARD"' do
41
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_FORWARD,1234])
42
+ mi100 = Mi100.new "COM5"
43
+ expect(mi100.move "FORWARD").to eq(["F",1234])
44
+ expect(mi100).to have_received(:send_command_get_response).with("F,300")
45
+ end
46
+
47
+ it 'should send "B,duration" on move "BACKWARD"' do
48
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_BACKWARD,1234])
49
+ mi100 = Mi100.new "COM5"
50
+ expect(mi100.move "BACKWARD").to eq(["B",1234])
51
+ expect(mi100).to have_received(:send_command_get_response).with("B,300")
52
+ end
53
+
54
+ it 'should send "R,140" on spin "RIGHT"' do
55
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_RIGHT,1234])
56
+ mi100 = Mi100.new "COM5"
57
+ expect(mi100.spin "RIGHT").to eq(["R",1234])
58
+ expect(mi100).to have_received(:send_command_get_response).with("R,140")
59
+ end
60
+
61
+ it 'should send "L,140" on spin "LEFT"' do
62
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_LEFT,1234])
63
+ mi100 = Mi100.new "COM5"
64
+ expect(mi100.spin "LEFT").to eq(["L",1234])
65
+ expect(mi100).to have_received(:send_command_get_response).with("L,140")
66
+ end
67
+
68
+ it 'should send "F,duration" on move_forward duration' do
69
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_FORWARD,1234])
70
+ mi100 = Mi100.new "COM5"
71
+ expect(mi100.move_forward 500).to eq(["F",1234])
72
+ expect(mi100).to have_received(:send_command_get_response).with("F,500")
73
+ end
74
+
75
+ it 'should send "B,duration" move_backward duration' do
76
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_BACKWARD,1234])
77
+ mi100 = Mi100.new "COM5"
78
+ expect(mi100.move_backward 500).to eq(["B",1234])
79
+ expect(mi100).to have_received(:send_command_get_response).with("B,500")
80
+ end
81
+
82
+ it 'should send "R,duration" spin_right duration' do
83
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_RIGHT,1234])
84
+ mi100 = Mi100.new "COM5"
85
+ expect(mi100.spin_right 500).to eq(["R",1234])
86
+ expect(mi100).to have_received(:send_command_get_response).with("R,500")
87
+ end
88
+
89
+ it 'should send "L,duration" on spin_left duration' do
90
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_LEFT,1234])
91
+ mi100 = Mi100.new "COM5"
92
+ expect(mi100.spin_left 500).to eq(["L",1234])
93
+ expect(mi100).to have_received(:send_command_get_response).with("L,500")
94
+ end
95
+
96
+ it 'should send "U,duration" on turn_right duration' do
97
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TURN_RIGHT,123])
98
+ mi100 = Mi100.new "COM5"
99
+ expect(mi100.turn_right 500).to eq(["U",123])
100
+ expect(mi100).to have_received(:send_command_get_response).with("U,500")
101
+ end
102
+
103
+ it 'should send "A,duration" on turn_left duration' do
104
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TURN_LEFT,123])
105
+ mi100 = Mi100.new "COM5"
106
+ expect(mi100.turn_left 500).to eq(["A",123])
107
+ expect(mi100).to have_received(:send_command_get_response).with("A,500")
108
+ end
109
+
110
+ it 'should send "F, 300" on movef' do
111
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_FORWARD,1234])
112
+ mi100 = Mi100.new "COM5"
113
+ expect(mi100.movef).to eq(["F",1234])
114
+ expect(mi100).to have_received(:send_command_get_response).with("F,300")
115
+ end
116
+
117
+ it 'should send "B,300" on moveb' do
118
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_MOVE_BACKWARD,1234])
119
+ mi100 = Mi100.new "COM5"
120
+ expect(mi100.moveb).to eq(["B",1234])
121
+ expect(mi100).to have_received(:send_command_get_response).with("B,300")
122
+ end
123
+
124
+ it 'should send "R,140" spinr' do
125
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_RIGHT,1234])
126
+ mi100 = Mi100.new "COM5"
127
+ expect(mi100.spinr).to eq(["R",1234])
128
+ expect(mi100).to have_received(:send_command_get_response).with("R,140")
129
+ end
130
+
131
+ it 'should send "L,140" on spinl' do
132
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SPIN_LEFT,1234])
133
+ mi100 = Mi100.new "COM5"
134
+ expect(mi100.spinl).to eq(["L",1234])
135
+ expect(mi100).to have_received(:send_command_get_response).with("L,140")
136
+ end
137
+
138
+ it 'should send "Z,right_speed,left_speed,duration" on drive right_speed, left_speed, duration' do
139
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
140
+ mi100 = Mi100.new "COM5"
141
+ expect(mi100.drive 123, 234, 56).to eq(["Z",1234])
142
+ expect(mi100).to have_received(:send_command_get_response).with("Z,123,234,56")
143
+ end
144
+
145
+ it 'should send "Z,right_speed,left_speed,duration" on drive right_speed, left_speed' do
146
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
147
+ mi100 = Mi100.new "COM5"
148
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
149
+ expect(mi100.drive 123, 234).to eq(["Z",1234])
150
+ expect(mi100).to have_received(:send_command_get_response).with("Z,123,234,50")
151
+ end
152
+
153
+ it 'should send "Z,right_speed,left_speed,duration" on drive_right turn_speed, speed, duration' do
154
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
155
+ mi100 = Mi100.new "COM5"
156
+ expect(mi100.drive_right 111, 123, 56).to eq(["Z",1234])
157
+ expect(mi100).to have_received(:send_command_get_response).with("Z,123,234,56")
158
+ end
159
+
160
+ it 'should send "Z,right_speed,left_speed,duration" on drive_right turn_speed' do
161
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
162
+ mi100 = Mi100.new "COM5"
163
+ mi100.instance_variable_set(:@speed, 123)
164
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
165
+ expect(mi100.drive_right 111).to eq(["Z",1234])
166
+ expect(mi100).to have_received(:send_command_get_response).with("Z,123,234,50")
167
+ end
168
+
169
+ it 'should send "Z,right_speed,left_speed,duration" on drive_left turn_speed, speed, duration' do
170
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
171
+ mi100 = Mi100.new "COM5"
172
+ expect(mi100.drive_left 111, 123, 56).to eq(["Z",1234])
173
+ expect(mi100).to have_received(:send_command_get_response).with("Z,234,123,56")
174
+ end
175
+
176
+ it 'should send "Z,right_speed,left_speed,duration" on drive_left turn_speed' do
177
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_DRIVE,1234])
178
+ mi100 = Mi100.new "COM5"
179
+ mi100.instance_variable_set(:@speed, 123)
180
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
181
+ expect(mi100.drive_left 111).to eq(["Z",1234])
182
+ expect(mi100).to have_received(:send_command_get_response).with("Z,234,123,50")
183
+ end
184
+
185
+ it 'should send "F,duration" on move_forward! duration in async (via sendln)' do
186
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
187
+ mi100 = Mi100.new "COM5"
188
+ expect(mi100.move_forward! 500).to eq(6)
189
+ expect(mi100).to have_received(:sendln).with("F,500")
190
+ end
191
+
192
+ it 'should send "B,duration" on move_backward! duration in async' do
193
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
194
+ mi100 = Mi100.new "COM5"
195
+ expect(mi100.move_backward! 500).to eq(6)
196
+ expect(mi100).to have_received(:sendln).with("B,500")
197
+ end
198
+
199
+ it 'should send "R,duration" on spin_right! duration in async' do
200
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
201
+ mi100 = Mi100.new "COM5"
202
+ expect(mi100.spin_right! 500).to eq(6)
203
+ expect(mi100).to have_received(:sendln).with("R,500")
204
+ end
205
+
206
+ it 'should send "L,duration" on spin_left! duration in async' do
207
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
208
+ mi100 = Mi100.new "COM5"
209
+ expect(mi100.spin_left! 500).to eq(6)
210
+ expect(mi100).to have_received(:sendln).with("L,500")
211
+ end
212
+
213
+ it 'should send "U,duration" on turn_right! duration in async' do
214
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
215
+ mi100 = Mi100.new "COM5"
216
+ expect(mi100.turn_right! 500).to eq(6)
217
+ expect(mi100).to have_received(:sendln).with("U,500")
218
+ end
219
+
220
+ it 'should send "A,duration" on turn_left! duration in async' do
221
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
222
+ mi100 = Mi100.new "COM5"
223
+ expect(mi100.turn_left! 500).to eq(6)
224
+ expect(mi100).to have_received(:sendln).with("A,500")
225
+ end
226
+
227
+ it 'should send "Z,right_speed,left_speed,duration" on drive! right_speed, left_speed, duration in async' do
228
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
229
+ mi100 = Mi100.new "COM5"
230
+ expect(mi100.drive! 123, 234, 56).to eq(6)
231
+ expect(mi100).to have_received(:sendln).with("Z,123,234,56")
232
+ end
233
+
234
+ it 'should send "Z,right_speed,left_speed,duration" on drive! right_speed, left_speed in async' do
235
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
236
+ mi100 = Mi100.new "COM5"
237
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
238
+ expect(mi100.drive! 123, 234).to eq(6)
239
+ expect(mi100).to have_received(:sendln).with("Z,123,234,50")
240
+ end
241
+
242
+ it 'should send "Z,right_speed,left_speed,duration" on drive_right! turn_speed, speed, duration in async' do
243
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
244
+ mi100 = Mi100.new "COM5"
245
+ expect(mi100.drive_right! 111, 123, 56).to eq(6)
246
+ expect(mi100).to have_received(:sendln).with("Z,123,234,56")
247
+ end
248
+
249
+ it 'should send "Z,right_speed,left_speed,duration" on drive_right! turn_speed in async' do
250
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
251
+ mi100 = Mi100.new "COM5"
252
+ mi100.instance_variable_set(:@speed, 123)
253
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
254
+ expect(mi100.drive_right! 111).to eq(6)
255
+ expect(mi100).to have_received(:sendln).with("Z,123,234,50")
256
+ end
257
+
258
+ it 'should send "Z,right_speed,left_speed,duration" on drive_left! turn_speed, speed, duration in async' do
259
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
260
+ mi100 = Mi100.new "COM5"
261
+ expect(mi100.drive_left! 111, 123, 56).to eq(6)
262
+ expect(mi100).to have_received(:sendln).with("Z,234,123,56")
263
+ end
264
+
265
+ it 'should send "Z,right_speed,left_speed,duration" on drive_left! turn_speed in async' do
266
+ allow_any_instance_of(Mi100).to receive(:sendln).and_return(6)
267
+ mi100 = Mi100.new "COM5"
268
+ mi100.instance_variable_set(:@speed, 123)
269
+ mi100.instance_variable_set(:@drive_duration, Mi100::DEFAULT_DRIVE_DURATION)
270
+ expect(mi100.drive_left! 111).to eq(6)
271
+ expect(mi100).to have_received(:sendln).with("Z,234,123,50")
272
+ end
273
+
274
+ it 'should send "W,pwm_value" on speed pwm_value' do
275
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SET_SPEED,1234])
276
+ mi100 = Mi100.new "COM5"
277
+ expect(mi100.speed 500).to eq(["W",1234])
278
+ expect(mi100).to have_received(:send_command_get_response).with("W,500")
279
+ end
280
+
281
+ it 'should reset default speed' do
282
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_SET_SPEED,1234])
283
+ mi100 = Mi100.new "COM5"
284
+ expect(mi100.speed Mi100::DEFAULT_SPEED).to eq(["W",1234])
285
+ expect(mi100).to have_received(:send_command_get_response).with("W,1023")
286
+ end
287
+
288
+ it 'should set drive_duration to 56mS' do
289
+ mi100 = Mi100.new "COM5"
290
+ expect(mi100.drive_duration 56).to eq(mi100.instance_variable_get(:@drive_duration))
291
+ end
292
+
293
+ it 'should send "M" and get free ram memory area on :free_ram' do
294
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_FREE_RAM,1234])
295
+ mi100 = Mi100.new "COM5"
296
+ expect(mi100.free_ram).to eq(["M",1234])
297
+ expect(mi100).to have_received(:send_command_get_response).with("M")
298
+ end
299
+
300
+ it 'should send "D,100,50,50,duration" on blink 100,50,50,duration' do
301
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_BLINK_LED,1234])
302
+ mi100 = Mi100.new "COM5"
303
+ expect(mi100.blink 100,50,50,100).to eq(["D",1234])
304
+ expect(mi100).to have_received(:send_command_get_response).with("D,100,50,50,100")
305
+ end
306
+
307
+ it 'should send "V,100,0,100" on turn_led 100,0,100' do
308
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TURN_LED_RGB,1234])
309
+ mi100 = Mi100.new "COM5"
310
+ expect(mi100.turn_led 100,0,100).to eq(["V",1234])
311
+ expect(mi100).to have_received(:send_command_get_response).with("V,100,0,100")
312
+ end
313
+
314
+ it 'should send "S" on stop' do
315
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_STOP,1234])
316
+ mi100 = Mi100.new "COM5"
317
+ expect(mi100.stop).to eq(["S",1234])
318
+ expect(mi100).to have_received(:send_command_get_response).with("S")
319
+ end
320
+
321
+ it 'should send "T,random frequency, 300" on tone' do
322
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TONE,1234])
323
+ mi100 = Mi100.new "COM5"
324
+ expect(mi100.tone).to eq(["T",1234])
325
+ expect(mi100).to have_received(:send_command_get_response).with(/T,\d{2,4},300/)
326
+ end
327
+
328
+ it 'should limit minimum tone frequency to 28 on tome' do
329
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TONE,1234])
330
+ mi100 = Mi100.new "COM5"
331
+ mi100.tone 20, 500
332
+ expect(mi100).to have_received(:send_command_get_response).with("T,28,500")
333
+ end
334
+
335
+ it 'should send "T,440,1000" on sound "la", 1000' do
336
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TONE,1234])
337
+ mi100 = Mi100.new "COM5"
338
+ mi100.sound "la",1000
339
+ expect(mi100).to have_received(:send_command_get_response).with("T,440,1000")
340
+ end
341
+
342
+ it 'sound should not call tone with false frequency' do
343
+ mi100 = Mi100.new "COM5"
344
+ allow(mi100).to receive(:tone)
345
+ mi100.sound false, 300
346
+ expect(mi100).not_to have_received(:tone)
347
+ end
348
+
349
+ it 'should send three good tones on good' do
350
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TONE,1234])
351
+ mi100 = Mi100.new "COM5"
352
+ mi100.good
353
+ expect(mi100).to have_received(:send_command_get_response).exactly(3).times
354
+ end
355
+
356
+ it 'should send one bad tone on bad' do
357
+ allow_any_instance_of(Mi100).to receive(:send_command_get_response).and_return([Mi100::CMD_TONE,1234])
358
+ mi100 = Mi100.new "COM5"
359
+ mi100.bad
360
+ expect(mi100).to have_received(:send_command_get_response).once
361
+ end
362
+
363
+ it 'should talk morse code' do
364
+ mi100 = Mi100.new "COM5"
365
+ allow(mi100).to receive(:tone)
366
+ mi100.talk "sos"
367
+ expect(mi100).to have_received(:tone).exactly(6).times.with(4000,50)
368
+ expect(mi100).to have_received(:tone).exactly(3).times.with(4000,150)
369
+ end
370
+
371
+ it 'should access to morse_frequency' do
372
+ mi100 = Mi100.new "COM5"
373
+ expect(mi100.morse_frequency).to eq(4000)
374
+ expect(mi100.morse_frequency = 440).to eq(440)
375
+ expect(mi100.morse_frequency).to eq(440)
376
+ end
377
+
378
+ it 'should access to morse_unit' do
379
+ mi100 = Mi100.new "COM5"
380
+ expect(mi100.morse_unit).to eq(50)
381
+ expect(mi100.morse_unit = 100).to eq(100)
382
+ expect(mi100.morse_unit).to eq(100)
383
+ end
384
+
385
+ it 'should talk with new default frequency and unit' do
386
+ mi100 = Mi100.new "COM5"
387
+ allow(mi100).to receive(:tone)
388
+ mi100.talk "sos"
389
+ expect(mi100).to have_received(:tone).exactly(6).times.with(440,100)
390
+ expect(mi100).to have_received(:tone).exactly(3).times.with(440,300)
391
+ end
392
+
393
+ it 'should reset default frequency and unit' do
394
+ Mi100::Morsecoder.reset
395
+ expect(Mi100::Morsecoder.default_frequency).to eq(4000)
396
+ expect(Mi100::Morsecoder.default_unit).to eq(50)
397
+ mi100 = Mi100.new "COM5"
398
+ expect(mi100.morse_frequency).to eq(4000)
399
+ expect(mi100.morse_unit).to eq(50)
400
+ end
401
+
402
+ it 'should provide Morsecode.to_morse_from class method converting the string to the morse code array' do
403
+ morsecode = Mi100::Morsecoder.to_morse_from "sos"
404
+ expect(morsecode.size).to eq(21)
405
+ expect(morsecode[0]).to eq ({:frequency=>4000, :duration=>50})
406
+ end
407
+
408
+ end