Class: NXT

Inherits:
Object
  • Object
show all
Defined in:
lib/nxt.rb

Direct Known Subclasses

NXTAsync

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_path, communication = nil) ⇒ NXT

Returns a new instance of NXT.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/nxt.rb', line 17

def initialize(device_path, communication=nil)
  raise ArgumentError, "device_path must be non-nil" if device_path.nil?

  @wait_for_reply = true
  @device_path = device_path
  @communication = communication || BluetoothCommunication.new(device_path)
  @async = nil
end

Instance Attribute Details

#communicationObject (readonly)

Returns the value of attribute communication.



15
16
17
# File 'lib/nxt.rb', line 15

def communication
  @communication
end

#device_pathObject (readonly)

Returns the value of attribute device_path.



15
16
17
# File 'lib/nxt.rb', line 15

def device_path
  @device_path
end

#wait_for_replyObject (readonly)

Returns the value of attribute wait_for_reply.



15
16
17
# File 'lib/nxt.rb', line 15

def wait_for_reply
  @wait_for_reply
end

Instance Method Details

#asyncObject



36
37
38
39
# File 'lib/nxt.rb', line 36

def async
  @async = NXTAsync.new(@device_path, @communication) if @async.nil?
  @async
end

#connectObject



26
27
28
29
# File 'lib/nxt.rb', line 26

def connect
  @communication.connect
  self
end

#disconnectObject



31
32
33
34
# File 'lib/nxt.rb', line 31

def disconnect
  @communication.disconnect
  self
end

#get_battery_levelObject



75
76
77
# File 'lib/nxt.rb', line 75

def get_battery_level
  send_message(GetBatteryLevel.new, GetBatteryLevelReply)
end

#get_current_program_nameObject



50
51
52
53
# File 'lib/nxt.rb', line 50

def get_current_program_name
  # always requires a reply, so no constructor argument to allow an override
  send_message(GetCurrentProgramName.new, GetCurrentProgramNameReply)
end

#get_device_infoObject

System commands



97
98
99
100
# File 'lib/nxt.rb', line 97

def get_device_info
  # always requires a reply, so no constructor argument to allow an override
  send_message(GetDeviceInfo.new, GetDeviceInfoReply)
end

#play_sound_file(sound_filename, loop_sound = false) ⇒ Object



59
60
61
# File 'lib/nxt.rb', line 59

def play_sound_file(sound_filename, loop_sound=false)
  send_message(PlaySoundFile.new(sound_filename, loop_sound, wait_for_reply), PlaySoundFileReply)
end

#play_tone(frequency_hz, duration_ms) ⇒ Object



63
64
65
# File 'lib/nxt.rb', line 63

def play_tone(frequency_hz, duration_ms)
  send_message(PlayTone.new(frequency_hz, duration_ms, wait_for_reply), PlayToneReply)
end

#reset_motor_positionObject



79
80
81
# File 'lib/nxt.rb', line 79

def reset_motor_position
  send_message(ResetMotorPosition.new(wait_for_reply), ResetMotorPositionReply)
end

#send_message(command, reply_type = nil) ⇒ Object

Lowest-level API



103
104
105
# File 'lib/nxt.rb', line 103

def send_message(command, reply_type=nil)
  @communication.send_message(command, reply_type)
end

#set_input_mode(input_port, sensor_type, sensor_mode) ⇒ Object



67
68
69
# File 'lib/nxt.rb', line 67

def set_input_mode(input_port, sensor_type, sensor_mode)
  send_message(SetInputMode.new(input_port, sensor_type, sensor_mode, wait_for_reply), SetInputModeReply)
end

#set_output_state(output_state) ⇒ Object



71
72
73
# File 'lib/nxt.rb', line 71

def set_output_state(output_state)
  send_message(SetOutputState.new(output_state, wait_for_reply),SetOutputStateReply)
end

#start_program(program_name) ⇒ Object



46
47
48
# File 'lib/nxt.rb', line 46

def start_program(program_name)
  send_message(StartProgram.new(program_name, wait_for_reply), StartProgramReply)
end

#stop_programObject

Direct commands



42
43
44
# File 'lib/nxt.rb', line 42

def stop_program
  send_message(StopProgram.new(wait_for_reply), StopProgramReply)
end

#stop_sound_playbackObject



55
56
57
# File 'lib/nxt.rb', line 55

def stop_sound_playback
  send_message(StopSoundPlayback.new(wait_for_reply), StopSoundPlaybackReply)
end