i2c 0.4.2.dev → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/i2c/backends/i2c-dev.rb +104 -100
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2254a2d32b8a0d9feb7e7718477e4bca157a521d
4
- data.tar.gz: 918e34eca0c218eab8330efa1ee5b71426c91625
3
+ metadata.gz: 67d39ae8002f56f83353c642d7eda7d04ac00533
4
+ data.tar.gz: 0aa819ee206b76f7db54d20c46841b6dcc919187
5
5
  SHA512:
6
- metadata.gz: 453ebbf2217f1ca9e105c95cd188b1c9d08c311d7a913927312ca7aabfaed490c0836052076e07425f5709a9409430f75626d1d064e3e40c047a0f20b8980c00
7
- data.tar.gz: fbd3cd2ea165014d405a781591f13c88b96bc018c9a31cc223bdac2490c8859dc4a1969b2902d85a2546fe4a133b22a00280fcdd2fbe5730c25eef827b774890
6
+ metadata.gz: 7835158a580b3a5b8dccf31b66ab579515a527be631474f943edc651587b5426c3428ae63d11759eaa813bf293cabb180e3b0c58afc5e1743a8a77232bc9f836
7
+ data.tar.gz: 321dfa963f27de78e8afe95ed93a74d087d8e1abe9e11863c3977d2a256734041aed280826d3eba5a6fee97eb3543f81da4c68bc335acb503b4370af666071c0
@@ -7,113 +7,117 @@
7
7
  # License Version 2.
8
8
  #
9
9
  module I2C
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]
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
- setup_device(address);
38
- raw_write(params);
39
-
40
- @comsMutex.unlock() unless keepLock;
41
- end
42
-
43
- # this tries to lock the coms mutex (unless already held),
44
- # then sends *params, if given, and then tries to read
45
- # +size+ bytes. The result is a String which can be treated with
46
- # String#unpack afterwards
47
- def read(address, size, *params)
48
- if(@comsMutex.owned?)
49
- keepLock = true;
50
- else
51
- @comsMutex.lock;
52
- end
53
-
54
- setup_device(address);
55
- raw_write(params) unless params.empty?
56
- result = raw_read(size);
57
-
58
- @comsMutex.unlock() unless keepLock;
59
- return result;
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
- # Read a byte from the current address. Return a one char String which
63
- # can be treated with String#unpack
64
- def read_byte(address)
65
- read(address, 1);
66
- end
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
- private
69
- # Set up @device for a I2C communication to address
70
- def setup_device(address)
71
- @device.ioctl(I2C_SLAVE, address);
72
- end
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
- # Read size bytes from @device, if possible. Raise an error otherwise
75
- def raw_read(size)
76
- return @device.sysread(size)
77
- end
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
- # Write "params" to @device, unrolling them first should they be an array.
80
- # params should be a string, formatted with Array.pack as explained for write()
81
- def raw_write(params)
82
- data = String.new();
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
- if(params.is_a? Array)
86
- params.each do |i| data << i; end
87
- else
88
- data << params;
89
- end
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
- @device.syswrite(data);
92
- end
89
+ if(params.is_a? Array)
90
+ params.each do |i| data << i; end
91
+ else
92
+ data << params;
93
+ end
93
94
 
94
- def initialize(device_path)
95
- @comsMutex = Mutex.new();
95
+ @device.syswrite(data);
96
+ end
96
97
 
97
- @device = File.new(device_path, 'r+')
98
- # change the sys* functions of the file object to meet our requirements
99
- class << @device
100
- alias :syswrite_orig :syswrite
101
- def syswrite(var)
102
- begin
103
- syswrite_orig var
104
- rescue Errno::EREMOTEIO, Errno::EIO
105
- raise AckError, "No acknowledge received"
106
- end
107
- end
108
- alias :sysread_orig :sysread
109
- def sysread(var)
110
- begin
111
- sysread_orig var
112
- rescue Errno::EREMOTEIO, Errno::EIO
113
- raise AckError, "No acknowledge received"
114
- end
115
- end
116
- end # class
117
- end # initialize
118
- end
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.dev
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 00:00:00.000000000 Z
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: 1.3.1
48
+ version: '0'
49
49
  requirements: []
50
50
  rubyforge_project:
51
51
  rubygems_version: 2.6.14.1