mruby-linux-i2c 0.9.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 46c4225897adb0648942d42ea1e4eb65c243e61a2af6de9c504879f3c1afcd5c
4
+ data.tar.gz: eafb1599d08a9815f626117fce36808c4c65ad882ce67421bb66480c0ede0ffd
5
+ SHA512:
6
+ metadata.gz: 85391b9c9aa58c83499536de06aea0b8abddf4ae0d4d036888c3981cdbdb604dc91720b98d04c1cdc4f6cda7986ca3b8a5e5f5d3fda607ee3b7e51a5bd9d2506
7
+ data.tar.gz: b43912f6aa1f1369a202cc56e3ee1331600d5790b22c9be7a44cf7932b0468809880172bd05250e5c3ab3f25f5de922de53bef099e9017b5fa160ca8fed6c088
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, Shimane IT Open-Innovation Center.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # I2C class using Linux i2cdev.
2
+
3
+ ## Overview
4
+
5
+ This is an implementation of the I2C class library for Linux.
6
+ Follows [mruby, mruby/c common I/O API guidelines.](https://github.com/mruby/microcontroller-peripheral-interface-guide)
7
+
8
+ This library uses the Linux i2cdev device driver.
9
+
10
+ ## Installation
11
+
12
+ $ gem install mruby-linux-i2c
13
+
14
+
15
+ ## Features
16
+
17
+ * This library only implements the high-level methods that the guidelines say.
18
+ * This library only supports master devices with 7-bit addresses.
19
+
20
+
21
+ ## Usage
22
+
23
+ about RaspberryPi...
24
+
25
+ ```
26
+ # Connect pin #3 as SDA, #5 as SCL.
27
+
28
+ require "mruby/i2c"
29
+
30
+ # create instance
31
+ i2c = I2C.new("/dev/i2c-1")
32
+
33
+ # Write to device at address 0x5c, data 0x20, 0x90.
34
+ i2c.write( 0x5c, 0x20, 0x90 )
35
+
36
+ # Read 5 bytes from the device at address 0x5c.
37
+ # Outputs 0xa8 before reading.
38
+ s = i2c.read( 0x5c, 5, 0xa8 )
39
+ ```
40
+
41
+ Other case, see original guidelines.
42
+ https://github.com/mruby/microcontroller-peripheral-interface-guide/blob/main/mruby_io_I2C_en.md
43
+
44
+
45
+ ## Licence
46
+
47
+ BSD 3-Clause License. see LICENSE file.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I2C
4
+ VERSION = "0.9.1"
5
+ end
data/lib/mruby/i2c.rb ADDED
@@ -0,0 +1,152 @@
1
+ #
2
+ # I2C class using Linux i2cdev.
3
+ #
4
+ # Copyright (c) 2023 Shimane IT Open-Innovation Center.
5
+ #
6
+ # see: LICENSE file.
7
+ # see: https://github.com/mruby/microcontroller-peripheral-interface-guide/blob/main/mruby_io_I2C_en.md
8
+ #
9
+ # frozen_string_literal: true
10
+ #
11
+
12
+ require_relative "i2c/version"
13
+
14
+ ##
15
+ # I2C namespace
16
+ #
17
+ module I2C
18
+ ##
19
+ # constructor
20
+ #
21
+ #@see I2C::LinuxI2Cdev.initialize
22
+ #
23
+ def self.new(node = "/dev/i2c-1", *params)
24
+ I2C::LinuxI2Cdev.new(node, *params)
25
+ end
26
+ end
27
+
28
+
29
+ ##
30
+ # I2C bus driver
31
+ #
32
+ # only implement high-level methods.
33
+ #
34
+ class I2C::LinuxI2Cdev
35
+
36
+ # see /usr/include/linux/i2c.h i2c-dev.h
37
+ I2C_SLAVE = 0x0703
38
+ I2C_RDWR = 0x0707
39
+ I2C_M_RD = 0x0001
40
+
41
+
42
+ ##
43
+ # constructor
44
+ #
45
+ #@param [String] node device node of I2C
46
+ #@param [nil] params dummy.
47
+ #@see I2C.new
48
+ #
49
+ def initialize(node, *params)
50
+ @device = File.open(node, "r+:ASCII-8BIT")
51
+ end
52
+
53
+
54
+ def read
55
+ end
56
+
57
+
58
+ ##
59
+ # Reads data of read_bytes bytes from the device with the address i2c_adrs_7.
60
+ # using sysread
61
+ #
62
+ #@see #read_ioctl
63
+ #
64
+ def read_sysread( i2c_adrs_7, read_bytes, *param )
65
+ out_data = _rebuild_output_data( param )
66
+
67
+ _use_slave_adrs( i2c_adrs_7 )
68
+ @device.syswrite( out_data ) if !out_data.empty?
69
+ @device.sysread( read_bytes )
70
+ end
71
+
72
+
73
+ ##
74
+ # Reads data of read_bytes bytes from the device with the address i2c_adrs_7.
75
+ # using ioctl
76
+ #
77
+ #@param [Integer] i2c_adrs_7 I2C slave address (7bit address)
78
+ #@param [Integer] read_bytes read bytes.
79
+ #@param [Integer,String,Array<Integer>] param output data before reading.
80
+ #@return [String] reading datas.
81
+ #
82
+ def read_ioctl( i2c_adrs_7, read_bytes, *param )
83
+ out_data = _rebuild_output_data( param )
84
+ recv_data = "\x00".b * read_bytes
85
+
86
+ # prepare the struct i2c_msg[2]
87
+ # struct i2c_msg {
88
+ # __u16 addr;
89
+ # __u16 flags;
90
+ # __u16 len; << hidden padding _u16
91
+ # __u8 *buf;
92
+ # };
93
+ i2c_msg_s = [ i2c_adrs_7, 0, out_data.bytesize, 0,
94
+ [out_data].pack('p').unpack1('J') ].pack('SSSSJ')
95
+ i2c_msg_r = [ i2c_adrs_7, I2C_M_RD, read_bytes, 0,
96
+ [recv_data].pack('p').unpack1('J') ].pack('SSSSJ')
97
+
98
+ # prepare the struct i2c_rdwr_ioctl_data
99
+ # struct i2c_rdwr_ioctl_data {
100
+ # struct i2c_msg *msgs; /* pointers to i2c_msgs */
101
+ # __u32 nmsgs; /* number of i2c_msgs */
102
+ # };
103
+ if out_data.empty?
104
+ arg = [ [i2c_msg_r ].pack('P').unpack1('J'), 1 ].pack('JL')
105
+ else
106
+ arg = [ [i2c_msg_s + i2c_msg_r].pack('P').unpack1('J'), 2 ].pack('JL')
107
+ end
108
+
109
+ @device.ioctl( I2C_RDWR, arg )
110
+ return recv_data
111
+ end
112
+
113
+ alias read read_ioctl
114
+
115
+
116
+ ##
117
+ # Writes data specified in outputs to the device with the address i2c_adrs_7.
118
+ #
119
+ #@param [Integer] i2c_adrs_7 I2C slave address (7bit address)
120
+ #@param [Integer,String,Array<Integer>] outputs output data.
121
+ #@return [Integer] number of bytes actually write.
122
+ #
123
+ def write( i2c_adrs_7 , *outputs )
124
+ out_data = _rebuild_output_data( outputs )
125
+
126
+ _use_slave_adrs( i2c_adrs_7 )
127
+ @device.syswrite( out_data )
128
+ end
129
+
130
+
131
+ private
132
+ def _use_slave_adrs( i2c_adrs_7 )
133
+ @device.ioctl( I2C_SLAVE, i2c_adrs_7 )
134
+ end
135
+
136
+ def _rebuild_output_data( arg )
137
+ data = "".b
138
+ arg.flatten.each {|d|
139
+ case d
140
+ when Integer
141
+ data << d.chr
142
+ when String
143
+ data << d.force_encoding(Encoding::ASCII_8BIT)
144
+ else
145
+ raise ArgumentError
146
+ end
147
+ }
148
+
149
+ return data
150
+ end
151
+
152
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mruby-linux-i2c
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - HirohitoHigashi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - higashi@s-itoc.jp
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/mruby/i2c.rb
23
+ - lib/mruby/i2c/version.rb
24
+ homepage: https://github.com/HirohitoHigashi/mruby-mio/tree/main/mruby-linux-i2c
25
+ licenses:
26
+ - BSD 3-CLAUSE
27
+ metadata:
28
+ homepage_uri: https://github.com/HirohitoHigashi/mruby-mio/tree/main/mruby-linux-i2c
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.6.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.4.13
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: I2C bus driver class library using Linux i2cdev.
48
+ test_files: []