Class: Ftdi::Context
- Inherits:
-
FFI::ManagedStruct
- Object
- FFI::ManagedStruct
- Ftdi::Context
- Defined in:
- lib/ftdi.rb
Overview
Represents libftdi context and end-user API.
Class Method Summary (collapse)
-
+ (NilClass) release(p)
Deinitialize and free an ftdi context.
Instance Method Summary (collapse)
-
- (Fixnum) baudrate
Gets the chip baud rate.
-
- (NilClass) baudrate=(new_baudrate)
Sets the chip baud rate.
-
- (String) error_string
Gets error text.
-
- (Fixnum) flowctrl=(new_flowctrl)
Set flow control setting for ftdi chip.
-
- (Context) initialize
constructor
Initializes new libftdi context.
-
- (Interface) interface
Gets used interface of the device.
-
- (Interface) interface=(new_interface)
Open selected channels on a chip, otherwise use first channel.
-
- (String) read_data
Reads data in chunks from the chip.
-
- (Fixnum) read_data_chunksize
Gets read buffer chunk size.
-
- (Fixnum) read_data_chunksize=(new_chunksize)
Configure read buffer chunk size.
-
- (Fixnum) read_pins
Directly read pin state, circumventing the read buffer.
-
- (NilClass) set_bitmode(bitmask, mode)
Set Bitbang mode for ftdi chip.
-
- (NilClass) set_line_property(bits, stopbits, parity)
Set (RS232) line characteristics.
-
- (NilClass) set_line_property2(bits, stopbits, parity, _break)
Set (RS232) line characteristics.
-
- (NilClass) usb_close
Closes the ftdi device.
-
- (NilClass) usb_open(vendor, product)
Opens the first device with a given vendor and product ids.
-
- (NilClass) usb_open_desc(vendor, product, description, serial)
Opens the first device with a given vendor and product ids, description and serial.
-
- (NilClass) usb_open_desc_index(vendor, product, description, serial, index)
Opens the index-th device with a given vendor and product ids, description and serial.
-
- (NilClass) usb_reset
Resets the ftdi device.
-
- (Fixnum) write_data(bytes)
Writes data.
-
- (Fixnum) write_data_chunksize
Gets write buffer chunk size.
-
- (Fixnum) write_data_chunksize=(new_chunksize)
Configure write buffer chunk size.
Constructor Details
- (Context) initialize
Initializes new libftdi context.
147 148 149 150 151 |
# File 'lib/ftdi.rb', line 147 def initialize ptr = Ftdi.ftdi_new raise CannotInitializeContextError.new if ptr.nil? super(ptr) end |
Class Method Details
+ (NilClass) release(p)
Deinitialize and free an ftdi context.
155 156 157 158 |
# File 'lib/ftdi.rb', line 155 def self.release(p) Ftdi.ftdi_free(p) nil end |
Instance Method Details
- (Fixnum) baudrate
Gets the chip baud rate.
225 226 227 |
# File 'lib/ftdi.rb', line 225 def baudrate self[:baudrate] end |
- (NilClass) baudrate=(new_baudrate)
Sets the chip baud rate.
233 234 235 236 |
# File 'lib/ftdi.rb', line 233 def baudrate=(new_baudrate) raise ArgumentError.new('baudrate should be Fixnum') unless new_baudrate.kind_of?(Fixnum) check_result(Ftdi.ftdi_set_baudrate(ctx, new_baudrate)) end |
- (String) error_string
Gets error text.
162 163 164 |
# File 'lib/ftdi.rb', line 162 def error_string self[:error_str] end |
- (Fixnum) flowctrl=(new_flowctrl)
Set flow control setting for ftdi chip.
268 269 270 271 |
# File 'lib/ftdi.rb', line 268 def flowctrl=(new_flowctrl) check_result(Ftdi.ftdi_setflowctrl(ctx, new_flowctrl)) new_flowctrl end |
- (Interface) interface
Gets used interface of the device.
366 367 368 |
# File 'lib/ftdi.rb', line 366 def interface Interface[self[:interface]] end |
- (Interface) interface=(new_interface)
Open selected channels on a chip, otherwise use first channel.
374 375 376 377 |
# File 'lib/ftdi.rb', line 374 def interface=(new_interface) check_result(Ftdi.ftdi_set_interface(ctx, new_interface)) new_interface end |
- (String) read_data
Reads data in chunks from the chip. Returns when at least one byte is available or when the latency timer has elapsed. Automatically strips the two modem status bytes transfered during every read.
344 345 346 347 348 349 350 351 352 |
# File 'lib/ftdi.rb', line 344 def read_data chunksize = read_data_chunksize p = FFI::MemoryPointer.new(:char, chunksize) bytes_read = Ftdi.ftdi_read_data(ctx, p, chunksize) check_result(bytes_read) r = p.read_bytes(bytes_read) r.force_encoding("ASCII-8BIT") if r.respond_to?(:force_encoding) r end |
- (Fixnum) read_data_chunksize
Gets read buffer chunk size.
321 322 323 324 325 |
# File 'lib/ftdi.rb', line 321 def read_data_chunksize p = FFI::MemoryPointer.new(:uint, 1) check_result(Ftdi.ftdi_read_data_get_chunksize(ctx, p)) p.read_uint end |
- (Fixnum) read_data_chunksize=(new_chunksize)
Default is 4096.
Configure read buffer chunk size. Automatically reallocates the buffer.
333 334 335 336 |
# File 'lib/ftdi.rb', line 333 def read_data_chunksize=(new_chunksize) check_result(Ftdi.ftdi_read_data_set_chunksize(ctx, new_chunksize)) new_chunksize end |
- (Fixnum) read_pins
Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
358 359 360 361 362 |
# File 'lib/ftdi.rb', line 358 def read_pins p = FFI::MemoryPointer.new(:uchar, 1) check_result(Ftdi.ftdi_read_pins(ctx, p)) p.read_uchar end |
- (NilClass) set_bitmode(bitmask, mode)
Set Bitbang mode for ftdi chip.
278 279 280 |
# File 'lib/ftdi.rb', line 278 def set_bitmode(bitmask, mode) check_result(Ftdi.ftdi_set_bitmode(ctx, bitmask, mode)) end |
- (NilClass) set_line_property(bits, stopbits, parity)
Set (RS232) line characteristics. The break type can only be set via #set_line_property2 and defaults to "off".
245 246 247 |
# File 'lib/ftdi.rb', line 245 def set_line_property(bits, stopbits, parity) check_result(Ftdi.ftdi_set_line_property(ctx, bits, stopbits, parity)) end |
- (NilClass) set_line_property2(bits, stopbits, parity, _break)
Set (RS232) line characteristics.
256 257 258 |
# File 'lib/ftdi.rb', line 256 def set_line_property2(bits, stopbits, parity, _break) check_result(Ftdi.ftdi_set_line_property2(ctx, bits, stopbits, parity, _break)) end |
- (NilClass) usb_close
Closes the ftdi device.
218 219 220 221 |
# File 'lib/ftdi.rb', line 218 def usb_close Ftdi.ftdi_usb_close(ctx) nil end |
- (NilClass) usb_open(vendor, product)
Opens the first device with a given vendor and product ids.
172 173 174 175 176 |
# File 'lib/ftdi.rb', line 172 def usb_open(vendor, product) raise ArgumentError.new('vendor should be Fixnum') unless vendor.kind_of?(Fixnum) raise ArgumentError.new('product should be Fixnum') unless product.kind_of?(Fixnum) check_result(Ftdi.ftdi_usb_open(ctx, vendor, product)) end |
- (NilClass) usb_open_desc(vendor, product, description, serial)
Opens the first device with a given vendor and product ids, description and serial.
186 187 188 189 190 |
# File 'lib/ftdi.rb', line 186 def usb_open_desc(vendor, product, description, serial) raise ArgumentError.new('vendor should be Fixnum') unless vendor.kind_of?(Fixnum) raise ArgumentError.new('product should be Fixnum') unless product.kind_of?(Fixnum) check_result(Ftdi.ftdi_usb_open_desc(ctx, vendor, product, description, serial)) end |
- (NilClass) usb_open_desc_index(vendor, product, description, serial, index)
Opens the index-th device with a given vendor and product ids, description and serial.
201 202 203 204 205 206 207 |
# File 'lib/ftdi.rb', line 201 def usb_open_desc_index(vendor, product, description, serial, index) raise ArgumentError.new('vendor should be Fixnum') unless vendor.kind_of?(Fixnum) raise ArgumentError.new('product should be Fixnum') unless product.kind_of?(Fixnum) raise ArgumentError.new('index should be Fixnum') unless index.kind_of?(Fixnum) raise ArgumentError.new('index should be greater than or equal to zero') if index < 0 check_result(Ftdi.ftdi_usb_open_desc_index(ctx, vendor, product, description, serial, index)) end |
- (NilClass) usb_reset
Resets the ftdi device.
212 213 214 |
# File 'lib/ftdi.rb', line 212 def usb_reset check_result(Ftdi.ftdi_usb_reset(ctx)) end |
- (Fixnum) write_data(bytes)
Writes data.
307 308 309 310 311 312 313 314 315 |
# File 'lib/ftdi.rb', line 307 def write_data(bytes) bytes = bytes.pack('c*') if bytes.respond_to?(:pack) size = bytes.respond_to?(:bytesize) ? bytes.bytesize : bytes.size mem_buf = FFI::MemoryPointer.new(:char, size) mem_buf.put_bytes(0, bytes) bytes_written = Ftdi.ftdi_write_data(ctx, mem_buf, size) check_result(bytes_written) bytes_written end |
- (Fixnum) write_data_chunksize
Gets write buffer chunk size.
286 287 288 289 290 |
# File 'lib/ftdi.rb', line 286 def write_data_chunksize p = FFI::MemoryPointer.new(:uint, 1) check_result(Ftdi.ftdi_write_data_get_chunksize(ctx, p)) p.read_uint end |
- (Fixnum) write_data_chunksize=(new_chunksize)
Default is 4096.
Configure write buffer chunk size. Automatically reallocates the buffer.
298 299 300 301 |
# File 'lib/ftdi.rb', line 298 def write_data_chunksize=(new_chunksize) check_result(Ftdi.ftdi_write_data_set_chunksize(ctx, new_chunksize)) new_chunksize end |