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
-
.release(p) ⇒ NilClass
Deinitialize and free an ftdi context.
Instance Method Summary collapse
-
#baudrate ⇒ Fixnum
Gets the chip baud rate.
-
#baudrate=(new_baudrate) ⇒ NilClass
Sets the chip baud rate.
-
#dtr=(value) ⇒ Fixnum
Set the DTR control line value.
-
#error_string ⇒ String
Gets error text.
-
#flowctrl=(new_flowctrl) ⇒ Fixnum
Set flow control setting for ftdi chip.
-
#initialize ⇒ Context
constructor
Initializes new libftdi context.
-
#interface ⇒ Interface
Gets used interface of the device.
-
#interface=(new_interface) ⇒ Interface
Open selected channels on a chip, otherwise use first channel.
-
#read_data ⇒ String
Reads data in chunks from the chip.
-
#read_data_chunksize ⇒ Fixnum
Gets read buffer chunk size.
-
#read_data_chunksize=(new_chunksize) ⇒ Fixnum
Configure read buffer chunk size.
-
#read_pins ⇒ Fixnum
Directly read pin state, circumventing the read buffer.
-
#rts=(value) ⇒ Fixnum
Set the RTS control line value.
-
#set_bitmode(bitmask, mode) ⇒ NilClass
Set Bitbang mode for ftdi chip.
-
#set_line_property(bits, stopbits, parity) ⇒ NilClass
Set (RS232) line characteristics.
-
#set_line_property2(bits, stopbits, parity, _break) ⇒ NilClass
Set (RS232) line characteristics.
-
#usb_close ⇒ NilClass
Closes the ftdi device.
-
#usb_open(vendor, product) ⇒ NilClass
Opens the first device with a given vendor and product ids.
-
#usb_open_desc(vendor, product, description, serial) ⇒ NilClass
Opens the first device with a given vendor and product ids, description and serial.
-
#usb_open_desc_index(vendor, product, description, serial, index) ⇒ NilClass
Opens the index-th device with a given vendor and product ids, description and serial.
-
#usb_reset ⇒ NilClass
Resets the ftdi device.
-
#write_data(bytes) ⇒ Fixnum
Writes data.
-
#write_data_chunksize ⇒ Fixnum
Gets write buffer chunk size.
-
#write_data_chunksize=(new_chunksize) ⇒ Fixnum
Configure write buffer chunk size.
Constructor Details
#initialize ⇒ Context
Initializes new libftdi context.
150 151 152 153 154 |
# File 'lib/ftdi.rb', line 150 def initialize ptr = Ftdi.ftdi_new raise CannotInitializeContextError.new if ptr.nil? super(ptr) end |
Class Method Details
.release(p) ⇒ NilClass
Deinitialize and free an ftdi context.
158 159 160 161 |
# File 'lib/ftdi.rb', line 158 def self.release(p) Ftdi.ftdi_free(p) nil end |
Instance Method Details
#baudrate ⇒ Fixnum
Gets the chip baud rate.
228 229 230 |
# File 'lib/ftdi.rb', line 228 def baudrate self[:baudrate] end |
#baudrate=(new_baudrate) ⇒ NilClass
Sets the chip baud rate.
236 237 238 239 |
# File 'lib/ftdi.rb', line 236 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 |
#dtr=(value) ⇒ Fixnum
Set the DTR control line value.
386 387 388 389 |
# File 'lib/ftdi.rb', line 386 def dtr=(value) check_result(Ftdi.ftdi_setdtr(ctx, value)) value end |
#error_string ⇒ String
Gets error text.
165 166 167 |
# File 'lib/ftdi.rb', line 165 def error_string self[:error_str] end |
#flowctrl=(new_flowctrl) ⇒ Fixnum
Set flow control setting for ftdi chip.
271 272 273 274 |
# File 'lib/ftdi.rb', line 271 def flowctrl=(new_flowctrl) check_result(Ftdi.ftdi_setflowctrl(ctx, new_flowctrl)) new_flowctrl end |
#interface ⇒ Interface
Gets used interface of the device.
369 370 371 |
# File 'lib/ftdi.rb', line 369 def interface Interface[self[:interface]] end |
#interface=(new_interface) ⇒ Interface
Open selected channels on a chip, otherwise use first channel.
377 378 379 380 |
# File 'lib/ftdi.rb', line 377 def interface=(new_interface) check_result(Ftdi.ftdi_set_interface(ctx, new_interface)) new_interface end |
#read_data ⇒ String
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.
347 348 349 350 351 352 353 354 355 |
# File 'lib/ftdi.rb', line 347 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 |
#read_data_chunksize ⇒ Fixnum
Gets read buffer chunk size.
324 325 326 327 328 |
# File 'lib/ftdi.rb', line 324 def read_data_chunksize p = FFI::MemoryPointer.new(:uint, 1) check_result(Ftdi.ftdi_read_data_get_chunksize(ctx, p)) p.read_uint end |
#read_data_chunksize=(new_chunksize) ⇒ Fixnum
Default is 4096.
Configure read buffer chunk size. Automatically reallocates the buffer.
336 337 338 339 |
# File 'lib/ftdi.rb', line 336 def read_data_chunksize=(new_chunksize) check_result(Ftdi.ftdi_read_data_set_chunksize(ctx, new_chunksize)) new_chunksize end |
#read_pins ⇒ Fixnum
Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
361 362 363 364 365 |
# File 'lib/ftdi.rb', line 361 def read_pins p = FFI::MemoryPointer.new(:uchar, 1) check_result(Ftdi.ftdi_read_pins(ctx, p)) p.read_uchar end |
#rts=(value) ⇒ Fixnum
Set the RTS control line value.
395 396 397 398 |
# File 'lib/ftdi.rb', line 395 def rts=(value) check_result(Ftdi.ftdi_setrts(ctx, value)) value end |
#set_bitmode(bitmask, mode) ⇒ NilClass
Set Bitbang mode for ftdi chip.
281 282 283 |
# File 'lib/ftdi.rb', line 281 def set_bitmode(bitmask, mode) check_result(Ftdi.ftdi_set_bitmode(ctx, bitmask, mode)) end |
#set_line_property(bits, stopbits, parity) ⇒ NilClass
Set (RS232) line characteristics. The break type can only be set via #set_line_property2 and defaults to "off".
248 249 250 |
# File 'lib/ftdi.rb', line 248 def set_line_property(bits, stopbits, parity) check_result(Ftdi.ftdi_set_line_property(ctx, bits, stopbits, parity)) end |
#set_line_property2(bits, stopbits, parity, _break) ⇒ NilClass
Set (RS232) line characteristics.
259 260 261 |
# File 'lib/ftdi.rb', line 259 def set_line_property2(bits, stopbits, parity, _break) check_result(Ftdi.ftdi_set_line_property2(ctx, bits, stopbits, parity, _break)) end |
#usb_close ⇒ NilClass
Closes the ftdi device.
221 222 223 224 |
# File 'lib/ftdi.rb', line 221 def usb_close Ftdi.ftdi_usb_close(ctx) nil end |
#usb_open(vendor, product) ⇒ NilClass
Opens the first device with a given vendor and product ids.
175 176 177 178 179 |
# File 'lib/ftdi.rb', line 175 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 |
#usb_open_desc(vendor, product, description, serial) ⇒ NilClass
Opens the first device with a given vendor and product ids, description and serial.
189 190 191 192 193 |
# File 'lib/ftdi.rb', line 189 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 |
#usb_open_desc_index(vendor, product, description, serial, index) ⇒ NilClass
Opens the index-th device with a given vendor and product ids, description and serial.
204 205 206 207 208 209 210 |
# File 'lib/ftdi.rb', line 204 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 |
#usb_reset ⇒ NilClass
Resets the ftdi device.
215 216 217 |
# File 'lib/ftdi.rb', line 215 def usb_reset check_result(Ftdi.ftdi_usb_reset(ctx)) end |
#write_data(bytes) ⇒ Fixnum
Writes data.
310 311 312 313 314 315 316 317 318 |
# File 'lib/ftdi.rb', line 310 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 |
#write_data_chunksize ⇒ Fixnum
Gets write buffer chunk size.
289 290 291 292 293 |
# File 'lib/ftdi.rb', line 289 def write_data_chunksize p = FFI::MemoryPointer.new(:uint, 1) check_result(Ftdi.ftdi_write_data_get_chunksize(ctx, p)) p.read_uint end |
#write_data_chunksize=(new_chunksize) ⇒ Fixnum
Default is 4096.
Configure write buffer chunk size. Automatically reallocates the buffer.
301 302 303 304 |
# File 'lib/ftdi.rb', line 301 def write_data_chunksize=(new_chunksize) check_result(Ftdi.ftdi_write_data_set_chunksize(ctx, new_chunksize)) new_chunksize end |