mi100 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/mi100.rb +96 -45
  2. data/lib/mi100/version.rb +1 -1
  3. metadata +8 -8
data/lib/mi100.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require "mi100/version"
2
+ require 'timeout'
2
3
 
3
4
  class Mi100
4
5
 
5
6
  DEFAULT_RETRIES = 3
6
7
  READ_TIMEOUT = 5000
8
+ WRITE_TIMEOUT = 5000
9
+ SHORT_READ_TIMEOUT = 1
7
10
 
8
11
  CMD_PING = "H"
9
12
  CMD_GET_POWER_LEVEL = "H"
@@ -14,6 +17,7 @@ class Mi100
14
17
  CMD_SPIN_LEFT = "L"
15
18
  CMD_BLINK_LED = "D"
16
19
  CMD_TONE = "T"
20
+ CMD_GET_LIGHT = "P"
17
21
 
18
22
  DEFAULT_MOVE_DURATION = 300
19
23
  DEFAULT_SPIN_DURATION = 140
@@ -38,82 +42,109 @@ class Mi100
38
42
  end
39
43
 
40
44
  def close
41
- #stop
42
45
  sleep 2
43
46
  @sp.close
44
47
  end
45
48
 
46
49
  def ping
47
- cmd = CMD_PING
48
- sendln cmd
49
- acked? cmd
50
+ send_command_get_response CMD_PING
50
51
  end
51
52
 
52
53
  def power
53
- cmd = CMD_GET_POWER_LEVEL
54
- sendln cmd
55
- str = recieveln
56
- strAry = str.chomp.split(",")
57
- v = strAry[1].to_i / 1000.0
58
- v.round 2
54
+ response = send_command_get_response CMD_GET_POWER_LEVEL
55
+ voltage = response[1].to_i / 1000.0
56
+ voltage.round 2
57
+ end
58
+
59
+ def light
60
+ response = send_command_get_response CMD_GET_LIGHT
61
+ response[1].to_i
59
62
  end
60
63
 
61
64
  def move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION)
62
65
  cmd = direction.upcase == "BACKWARD" ? CMD_MOVE_BACKWARD : CMD_MOVE_FORWARD
63
- sendln cmd + "," + duration.to_s
64
- acked? cmd
66
+ send_command_get_response cmd + "," + duration.to_s
65
67
  end
66
68
 
67
- def movef(duration = DEFAULT_MOVE_DURATION)
68
- move "FORWARD", duration
69
+ def spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION)
70
+ cmd = direction.upcase == "LEFT" ? CMD_SPIN_LEFT : CMD_SPIN_RIGHT
71
+ send_command_get_response cmd + "," + duration.to_s
72
+ end
73
+
74
+ def move_forward(duration)
75
+ send_command_get_response CMD_MOVE_FORWARD + "," + duration.to_s
69
76
  end
70
77
 
71
- def moveb(duration = DEFAULT_MOVE_DURATION)
72
- move "BACKWARD", duration
78
+ def move_backward(duration)
79
+ send_command_get_response CMD_MOVE_BACKWARD + "," + duration.to_s
73
80
  end
74
81
 
75
- def spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION)
76
- cmd = direction.upcase == "LEFT" ? CMD_SPIN_LEFT : CMD_SPIN_RIGHT
77
- sendln cmd + "," + duration.to_s
78
- acked? cmd
82
+ def spin_right(duration)
83
+ send_command_get_response CMD_SPIN_RIGHT + "," + duration.to_s
84
+ end
85
+
86
+ def spin_left(duration)
87
+ send_command_get_response CMD_SPIN_LEFT + "," + duration.to_s
88
+ end
89
+
90
+ def movef(duration = DEFAULT_MOVE_DURATION)
91
+ move_forward duration
92
+ end
93
+
94
+ def moveb(duration = DEFAULT_MOVE_DURATION)
95
+ move_backward duration
79
96
  end
80
97
 
81
98
  def spinr(duration = DEFAULT_SPIN_DURATION)
82
- spin "RIGHT", duration
99
+ spin_right duration
83
100
  end
84
101
 
85
102
  def spinl(duration = DEFAULT_SPIN_DURATION)
86
- spin "LEFT" , duration
103
+ spin_left duration
104
+ end
105
+
106
+ def move_forward!(duration)
107
+ sendln CMD_MOVE_FORWARD + "," + duration.to_s
108
+ end
109
+
110
+ def move_backward!(duration)
111
+ sendln CMD_MOVE_BACKWARD + "," + duration.to_s
112
+ end
113
+
114
+ def spin_right!(duration)
115
+ sendln CMD_SPIN_RIGHT + "," + duration.to_s
116
+ end
117
+
118
+ def spin_left!(duration)
119
+ sendln CMD_SPIN_LEFT + "," + duration.to_s
87
120
  end
88
121
 
89
122
  def blink(r = 0, g = 0, b = 0, duration = DEFAULT_BLINK_DURATION)
123
+
90
124
  if r + g + b == 0
91
125
  r = rand(100)+1
92
126
  g = rand(100)+1
93
127
  b = rand(100)+1
94
128
  end
129
+
95
130
  r = r <= 100 ? r : 100
96
131
  g = g <= 100 ? g : 100
97
132
  b = b <= 100 ? b : 100
98
133
 
99
- cmd = CMD_BLINK_LED
100
- sendln cmd + "," + r.to_s + "," + g.to_s + "," + b.to_s + "," + duration.to_s
101
- acked? cmd
134
+ send_command_get_response CMD_BLINK_LED + "," + r.to_s + "," + g.to_s + "," + b.to_s + "," + duration.to_s
135
+ end
136
+
137
+ def stop
138
+ send_command_get_response CMD_STOP
102
139
  end
103
140
 
141
+
104
142
  def tone(frequency = 0, duration = DEFAULT_TONE_DURATION)
105
143
  frequency = rand(4186 - 28) + 28 if frequency < 28
106
- cmd = CMD_TONE
107
- sendln cmd + "," + frequency.to_s + "," + duration.to_s
108
- acked? cmd
109
- end
110
-
111
- def stop
112
- cmd = CMD_STOP
113
- sendln cmd
114
- acked? cmd
144
+ send_command_get_response CMD_TONE + "," + frequency.to_s + "," + duration.to_s
115
145
  end
116
146
 
147
+
117
148
  def sound(pitch = nil, duration = DEFAULT_TONE_DURATION)
118
149
  pitch ||= FREQUENCY.keys[rand(FREQUENCY.size)].to_s
119
150
  tone FREQUENCY[pitch.upcase.to_sym], duration
@@ -142,28 +173,48 @@ class Mi100
142
173
  @sp = SerialPort.new dev, 38400, 8, 1, SerialPort::NONE
143
174
  if is_windows?
144
175
  @sp.read_timeout = READ_TIMEOUT
145
- @sp.write_timeout = 0
176
+ @sp.write_timeout = WRITE_TIMEOUT
177
+ end
178
+ end
179
+
180
+
181
+ def send_command_get_response(cmd = CMD_PING)
182
+ empty_receive_buffer
183
+ sendln cmd
184
+ received_line = receiveln
185
+
186
+ begin
187
+ timeout(READ_TIMEOUT/1000) do
188
+ received_line = receiveln until received_line[0] == cmd[0]
189
+ end
190
+ rescue
191
+ return nil
146
192
  end
193
+ received_line.chomp.split(",")
147
194
  end
148
195
 
149
196
  def sendln str
150
197
  @sp.write str + "\n"
151
198
  end
152
199
 
153
- def acked?(cmd)
154
- str = recieveln
155
- str[0] == cmd
200
+ def empty_receive_buffer
201
+ @sp.read_timeout = SHORT_READ_TIMEOUT
202
+ char = @sp.read 1
203
+ while char.length > 0
204
+ char = @sp.read 1
205
+ end
206
+ @sp.read_timeout = READ_TIMEOUT
156
207
  end
157
208
 
158
- def recieveln
209
+ def receiveln
159
210
  if is_windows?
160
- stime = Time.now
211
+ start_time = Time.now
161
212
  str = ""
162
- while (Time.now - stime) * 1000.0 < READ_TIMEOUT do
163
- c = @sp.read 1
164
- if c.length > 0
165
- str += c
166
- break if c == "\n"
213
+ while (Time.now - start_time) * 1000.0 < READ_TIMEOUT do
214
+ char = @sp.read 1
215
+ if char.length > 0
216
+ str += char
217
+ break if char == "\n"
167
218
  end
168
219
  end
169
220
  else
data/lib/mi100/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Mi100
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mi100
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-20 00:00:00.000000000Z
12
+ date: 2014-02-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &20673168 !ruby/object:Gem::Requirement
16
+ requirement: &16297968 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *20673168
24
+ version_requirements: *16297968
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &20672916 !ruby/object:Gem::Requirement
27
+ requirement: &16297716 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20672916
35
+ version_requirements: *16297716
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &20672640 !ruby/object:Gem::Requirement
38
+ requirement: &16297440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20672640
46
+ version_requirements: *16297440
47
47
  description: A ruby gem for controlling MI100 of monoxit through bluetooth virtual
48
48
  serial port.
49
49
  email: