birdbrain 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +57 -0
- data/.rubocop.yml +135 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +14 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/birdbrain.gemspec +37 -0
- data/lib/birdbrain/birdbrain_device.rb +60 -0
- data/lib/birdbrain/birdbrain_exception.rb +14 -0
- data/lib/birdbrain/birdbrain_finch.rb +168 -0
- data/lib/birdbrain/birdbrain_finch_input.rb +115 -0
- data/lib/birdbrain/birdbrain_finch_output.rb +36 -0
- data/lib/birdbrain/birdbrain_hummingbird.rb +75 -0
- data/lib/birdbrain/birdbrain_hummingbird_input.rb +34 -0
- data/lib/birdbrain/birdbrain_hummingbird_output.rb +33 -0
- data/lib/birdbrain/birdbrain_microbit.rb +46 -0
- data/lib/birdbrain/birdbrain_microbit_input.rb +70 -0
- data/lib/birdbrain/birdbrain_microbit_output.rb +28 -0
- data/lib/birdbrain/birdbrain_request.rb +123 -0
- data/lib/birdbrain/birdbrain_state.rb +22 -0
- data/lib/birdbrain/version.rb +8 -0
- data/lib/birdbrain.rb +21 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 927e03b41176f419971ac1faed0d8243638448c044918a2035151dd8a05a796e
|
4
|
+
data.tar.gz: 5f365f6debd7c9af03de923075613252a7c96a47ba94ae683991c7426d24bdf9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 847a2bbabefcc8cb78319770ced934d7f60c3b76a6d5124a68a53cd0f1580cf63b472c063bc5da6650461a2ea4561792d46488554723ac427f3f4b0f81b2cbc9
|
7
|
+
data.tar.gz: a5f1cb8a3e84329609004645cc1777a2b5ec505c12156e6547977096c670ef535397a9cbc34778722743e884234ae0d949fd8959b29a9f239840ed4063165dc9
|
data/.gitignore
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
#/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
56
|
+
# .rubocop-https?--*
|
57
|
+
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
Metrics/BlockLength:
|
2
|
+
Max: 32
|
3
|
+
Naming/MethodParameterName:
|
4
|
+
Enabled: false
|
5
|
+
Metrics/ClassLength:
|
6
|
+
Max: 200
|
7
|
+
Metrics/MethodLength:
|
8
|
+
Max: 200
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
Style/TernaryParentheses:
|
12
|
+
Enabled: false
|
13
|
+
Style/NumericPredicate:
|
14
|
+
Enabled: false
|
15
|
+
Style/NumericLiterals:
|
16
|
+
Enabled: false
|
17
|
+
Style/IfUnlessModifier:
|
18
|
+
Enabled: false
|
19
|
+
Style/SafeNavigation:
|
20
|
+
Enabled: false
|
21
|
+
Metrics/CyclomaticComplexity:
|
22
|
+
Max: 20
|
23
|
+
Metrics/PerceivedComplexity:
|
24
|
+
Max: 20
|
25
|
+
Metrics/AbcSize:
|
26
|
+
Max: 200
|
27
|
+
Layout/LineLength:
|
28
|
+
Max: 150
|
29
|
+
Style/FrozenStringLiteralComment:
|
30
|
+
Enabled: false
|
31
|
+
Style/MutableConstant:
|
32
|
+
Enabled: false
|
33
|
+
Layout/BeginEndAlignment: # (new in 0.91)
|
34
|
+
Enabled: true
|
35
|
+
Layout/EmptyLinesAroundAttributeAccessor: # (new in 0.83)
|
36
|
+
Enabled: true
|
37
|
+
Layout/SpaceAroundMethodCallOperator: # (new in 0.82)
|
38
|
+
Enabled: true
|
39
|
+
Lint/BinaryOperatorWithIdenticalOperands: # (new in 0.89)
|
40
|
+
Enabled: true
|
41
|
+
Lint/ConstantDefinitionInBlock: # (new in 0.91)
|
42
|
+
Enabled: true
|
43
|
+
Lint/DeprecatedOpenSSLConstant: # (new in 0.84)
|
44
|
+
Enabled: true
|
45
|
+
Lint/DuplicateElsifCondition: # (new in 0.88)
|
46
|
+
Enabled: true
|
47
|
+
Lint/DuplicateRequire: # (new in 0.90)
|
48
|
+
Enabled: true
|
49
|
+
Lint/DuplicateRescueException: # (new in 0.89)
|
50
|
+
Enabled: true
|
51
|
+
Lint/EmptyConditionalBody: # (new in 0.89)
|
52
|
+
Enabled: true
|
53
|
+
Lint/EmptyFile: # (new in 0.90)
|
54
|
+
Enabled: true
|
55
|
+
Lint/FloatComparison: # (new in 0.89)
|
56
|
+
Enabled: true
|
57
|
+
Lint/HashCompareByIdentity: # (new in 0.93)
|
58
|
+
Enabled: true
|
59
|
+
Lint/IdentityComparison: # (new in 0.91)
|
60
|
+
Enabled: true
|
61
|
+
Lint/MissingSuper: # (new in 0.89)
|
62
|
+
Enabled: true
|
63
|
+
Lint/MixedRegexpCaptureTypes: # (new in 0.85)
|
64
|
+
Enabled: true
|
65
|
+
Lint/OutOfRangeRegexpRef: # (new in 0.89)
|
66
|
+
Enabled: true
|
67
|
+
Lint/RaiseException: # (new in 0.81)
|
68
|
+
Enabled: true
|
69
|
+
Lint/RedundantSafeNavigation: # (new in 0.93)
|
70
|
+
Enabled: true
|
71
|
+
Lint/SelfAssignment: # (new in 0.89)
|
72
|
+
Enabled: true
|
73
|
+
Lint/StructNewOverride: # (new in 0.81)
|
74
|
+
Enabled: true
|
75
|
+
Lint/TopLevelReturnWithArgument: # (new in 0.89)
|
76
|
+
Enabled: true
|
77
|
+
Lint/TrailingCommaInAttributeDeclaration: # (new in 0.90)
|
78
|
+
Enabled: true
|
79
|
+
Lint/UnreachableLoop: # (new in 0.89)
|
80
|
+
Enabled: true
|
81
|
+
Lint/UselessMethodDefinition: # (new in 0.90)
|
82
|
+
Enabled: true
|
83
|
+
Lint/UselessTimes: # (new in 0.91)
|
84
|
+
Enabled: true
|
85
|
+
Style/AccessorGrouping: # (new in 0.87)
|
86
|
+
Enabled: false
|
87
|
+
Style/BisectedAttrAccessor: # (new in 0.87)
|
88
|
+
Enabled: true
|
89
|
+
Style/CaseLikeIf: # (new in 0.88)
|
90
|
+
Enabled: true
|
91
|
+
Style/ClassEqualityComparison: # (new in 0.93)
|
92
|
+
Enabled: true
|
93
|
+
Style/CombinableLoops: # (new in 0.90)
|
94
|
+
Enabled: true
|
95
|
+
Style/ExplicitBlockArgument: # (new in 0.89)
|
96
|
+
Enabled: false
|
97
|
+
Style/ExponentialNotation: # (new in 0.82)
|
98
|
+
Enabled: true
|
99
|
+
Style/GlobalStdStream: # (new in 0.89)
|
100
|
+
Enabled: true
|
101
|
+
Style/HashAsLastArrayItem: # (new in 0.88)
|
102
|
+
Enabled: true
|
103
|
+
Style/HashEachMethods: # (new in 0.80)
|
104
|
+
Enabled: true
|
105
|
+
Style/HashLikeCase: # (new in 0.88)
|
106
|
+
Enabled: true
|
107
|
+
Style/HashTransformKeys: # (new in 0.80)
|
108
|
+
Enabled: true
|
109
|
+
Style/HashTransformValues: # (new in 0.80)
|
110
|
+
Enabled: true
|
111
|
+
Style/KeywordParametersOrder: # (new in 0.90)
|
112
|
+
Enabled: true
|
113
|
+
Style/OptionalBooleanParameter: # (new in 0.89)
|
114
|
+
Enabled: false
|
115
|
+
Style/RedundantAssignment: # (new in 0.87)
|
116
|
+
Enabled: true
|
117
|
+
Style/RedundantFetchBlock: # (new in 0.86)
|
118
|
+
Enabled: true
|
119
|
+
Style/RedundantFileExtensionInRequire: # (new in 0.88)
|
120
|
+
Enabled: true
|
121
|
+
Style/RedundantRegexpCharacterClass: # (new in 0.85)
|
122
|
+
Enabled: true
|
123
|
+
Style/RedundantRegexpEscape: # (new in 0.85)
|
124
|
+
Enabled: true
|
125
|
+
Style/RedundantSelfAssignment: # (new in 0.90)
|
126
|
+
Enabled: true
|
127
|
+
Style/SingleArgumentDig: # (new in 0.89)
|
128
|
+
Enabled: true
|
129
|
+
Style/SlicingWithRange: # (new in 0.83)
|
130
|
+
Enabled: true
|
131
|
+
Style/SoleNestedConditional: # (new in 0.89)
|
132
|
+
Enabled: true
|
133
|
+
Style/StringConcatenation: # (new in 0.89)
|
134
|
+
Enabled: true
|
135
|
+
|
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
birdbrain (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.1)
|
10
|
+
coderay (1.1.3)
|
11
|
+
method_source (1.0.0)
|
12
|
+
minitest (5.14.3)
|
13
|
+
parallel (1.20.1)
|
14
|
+
parser (3.0.0.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
pry (0.13.1)
|
17
|
+
coderay (~> 1.1)
|
18
|
+
method_source (~> 1.0)
|
19
|
+
rainbow (3.0.0)
|
20
|
+
rake (13.0.3)
|
21
|
+
regexp_parser (2.0.3)
|
22
|
+
rexml (3.2.4)
|
23
|
+
rubocop (0.93.1)
|
24
|
+
parallel (~> 1.10)
|
25
|
+
parser (>= 2.7.1.5)
|
26
|
+
rainbow (>= 2.2.2, < 4.0)
|
27
|
+
regexp_parser (>= 1.8)
|
28
|
+
rexml
|
29
|
+
rubocop-ast (>= 0.6.0)
|
30
|
+
ruby-progressbar (~> 1.7)
|
31
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
32
|
+
rubocop-ast (1.4.0)
|
33
|
+
parser (>= 2.7.1.5)
|
34
|
+
ruby-progressbar (1.11.0)
|
35
|
+
unicode-display_width (1.7.0)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
x86_64-darwin-19
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
birdbrain!
|
42
|
+
minitest (~> 5.0)
|
43
|
+
pry
|
44
|
+
rake (~> 13.0)
|
45
|
+
rubocop (~> 0.80)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
2.2.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Base2 Incorporated (Frank Morton)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Birdbrain-Ruby-Library
|
2
|
+
|
3
|
+
Ruby Library for the BirdBrainTechnologies Hummingbird Bit and Finch 2
|
4
|
+
|
5
|
+
This Ruby library allows students to use Ruby to read sensors and set motors and LEDs with the Hummingbird Bit. To use Ruby with the Hummingbird Bit, you must connect to the Hummingbird Bit via bluetooth with the BlueBird Connector.
|
6
|
+
|
7
|
+
For more information about setting up the BlueBird Connector used with their Python library, see www.birdbraintechnologies.com/hummingbirdbit/python/
|
8
|
+
|
9
|
+
Rdoc files are available at https://rdoc.info/github/fmorton/BirdBrain-Ruby-Library/
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'birdbrain'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install birdbrain
|
26
|
+
|
27
|
+
The library is distributed through https://rubygems.org/gems/birdbrain/
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
TODO: Write usage instructions here
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fmorton/BirdBrain-Ruby-Library.
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rubocop/rake_task'
|
11
|
+
|
12
|
+
RuboCop::RakeTask.new
|
13
|
+
|
14
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'birdbrain'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, do not forget to add pry to your Gemfile!)
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/birdbrain.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'lib/birdbrain/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'birdbrain'
|
5
|
+
spec.version = Birdbrain::VERSION
|
6
|
+
spec.authors = ['fmorton']
|
7
|
+
spec.email = ['fmorton@mac.com']
|
8
|
+
|
9
|
+
spec.summary = 'Ruby Library for Hummingbird Bit (and maybe Finch 2 later)'
|
10
|
+
spec.description = 'This Ruby library allows students to use Ruby to read sensors and set motors and LEDs with the '\
|
11
|
+
'Birdbrain Technologies Hummingbird Bit. To use Ruby with the Hummingbird Bit, you must connect '\
|
12
|
+
'to the Hummingbird Bit via bluetooth with the BlueBird Connector.'
|
13
|
+
spec.homepage = 'https://github.com/fmorton/BirdBrain-Ruby-Library'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
|
16
|
+
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/fmorton/BirdBrain-Ruby-Library'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/fmorton/BirdBrain-Ruby-Library/CHANGELOG.md'
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency 'example-gem', '~> 1.0'
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, checkout our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
2
|
+
# Copyright (c) 2021 Base2 Incorporated--All Rights Reserved.
|
3
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
4
|
+
class BirdbrainDevice
|
5
|
+
DEFAULT_DEVICE = 'A'
|
6
|
+
LEFT = 'L'
|
7
|
+
RIGHT = 'R'
|
8
|
+
VALID_DEVICES = 'ABC'
|
9
|
+
|
10
|
+
attr_accessor :state
|
11
|
+
attr_accessor :connected
|
12
|
+
attr_accessor :device
|
13
|
+
|
14
|
+
def initialize(device = DEFAULT_DEVICE)
|
15
|
+
raise(BirdbrainException, 'Missing device name') if device.nil?
|
16
|
+
raise(BirdbrainException, "Invalid device name: #{device}") unless VALID_DEVICES.include?(device)
|
17
|
+
|
18
|
+
self.state = BirdbrainState.new
|
19
|
+
self.device = device
|
20
|
+
self.connected = BirdbrainRequest.connected?(device)
|
21
|
+
|
22
|
+
raise(BirdbrainException, 'No connection') unless connected?
|
23
|
+
end
|
24
|
+
|
25
|
+
def connected?
|
26
|
+
connected
|
27
|
+
end
|
28
|
+
|
29
|
+
def disconnect
|
30
|
+
BirdbrainRequest.disconnect(device) if connected?
|
31
|
+
|
32
|
+
state.microbit_display_map_clear unless state.nil?
|
33
|
+
|
34
|
+
self.connected = false
|
35
|
+
self.device = nil
|
36
|
+
self.state = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
def microbit?
|
40
|
+
BirdbrainMicrobitInput.microbit?(device) if connected?
|
41
|
+
end
|
42
|
+
|
43
|
+
def hummingbird?
|
44
|
+
BirdbrainHummingbirdInput.hummingbird?(device) if connected?
|
45
|
+
end
|
46
|
+
|
47
|
+
def finch?
|
48
|
+
BirdbrainFinchInput.finch?(device) if connected?
|
49
|
+
end
|
50
|
+
|
51
|
+
def valid?(validate, valid_range)
|
52
|
+
return false if validate.nil?
|
53
|
+
|
54
|
+
valid_range.include?(validate.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def connected_and_valid?(validate, valid_range)
|
58
|
+
valid?(validate, valid_range) && connected?
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
2
|
+
# Copyright (c) 2021 Base2 Incorporated--All Rights Reserved.
|
3
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
4
|
+
class BirdbrainException < StandardError
|
5
|
+
def self.disconnect(device, message)
|
6
|
+
begin
|
7
|
+
BirdbrainRequest.disconnect(device)
|
8
|
+
rescue BirdbrainException
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
raise(BirdbrainException, message)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
2
|
+
# Copyright (c) 2021 Base2 Incorporated--All Rights Reserved.
|
3
|
+
#-----------------------------------------------------------------------------------------------------------------------------------
|
4
|
+
class BirdbrainFinch < BirdbrainMicrobit
|
5
|
+
FORWARD = 'F'
|
6
|
+
BACKWARD = 'B'
|
7
|
+
LEFT = BirdbrainDevice::LEFT
|
8
|
+
RIGHT = BirdbrainDevice::RIGHT
|
9
|
+
MOVE_START_WAIT_SECONDS = 0.15
|
10
|
+
MOVE_TIMEOUT_SECONDS = 60.0
|
11
|
+
VALID_LED_PORTS = '123'
|
12
|
+
VALID_TAIL_PORTS = '1234all'
|
13
|
+
VALID_TRILED_PORTS = '1234'
|
14
|
+
VALID_SENSOR_PORTS = '123'
|
15
|
+
VALID_SERVO_PORTS = '1234'
|
16
|
+
VALID_MOVE_DIRECTION = 'FB'
|
17
|
+
VALID_TURN_DIRECTION = 'LR'
|
18
|
+
|
19
|
+
attr_accessor :move_start_wait_seconds
|
20
|
+
attr_accessor :move_start_time
|
21
|
+
attr_accessor :move_timeout_seconds
|
22
|
+
|
23
|
+
def initialize(device = DEFAULT_DEVICE)
|
24
|
+
super
|
25
|
+
self.move_start_wait_seconds = MOVE_START_WAIT_SECONDS # seconds to allow finch to start moving
|
26
|
+
self.move_timeout_seconds = MOVE_TIMEOUT_SECONDS # maximum number of seconds to wait for finch moving
|
27
|
+
self.move_start_time = 0 # after move records how long it took the startup to complete for tuning
|
28
|
+
end
|
29
|
+
|
30
|
+
def moving?
|
31
|
+
BirdbrainFinchInput.moving?(device) if connected?
|
32
|
+
end
|
33
|
+
|
34
|
+
def light(direction)
|
35
|
+
BirdbrainFinchInput.light(device, direction) if connected?
|
36
|
+
end
|
37
|
+
|
38
|
+
def distance
|
39
|
+
BirdbrainFinchInput.distance(device) if connected?
|
40
|
+
end
|
41
|
+
|
42
|
+
def line(direction)
|
43
|
+
BirdbrainFinchInput.line(device, direction) if connected?
|
44
|
+
end
|
45
|
+
|
46
|
+
def encoder(direction)
|
47
|
+
BirdbrainFinchInput.encoder(device, direction) if connected?
|
48
|
+
end
|
49
|
+
|
50
|
+
def accelerometer
|
51
|
+
BirdbrainFinchInput.accelerometer(device) if connected?
|
52
|
+
end
|
53
|
+
|
54
|
+
def compass
|
55
|
+
BirdbrainFinchInput.compass(device) if connected?
|
56
|
+
end
|
57
|
+
|
58
|
+
def magnetometer
|
59
|
+
BirdbrainFinchInput.magnetometer(device) if connected?
|
60
|
+
end
|
61
|
+
|
62
|
+
def orientation
|
63
|
+
BirdbrainFinchInput.orientation(device) if connected?
|
64
|
+
end
|
65
|
+
|
66
|
+
def orientation_beak_up?
|
67
|
+
BirdbrainFinchInput.orientation_beak_up?(device) if connected?
|
68
|
+
end
|
69
|
+
|
70
|
+
def orientation_beak_down?
|
71
|
+
BirdbrainFinchInput.orientation_beak_down?(device) if connected?
|
72
|
+
end
|
73
|
+
|
74
|
+
def orientation_tilt_left?
|
75
|
+
BirdbrainFinchInput.orientation_tilt_left?(device) if connected?
|
76
|
+
end
|
77
|
+
|
78
|
+
def orientation_tilt_right?
|
79
|
+
BirdbrainFinchInput.orientation_tilt_right?(device) if connected?
|
80
|
+
end
|
81
|
+
|
82
|
+
def orientation_level?
|
83
|
+
BirdbrainFinchInput.orientation_level?(device) if connected?
|
84
|
+
end
|
85
|
+
|
86
|
+
def orientation_upside_down?
|
87
|
+
BirdbrainFinchInput.orientation_upside_down?(device) if connected?
|
88
|
+
end
|
89
|
+
|
90
|
+
def beak(r_intensity, g_intensity, b_intensity)
|
91
|
+
BirdbrainHummingbirdOutput.tri_led(device, 1, r_intensity, g_intensity, b_intensity) if connected?
|
92
|
+
end
|
93
|
+
|
94
|
+
def tail(port, r_intensity, g_intensity, b_intensity)
|
95
|
+
return unless connected_and_valid?(port, VALID_TAIL_PORTS)
|
96
|
+
|
97
|
+
if port.to_s == 'all'
|
98
|
+
(2..5).each { |each_port| BirdbrainHummingbirdOutput.tri_led(device, each_port, r_intensity, g_intensity, b_intensity) }
|
99
|
+
else
|
100
|
+
BirdbrainHummingbirdOutput.tri_led(device, port + 1, r_intensity, g_intensity, b_intensity)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def play_note(note, beats)
|
105
|
+
BirdbrainHummingbirdOutput.play_note(device, note, beats) if connected?
|
106
|
+
end
|
107
|
+
|
108
|
+
def wait_until_movement
|
109
|
+
start_time = Time.now
|
110
|
+
|
111
|
+
sleep(0.01) while !moving? && ((Time.now - start_time) < move_start_wait_seconds) # short wait for finch to start moving
|
112
|
+
|
113
|
+
self.move_start_time = Time.now - start_time # close to amount of time it took the finch to startup for tuning
|
114
|
+
|
115
|
+
true
|
116
|
+
end
|
117
|
+
|
118
|
+
def wait
|
119
|
+
start_time = Time.now
|
120
|
+
|
121
|
+
sleep(0.01) while moving? && ((Time.now - start_time) < move_timeout_seconds)
|
122
|
+
|
123
|
+
true
|
124
|
+
end
|
125
|
+
|
126
|
+
def wait_until_movement_and_wait
|
127
|
+
wait_until_movement
|
128
|
+
wait
|
129
|
+
|
130
|
+
true
|
131
|
+
end
|
132
|
+
|
133
|
+
def move(direction, distance, speed, wait_to_finish_movement = true)
|
134
|
+
return false unless connected_and_valid?(direction, VALID_MOVE_DIRECTION)
|
135
|
+
|
136
|
+
unless (response = BirdbrainFinchOutput.move(device, direction, distance, speed))
|
137
|
+
return response
|
138
|
+
end
|
139
|
+
|
140
|
+
wait_until_movement_and_wait if wait_to_finish_movement
|
141
|
+
|
142
|
+
true
|
143
|
+
end
|
144
|
+
|
145
|
+
def turn(direction, angle, speed, wait_to_finish_movement = true)
|
146
|
+
return false unless connected_and_valid?(direction, VALID_TURN_DIRECTION)
|
147
|
+
|
148
|
+
unless (response = BirdbrainFinchOutput.turn(device, direction, angle, speed))
|
149
|
+
return response
|
150
|
+
end
|
151
|
+
|
152
|
+
wait_until_movement_and_wait if wait_to_finish_movement
|
153
|
+
|
154
|
+
true
|
155
|
+
end
|
156
|
+
|
157
|
+
def motors(left_speed, right_speed)
|
158
|
+
BirdbrainFinchOutput.motors(device, left_speed, right_speed) if connected?
|
159
|
+
end
|
160
|
+
|
161
|
+
def stop
|
162
|
+
BirdbrainFinchOutput.stop(device) if connected?
|
163
|
+
end
|
164
|
+
|
165
|
+
def reset_encoders
|
166
|
+
BirdbrainFinchOutput.reset_encoders(device) if connected?
|
167
|
+
end
|
168
|
+
end
|