birdbrain 0.2.1 → 0.9.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 927e03b41176f419971ac1faed0d8243638448c044918a2035151dd8a05a796e
4
- data.tar.gz: 5f365f6debd7c9af03de923075613252a7c96a47ba94ae683991c7426d24bdf9
3
+ metadata.gz: cb21675457d7a7b695a6f4b4858987a08cc68ebf295c1d3d869e8f0b5f5a5df6
4
+ data.tar.gz: cac303fd2d781c1422ba1409e6f18d95b0878d08fb46809dc850c0beefa95e83
5
5
  SHA512:
6
- metadata.gz: 847a2bbabefcc8cb78319770ced934d7f60c3b76a6d5124a68a53cd0f1580cf63b472c063bc5da6650461a2ea4561792d46488554723ac427f3f4b0f81b2cbc9
7
- data.tar.gz: a5f1cb8a3e84329609004645cc1777a2b5ec505c12156e6547977096c670ef535397a9cbc34778722743e884234ae0d949fd8959b29a9f239840ed4063165dc9
6
+ metadata.gz: 507384baf7913ffd3cfac814352828169d92b73134846fbac52566416e1ecc87e3e798f9921e3f565ccb7b3ed61a16c892fcd1722f11340c0a2edf41e79be54e
7
+ data.tar.gz: 4e24f99216233d97443b45a88139e68515239d75c0ef1c07265cdd2f8648361e6bf9c6f17105fbffcc50edd660e8554b3f5dc2569a5bda25903ba5b7d8c4d80c
@@ -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
@@ -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.0'
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.0
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