mi100 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mi100.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Masami Yamakawa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Mi100
2
+
3
+ Controlling your Bluetooth<sup>&reg;</sup> wireless technology embedded robot MI100.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mi100'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mi100
19
+
20
+ ## Usage
21
+
22
+ Pairing must be performed between a *Bluetooth* wireless technology enabled device and MI100 in advance.
23
+ The below example shows that pairing is done between a Windows device and MI100, and the virtual serial port is COM12.
24
+
25
+ require 'serialport' #hybridgroup-serialport
26
+ require 'mi100'
27
+
28
+ mi100 = Mi100.new 'COM12'
29
+
30
+ 3.times do
31
+ res = mi100.ping
32
+
33
+ # Get battery level
34
+ res = mi100.power
35
+ puts res
36
+
37
+ # Move forward 300mS
38
+ mi100.move "forward", 300
39
+
40
+ # Move backward 300mS
41
+ mi100.move "backward", 300
42
+
43
+ # Spin right 500mS
44
+ mi100.spin "right", 500
45
+
46
+ # Spin left 500mS
47
+ mi100.spin "left", 500
48
+
49
+ # Blink LED 100% red, 50% green, 20% blue 1000mS
50
+ mi100.blink 100, 50, 20, 1000
51
+
52
+ # LED random color blink
53
+ mi100.blink
54
+
55
+ # Tone 440Hz 200mS
56
+ mi100.tone 440, 200
57
+
58
+ # Do Re Mi
59
+ mi100.sound "DO"
60
+ mi100.sound "RE"
61
+ mi100.sound "MI"
62
+
63
+ sleep 1
64
+ end
65
+
66
+ mi100.close
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ class Mi100
2
+ VERSION = "0.1.8"
3
+ end
data/lib/mi100.rb ADDED
@@ -0,0 +1,180 @@
1
+ require "mi100/version"
2
+
3
+ class Mi100
4
+
5
+ DEFAULT_RETRIES = 3
6
+ READ_TIMEOUT = 5000
7
+
8
+ CMD_PING = "H"
9
+ CMD_GET_POWER_LEVEL = "H"
10
+ CMD_STOP = "S"
11
+ CMD_MOVE_FORWARD = "F"
12
+ CMD_MOVE_BACKWARD = "B"
13
+ CMD_SPIN_RIGHT = "R"
14
+ CMD_SPIN_LEFT = "L"
15
+ CMD_BLINK_LED = "D"
16
+ CMD_TONE = "T"
17
+
18
+ DEFAULT_MOVE_DURATION = 300
19
+ DEFAULT_SPIN_DURATION = 140
20
+ DEFAULT_BLINK_DURATION = 600
21
+ DEFAULT_TONE_DURATION = 300
22
+
23
+ DEFAULT_MOVE_DIRECTION = "FORWARD"
24
+ DEFAULT_SPIN_DIRECTION = "RIGHT"
25
+
26
+ FREQUENCY = {DO: 262, RE: 294, MI: 330, FA: 349, SO: 392, LA: 440, SI: 494, HDO: 523}
27
+
28
+ def initialize(dev)
29
+ retries_left = DEFAULT_RETRIES
30
+ begin
31
+ initialize_serialport dev
32
+ rescue Errno::ENOENT
33
+ puts retries_left
34
+ retries_left -= 1
35
+ retry unless retries_left < 0
36
+ raise
37
+ end
38
+ end
39
+
40
+ def close
41
+ #stop
42
+ sleep 2
43
+ @sp.close
44
+ end
45
+
46
+ def ping
47
+ cmd = CMD_PING
48
+ sendln cmd
49
+ acked? cmd
50
+ end
51
+
52
+ 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
59
+ end
60
+
61
+ def move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION)
62
+ cmd = direction.upcase == "BACKWARD" ? CMD_MOVE_BACKWARD : CMD_MOVE_FORWARD
63
+ sendln cmd + "," + duration.to_s
64
+ acked? cmd
65
+ end
66
+
67
+ def movef(duration = DEFAULT_MOVE_DURATION)
68
+ move "FORWARD", duration
69
+ end
70
+
71
+ def moveb(duration = DEFAULT_MOVE_DURATION)
72
+ move "BACKWARD", duration
73
+ end
74
+
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
79
+ end
80
+
81
+ def spinr(duration = DEFAULT_SPIN_DURATION)
82
+ spin "RIGHT", duration
83
+ end
84
+
85
+ def spinl(duration = DEFAULT_SPIN_DURATION)
86
+ spin "LEFT" , duration
87
+ end
88
+
89
+ def blink(r = 0, g = 0, b = 0, duration = DEFAULT_BLINK_DURATION)
90
+ if r + g + b == 0
91
+ r = rand(100)+1
92
+ g = rand(100)+1
93
+ b = rand(100)+1
94
+ end
95
+ r = r <= 100 ? r : 100
96
+ g = g <= 100 ? g : 100
97
+ b = b <= 100 ? b : 100
98
+
99
+ cmd = CMD_BLINK_LED
100
+ sendln cmd + "," + r.to_s + "," + g.to_s + "," + b.to_s + "," + duration.to_s
101
+ acked? cmd
102
+ end
103
+
104
+ def tone(frequency = 0, duration = DEFAULT_TONE_DURATION)
105
+ 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
115
+ end
116
+
117
+ def sound(pitch = nil, duration = DEFAULT_TONE_DURATION)
118
+ pitch ||= FREQUENCY.keys[rand(FREQUENCY.size)].to_s
119
+ tone FREQUENCY[pitch.upcase.to_sym], duration
120
+ duration+=100
121
+ sleep duration / 1000
122
+ end
123
+
124
+ def good
125
+ tone 440, 100
126
+ sleep 0.1
127
+ tone 880, 100
128
+ sleep 0.1
129
+ tone 1760, 100
130
+ sleep 0.1
131
+ end
132
+
133
+ def bad
134
+ tone 100, 400
135
+ sleep 0.4
136
+ end
137
+
138
+ # Private methods
139
+ private
140
+
141
+ def initialize_serialport(dev)
142
+ require 'serialport'
143
+ @sp = SerialPort.new dev, 38400, 8, 1, SerialPort::NONE
144
+ if is_windows?
145
+ @sp.read_timeout = READ_TIMEOUT
146
+ @sp.write_timeout = 0
147
+ end
148
+ end
149
+
150
+ def sendln str
151
+ @sp.write str + "\n"
152
+ end
153
+
154
+ def acked?(cmd)
155
+ str = recieveln
156
+ str[0] == cmd
157
+ end
158
+
159
+ def recieveln
160
+ if is_windows?
161
+ stime = Time.now
162
+ str = ""
163
+ while (Time.now - stime) * 1000.0 < READ_TIMEOUT do
164
+ c = @sp.read 1
165
+ if c.length > 0
166
+ str += c
167
+ break if c == "\n"
168
+ end
169
+ end
170
+ else
171
+ str = @sp.readline
172
+ end
173
+ str
174
+ end
175
+
176
+ def is_windows?
177
+ os = RUBY_PLATFORM.split("-")[1]
178
+ os == 'mswin' or os == 'bccwin' or os == 'mingw' or os == 'mingw32'
179
+ end
180
+ end
data/mi100.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mi100/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mi100"
8
+ spec.version = Mi100::VERSION
9
+ spec.authors = ["Masami Yamakawa"]
10
+ spec.email = ["silkycove@gmail.com"]
11
+ spec.description = %q{A ruby gem for controlling MI100 of monoxit through bluetooth virtual serial port.}
12
+ spec.summary = %q{A ruby gem for controlling MI100}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mi100 do
4
+ it 'should have a version number' do
5
+ Mi100::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should do something useful' do
9
+ false.should be_true
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'mi100'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mi100
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Masami Yamakawa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &20957208 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *20957208
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &20956956 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *20956956
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &20956680 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *20956680
47
+ description: A ruby gem for controlling MI100 of monoxit through bluetooth virtual
48
+ serial port.
49
+ email:
50
+ - silkycove@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rspec
57
+ - .travis.yml
58
+ - Gemfile
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - lib/mi100.rb
63
+ - lib/mi100/version.rb
64
+ - mi100.gemspec
65
+ - spec/mi100_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: ''
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.7.2
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A ruby gem for controlling MI100
92
+ test_files:
93
+ - spec/mi100_spec.rb
94
+ - spec/spec_helper.rb