ftdi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README +6 -0
- data/examples/onoff.rb +54 -0
- data/examples/simple.rb +33 -0
- data/examples/updown.rb +56 -0
- data/ext/extconf.rb +43 -0
- data/ext/ftdi.i +20 -0
- data/test/test_ftdi.rb +15 -0
- metadata +68 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Jason Heiss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README
ADDED
data/examples/onoff.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
# An example using bitbang mode which turns all 8 pins off, waits a second,
|
4
|
+
# then all on and waits for a second, then off and exits.
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'ftdi'
|
8
|
+
|
9
|
+
puts 'ftdi_new'
|
10
|
+
ftdi1 = Ftdi.ftdi_new
|
11
|
+
|
12
|
+
puts 'ftdi_usb_open_desc ?'
|
13
|
+
if (r = Ftdi.ftdi_usb_open_desc(ftdi1, 0x0403, 0x6001, 'USBMOD4', '?')) < 0
|
14
|
+
abort "unable to open ftdi device: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
15
|
+
end
|
16
|
+
|
17
|
+
puts 'ftdi_enable_bitbang'
|
18
|
+
if (r = Ftdi.ftdi_enable_bitbang(ftdi1, 0xFF)) < 0
|
19
|
+
abort "unable to enable bitbang mode: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
20
|
+
end
|
21
|
+
|
22
|
+
buf = Ftdi::UnsignedCharArray.new(1)
|
23
|
+
buf[0] = 0x0
|
24
|
+
puts 'turning everything off'
|
25
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
26
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
27
|
+
end
|
28
|
+
|
29
|
+
sleep 1
|
30
|
+
|
31
|
+
buf[0] = 0xFF
|
32
|
+
puts 'turning everything on'
|
33
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
34
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
35
|
+
end
|
36
|
+
|
37
|
+
sleep 1
|
38
|
+
|
39
|
+
buf[0] = 0x0
|
40
|
+
puts 'turning everything off'
|
41
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
42
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
43
|
+
end
|
44
|
+
|
45
|
+
puts 'ftdi_disable_bitbang'
|
46
|
+
if (r = Ftdi.ftdi_disable_bitbang(ftdi1)) < 0
|
47
|
+
abort "unable to disable bitbang mode: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
48
|
+
end
|
49
|
+
|
50
|
+
puts 'ftdi_usb_close'
|
51
|
+
if (r = Ftdi.ftdi_usb_close(ftdi1)) < 0
|
52
|
+
abort "unable to close ftdi device: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
53
|
+
end
|
54
|
+
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
# Simple libftdi usage example
|
4
|
+
# Translation of the example from
|
5
|
+
# http://www.intra2net.com/en/developer/libftdi/documentation/
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'ftdi'
|
9
|
+
|
10
|
+
# Not quite an exact translation of the C example (which statically allocates
|
11
|
+
# the ftdi_context and then calls ftdi_init on it, whereas I'm using ftdi_new
|
12
|
+
# to allocate and init in one shot so that I can avoid figuring out how to
|
13
|
+
# statically allocate a C struct from Ruby). If someone wants to figure that
|
14
|
+
# out I think it would be nice for this script to be a more direct
|
15
|
+
# translation, the other examples can take shortcuts.
|
16
|
+
ftdic = Ftdi.ftdi_new
|
17
|
+
|
18
|
+
if (ret = Ftdi.ftdi_usb_open(ftdic, 0x0403, 0x6001)) < 0
|
19
|
+
abort "unable to open ftdi device: #{ret} (#{Ftdi.ftdi_get_error_string(ftdic)})"
|
20
|
+
end
|
21
|
+
|
22
|
+
# Read out FTDIChip-ID of R type chips
|
23
|
+
if ftdic.type == Ftdi::TYPE_R
|
24
|
+
chipid = IntPointer.new
|
25
|
+
puts "ftdi_read_chipid: #{Ftdi.ftdi_read_chipid(ftdic, chipid)}"
|
26
|
+
puts "FTDI chipid: #{chipid.value}"
|
27
|
+
end
|
28
|
+
|
29
|
+
Ftdi.ftdi_usb_close(ftdic)
|
30
|
+
Ftdi.ftdi_deinit(ftdic)
|
31
|
+
|
32
|
+
exit(true)
|
33
|
+
|
data/examples/updown.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
# An example using bitbang mode which turns all 8 pins off, waits a second,
|
4
|
+
# then turns each pin on in turn for 1/2 sec each, then turns everything back
|
5
|
+
# off and exits.
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'ftdi'
|
9
|
+
|
10
|
+
puts 'ftdi_new'
|
11
|
+
ftdi1 = Ftdi.ftdi_new
|
12
|
+
|
13
|
+
puts 'ftdi_usb_open_desc ?'
|
14
|
+
if (r = Ftdi.ftdi_usb_open_desc(ftdi1, 0x0403, 0x6001, 'USBMOD4', '?')) < 0
|
15
|
+
abort "unable to open ftdi device: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
16
|
+
end
|
17
|
+
|
18
|
+
puts 'ftdi_enable_bitbang'
|
19
|
+
if (r = Ftdi.ftdi_enable_bitbang(ftdi1, 0xFF)) < 0
|
20
|
+
abort "unable to enable bitbang mode: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
21
|
+
end
|
22
|
+
|
23
|
+
buf = Ftdi::UnsignedCharArray.new(1)
|
24
|
+
buf[0] = 0x0
|
25
|
+
puts 'turning everything off'
|
26
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
27
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
28
|
+
end
|
29
|
+
|
30
|
+
sleep 1
|
31
|
+
|
32
|
+
0.upto(7) do |i|
|
33
|
+
buf[0] = 0 | (0x0 ^ 1 << (i % 8))
|
34
|
+
printf "%08b\n", buf[0]
|
35
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
36
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
37
|
+
end
|
38
|
+
sleep 0.5
|
39
|
+
end
|
40
|
+
|
41
|
+
buf[0] = 0x0
|
42
|
+
puts 'turning everything off'
|
43
|
+
if (r = Ftdi.ftdi_write_data(ftdi1, buf, 1)) < 0
|
44
|
+
abort "unable to write data: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
45
|
+
end
|
46
|
+
|
47
|
+
puts 'ftdi_disable_bitbang'
|
48
|
+
if (r = Ftdi.ftdi_disable_bitbang(ftdi1)) < 0
|
49
|
+
abort "unable to disable bitbang mode: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
50
|
+
end
|
51
|
+
|
52
|
+
puts 'ftdi_usb_close'
|
53
|
+
if (r = Ftdi.ftdi_usb_close(ftdi1)) < 0
|
54
|
+
abort "unable to close ftdi device: #{r} (#{Ftdi.ftdi_get_error_string(ftdi1)})"
|
55
|
+
end
|
56
|
+
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
idir, ldir = dir_config('ftdi')
|
3
|
+
have_library('ftdi', 'ftdi_init')
|
4
|
+
|
5
|
+
# Swig seemingly has no notion of searching any standard system directories
|
6
|
+
# for included header files. And figuring out where the user has ftdi
|
7
|
+
# installed is potentially tricky. Seems like there are three
|
8
|
+
# possibilities for where mkmf will end finding ftdi:
|
9
|
+
# - Installed in a standard system location searched by cpp by default
|
10
|
+
# - Installed in the same location as Ruby, as mkmf adds
|
11
|
+
# Config::CONFIG['CPPFLAGS'] to the compiler args, and thus adds the include
|
12
|
+
# directory associated with the Ruby install location even if cpp wouldn't
|
13
|
+
# normally check there by default.
|
14
|
+
# - A location specified by the user at install time via the dir_config
|
15
|
+
# feature
|
16
|
+
# This all seems horribly cheesy, so if a someone who knows anything about
|
17
|
+
# swig happens to read this I'd appreciate pointers on how other folks handle
|
18
|
+
# this.
|
19
|
+
ftdi_includes = []
|
20
|
+
# Standard system locations
|
21
|
+
IO.popen('cpp -M -include ftdi.h < /dev/null') do |pipe|
|
22
|
+
pipe.each do |line|
|
23
|
+
line.split(' ').each do |inc|
|
24
|
+
if inc.include?('/ftdi.h')
|
25
|
+
ftdi_includes << '-I' + inc
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
# Any -I directories in Config::CONFIG['CPPFLAGS']
|
31
|
+
Config::CONFIG['CPPFLAGS'].split(' ').each do |rbcppflag|
|
32
|
+
if rbcppflag =~ /^-I/
|
33
|
+
ftdi_includes << rbcppflag
|
34
|
+
end
|
35
|
+
end
|
36
|
+
# User specified directory via dir_config
|
37
|
+
if idir
|
38
|
+
ftdi_includes << '-I' + idir
|
39
|
+
end
|
40
|
+
system("swig -ruby #{ftdi_includes.uniq.join(' ')} ftdi.i") || abort("swig failed")
|
41
|
+
|
42
|
+
create_makefile("ftdi")
|
43
|
+
|
data/ext/ftdi.i
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
%module ftdi
|
2
|
+
|
3
|
+
%include "cpointer.i"
|
4
|
+
%include "carrays.i"
|
5
|
+
%include "cdata.i"
|
6
|
+
|
7
|
+
/* Wrap a class interface around an "int *" */
|
8
|
+
/* http://www.swig.org/Doc1.3/SWIGDocumentation.html#Library_nn4 */
|
9
|
+
%pointer_class(int, IntPointer);
|
10
|
+
|
11
|
+
/* Wrap a class interface around an "unsigned char *" */
|
12
|
+
/* http://www.swig.org/Doc1.3/SWIGDocumentation.html#Library_nn7 */
|
13
|
+
%array_class(unsigned char, UnsignedCharArray);
|
14
|
+
|
15
|
+
%{
|
16
|
+
#include <ftdi.h>
|
17
|
+
%}
|
18
|
+
|
19
|
+
%include "ftdi.h"
|
20
|
+
|
data/test/test_ftdi.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ftdi'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestTest < Test::Unit::TestCase
|
6
|
+
def test_ftdi
|
7
|
+
t = Ftdi.new
|
8
|
+
assert_equal(Object, Ftdi.superclass)
|
9
|
+
assert_equal(Ftdi, t.class)
|
10
|
+
t.add(1)
|
11
|
+
t.add(2)
|
12
|
+
assert_equal([1,2], t.instance_eval("@arr"))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ftdi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Heiss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-16 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: |+
|
17
|
+
Ruby bindings for libftdi: http://www.intra2net.com/en/developer/libftdi/
|
18
|
+
Also requires swig: http://www.swig.org/
|
19
|
+
|
20
|
+
The gem includes example scripts. Where they end up depends on your
|
21
|
+
system, but can be found with: gem contents ftdi | grep examples
|
22
|
+
|
23
|
+
email: jheiss@aput.net
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions:
|
27
|
+
- ext/extconf.rb
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
- examples/onoff.rb
|
34
|
+
- examples/simple.rb
|
35
|
+
- examples/updown.rb
|
36
|
+
- ext/extconf.rb
|
37
|
+
- ext/ftdi.i
|
38
|
+
- test/test_ftdi.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/jheiss/ftdi
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "1.8"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements:
|
61
|
+
- libftdi
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.5
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Ruby bindings for libftdi
|
67
|
+
test_files:
|
68
|
+
- test/test_ftdi.rb
|