i2c 0.4.2.dev → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/i2c/backends/i2c-dev.rb +104 -100
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67d39ae8002f56f83353c642d7eda7d04ac00533
|
4
|
+
data.tar.gz: 0aa819ee206b76f7db54d20c46841b6dcc919187
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7835158a580b3a5b8dccf31b66ab579515a527be631474f943edc651587b5426c3428ae63d11759eaa813bf293cabb180e3b0c58afc5e1743a8a77232bc9f836
|
7
|
+
data.tar.gz: 321dfa963f27de78e8afe95ed93a74d087d8e1abe9e11863c3977d2a256734041aed280826d3eba5a6fee97eb3543f81da4c68bc335acb503b4370af666071c0
|
data/lib/i2c/backends/i2c-dev.rb
CHANGED
@@ -7,113 +7,117 @@
|
|
7
7
|
# License Version 2.
|
8
8
|
#
|
9
9
|
module I2C
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
attr_reader :comsMutex
|
22
|
-
|
23
|
-
# this tries to lock the coms mutex, unless already held,
|
24
|
-
# then sends every param, begining with +params[0]+
|
25
|
-
# If the current param is a Fixnum, it is treated as one byte.
|
26
|
-
# If the param is a String, this string will be sent byte by byte.
|
27
|
-
# You can use Array#pack to create a string from an array
|
28
|
-
# For Fixnum there is a convenient function to_short which transforms
|
29
|
-
# the number to a string this way: 12345.to_short == [12345].pack("s")
|
30
|
-
def write(address, *params)
|
31
|
-
if(@comsMutex.owned?)
|
32
|
-
keepLock = true;
|
33
|
-
else
|
34
|
-
@comsMutex.lock;
|
10
|
+
class Dev
|
11
|
+
# see i2c-dev.h
|
12
|
+
I2C_SLAVE = 0x0703
|
13
|
+
|
14
|
+
def self.create(device_path)
|
15
|
+
raise Errno::ENOENT, "Device #{device_path} not found." unless File.exists?(device_path)
|
16
|
+
@instances ||= Hash.new
|
17
|
+
@instances[device_path] = Dev.new(device_path) unless @instances.has_key?(device_path)
|
18
|
+
@instances[device_path]
|
35
19
|
end
|
36
20
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
21
|
+
attr_reader :comsMutex
|
22
|
+
|
23
|
+
# this tries to lock the coms mutex, unless already held,
|
24
|
+
# then sends every param, begining with +params[0]+
|
25
|
+
# If the current param is a Fixnum, it is treated as one byte.
|
26
|
+
# If the param is a String, this string will be sent byte by byte.
|
27
|
+
# You can use Array#pack to create a string from an array
|
28
|
+
# For Fixnum there is a convenient function to_short which transforms
|
29
|
+
# the number to a string this way: 12345.to_short == [12345].pack("s")
|
30
|
+
def write(address, *params)
|
31
|
+
if(@comsMutex.owned?)
|
32
|
+
keepLock = true;
|
33
|
+
else
|
34
|
+
@comsMutex.lock;
|
35
|
+
end
|
36
|
+
|
37
|
+
begin
|
38
|
+
setup_device(address);
|
39
|
+
raw_write(params);
|
40
|
+
ensure
|
41
|
+
@comsMutex.unlock() unless keepLock;
|
42
|
+
end
|
43
|
+
end
|
61
44
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
45
|
+
# this tries to lock the coms mutex (unless already held),
|
46
|
+
# then sends *params, if given, and then tries to read
|
47
|
+
# +size+ bytes. The result is a String which can be treated with
|
48
|
+
# String#unpack afterwards
|
49
|
+
def read(address, size, *params)
|
50
|
+
if(@comsMutex.owned?)
|
51
|
+
keepLock = true;
|
52
|
+
else
|
53
|
+
@comsMutex.lock;
|
54
|
+
end
|
55
|
+
|
56
|
+
begin
|
57
|
+
setup_device(address);
|
58
|
+
raw_write(params) unless params.empty?
|
59
|
+
result = raw_read(size);
|
60
|
+
ensure
|
61
|
+
@comsMutex.unlock() unless keepLock;
|
62
|
+
return result;
|
63
|
+
end
|
64
|
+
end
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
# Read a byte from the current address. Return a one char String which
|
67
|
+
# can be treated with String#unpack
|
68
|
+
def read_byte(address)
|
69
|
+
read(address, 1);
|
70
|
+
end
|
73
71
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
72
|
+
private
|
73
|
+
# Set up @device for a I2C communication to address
|
74
|
+
def setup_device(address)
|
75
|
+
@device.ioctl(I2C_SLAVE, address);
|
76
|
+
end
|
78
77
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
data.force_encoding("US-ASCII")
|
78
|
+
# Read size bytes from @device, if possible. Raise an error otherwise
|
79
|
+
def raw_read(size)
|
80
|
+
return @device.sysread(size)
|
81
|
+
end
|
84
82
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
83
|
+
# Write "params" to @device, unrolling them first should they be an array.
|
84
|
+
# params should be a string, formatted with Array.pack as explained for write()
|
85
|
+
def raw_write(params)
|
86
|
+
data = String.new();
|
87
|
+
data.force_encoding("US-ASCII")
|
90
88
|
|
91
|
-
|
92
|
-
|
89
|
+
if(params.is_a? Array)
|
90
|
+
params.each do |i| data << i; end
|
91
|
+
else
|
92
|
+
data << params;
|
93
|
+
end
|
93
94
|
|
94
|
-
|
95
|
-
|
95
|
+
@device.syswrite(data);
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
98
|
+
def initialize(device_path)
|
99
|
+
@comsMutex = Mutex.new();
|
100
|
+
|
101
|
+
@device = File.new(device_path, 'r+')
|
102
|
+
# change the sys* functions of the file object to meet our requirements
|
103
|
+
class << @device
|
104
|
+
alias :syswrite_orig :syswrite
|
105
|
+
def syswrite(var)
|
106
|
+
begin
|
107
|
+
syswrite_orig var
|
108
|
+
rescue Errno::EREMOTEIO, Errno::EIO
|
109
|
+
raise AckError, "No acknowledge received"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
alias :sysread_orig :sysread
|
113
|
+
def sysread(var)
|
114
|
+
begin
|
115
|
+
sysread_orig var
|
116
|
+
rescue Errno::EREMOTEIO, Errno::EIO
|
117
|
+
raise AckError, "No acknowledge received"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end # virtual class
|
121
|
+
end # initialize
|
122
|
+
end # Class
|
123
|
+
end # Module
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i2c
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.2
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christoph Anderegg
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-06-
|
12
|
+
date: 2018-06-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Interface to I2C (aka TWI) implementations. Also provides abstractions
|
15
15
|
for some I2c-devices. Created with the Raspberry Pi in mind.
|
@@ -43,9 +43,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
43
|
version: '0'
|
44
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
48
|
+
version: '0'
|
49
49
|
requirements: []
|
50
50
|
rubyforge_project:
|
51
51
|
rubygems_version: 2.6.14.1
|