nxt 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +45 -0
  3. data/lib/communication/bluetooth_communication.rb +85 -0
  4. data/lib/communication/serial_port_profile.rb +71 -0
  5. data/lib/nxt.rb +111 -20
  6. data/lib/ruby-nxt.sublime-project +8 -0
  7. data/lib/ruby-nxt.sublime-workspace +288 -0
  8. data/lib/telegrams/commands/direct/get_battery_level.rb +8 -0
  9. data/lib/telegrams/commands/direct/get_current_program_name.rb +8 -0
  10. data/lib/{nxt/commands/sound.rb → telegrams/commands/direct/get_input_values.rb} +0 -0
  11. data/lib/{nxt/commands/tone.rb → telegrams/commands/direct/keep_alive.rb} +0 -0
  12. data/lib/telegrams/commands/direct/ls_get_status.rb +0 -0
  13. data/lib/telegrams/commands/direct/ls_read.rb +0 -0
  14. data/lib/telegrams/commands/direct/ls_write.rb +0 -0
  15. data/lib/telegrams/commands/direct/message_read.rb +0 -0
  16. data/lib/telegrams/commands/direct/message_write.rb +0 -0
  17. data/lib/telegrams/commands/direct/output_state.rb +229 -0
  18. data/lib/telegrams/commands/direct/play_sound_file.rb +38 -0
  19. data/lib/telegrams/commands/direct/play_tone.rb +34 -0
  20. data/lib/telegrams/commands/direct/replies/get_battery_level_reply.rb +28 -0
  21. data/lib/telegrams/commands/direct/replies/get_current_program_name_reply.rb +30 -0
  22. data/lib/telegrams/commands/direct/replies/play_sound_file_reply.rb +13 -0
  23. data/lib/telegrams/commands/direct/replies/play_tone_reply.rb +14 -0
  24. data/lib/telegrams/commands/direct/replies/reset_motor_position_reply.rb +13 -0
  25. data/lib/telegrams/commands/direct/replies/set_input_mode_reply.rb +11 -0
  26. data/lib/telegrams/commands/direct/replies/set_output_state_reply.rb +11 -0
  27. data/lib/telegrams/commands/direct/replies/start_program_reply.rb +13 -0
  28. data/lib/telegrams/commands/direct/replies/stop_program_reply.rb +13 -0
  29. data/lib/telegrams/commands/direct/replies/stop_sound_playback_reply.rb +13 -0
  30. data/lib/telegrams/commands/direct/reset_input_scaled_value.rb +0 -0
  31. data/lib/telegrams/commands/direct/reset_motor_position.rb +8 -0
  32. data/lib/telegrams/commands/direct/set_input_mode.rb +101 -0
  33. data/lib/telegrams/commands/direct/set_output_state.rb +29 -0
  34. data/lib/telegrams/commands/direct/start_program.rb +30 -0
  35. data/lib/telegrams/commands/direct/stop_program.rb +9 -0
  36. data/lib/telegrams/commands/direct/stop_sound_playback.rb +8 -0
  37. data/lib/telegrams/commands/direct_command.rb +8 -0
  38. data/lib/telegrams/commands/direct_command_reply.rb +10 -0
  39. data/lib/telegrams/commands/message_translator.rb +46 -0
  40. data/lib/telegrams/commands/system/get_device_info.rb +8 -0
  41. data/lib/telegrams/commands/system/replies/get_device_info_reply.rb +41 -0
  42. data/lib/telegrams/commands/system_command.rb +8 -0
  43. data/lib/telegrams/messages/error.rb +0 -0
  44. data/lib/telegrams/messages/message.rb +0 -0
  45. data/lib/telegrams/messages/success.rb +0 -0
  46. data/lib/telegrams/no_message_reply.rb +10 -0
  47. data/lib/telegrams/reply.rb +82 -0
  48. data/lib/telegrams/respondable_telegram.rb +29 -0
  49. data/lib/telegrams/telegram.rb +14 -0
  50. data/spec/communication/bluetooth_communication_spec.rb +170 -0
  51. data/spec/communication/serial_port_profile_spec.rb +139 -0
  52. data/spec/helper.rb +1 -0
  53. data/spec/nxt_spec.rb +438 -0
  54. data/spec/telegrams/commands/direct/get_battery_level_spec.rb +26 -0
  55. data/spec/telegrams/commands/direct/get_current_program_name_spec.rb +26 -0
  56. data/spec/telegrams/commands/direct/output_state_spec.rb +198 -0
  57. data/spec/telegrams/commands/direct/play_sound_file_spec.rb +75 -0
  58. data/spec/telegrams/commands/direct/play_tone_spec.rb +63 -0
  59. data/spec/telegrams/commands/direct/replies/get_battery_level_reply_spec.rb +40 -0
  60. data/spec/telegrams/commands/direct/replies/get_current_program_name_reply_spec.rb +33 -0
  61. data/spec/telegrams/commands/direct/replies/play_sound_file_reply_spec.rb +13 -0
  62. data/spec/telegrams/commands/direct/replies/play_tone_reply_spec.rb +14 -0
  63. data/spec/telegrams/commands/direct/replies/reset_motor_position_reply_spec.rb +13 -0
  64. data/spec/telegrams/commands/direct/replies/set_input_mode_reply_spec.rb +12 -0
  65. data/spec/telegrams/commands/direct/replies/set_output_state_reply_spec.rb +12 -0
  66. data/spec/telegrams/commands/direct/replies/start_program_reply_spec.rb +12 -0
  67. data/spec/telegrams/commands/direct/replies/stop_program_reply_spec.rb +13 -0
  68. data/spec/telegrams/commands/direct/replies/stop_sound_playback_reply_spec.rb +13 -0
  69. data/spec/telegrams/commands/direct/reset_motor_position_spec.rb +31 -0
  70. data/spec/telegrams/commands/direct/set_input_mode_spec.rb +122 -0
  71. data/spec/telegrams/commands/direct/set_output_state_spec.rb +72 -0
  72. data/spec/telegrams/commands/direct/start_program_spec.rb +58 -0
  73. data/spec/telegrams/commands/direct/stop_program_spec.rb +34 -0
  74. data/spec/telegrams/commands/direct/stop_sound_playback_spec.rb +34 -0
  75. data/spec/telegrams/commands/direct_command_reply_spec.rb +7 -0
  76. data/spec/telegrams/commands/direct_command_spec.rb +34 -0
  77. data/spec/telegrams/commands/system/get_device_info_spec.rb +16 -0
  78. data/spec/telegrams/commands/system/replies/get_device_info_reply_spec.rb +63 -0
  79. data/spec/telegrams/commands/system_command_spec.rb +26 -0
  80. data/spec/telegrams/no_message_reply_spec.rb +12 -0
  81. data/spec/telegrams/reply_spec.rb +63 -0
  82. data/spec/telegrams/respondable_telegram_spec.rb +66 -0
  83. data/spec/telegrams/telegram_spec.rb +38 -0
  84. metadata +97 -116
  85. data/README.markdown +0 -52
  86. data/Rakefile +0 -35
  87. data/lib/nxt/commands/base.rb +0 -51
  88. data/lib/nxt/commands/input.rb +0 -60
  89. data/lib/nxt/commands/output.rb +0 -105
  90. data/lib/nxt/commands/program.rb +0 -70
  91. data/lib/nxt/connectors/base.rb +0 -35
  92. data/lib/nxt/connectors/input/color.rb +0 -30
  93. data/lib/nxt/connectors/input/touch.rb +0 -11
  94. data/lib/nxt/connectors/input/ultrasonic.rb +0 -11
  95. data/lib/nxt/connectors/output/motor.rb +0 -114
  96. data/lib/nxt/errors.rb +0 -25
  97. data/lib/nxt/exceptions.rb +0 -26
  98. data/lib/nxt/interfaces/base.rb +0 -36
  99. data/lib/nxt/interfaces/serial_port.rb +0 -88
  100. data/lib/nxt/interfaces/usb.rb +0 -8
  101. data/lib/nxt/nxt_brick.rb +0 -167
  102. data/lib/nxt/patches/module.rb +0 -22
  103. data/lib/nxt/patches/string.rb +0 -29
  104. data/lib/nxt/utils/accessors.rb +0 -24
  105. data/spec/matchers.rb +0 -7
  106. data/spec/nxt/connectors/output/motor_spec.rb +0 -55
  107. data/spec/nxt/interfaces/serial_port_spec.rb +0 -73
  108. data/spec/nxt/nxt_brick_spec.rb +0 -199
  109. data/spec/spec_helper.rb +0 -4
@@ -1,22 +0,0 @@
1
- class Module
2
- # Creates an invariant accessor that allows getting and setting from the same
3
- # endpoint. It will operate in getter mode if you don't pass any arguments
4
- # when calling it, otherwise it will work in setter mode. Useful when needing
5
- # to chain methods (you can't chain standard attr_writer methods because
6
- # of the `= something` part).
7
- def attr_combined_accessor(sym, default = nil)
8
- define_method(sym) do |*args|
9
- if args.empty?
10
- instance_var = :"@#{sym}"
11
- if (value = self.instance_variable_get(instance_var))
12
- value
13
- else
14
- self.instance_variable_set(instance_var, default)
15
- default
16
- end
17
- else
18
- send(:"#{sym}=", *args)
19
- end
20
- end
21
- end
22
- end
@@ -1,29 +0,0 @@
1
- class String
2
- # Convert the given string to a hexadecimal representation of the same data.
3
- # This method is non-destructive, it will return a new copy of the string
4
- # convered to hex.
5
- def to_hex_str
6
- str = ''
7
- self.each_byte {|b| str << '0x%02x ' % b}
8
- str
9
- end
10
-
11
- #
12
- def from_hex_str_two
13
- data = self.split(' ')
14
- str = ''
15
- data.each {|h| eval "str += '%c' % #{h}"}
16
- str
17
- end
18
-
19
- def from_hex_str
20
- data = self.split(' ')
21
- str = ''
22
-
23
- data.each do |h|
24
- str += '%c' % h
25
- end
26
-
27
- str
28
- end
29
- end
@@ -1,24 +0,0 @@
1
- module NXT
2
- module Utils
3
- module Accessors
4
- def attr_setter(name, options)
5
- define_method("#{name}=") do |value|
6
- if options.include?(:is)
7
- raise TypeError.new('Expected value to be a #{options[:is]}') unless duration.is_a?(options[:is])
8
- end
9
-
10
- if options.include?(:is_in)
11
- raise TypeError.new("Expected value to be one of: #{options[:is_in].join(', ')}") unless options[:is_in].include?(value)
12
- end
13
-
14
- if options.include?(:is_key_in)
15
- raise TypeError.new("Expected value to be one of: :#{options[:is_key_in].keys.join(', :')}") unless options[:is_key_in].include?(value)
16
- end
17
-
18
- instance_variable_set("@#{name}", value)
19
- self
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,7 +0,0 @@
1
- require 'rspec/expectations'
2
-
3
- RSpec::Matchers.define :have_constant do |expected|
4
- match do |actual|
5
- actual.class.constants.include?(expected)
6
- end
7
- end
@@ -1,55 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe NXT::Connector::Output::Motor do
4
-
5
- subject do
6
- NXT::Connector::Output::Motor.new(nil, nil)
7
- end
8
-
9
- context '#duration' do
10
- # TODO: Should be OK to use any number, but documenting current behavior
11
- it 'should not allow a non-Integer duration' do
12
- expect do
13
- subject.duration(0.5)
14
- end.to raise_exception(TypeError)
15
- end
16
-
17
- it 'should allow valid type options' do
18
- subject.duration(1, :type => :rotations)
19
- subject.duration(1, :type => :seconds)
20
- subject.duration(1, :type => :degrees)
21
- end
22
-
23
- it 'should not allow invalid type option' do
24
- expect do
25
- subject.duration(1, :type => :incorrect_duration_after_type)
26
- end.to raise_exception(TypeError)
27
- end
28
-
29
- it 'should default to seconds as the duration unit' do
30
- subject.duration(1)
31
-
32
- subject.duration_type.should == :seconds
33
- end
34
-
35
- context "setting the :after option" do
36
- # TODO: This isn't right...should change this
37
- # but I'm just documenting current behavior
38
- it 'should prevent using the :after option unless duration_type is :seconds' do
39
- expect do
40
- subject.duration(1, :type => :rotations, :after => :coast)
41
- end.to raise_exception(TypeError)
42
- end
43
-
44
- it 'should allow using the :afer option when the duration type is :seconds' do
45
- subject.duration(1, :type => :seconds, :after => :coast)
46
- end
47
-
48
- it 'should not allow setting an invalid value' do
49
- expect do
50
- subject.duration(1, :type => :seconds, :after => :foobar)
51
- end.to raise_exception(TypeError)
52
- end
53
- end
54
- end
55
- end
@@ -1,73 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe NXT::Interface::SerialPort do
4
- before do
5
- @device = '/dev/zero'
6
- @bad_device = 'hello world'
7
- end
8
-
9
- subject do
10
- NXT::Interface::SerialPort.new(@device)
11
- end
12
-
13
- describe 'constants' do
14
- it 'should have a BAUD_RATE constant' do
15
- should have_constant(:BAUD_RATE)
16
- should have_constant(:DATA_BITS)
17
- should have_constant(:STOP_BITS)
18
- should have_constant(:PARITY)
19
- should have_constant(:READ_TIMEOUT)
20
- end
21
- end
22
-
23
- describe 'accessors' do
24
- it 'should have read/write accessor for @dev' do
25
- should respond_to(:dev)
26
- should respond_to(:dev=)
27
- end
28
- end
29
-
30
- describe '#initialize' do
31
- it 'should set the device to the incomming argument' do
32
- subject.dev.should equal(@device)
33
- end
34
-
35
- it 'should raise an exception when trying to connect to invalid dev files' do
36
- expect do
37
- serial_port = subject.class.new(@bad_device)
38
- end.to raise_exception(InvalidDeviceError)
39
- end
40
- end
41
-
42
- describe '#connect' do
43
- it 'should raise an exception when the SerialPort connection failed' do
44
- expect do
45
- subject.connect
46
- end.to raise_exception(SerialPortConnectionError, "The #{@device} device is not a valid SerialPort")
47
- end
48
-
49
- it 'should raise an exception when the SerialPort connection is nil' do
50
- ::SerialPort.should_receive(:new).and_return(nil)
51
- expect do
52
- subject.connect
53
- end.to raise_exception(SerialPortConnectionError, "Could not establish a SerialPort connection to #{@device}")
54
- end
55
-
56
- it 'should set the flow control and read timeout when the connection is established' do
57
- serial_port_stub = stub()
58
- serial_port_stub.should_receive(:flow_control=).with(::SerialPort::HARD).once
59
- serial_port_stub.should_receive(:read_timeout=).with(subject.class::READ_TIMEOUT).once
60
- ::SerialPort.should_receive(:new).and_return(serial_port_stub)
61
-
62
- subject.connect
63
- end
64
- end
65
-
66
- describe '#send' do
67
-
68
- end
69
-
70
- describe '#receive' do
71
-
72
- end
73
- end
@@ -1,199 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module NXT
4
- module Interface
5
- class MockInterface < Base
6
- def connect; end
7
- def disconnect; end
8
- end
9
- end
10
- end
11
-
12
- describe NXTBrick do
13
- before do
14
- @interface = :mock_interface
15
- end
16
-
17
- subject do
18
- NXTBrick.new(@interface)
19
- end
20
-
21
- describe 'accessors' do
22
- it 'should have read/write accessors for @interface' do
23
- should respond_to(:interface)
24
- should respond_to(:interface=)
25
- end
26
-
27
- it 'should have a read accessor for @a' do
28
- should respond_to(:a)
29
- should_not respond_to(:a=)
30
- end
31
-
32
- it 'should have a read accessor for @b' do
33
- should respond_to(:b)
34
- should_not respond_to(:b=)
35
- end
36
-
37
- it 'should have a read accessor for @c' do
38
- should respond_to(:c)
39
- should_not respond_to(:c=)
40
- end
41
-
42
- it 'should have a read accessor for @one' do
43
- should respond_to(:one)
44
- should_not respond_to(:one=)
45
- end
46
-
47
- it 'should have a read accessor for @two' do
48
- should respond_to(:two)
49
- should_not respond_to(:two=)
50
- end
51
-
52
- it 'should have a read accessor for @three' do
53
- should respond_to(:three)
54
- should_not respond_to(:three=)
55
- end
56
-
57
- it 'should have a read accessor for @four' do
58
- should respond_to(:four)
59
- should_not respond_to(:four=)
60
- end
61
-
62
- it 'should have a read accessor for @port_identifiers' do
63
- should respond_to(:port_identifiers)
64
- should_not respond_to(:port_identifiers=)
65
- end
66
- end
67
-
68
- describe '#initialize' do
69
- it 'should raise an exception if an invalid type of interface is given' do
70
- expect do
71
- NXTBrick.new(:foobar_biz_buzz)
72
- end.to raise_exception(InvalidInterfaceError)
73
- end
74
-
75
- it 'should set the interface to the incomming argument' do
76
- subject.interface.class.should equal(NXT::Interface::MockInterface)
77
- end
78
-
79
- it 'should call yield if given a block, passing self' do
80
- block_called = false
81
-
82
- NXTBrick.new(@interface) do |nxt|
83
- block_called = true
84
- nxt.should be_an_instance_of(NXTBrick)
85
- end
86
-
87
- block_called.should be_true
88
- end
89
- end
90
-
91
- describe '#add' do
92
- it 'should raise an exception if an invalid type of port is given' do
93
- expect do
94
- subject.add('not a symbol', :symbol, Class)
95
- end.to raise_exception(TypeError, 'Expected port to be a Symbol')
96
- end
97
-
98
- it 'should raise an exception if an invalid type of identifier is given' do
99
- expect do
100
- subject.add(:symbol, 'not a symbol', Class)
101
- end.to raise_exception(TypeError, 'Expected identifier to be a Symbol')
102
- end
103
-
104
- it 'should raise an exception if an invalid type of klass is given' do
105
- expect do
106
- subject.add(:symbol, :symbol, 'not a class')
107
- end.to raise_exception(TypeError, 'Expected klass to be a Class')
108
- end
109
-
110
- it 'should raise an exception if an invalid port number or letter is given' do
111
- expect do
112
- subject.add(:invalid_port, :symbol, Class)
113
- end.to raise_exception(TypeError, 'Expected port to be one of: :a, :b, :c, :one, :two, :three, :four')
114
- end
115
-
116
- it 'should create a new instance of the passed klass and store it in the attribute for the given port' do
117
- port = :a
118
- class_stub = Class.new
119
- class_return_stub = stub()
120
-
121
- class_stub.should_receive(:new) do
122
- class_return_stub
123
- end.with(port, anything).once()
124
-
125
- subject.add(port, :hello, class_stub)
126
-
127
- subject.send(port).should equal(class_return_stub)
128
- end
129
-
130
- it 'should raise an exception if the port given is already set' do
131
- port = :a
132
-
133
- class_stub = Class.new
134
- class_stub.stub(:new)
135
- subject.stub(:hello)
136
- subject.instance_variable_set(:"@#{port}", 'some value already there')
137
-
138
- expect do
139
- subject.add(port, :hello, class_stub)
140
- end.to raise_error(PortTakenError, "Port #{port} is already set, call remove first")
141
- end
142
-
143
- it 'should raise an exception if trying to use an identifier that is the name of a defined methodz' do
144
- port = :a
145
- identifier = :hello
146
-
147
- class_stub = Class.new
148
- class_stub.stub(:new)
149
- subject.stub(identifier)
150
-
151
- expect do
152
- subject.add(port, :hello, class_stub)
153
- end.to raise_error(InvalidIdentifierError, "Cannot use identifier #{identifier}, a method on NXTBrick is already using it.")
154
- end
155
-
156
- it 'should set up the port identifiers correctly' do
157
- port = :a
158
- identifier = :hello_world
159
- class_stub = Class.new
160
- class_stub.stub(:new)
161
-
162
- subject.add(port, identifier, class_stub)
163
-
164
- subject.port_identifiers[identifier].should equal(port)
165
- end
166
- end
167
-
168
- describe '#remove' do
169
- it 'should raise an exception if an invalid type of identifier is given' do
170
- expect do
171
- subject.remove('not a symbol')
172
- end.to raise_exception(TypeError, 'Expected identifier to be a Symbol')
173
- end
174
-
175
- it 'should remove any matching identifiers' do
176
- identifier = :hello_world
177
- port_identifiers = {}
178
- subject.instance_variable_set(:@port_identifiers, port_identifiers)
179
-
180
- port_identifiers.should_receive(:delete).with(identifier).once()
181
- subject.remove(identifier)
182
- end
183
-
184
- it 'should return a boolean indicating whether it removed anything' do
185
- identifier = :hello_world
186
- port_identifiers = {}
187
- port_identifiers[identifier] = true
188
- subject.instance_variable_set(:@port_identifiers, port_identifiers)
189
-
190
- return_value = subject.remove(identifier)
191
- return_value.should be_true
192
-
193
- port_identifiers.should_not include(identifier)
194
-
195
- return_value = subject.remove(identifier)
196
- return_value.should be_false
197
- end
198
- end
199
- end
@@ -1,4 +0,0 @@
1
- require 'matchers'
2
- require 'nxt'
3
-
4
- include NXT::Exceptions