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.



90
91
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
# File 'lib/simple_raspberrypi.rb', line 90

def initialize(x=[])
  
  a = case x
  when Fixnum
    [x]
  when String
    [x]
  when Array
    x
  end

  unexport_all a
  
  
  @pins = a.map {|pin| PinX.new pin.to_i }
  
  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



106
107
108
109
110
111
112
113
114
# File 'lib/simple_raspberrypi.rb', line 106

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



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

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

Instance Method Details

#on_exitObject



140
141
142
# File 'lib/simple_raspberrypi.rb', line 140

def on_exit
  unexport_all @pins
end

#pinObject



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

def pin()   @pins.first  end

#pinsObject



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

def pins()  @pins        end

#unexport_all(pins) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/simple_raspberrypi.rb', line 128

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