class RPiLedSimulator

Public Class Methods

new(x=[], color: :red, symbol: '▋', spacer: 0, colors: []) click to toggle source
# File lib/rpi_led_simulator.rb, line 30
def initialize(x=[], color: :red, symbol: '▋', spacer: 0, colors: [])

  @color, @symbol, @spacer, @colors = color, symbol, spacer, colors
  
  a = case x
  when Integer
    [x]    
  when Fixnum
    [x]
  when String
    [x]
  when Array
    x
  end

  @pins = a.map.with_index do |pin, i|
    
    Led.new pin, color: colors[i] || color, parent: self 
    
  end
  
  def @pins.[](i)

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

Public Instance Methods

pin() click to toggle source
# File lib/rpi_led_simulator.rb, line 63
def pin()   @pins.first  end
pins() click to toggle source
# File lib/rpi_led_simulator.rb, line 65
def pins()
  
  r = @pins
  
  def @pins.inspect()
    self.map(&:id)
  end
  
  r
end
refresh(n=0) click to toggle source
# File lib/rpi_led_simulator.rb, line 76
def refresh(n=0)

  # get the pin value

  colors = @pins.map {|pin| pin.color }
  a = [" ".on_white]
  a.concat colors.map{|c| @symbol.method((c.to_s + '_on_white').to_sym).call }
  
  print "\r " + a.join(" ".on_white * @spacer.to_i) + " ".on_white + " "

  return if n < 1

  sleep 0.01
  refresh n-1

end
watch(n=2.8) click to toggle source
# File lib/rpi_led_simulator.rb, line 93
def watch(n=2.8)
  refresh n * 100
  puts
end