Class: I2CDevice::Driver::GPIO
- Inherits:
-
Object
- Object
- I2CDevice::Driver::GPIO
- Defined in:
- lib/i2c/driver/gpio.rb
Constant Summary collapse
- @@DEBUG =
false
Instance Attribute Summary collapse
-
#scl ⇒ Object
readonly
Returns the value of attribute scl.
-
#sda ⇒ Object
readonly
Returns the value of attribute sda.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
Class Method Summary collapse
- .direction(pin, direction) ⇒ Object
- .export(pin) ⇒ Object
- .finalizer(ports) ⇒ Object
- .read(pin) ⇒ Object
- .unexport(pin) ⇒ Object
- .write(pin, val) ⇒ Object
Instance Method Summary collapse
- #i2cget(address, param, length = 1) ⇒ Object
- #i2cset(address, *data) ⇒ Object
-
#initialize(opts = {}) ⇒ GPIO
constructor
A new instance of GPIO.
Constructor Details
#initialize(opts = {}) ⇒ GPIO
Returns a new instance of GPIO.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/i2c/driver/gpio.rb', line 52 def initialize(opts={}) @sda = opts[:sda] or raise "opts[:sda] = [gpio pin number] is required" @scl = opts[:scl] or raise "opts[:scl] = [gpio pin number] is required" @speed = opts[:speed] || 1 # kHz but insane @clock = 1.0 / (@speed * 1000) begin GPIO.export(@sda) GPIO.export(@scl) rescue Errno::EBUSY => e end ObjectSpace.define_finalizer(self, self.class.finalizer([@scl, @sda])) begin GPIO.direction(@sda, :high) GPIO.direction(@scl, :high) GPIO.direction(@sda, :in) GPIO.direction(@scl, :in) rescue Errno::EACCES => e # writing to gpio after export is failed in a while retry end end |
Instance Attribute Details
#scl ⇒ Object (readonly)
Returns the value of attribute scl.
50 51 52 |
# File 'lib/i2c/driver/gpio.rb', line 50 def scl @scl end |
#sda ⇒ Object (readonly)
Returns the value of attribute sda.
50 51 52 |
# File 'lib/i2c/driver/gpio.rb', line 50 def sda @sda end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
50 51 52 |
# File 'lib/i2c/driver/gpio.rb', line 50 def speed @speed end |
Class Method Details
.direction(pin, direction) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/i2c/driver/gpio.rb', line 23 def self.direction(pin, direction) # [:in, :out, :high, :low].include?(direction) or raise "direction must be :in, :out, :high or :low" File.open("/sys/class/gpio/gpio#{pin}/direction", "w") do |f| f.syswrite(direction) end end |
.export(pin) ⇒ Object
11 12 13 14 15 |
# File 'lib/i2c/driver/gpio.rb', line 11 def self.export(pin) File.open("/sys/class/gpio/export", "w") do |f| f.syswrite(pin) end end |
.finalizer(ports) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/i2c/driver/gpio.rb', line 42 def self.finalizer(ports) proc do ports.each do |pin| GPIO.unexport(pin) end end end |
.read(pin) ⇒ Object
30 31 32 33 34 |
# File 'lib/i2c/driver/gpio.rb', line 30 def self.read(pin) File.open("/sys/class/gpio/gpio#{pin}/value", "r") do |f| f.sysread(1).to_i end end |
.unexport(pin) ⇒ Object
17 18 19 20 21 |
# File 'lib/i2c/driver/gpio.rb', line 17 def self.unexport(pin) File.open("/sys/class/gpio/unexport", "w") do |f| f.syswrite(pin) end end |
.write(pin, val) ⇒ Object
36 37 38 39 40 |
# File 'lib/i2c/driver/gpio.rb', line 36 def self.write(pin, val) File.open("/sys/class/gpio/gpio#{pin}/value", "w") do |f| f.syswrite(val && val.nonzero?? "1" : "0") end end |
Instance Method Details
#i2cget(address, param, length = 1) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/i2c/driver/gpio.rb', line 74 def i2cget(address, param, length=1) ret = "" start_condition unless write( (address << 1) + 0) raise I2CDevice::I2CIOError, "Unknown slave device (address:#{address})" end write(param) stop_condition # AVR stucked with SCL low without this (Does not AVR support Sr condition?) start_condition unless write( (address << 1) + 1) raise I2CDevice::I2CIOError, "Unknown slave device (address:#{address})" end length.times do |n| ret << read(n != length - 1).chr end ret ensure stop_condition end |
#i2cset(address, *data) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/i2c/driver/gpio.rb', line 94 def i2cset(address, *data) sent = 0 start_condition unless write( (address << 1) + 0) raise I2CDevice::I2CIOError, "Unknown slave device (address:#{address})" end data.each do |c| unless write(c) break end sent += 1 end sent ensure stop_condition end |