i2c-devices 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/ChangeLog +4 -0
- data/README.md +130 -0
- data/Rakefile +22 -0
- data/i2c-devices.gemspec +22 -0
- data/lib/i2c.rb +28 -0
- data/lib/i2c/device/acm1602ni.rb +18 -0
- data/lib/i2c/device/adt7410.rb +146 -0
- data/lib/i2c/device/aqm0802.rb +58 -0
- data/lib/i2c/device/hd44780.rb +137 -0
- data/lib/i2c/device/mpl115a2.rb +39 -0
- data/lib/i2c/driver/gpio.rb +198 -0
- data/lib/i2c/driver/i2c-dev.rb +53 -0
- data/lib/i2c/mocki2cdevice.rb +67 -0
- data/spec/device/adt7410_spec.rb +87 -0
- data/spec/device/hd44780_spec.rb +155 -0
- data/spec/driver/gpio_spec.rb +514 -0
- data/spec/i2cdevice_spec.rb +89 -0
- data/xt/acm1602ni.rb +22 -0
- data/xt/driver-gpio.rb +34 -0
- data/xt/i2cdetect.rb +69 -0
- data/xt/mpl115a2.rb +12 -0
- metadata +97 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
#!rspec
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift "lib"
|
4
|
+
|
5
|
+
require "i2c"
|
6
|
+
require "i2c/driver/i2c-dev"
|
7
|
+
require "tempfile"
|
8
|
+
|
9
|
+
describe I2CDevice do
|
10
|
+
before do
|
11
|
+
@i2cout = ""
|
12
|
+
@i2cin = ""
|
13
|
+
@ioctl = nil
|
14
|
+
|
15
|
+
ioctl = proc do |cmd, arg|
|
16
|
+
@ioctl = [ cmd, arg ]
|
17
|
+
end
|
18
|
+
|
19
|
+
syswrite = proc do |str|
|
20
|
+
@i2cout << str
|
21
|
+
end
|
22
|
+
|
23
|
+
sysread = proc do |n|
|
24
|
+
@i2cin
|
25
|
+
end
|
26
|
+
|
27
|
+
@temp = Tempfile.new("i2c")
|
28
|
+
file = nil
|
29
|
+
open = File.method(:open)
|
30
|
+
File.stub(:open) do
|
31
|
+
file = open.call(@temp.path, "r+")
|
32
|
+
file.define_singleton_method(:ioctl) {|cmd,arg| ioctl.call(cmd, arg) }
|
33
|
+
file.define_singleton_method(:syswrite) {|str| syswrite.call(str) }
|
34
|
+
file.define_singleton_method(:sysread) {|n| sysread.call(n) }
|
35
|
+
file
|
36
|
+
end
|
37
|
+
|
38
|
+
@driver = I2CDevice::Driver::I2CDev.new(@temp.path)
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#i2cset" do
|
42
|
+
it "should be write 1 byte" do
|
43
|
+
i2c = I2CDevice.new(address: 0x10, driver: @driver)
|
44
|
+
|
45
|
+
i2c.i2cset(0x00)
|
46
|
+
|
47
|
+
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
|
48
|
+
expect(@i2cout).to eq("\x00")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be write multi bytes" do
|
52
|
+
i2c = I2CDevice.new(address: 0x10, driver: @driver)
|
53
|
+
|
54
|
+
i2c.i2cset(0x00, 0x01, 0x02)
|
55
|
+
|
56
|
+
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
|
57
|
+
expect(@i2cout).to eq("\x00\x01\x02")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#i2cget" do
|
62
|
+
it "should be read 1 byte" do
|
63
|
+
i2c = I2CDevice.new(address: 0x10, driver: @driver)
|
64
|
+
|
65
|
+
@i2cin = "\x01"
|
66
|
+
|
67
|
+
ret = i2c.i2cget(0x00)
|
68
|
+
|
69
|
+
expect(ret).to eq("\x01")
|
70
|
+
|
71
|
+
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
|
72
|
+
expect(@i2cout).to eq("\x00")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should be read multi byte" do
|
76
|
+
i2c = I2CDevice.new(address: 0x10, driver: @driver)
|
77
|
+
|
78
|
+
@i2cin = "\x01\x02\x03"
|
79
|
+
|
80
|
+
ret = i2c.i2cget(0x00)
|
81
|
+
|
82
|
+
expect(ret).to eq("\x01\x02\x03")
|
83
|
+
|
84
|
+
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
|
85
|
+
expect(@i2cout).to eq("\x00")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
data/xt/acm1602ni.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift "lib"
|
4
|
+
|
5
|
+
require "i2c/device/acm1602ni"
|
6
|
+
|
7
|
+
lcd = ACM1602NI.new
|
8
|
+
|
9
|
+
lcd.define_character(0, [
|
10
|
+
1,0,1,0,1,
|
11
|
+
1,0,1,0,1,
|
12
|
+
1,0,1,0,1,
|
13
|
+
1,0,1,0,1,
|
14
|
+
1,0,1,0,1,
|
15
|
+
1,0,1,0,1,
|
16
|
+
1,0,1,0,1,
|
17
|
+
1,0,1,0,1,
|
18
|
+
])
|
19
|
+
|
20
|
+
lcd.put_line(0, "1234567890abcdef")
|
21
|
+
lcd.put_line(1, "\x00" * 16)
|
22
|
+
|
data/xt/driver-gpio.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift "lib"
|
4
|
+
|
5
|
+
require "i2c/device/mpl115a2"
|
6
|
+
require "i2c/driver/gpio"
|
7
|
+
|
8
|
+
# I2CDevice::Driver::GPIO.class_variable_set(:@@DEBUG, true)
|
9
|
+
|
10
|
+
driver = I2CDevice::Driver::GPIO.new(
|
11
|
+
sda: 23, # pin 16 in raspberry pi
|
12
|
+
scl: 24, # pin 18 in raspberry pi
|
13
|
+
)
|
14
|
+
|
15
|
+
|
16
|
+
device = I2CDevice.new(address: 0x20, driver: driver)
|
17
|
+
loop do
|
18
|
+
seq = Array.new(8) { rand(0x10) }
|
19
|
+
seq = (0..7).map {|n| 0x10 + n }
|
20
|
+
seq = (1..10).to_a
|
21
|
+
p seq
|
22
|
+
p device.i2cset(0x00, *seq)
|
23
|
+
# p device.i2cget(0x00, 8).unpack("C*")
|
24
|
+
|
25
|
+
exit 1
|
26
|
+
sleep 1
|
27
|
+
end
|
28
|
+
|
29
|
+
#mpl = MPL115A2.new(driver: driver)
|
30
|
+
#loop do
|
31
|
+
# p mpl.calculate_hPa
|
32
|
+
# sleep 1
|
33
|
+
#end
|
34
|
+
|
data/xt/i2cdetect.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift "lib"
|
4
|
+
|
5
|
+
require "optparse"
|
6
|
+
|
7
|
+
require "i2c"
|
8
|
+
require "i2c/driver/i2c-dev"
|
9
|
+
require "i2c/driver/gpio"
|
10
|
+
|
11
|
+
# Address List
|
12
|
+
# .... ... r/w
|
13
|
+
# 0000 000 0 -> general call address
|
14
|
+
# 0000 000 1 -> start byte
|
15
|
+
# 0000 001 x -> cbus address
|
16
|
+
# 0000 010 x -> reserved for other protocols
|
17
|
+
# 0000 011 x -> reserved for future
|
18
|
+
# 0000 1xx x -> Hs-mod master code
|
19
|
+
# 1111 1xx x -> device id
|
20
|
+
# 1111 0xx x -> 10-bit addressing
|
21
|
+
|
22
|
+
i2c_devices = Dir.glob("/dev/i2c-*")
|
23
|
+
is_gpio_supported = Dir.glob("/sys/class/gpio")
|
24
|
+
|
25
|
+
driver = nil
|
26
|
+
|
27
|
+
OptionParser.new do |opt|
|
28
|
+
opt.banner = <<-EOB.gsub(/^\t+/, "")
|
29
|
+
Usage: #{$0} [opts]
|
30
|
+
EOB
|
31
|
+
|
32
|
+
opt.separator ""
|
33
|
+
opt.separator "Options:"
|
34
|
+
opt.on("-d BACKEND", "--driver BACKEND", [ i2c_devices.empty?? "" : "i2cdev,[path]", is_gpio_supported ? "gpio,[sda pin],[scl pin]" : "" ].join("\t")) do |backend|
|
35
|
+
backend = backend.split(/,/)
|
36
|
+
case backend.shift
|
37
|
+
when "i2cdev"
|
38
|
+
driver = I2CDevice::Driver::I2CDev.new(backend.shift)
|
39
|
+
when "gpio"
|
40
|
+
driver = I2CDevice::Driver::GPIO.new(sda: backend[0], scl: backend[1])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
i2c_devices.each do |path|
|
44
|
+
opt.separator "\t\t\t--backend i2cdev,#{path}"
|
45
|
+
end
|
46
|
+
opt.separator "\t\t\t--backend gpio,23,24"
|
47
|
+
|
48
|
+
opt.parse!(ARGV)
|
49
|
+
end
|
50
|
+
|
51
|
+
puts "Using %p" % driver if driver
|
52
|
+
|
53
|
+
puts "% 2s %s" % ["", 16.times.map {|n| "% 2x" % n }.join(" ") ]
|
54
|
+
8.times do |high|
|
55
|
+
puts "%02x: %s" % [ high << 4, 16.times.map {|low|
|
56
|
+
a = high << 4 | low
|
57
|
+
if (a >> 3) == 0b1111 || (a >> 3) == 0b0000
|
58
|
+
" "
|
59
|
+
else
|
60
|
+
d = I2CDevice.new(address: a, driver: driver)
|
61
|
+
begin
|
62
|
+
d.i2cset(0x00)
|
63
|
+
"%02x" % a
|
64
|
+
rescue I2CDevice::I2CIOError => e
|
65
|
+
"--"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
}.join(" ") ]
|
69
|
+
end
|
data/xt/mpl115a2.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i2c-devices
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cho45
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: i2c-devices is a drivers for i2c devices
|
42
|
+
email: cho45@lowreal.net
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- ChangeLog
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- i2c-devices.gemspec
|
52
|
+
- lib/i2c.rb
|
53
|
+
- lib/i2c/device/acm1602ni.rb
|
54
|
+
- lib/i2c/device/adt7410.rb
|
55
|
+
- lib/i2c/device/aqm0802.rb
|
56
|
+
- lib/i2c/device/hd44780.rb
|
57
|
+
- lib/i2c/device/mpl115a2.rb
|
58
|
+
- lib/i2c/driver/gpio.rb
|
59
|
+
- lib/i2c/driver/i2c-dev.rb
|
60
|
+
- lib/i2c/mocki2cdevice.rb
|
61
|
+
- spec/device/adt7410_spec.rb
|
62
|
+
- spec/device/hd44780_spec.rb
|
63
|
+
- spec/driver/gpio_spec.rb
|
64
|
+
- spec/i2cdevice_spec.rb
|
65
|
+
- xt/acm1602ni.rb
|
66
|
+
- xt/driver-gpio.rb
|
67
|
+
- xt/i2cdetect.rb
|
68
|
+
- xt/mpl115a2.rb
|
69
|
+
homepage: https://github.com/cho45/ruby-i2c-devices
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.2.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: i2c device drivers
|
93
|
+
test_files:
|
94
|
+
- spec/device/adt7410_spec.rb
|
95
|
+
- spec/device/hd44780_spec.rb
|
96
|
+
- spec/driver/gpio_spec.rb
|
97
|
+
- spec/i2cdevice_spec.rb
|