Class: Phidgets::IR

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/phidgets-ffi/ir.rb

Overview

This class represents a PhidgetIR.

Defined Under Namespace

Classes: IR_code_info

Constant Summary collapse

Klass =
Phidgets::FFI::CPhidgetIR

Instance Attribute Summary

Attributes included from Common

#attributes

Instance Method Summary collapse

Methods included from Common

#attached?, #attached_to_server?, attributes, #close, #detached?, #detached_to_server?, device_class, #device_class, device_id, #id, #initialize, #label, #label=, #name, #on_attach, #on_detach, #on_error, #on_server_connect, #on_server_disconnect, #on_sleep, #on_wake, #serial_number, #server_address, server_address, #server_id, server_id, server_status, #type, #version, #wait_for_attachment

Instance Method Details

#last_codeArray<String>, ...

Gets the last code that was received.

Returns:

  • (Array<String>)

    returns the last code, or raises an error.

  • (Integer)

    returns the data length, or raises an error.

  • (Object)

    returns the bit count, or raises an error.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/phidgets-ffi/ir.rb', line 230

def last_code
		data_ffi = ::FFI::MemoryPointer.new(:uchar, 16)
		
		data_length = ::FFI::MemoryPointer.new(:int)
		data_length.write_int(16)

		bit_count = ::FFI::MemoryPointer.new(:int)
		
		Klass.getLastCode(@handle, data_ffi, data_length, bit_count)

		data = []
		
		data_length.get_int(0).times { |i|
data << data_ffi[i].get_uchar(0).to_s(16)
		}
		
  [data, data_length.get_int(0), bit_count.get_int(0)]

end

#last_learned_codeArray<String>, ...

Gets the last code that was learned.

Returns:

  • (Array<String>)

    returns the last learned code, or raises an error.

  • (Integer)

    returns the data length, or raises an error.

  • (IR_code_info)

    returns the code info structure for the learned code, or raises an error.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/phidgets-ffi/ir.rb', line 254

def last_learned_code

	data_ffi = ::FFI::MemoryPointer.new(:uchar, 16)
	
	data_length = ::FFI::MemoryPointer.new(:int)
	data_length.write_int(16)

	code_info = ::FFI::MemoryPointer.new(IR_code_info)
	
	Klass.getLastLearnedCode(@handle, data_ffi, data_length, code_info)
	
	data = []
	
	data_length.get_int(0).times { |i|
		 data << data_ffi[i].get_uchar(0).to_s(16)
	}

       code_info_struct = IR_code_info.new(code_info)	

	[data, data_length.get_int(0), code_info_struct]		
		
end

#on_code(obj = nil, &block) ⇒ Boolean

Sets a code handler callback function. This is called whenever a new code is recognized.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

ir.on_code do |device, data, data_length, bit_count, repeat, obj|
  puts "Code #{data} received, length: #{data_length}, bit count: #{bit_count}, repeat: #{repeat}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/phidgets-ffi/ir.rb', line 129

def on_code(obj=nil, &block)
	  @on_code_obj = obj
  @on_code = Proc.new { |device, obj_ptr, data, data_length, bit_count, repeat|
		data_string = []
		data_length.times { |i|
			data_string[i] = data[i].get_uchar(0).to_s(16)
		}
		
 yield self, data_string, data_length, bit_count, repeat, object_for(obj_ptr)
	}
  Klass.set_OnCode_Handler(@handle, @on_code, pointer_for(obj))
end

#on_learn(obj = nil, &block) ⇒ Boolean

Sets a learn handler callback function. This is called when a new code has been learned. This generally requires the button to be held down for a second or two.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

ir.on_learn do |device, data, data_length, code_info, obj|
  puts "Code #{data} learnt"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/phidgets-ffi/ir.rb', line 152

def on_learn(obj=nil, &block)
  @on_learn_obj = obj
  @on_learn = Proc.new { |device, obj_ptr, int_data, data_length, code_info|
		data = []
		data_length.times { |i|
			data[i] = int_data[i].get_uchar(0).to_s(16)
		}
		
		code_info_struct = IR_code_info.new(code_info)	

 yield self, data, data_length, code_info_struct, object_for(obj_ptr)
	}
  Klass.set_OnLearn_Handler(@handle, @on_learn, pointer_for(obj))
end

#on_raw_data(obj = nil, &block) ⇒ Boolean

Sets a raw data handler callback function. This is called whenever a new IR data is available. Data is in the form of an array of microsecond pulse values. This can be used if the user wishes to do their own data decoding, or for codes that the PhidgetIR cannot automatically recognize.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

ir.on_raw_data do |device, raw_data, data_length, obj|
  puts "Raw data: #{raw_data}, length: #{data_length}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/phidgets-ffi/ir.rb', line 106

def on_raw_data(obj=nil, &block)
	  @on_raw_data_obj = obj
  @on_raw_data = Proc.new { |device, obj_ptr, raw_data, data_length|
		data = []
		data_length.times { |i|		
			data << raw_data[i].get_int(0)
		}
		
 yield self, data, data_length, object_for(obj_ptr)
	}
  Klass.set_OnRawData_Handler(@handle, @on_raw_data, pointer_for(obj))
end

#read_raw_dataArray<Integer>, Integer

Reads any available raw data. This should be polled continuously(every 20ms) to avoid missing data. Read data always starts with a space and ends with a pulse.

Returns:

  • (Array<Integer>)

    returns true if the raw data was successfully transmitted, or raises an error.

  • (Integer)

    returns the data length, or raises an error.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/phidgets-ffi/ir.rb', line 209

def read_raw_data
	data_ffi = ::FFI::MemoryPointer.new(:int, 16)

	data_length = ::FFI::MemoryPointer.new(:int)
	data_length.write_int(16)

	Klass.getRawData(@handle, data_ffi, data_length)
	
	data = []
	
	data_length.get_int(0).times { |i|
		data << data_ffi[i].get_int(0)
	}
	
	[data, data_length.get_int(0)]
end

#transmit(data, code_info) ⇒ Boolean

Transmits a code

Parameters:

  • data (Array<String>)

    data to send

  • code_info (IR_code_info)

    code info structure specifying the attributes of the code to send. Anything that is not set is set to default.

Returns:

  • (Boolean)

    returns true if the code was successfully transmitted, or raises an error.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/phidgets-ffi/ir.rb', line 171

def transmit(data, code_info)

		pdata = ::FFI::MemoryPointer.new(:uchar, 16)
	
		data_ffi = []
		data.size.times { |i|
data_ffi[i] = data[i].to_i(16)
		}
		
		data_ffi = ::FFI::MemoryPointer.new(:uchar, 16).write_array_of_uchar(data_ffi)

		Klass.Transmit(@handle, data_ffi, code_info)  
		true
end

#transmit_raw(data, length, carrier_frequency, duty_cycle, gap) ⇒ Boolean

Transmits raw data as a series of pulses and spaces.

Parameters:

  • data (Array)

    data to send

  • length (Integer)

    length of the data array

  • carrier_frequency (Integer)

    carrier frequency in Hz. Leave as 0 for default

  • duty_cycle (Integer)

    duty cycle(10-50). Leave as 0 for default

  • gap_time (Integer)

    gap time in us. This guarantees a gap time(no transmitting) after the data is sent, but can be set to 0

Returns:

  • (Boolean)

    returns true if the raw data was successfully transmitted, or raises an error.



200
201
202
203
204
# File 'lib/phidgets-ffi/ir.rb', line 200

def transmit_raw(data, length, carrier_frequency, duty_cycle, gap)
		c_data = ::FFI::MemoryPointer.new(:int, data.size).write_array_of_int(data)
		Klass.TransmitRaw(@handle, c_data, length.to_i, carrier_frequency.to_i, duty_cycle.to_i, gap.to_i)
		true
end

#transmit_repeatBoolean

Transmits a repeat of a previously transmitted code, or raises an error.

Returns:

  • (Boolean)

    returns true if the code was successfully transmitted, or raises an error.



188
189
190
191
# File 'lib/phidgets-ffi/ir.rb', line 188

def transmit_repeat
		Klass.TransmitRepeat(@handle)
	  true
end