Class: Artoo::Adaptors::Raspi
- Inherits:
-
Adaptor
- Object
- Adaptor
- Artoo::Adaptors::Raspi
- Includes:
- IO
- Defined in:
- lib/artoo/adaptors/raspi.rb
Overview
Connect to a Raspberry Pi GPIO
Constant Summary collapse
- PINS =
{ 3 => {:rev1 => 0, :rev2 => 2}, 5 => {:rev1 => 1, :rev2 => 3}, 7 => 4, 8 => 14, 10 => 15, 11 => 17, 12 => 18, 13 => {:rev1 => 21, :rev2 => 27}, 15 => 22, 16 => 23, 18 => 24, 19 => 10, 21 => 9, 22 => 25, 23 => 11, 24 => 8, 26 => 7, }
Instance Attribute Summary collapse
-
#board_version ⇒ Object
readonly
Returns the value of attribute board_version.
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#i2c ⇒ Object
readonly
Returns the value of attribute i2c.
-
#pins ⇒ Object
readonly
Returns the value of attribute pins.
-
#pwm_pins ⇒ Object
readonly
Returns the value of attribute pwm_pins.
Instance Method Summary collapse
-
#connect ⇒ Boolean
Creates a connection with device.
-
#digital_read(pin) ⇒ Object
GPIO - digital interface.
- #digital_write(pin, val) ⇒ Object
-
#disconnect ⇒ Boolean
Closes connection with device.
- #firmware_name ⇒ Object
- #i2c_read(len) ⇒ Object
-
#i2c_start(address) ⇒ Object
i2c.
- #i2c_write(*data) ⇒ Object
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Uses method missing to call device actions.
- #pwm_write(pin, val) ⇒ Object
- #release_all_pwm_pins ⇒ Object
- #release_pwm(pin) ⇒ Object
- #version ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Uses method missing to call device actions
107 108 109 |
# File 'lib/artoo/adaptors/raspi.rb', line 107 def method_missing(method_name, *arguments, &block) device.send(method_name, *arguments, &block) end |
Instance Attribute Details
#board_version ⇒ Object (readonly)
Returns the value of attribute board_version.
32 33 34 |
# File 'lib/artoo/adaptors/raspi.rb', line 32 def board_version @board_version end |
#device ⇒ Object (readonly)
Returns the value of attribute device.
32 33 34 |
# File 'lib/artoo/adaptors/raspi.rb', line 32 def device @device end |
#i2c ⇒ Object (readonly)
Returns the value of attribute i2c.
32 33 34 |
# File 'lib/artoo/adaptors/raspi.rb', line 32 def i2c @i2c end |
#pins ⇒ Object (readonly)
Returns the value of attribute pins.
32 33 34 |
# File 'lib/artoo/adaptors/raspi.rb', line 32 def pins @pins end |
#pwm_pins ⇒ Object (readonly)
Returns the value of attribute pwm_pins.
32 33 34 |
# File 'lib/artoo/adaptors/raspi.rb', line 32 def pwm_pins @pwm_pins end |
Instance Method Details
#connect ⇒ Boolean
Creates a connection with device
36 37 38 39 40 41 |
# File 'lib/artoo/adaptors/raspi.rb', line 36 def connect @pins = {} if @pins.nil? @pwm_pins = {} if @pwm_pins.nil? @board_version = `cat /proc/cpuinfo | grep Revision`.split.last.unpack("CCCC").last super end |
#digital_read(pin) ⇒ Object
GPIO - digital interface
78 79 80 81 82 |
# File 'lib/artoo/adaptors/raspi.rb', line 78 def digital_read(pin) release_pwm(pin) if (pwm_used? pin) pin = raspi_pin(pin, "r") pin.digital_read end |
#digital_write(pin, val) ⇒ Object
84 85 86 87 88 |
# File 'lib/artoo/adaptors/raspi.rb', line 84 def digital_write(pin, val) release_pwm(pin) if (pwm_used? pin) pin = raspi_pin(pin, "w") pin.digital_write(val) end |
#disconnect ⇒ Boolean
Closes connection with device
45 46 47 48 49 |
# File 'lib/artoo/adaptors/raspi.rb', line 45 def disconnect puts "Disconnecting all PWM pins..." release_all_pwm_pins super end |
#firmware_name ⇒ Object
51 52 53 |
# File 'lib/artoo/adaptors/raspi.rb', line 51 def firmware_name "Raspberry Pi" end |
#i2c_read(len) ⇒ Object
73 74 75 |
# File 'lib/artoo/adaptors/raspi.rb', line 73 def i2c_read len @i2c.read len end |
#i2c_start(address) ⇒ Object
i2c
60 61 62 63 64 65 66 67 |
# File 'lib/artoo/adaptors/raspi.rb', line 60 def i2c_start address if @board_version >= 100 i2c_location = "/dev/i2c-1" else i2c_location = "/dev/i2c-0" end @i2c = I2c.new i2c_location, address end |
#i2c_write(*data) ⇒ Object
69 70 71 |
# File 'lib/artoo/adaptors/raspi.rb', line 69 def i2c_write *data @i2c.write *data end |
#pwm_write(pin, val) ⇒ Object
90 91 92 93 |
# File 'lib/artoo/adaptors/raspi.rb', line 90 def pwm_write(pin, val) pin = pwm_pin(pin) pin.pwm_write(val) end |
#release_all_pwm_pins ⇒ Object
101 102 103 |
# File 'lib/artoo/adaptors/raspi.rb', line 101 def release_all_pwm_pins pwm_pins.each_value { |pwm_pin| pwm_pin.release } end |
#release_pwm(pin) ⇒ Object
95 96 97 98 99 |
# File 'lib/artoo/adaptors/raspi.rb', line 95 def release_pwm(pin) pin = translate_pin(pin) pwm_pins[pin].release pwm_pins[pin] = nil end |