birdbrain 0.2.1 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 927e03b41176f419971ac1faed0d8243638448c044918a2035151dd8a05a796e
4
- data.tar.gz: 5f365f6debd7c9af03de923075613252a7c96a47ba94ae683991c7426d24bdf9
3
+ metadata.gz: 20010320d2ba577a1b00f0762ed60c0ca4833bbd6b9e4034106f874db85e81d1
4
+ data.tar.gz: 6415782f80bb4c6aea45510233ee8fd8928f24bf74c8edf4d9df91446cbb44ed
5
5
  SHA512:
6
- metadata.gz: 847a2bbabefcc8cb78319770ced934d7f60c3b76a6d5124a68a53cd0f1580cf63b472c063bc5da6650461a2ea4561792d46488554723ac427f3f4b0f81b2cbc9
7
- data.tar.gz: a5f1cb8a3e84329609004645cc1777a2b5ec505c12156e6547977096c670ef535397a9cbc34778722743e884234ae0d949fd8959b29a9f239840ed4063165dc9
6
+ metadata.gz: 491a486bd4cf93aa3bbc12f52ba2996bb3f88e0614729b774b19d5df40c17b62e9eff301170f47cc94c1a0d2c99e42c2774233444dea70ed2c4a8fb9af74fc96
7
+ data.tar.gz: ed9141db314a096d8e786b60ff8a0f07ad53cbf78a32070861d80c90bf716e074ab7200a1cc09006b613bc9881727b8866cc556c2411adf42d105459caa6a44c
data/README.md CHANGED
@@ -4,9 +4,9 @@ Ruby Library for the BirdBrainTechnologies Hummingbird Bit and Finch 2
4
4
 
5
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
6
 
7
- For more information about setting up the BlueBird Connector used with their Python library, see www.birdbraintechnologies.com/hummingbirdbit/python/
7
+ For more information about setting up the BlueBird Connector used with their Python library, see www.birdbraintechnologies.com/hummingbirdbit/python
8
8
 
9
- Rdoc files are available at https://rdoc.info/github/fmorton/BirdBrain-Ruby-Library/
9
+ RubyDoc files are available at https://rubydoc.info/github/fmorton/BirdBrain-Ruby-Library
10
10
 
11
11
  ## Installation
12
12
 
@@ -24,11 +24,64 @@ Or install it yourself as:
24
24
 
25
25
  $ gem install birdbrain
26
26
 
27
- The library is distributed through https://rubygems.org/gems/birdbrain/
27
+ The library is distributed through https://rubygems.org/gems/birdbrain
28
28
 
29
29
  ## Usage
30
30
 
31
- TODO: Write usage instructions here
31
+ Below are two examples causing a Finch 2 to move in the path of a box. The first example is the long way to do it:
32
+
33
+ ```ruby
34
+ require 'birdbrain'
35
+
36
+ finch = BirdbrainFinch.connect('A')
37
+
38
+ finch.beak(0, 50, 0)
39
+
40
+ finch.tail(1, 0, 50, 0)
41
+ finch.move(BirdbrainFinch::FORWARD, 2, 20)
42
+ finch.turn(BirdbrainFinch::LEFT, 90, 50)
43
+
44
+ finch.tail(2, 0, 50, 0)
45
+ finch.move(BirdbrainFinch::FORWARD, 2, 20)
46
+ finch.turn(BirdbrainFinch::RIGHT, 90, 50)
47
+ finch.tail(3, 0, 50, 0)
48
+
49
+ finch.move(BirdbrainFinch::BACKWARD, 2, 20)
50
+ finch.turn(BirdbrainFinch::RIGHT, 90, 50)
51
+ finch.tail(4, 0, 50, 0)
52
+
53
+ finch.move(BirdbrainFinch::FORWARD, 2, 20)
54
+ finch.turn(BirdbrainFinch::LEFT, 90, 50)
55
+ sleep(1)
56
+
57
+ finch.disconnect
58
+ ```
59
+
60
+ The second way has the same result, but is more concise:
61
+
62
+ ```ruby
63
+ require 'birdbrain'
64
+
65
+ def draw(finch, step, move_direction, turn_direction)
66
+ finch.tail(step, 0, 50, 0)
67
+ finch.move(move_direction, 2, 20)
68
+ finch.turn(turn_direction, 90, 50)
69
+ end
70
+
71
+ finch = BirdbrainFinch.connect('A')
72
+
73
+ finch.beak(0, 50, 0)
74
+
75
+ draw(finch, 1, BirdbrainFinch::FORWARD, BirdbrainFinch::LEFT)
76
+ draw(finch, 2, BirdbrainFinch::FORWARD, BirdbrainFinch::RIGHT)
77
+ draw(finch, 3, BirdbrainFinch::BACKWARD, BirdbrainFinch::RIGHT)
78
+ draw(finch, 4, BirdbrainFinch::FORWARD, BirdbrainFinch::LEFT)
79
+ sleep(1)
80
+
81
+ finch.disconnect
82
+ ```
83
+
84
+ RubyDoc files are available at https://rubydoc.info/github/fmorton/BirdBrain-Ruby-Library
32
85
 
33
86
  ## Development
34
87
 
@@ -12,30 +12,56 @@ class BirdbrainDevice
12
12
  attr_accessor :device
13
13
 
14
14
  def initialize(device = DEFAULT_DEVICE)
15
+ self.state = BirdbrainState.new
16
+ self.device = device
17
+ self.connected = nil
18
+ end
19
+
20
+ def self.connect(device = DEFAULT_DEVICE, raise_exception_if_no_connection = false)
15
21
  raise(BirdbrainException, 'Missing device name') if device.nil?
16
22
  raise(BirdbrainException, "Invalid device name: #{device}") unless VALID_DEVICES.include?(device)
17
23
 
18
- self.state = BirdbrainState.new
19
- self.device = device
20
- self.connected = BirdbrainRequest.connected?(device)
24
+ device = new(device)
25
+
26
+ device.connect
27
+
28
+ raise(BirdbrainException, 'No connection') if raise_exception_if_no_connection && !device.connected?
21
29
 
22
- raise(BirdbrainException, 'No connection') unless connected?
30
+ device
23
31
  end
24
32
 
25
- def connected?
33
+ def connect
34
+ self.connected = BirdbrainRequest.connected?(device)
35
+ self.connected = nil unless valid_device_type?
26
36
  connected
27
37
  end
28
38
 
39
+ def connected?
40
+ !!connected
41
+ end
42
+
29
43
  def disconnect
30
44
  BirdbrainRequest.disconnect(device) if connected?
31
45
 
32
46
  state.microbit_display_map_clear unless state.nil?
33
47
 
34
- self.connected = false
48
+ self.connected = nil
35
49
  self.device = nil
36
50
  self.state = nil
37
51
  end
38
52
 
53
+ def self.find_device
54
+ ('A'..'C').each do |device|
55
+ connected_device = connect(device)
56
+
57
+ return connected_device if connected_device.valid_device_type?
58
+ rescue BirdbrainException
59
+ next
60
+ end
61
+
62
+ new(nil)
63
+ end
64
+
39
65
  def microbit?
40
66
  BirdbrainMicrobitInput.microbit?(device) if connected?
41
67
  end
@@ -2,13 +2,4 @@
2
2
  # Copyright (c) 2021 Base2 Incorporated--All Rights Reserved.
3
3
  #-----------------------------------------------------------------------------------------------------------------------------------
4
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
5
  end
@@ -27,6 +27,10 @@ class BirdbrainFinch < BirdbrainMicrobit
27
27
  self.move_start_time = 0 # after move records how long it took the startup to complete for tuning
28
28
  end
29
29
 
30
+ def valid_device_type?
31
+ finch?
32
+ end
33
+
30
34
  def moving?
31
35
  BirdbrainFinchInput.moving?(device) if connected?
32
36
  end
@@ -92,7 +96,7 @@ class BirdbrainFinch < BirdbrainMicrobit
92
96
  end
93
97
 
94
98
  def tail(port, r_intensity, g_intensity, b_intensity)
95
- return unless connected_and_valid?(port, VALID_TAIL_PORTS)
99
+ return nil unless connected_and_valid?(port, VALID_TAIL_PORTS)
96
100
 
97
101
  if port.to_s == 'all'
98
102
  (2..5).each { |each_port| BirdbrainHummingbirdOutput.tri_led(device, each_port, r_intensity, g_intensity, b_intensity) }
@@ -131,7 +135,7 @@ class BirdbrainFinch < BirdbrainMicrobit
131
135
  end
132
136
 
133
137
  def move(direction, distance, speed, wait_to_finish_movement = true)
134
- return false unless connected_and_valid?(direction, VALID_MOVE_DIRECTION)
138
+ return nil unless connected_and_valid?(direction, VALID_MOVE_DIRECTION)
135
139
 
136
140
  unless (response = BirdbrainFinchOutput.move(device, direction, distance, speed))
137
141
  return response
@@ -143,7 +147,7 @@ class BirdbrainFinch < BirdbrainMicrobit
143
147
  end
144
148
 
145
149
  def turn(direction, angle, speed, wait_to_finish_movement = true)
146
- return false unless connected_and_valid?(direction, VALID_TURN_DIRECTION)
150
+ return nil unless connected_and_valid?(direction, VALID_TURN_DIRECTION)
147
151
 
148
152
  unless (response = BirdbrainFinchOutput.turn(device, direction, angle, speed))
149
153
  return response
@@ -9,11 +9,11 @@ class BirdbrainFinchInput < BirdbrainRequest
9
9
  DEFAULT_UNLIMITED_MIN_RESPONSE = -1000000
10
10
  DEFAULT_UNLIMITED_MAX_RESPONSE = 1000000
11
11
  ORIENTATIONS = ['Beak%20Up', 'Beak%20Down', 'Tilt%20Left', 'Tilt%20Right', 'Level', 'Upside%20Down']
12
- ORIENTATION_RESULTS = ['Beak up', 'Beak down', 'Tilt left', 'Tilt right', 'Level', 'Upside down']
12
+ ORIENTATION_RESULTS = ['Beak up', 'Beak down', 'Tilt left', 'Tilt right', 'Level', 'Upside down', 'In between']
13
13
  ORIENTATION_IN_BETWEEN = 'In between'
14
14
 
15
15
  def self.finch?(device)
16
- request_status(response_body('hummingbird', 'in', 'isHummingbird', 'static', device))
16
+ request_status(response_body('hummingbird', 'in', 'isFinch', 'static', device))
17
17
  end
18
18
 
19
19
  def self.moving?(device)
@@ -92,11 +92,11 @@ class BirdbrainFinchInput < BirdbrainRequest
92
92
  orientation_check(device, 5)
93
93
  end
94
94
 
95
- def self.orientation_check(device, index)
95
+ private_class_method def self.orientation_check(device, index)
96
96
  request_status(response_body('hummingbird', 'in', 'finchOrientation', ORIENTATIONS[index], device))
97
97
  end
98
98
 
99
- def self.sensor(device, sensor, other = nil, options = {})
99
+ private_class_method def self.sensor(device, sensor, other = nil, options = {})
100
100
  return false if other == false # for invalid directions
101
101
 
102
102
  factor = options.key?(:factor) ? options[:factor] : DEFAULT_FACTOR
@@ -9,6 +9,10 @@ class BirdbrainHummingbird < BirdbrainMicrobit
9
9
  VALID_SENSOR_PORTS = '123'
10
10
  VALID_SERVO_PORTS = '1234'
11
11
 
12
+ def valid_device_type?
13
+ hummingbird?
14
+ end
15
+
12
16
  def light(port)
13
17
  BirdbrainHummingbirdInput.light(device, port) if connected_and_valid?(port, VALID_SENSOR_PORTS)
14
18
  end
@@ -48,28 +52,4 @@ class BirdbrainHummingbird < BirdbrainMicrobit
48
52
  def play_note(note, beats)
49
53
  BirdbrainHummingbirdOutput.play_note(device, note, beats) if connected?
50
54
  end
51
-
52
- def orientation_screen_up?
53
- BirdbrainMicrobitInput.orientation_screen_up?(device) if connected?
54
- end
55
-
56
- def orientation_screen_down?
57
- BirdbrainMicrobitInput.orientation_screen_down?(device) if connected?
58
- end
59
-
60
- def orientation_tilt_left?
61
- BirdbrainMicrobitInput.orientation_tilt_left?(device) if connected?
62
- end
63
-
64
- def orientation_tilt_right?
65
- BirdbrainMicrobitInput.orientation_tilt_right?(device) if connected?
66
- end
67
-
68
- def orientation_logo_up?
69
- BirdbrainMicrobitInput.orientation_logo_up?(device) if connected?
70
- end
71
-
72
- def orientation_logo_down?
73
- BirdbrainMicrobitInput.orientation_logo_down?(device) if connected?
74
- end
75
55
  end
@@ -26,7 +26,7 @@ class BirdbrainHummingbirdInput < BirdbrainRequest
26
26
  sensor(device, port, 0.012941176470588235) # factor=3.3/255
27
27
  end
28
28
 
29
- def self.sensor(device, port, factor)
29
+ private_class_method def self.sensor(device, port, factor)
30
30
  response = response_body('hummingbird', 'in', 'sensor', port, device)
31
31
 
32
32
  (response.nil? ? nil : bounds((response.to_f * factor).to_i, 0, 100))
@@ -4,6 +4,10 @@
4
4
  class BirdbrainMicrobit < BirdbrainDevice
5
5
  VALID_BUTTONS = 'AB'
6
6
 
7
+ def valid_device_type?
8
+ microbit?
9
+ end
10
+
7
11
  def microbit_accelerometer
8
12
  BirdbrainMicrobitInput.microbit_accelerometer(device) if connected?
9
13
  end
@@ -28,6 +32,30 @@ class BirdbrainMicrobit < BirdbrainDevice
28
32
  BirdbrainMicrobitInput.microbit_orientation(device) if connected?
29
33
  end
30
34
 
35
+ def microbit_orientation_screen_up?
36
+ BirdbrainMicrobitInput.microbit_orientation_screen_up?(device) if connected?
37
+ end
38
+
39
+ def microbit_orientation_screen_down?
40
+ BirdbrainMicrobitInput.microbit_orientation_screen_down?(device) if connected?
41
+ end
42
+
43
+ def microbit_orientation_tilt_left?
44
+ BirdbrainMicrobitInput.microbit_orientation_tilt_left?(device) if connected?
45
+ end
46
+
47
+ def microbit_orientation_tilt_right?
48
+ BirdbrainMicrobitInput.microbit_orientation_tilt_right?(device) if connected?
49
+ end
50
+
51
+ def microbit_orientation_logo_up?
52
+ BirdbrainMicrobitInput.microbit_orientation_logo_up?(device) if connected?
53
+ end
54
+
55
+ def microbit_orientation_logo_down?
56
+ BirdbrainMicrobitInput.microbit_orientation_logo_down?(device) if connected?
57
+ end
58
+
31
59
  def microbit_display(led_list)
32
60
  BirdbrainMicrobitOutput.microbit_display(state, device, led_list) if connected?
33
61
  end
@@ -2,9 +2,9 @@
2
2
  # Copyright (c) 2021 Base2 Incorporated--All Rights Reserved.
3
3
  #-----------------------------------------------------------------------------------------------------------------------------------
4
4
  class BirdbrainMicrobitInput < BirdbrainRequest
5
- ORIENTATIONS = ['Screen%20Up', 'Screen%20Down', 'Tilt%20Left', 'Tilt%20Right', 'Logo%20Up', 'Logo%20Down']
6
- ORIENTATION_RESULTS = ['Screen up', 'Screen down', 'Tilt left', 'Tilt right', 'Logo up', 'Logo down']
7
- ORIENTATION_IN_BETWEEN = 'In between'
5
+ MICROBIT_ORIENTATIONS = ['Screen%20Up', 'Screen%20Down', 'Tilt%20Left', 'Tilt%20Right', 'Logo%20Up', 'Logo%20Down']
6
+ MICROBIT_ORIENTATION_RESULTS = ['Screen up', 'Screen down', 'Tilt left', 'Tilt right', 'Logo up', 'Logo down', 'In between']
7
+ MICROBIT_ORIENTATION_IN_BETWEEN = 'In between'
8
8
 
9
9
  def self.microbit?(device)
10
10
  request_status(response_body('hummingbird', 'in', 'isHummingbird', 'static', device))
@@ -31,40 +31,40 @@ class BirdbrainMicrobitInput < BirdbrainRequest
31
31
  end
32
32
 
33
33
  def self.microbit_orientation(device)
34
- ORIENTATIONS.each_with_index do |orientation, index|
34
+ MICROBIT_ORIENTATIONS.each_with_index do |orientation, index|
35
35
  return nil if (response = response_body('hummingbird', 'in', 'orientation', orientation, device)).nil?
36
36
 
37
- return ORIENTATION_RESULTS[index] if request_status(response)
37
+ return MICROBIT_ORIENTATION_RESULTS[index] if request_status(response)
38
38
  end
39
39
 
40
- ORIENTATION_IN_BETWEEN
40
+ MICROBIT_ORIENTATION_IN_BETWEEN
41
41
  end
42
42
 
43
- def self.orientation_screen_up?(device)
43
+ def self.microbit_orientation_screen_up?(device)
44
44
  orientation_check(device, 0)
45
45
  end
46
46
 
47
- def self.orientation_screen_down?(device)
47
+ def self.microbit_orientation_screen_down?(device)
48
48
  orientation_check(device, 1)
49
49
  end
50
50
 
51
- def self.orientation_tilt_left?(device)
51
+ def self.microbit_orientation_tilt_left?(device)
52
52
  orientation_check(device, 2)
53
53
  end
54
54
 
55
- def self.orientation_tilt_right?(device)
55
+ def self.microbit_orientation_tilt_right?(device)
56
56
  orientation_check(device, 3)
57
57
  end
58
58
 
59
- def self.orientation_logo_up?(device)
59
+ def self.microbit_orientation_logo_up?(device)
60
60
  orientation_check(device, 4)
61
61
  end
62
62
 
63
- def self.orientation_logo_down?(device)
63
+ def self.microbit_orientation_logo_down?(device)
64
64
  orientation_check(device, 5)
65
65
  end
66
66
 
67
- def self.orientation_check(device, index)
68
- request_status(response_body('hummingbird', 'in', 'orientation', ORIENTATIONS[index], device))
67
+ private_class_method def self.orientation_check(device, index)
68
+ request_status(response_body('hummingbird', 'in', 'orientation', MICROBIT_ORIENTATIONS[index], device))
69
69
  end
70
70
  end
@@ -4,5 +4,5 @@
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Birdbrain
7
- VERSION = '0.2.1'
7
+ VERSION = '0.9.4'
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birdbrain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - fmorton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-03 00:00:00.000000000 Z
11
+ date: 2021-02-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This Ruby library allows students to use Ruby to read sensors and set
14
14
  motors and LEDs with the Birdbrain Technologies Hummingbird Bit. To use Ruby with