mi100 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ # morsecoder.rb
2
+ # Copyright (c) 2014 Masami Yamakawa
3
+ #
4
+ # This software is released under the MIT License.
5
+ # http://opensource.org/lisenses/mit-license.php
6
+
7
+ class Mi100
8
+ class Morsecoder
9
+
10
+ attr_accessor :morseunit, :morsefrequency
11
+
12
+ DEFAULT_MORSEUNIT_MILLIS = 50
13
+ DEFAULT_MORSEFREQUENCY = 4000
14
+ MORSECODE = {
15
+ "1" => [1,3,3,3,3],
16
+ "2" => [1,1,3,3,3],
17
+ "3" => [1,1,1,3,3],
18
+ "4" => [1,1,1,1,3],
19
+ "5" => [1,1,1,1,1],
20
+ "6" => [3,1,1,1,1],
21
+ "7" => [3,3,1,1,1],
22
+ "8" => [3,3,3,1,1],
23
+ "9" => [3,3,3,3,1],
24
+ "0" => [3,3,3,3,3],
25
+ "A" => [1,3],
26
+ "B" => [3,1,1,1],
27
+ "C" => [3,1,3,1],
28
+ "D" => [3,1,1],
29
+ "E" => [1],
30
+ "F" => [1,1,3,1],
31
+ "G" => [3,3,1],
32
+ "H" => [1,1,1,1],
33
+ "I" => [1,1],
34
+ "J" => [1,3,3,3],
35
+ "K" => [1,3,1,1],
36
+ "L" => [1,3,1,1],
37
+ "M" => [3,3],
38
+ "N" => [3,1],
39
+ "O" => [3,3,3],
40
+ "P" => [1,3,3,1],
41
+ "Q" => [3,3,1,3],
42
+ "R" => [1,3,1],
43
+ "S" => [1,1,1],
44
+ "T" => [3],
45
+ "U" => [1,1,3],
46
+ "V" => [1,1,1,3],
47
+ "W" => [1,3,3],
48
+ "X" => [3,1,1,3],
49
+ "Y" => [3,1,3,3],
50
+ "Z" => [3,3,1,1],
51
+ "." => [1,3,1,3,1,3],
52
+ "," => [3,3,1,1,3,3],
53
+ "?" => [1,1,3,3,1,1],
54
+ "!" => [3,1,3,1,3,3],
55
+ "-" => [3,1,1,1,1,3],
56
+ "/" => [3,1,1,3,1],
57
+ "@" => [1,3,3,1,3,1],
58
+ "(" => [3,1,3,3,1],
59
+ ")" => [3,1,3,3,1,3]
60
+ }
61
+
62
+ def initialize
63
+ @morseunit = DEFAULT_MORSEUNIT_MILLIS
64
+ @morsefrequency = DEFAULT_MORSEFREQUENCY
65
+ end
66
+
67
+ def to_morse(str)
68
+ morsecode = []
69
+ str.split(//).each do |char|
70
+ code = MORSECODE[char.upcase]
71
+ if code
72
+ code.each do |dots|
73
+ morsecode << {frequency: @morsefrequency, duration: @morseunit * dots}
74
+ morsecode << {frequency: false, duration: @morseunit * dots}
75
+ end
76
+ morsecode << {frequency: false, duration: morseunit * 3}
77
+ else
78
+ morsecode << {frequency: false, duration: morseunit * 7}
79
+ end
80
+ end
81
+ morsecode
82
+ end
83
+
84
+ end
85
+ end
data/lib/mi100/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Mi100
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/mi100.rb CHANGED
@@ -1,9 +1,10 @@
1
- require "mi100/version"
1
+ require 'mi100/version'
2
+ require 'mi100/morsecoder'
2
3
  require 'timeout'
3
4
 
4
5
  class Mi100
5
6
 
6
- DEFAULT_RETRIES = 3
7
+ DEFAULT_RETRIES = 5
7
8
  READ_TIMEOUT = 5000
8
9
  WRITE_TIMEOUT = 5000
9
10
  SHORT_READ_TIMEOUT = 1
@@ -34,11 +35,15 @@ class Mi100
34
35
  begin
35
36
  initialize_serialport dev
36
37
  rescue Errno::ENOENT
37
- puts retries_left
38
+ puts "Retry Bluetooth connection: #{retries_left.to_s}"
38
39
  retries_left -= 1
39
40
  retry unless retries_left < 0
41
+ puts "Bluetooth connection failed."
40
42
  raise
41
43
  end
44
+
45
+ @morsecoder = Morsecoder.new
46
+
42
47
  end
43
48
 
44
49
  def close
@@ -63,28 +68,28 @@ class Mi100
63
68
 
64
69
  def move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION)
65
70
  cmd = direction.upcase == "BACKWARD" ? CMD_MOVE_BACKWARD : CMD_MOVE_FORWARD
66
- send_command_get_response cmd + "," + duration.to_s
71
+ send_command_get_response "#{cmd},#{duration.to_s}"
67
72
  end
68
73
 
69
74
  def spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION)
70
75
  cmd = direction.upcase == "LEFT" ? CMD_SPIN_LEFT : CMD_SPIN_RIGHT
71
- send_command_get_response cmd + "," + duration.to_s
76
+ send_command_get_response "#{cmd},#{duration.to_s}"
72
77
  end
73
78
 
74
79
  def move_forward(duration)
75
- send_command_get_response CMD_MOVE_FORWARD + "," + duration.to_s
80
+ send_command_get_response "#{CMD_MOVE_FORWARD},#{duration.to_s}"
76
81
  end
77
82
 
78
83
  def move_backward(duration)
79
- send_command_get_response CMD_MOVE_BACKWARD + "," + duration.to_s
84
+ send_command_get_response "#{CMD_MOVE_BACKWARD},#{duration.to_s}"
80
85
  end
81
86
 
82
87
  def spin_right(duration)
83
- send_command_get_response CMD_SPIN_RIGHT + "," + duration.to_s
88
+ send_command_get_response "#{CMD_SPIN_RIGHT},#{duration.to_s}"
84
89
  end
85
90
 
86
91
  def spin_left(duration)
87
- send_command_get_response CMD_SPIN_LEFT + "," + duration.to_s
92
+ send_command_get_response "#{CMD_SPIN_LEFT},#{duration.to_s}"
88
93
  end
89
94
 
90
95
  def movef(duration = DEFAULT_MOVE_DURATION)
@@ -104,65 +109,90 @@ class Mi100
104
109
  end
105
110
 
106
111
  def move_forward!(duration)
107
- sendln CMD_MOVE_FORWARD + "," + duration.to_s
112
+ sendln "#{CMD_MOVE_FORWARD},#{duration.to_s}"
108
113
  end
109
114
 
110
115
  def move_backward!(duration)
111
- sendln CMD_MOVE_BACKWARD + "," + duration.to_s
116
+ sendln "#{CMD_MOVE_BACKWARD},#{duration.to_s}"
112
117
  end
113
118
 
114
119
  def spin_right!(duration)
115
- sendln CMD_SPIN_RIGHT + "," + duration.to_s
120
+ sendln "#{CMD_SPIN_RIGHT},#{duration.to_s}"
116
121
  end
117
122
 
118
123
  def spin_left!(duration)
119
- sendln CMD_SPIN_LEFT + "," + duration.to_s
124
+ sendln "#{CMD_SPIN_LEFT},#{duration.to_s}"
120
125
  end
121
126
 
122
- def blink(r = 0, g = 0, b = 0, duration = DEFAULT_BLINK_DURATION)
123
-
124
- if r + g + b == 0
125
- r = rand(100)+1
126
- g = rand(100)+1
127
- b = rand(100)+1
128
- end
129
-
130
- r = r <= 100 ? r : 100
131
- g = g <= 100 ? g : 100
132
- b = b <= 100 ? b : 100
133
-
134
- send_command_get_response CMD_BLINK_LED + "," + r.to_s + "," + g.to_s + "," + b.to_s + "," + duration.to_s
127
+ def blink(r = nil, g = nil, b = nil, duration = DEFAULT_BLINK_DURATION)
128
+ r ||= rand(100)+1
129
+ g ||= rand(100)+1
130
+ b ||= rand(100)+1
131
+ r = 100 if r > 100
132
+ g = 100 if g > 100
133
+ b = 100 if b > 100
134
+ send_command_get_response "#{CMD_BLINK_LED},#{r.to_s},#{g.to_s},#{b.to_s},#{duration.to_s}"
135
135
  end
136
136
 
137
137
  def stop
138
138
  send_command_get_response CMD_STOP
139
139
  end
140
140
 
141
-
142
- def tone(frequency = 0, duration = DEFAULT_TONE_DURATION)
143
- frequency = rand(4186 - 28) + 28 if frequency < 28
144
- send_command_get_response CMD_TONE + "," + frequency.to_s + "," + duration.to_s
141
+ def tone(frequency = nil, duration = DEFAULT_TONE_DURATION)
142
+ frequency ||= rand(4186 - 28) + 28
143
+ frequency = 4186 if frequency > 4186
144
+ frequency = 28 if frequency < 28
145
+ send_command_get_response "#{CMD_TONE},#{frequency.to_s},#{duration.to_s}"
145
146
  end
146
147
 
147
-
148
- def sound(pitch = nil, duration = DEFAULT_TONE_DURATION)
149
- pitch ||= FREQUENCY.keys[rand(FREQUENCY.size)].to_s
150
- tone FREQUENCY[pitch.upcase.to_sym], duration
148
+ def sound(pitch = "?", duration = DEFAULT_TONE_DURATION)
149
+
150
+ if pitch.instance_of?(String)
151
+ pitch = FREQUENCY.keys[rand(FREQUENCY.size)].to_s if pitch == "?"
152
+ frequency = FREQUENCY[pitch.upcase.to_sym]
153
+ else
154
+ frequency = pitch
155
+ end
156
+
157
+ tone frequency, duration if frequency
151
158
  sleep duration.to_f / 1000.0
152
159
  end
153
160
 
154
161
  def good
155
- tone 440, 100
156
- sleep 0.1
157
- tone 880, 100
158
- sleep 0.1
159
- tone 1760, 100
160
- sleep 0.1
162
+ freqs = [440,880,1760]
163
+ duration = 100.0
164
+ freqs.each do |freq|
165
+ tone freq, duration
166
+ sleep duration / 1000.0
167
+ end
161
168
  end
162
169
 
163
170
  def bad
164
- tone 100, 400
165
- sleep 0.4
171
+ duration = 400.0
172
+ freq = 100
173
+ tone freq, duration
174
+ sleep duration / 1000.0
175
+ end
176
+
177
+ def talk(str)
178
+ morsecode = @morsecoder.to_morse(str)
179
+ morsecode.each {|code| sound(code[:frequency],code[:duration])}
180
+ end
181
+
182
+ def morsefrequency
183
+ @morsecoder.morsefrequency
184
+ end
185
+
186
+ def morsefrequency=(frequency)
187
+ @morsecoder.morsefrequency = frequency
188
+ end
189
+
190
+ def morseunit
191
+ @morsecoder.morseunit
192
+ end
193
+
194
+ def morseunit=(millisec)
195
+ @morsecoder.morseunit = millisec
166
196
  end
167
197
 
168
198
  # Private methods
@@ -199,10 +229,9 @@ class Mi100
199
229
 
200
230
  def empty_receive_buffer
201
231
  @sp.read_timeout = SHORT_READ_TIMEOUT
202
- char = @sp.read 1
203
- while char.length > 0
232
+ begin
204
233
  char = @sp.read 1
205
- end
234
+ end while char && char.length > 0
206
235
  @sp.read_timeout = READ_TIMEOUT
207
236
  end
208
237
 
@@ -218,7 +247,7 @@ class Mi100
218
247
  end
219
248
  end
220
249
  else
221
- str = @sp.readline
250
+ str = @sp.gets
222
251
  end
223
252
  str
224
253
  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.2.0
4
+ version: 0.3.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: 2014-02-04 00:00:00.000000000Z
12
+ date: 2014-03-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &16297968 !ruby/object:Gem::Requirement
16
+ requirement: &16038924 !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: *16297968
24
+ version_requirements: *16038924
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &16297716 !ruby/object:Gem::Requirement
27
+ requirement: &16038672 !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: *16297716
35
+ version_requirements: *16038672
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &16297440 !ruby/object:Gem::Requirement
38
+ requirement: &16038396 !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: *16297440
46
+ version_requirements: *16038396
47
47
  description: A ruby gem for controlling MI100 of monoxit through bluetooth virtual
48
48
  serial port.
49
49
  email:
@@ -60,6 +60,7 @@ files:
60
60
  - README.md
61
61
  - Rakefile
62
62
  - lib/mi100.rb
63
+ - lib/mi100/morsecoder.rb
63
64
  - lib/mi100/version.rb
64
65
  - mi100.gemspec
65
66
  - spec/mi100_spec.rb