Class: SimpleRaspberryPi

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_raspberrypi.rb

Defined Under Namespace

Classes: PinX, Void

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = []) ⇒ SimpleRaspberryPi

Returns a new instance of SimpleRaspberryPi.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/simple_raspberrypi.rb', line 92

def initialize(x=[])
  
  RPi::GPIO.set_numbering :bcm  
  
  a = case x
  when Fixnum
    [x]
  when String
    [x]
  when Array
    x
  end

  unexport_all a
  
  @pins = a.map {|pin| PinX.new pin }
  
  def @pins.[](i)

    if i.to_i >= self.length then
      puts "RPi warning: PinX instance #{i.inspect} not found"
      Void.new
    else
      self.at(i)
    end 
  end    
  
  at_exit do
    
    # to avoid "Device or resource busy @ fptr_finalize - /sys/class/gpio/export"
    # we unexport the pins we used
    
    unexport_all a
  end    
end

Class Method Details

.[](i) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/simple_raspberrypi.rb', line 109

def @pins.[](i)

  if i.to_i >= self.length then
    puts "RPi warning: PinX instance #{i.inspect} not found"
    Void.new
  else
    self.at(i)
  end 
end

.unexport(a) ⇒ Object



147
148
149
# File 'lib/simple_raspberrypi.rb', line 147

def self.unexport(a)
  a.each {|pin| File.write "/sys/class/gpio/unexport", pin }
end

Instance Method Details

#on_exitObject



143
144
145
# File 'lib/simple_raspberrypi.rb', line 143

def on_exit
  unexport_all @pins
end

#pinObject



128
# File 'lib/simple_raspberrypi.rb', line 128

def pin()   @pins.first  end

#pinsObject



129
# File 'lib/simple_raspberrypi.rb', line 129

def pins()  @pins        end

#unexport_all(pins) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/simple_raspberrypi.rb', line 131

def unexport_all(pins)
  
  pins.each do |pin|
    
    next unless File.exists? '/sys/class/gpio/gpio' + pin.to_s

    File.write "/sys/class/gpio/unexport", pin

  end
  
end