class RPiLedBarGraph

Attributes

brightness[RW]

Public Class Methods

new(pins, wait: 0.1, dir: :ltr, invert: false) click to toggle source
# File lib/rpi_led_bargraph.rb, line 12
def initialize(pins, wait: 0.1, dir: :ltr, invert: false)
  
  @pins = pins.map {|x| RPiLed.new x, smooth: false}
  @wait, @dir, @invert = wait, dir, invert
  @level, @brightness, @blink = 100, 100, false
  
end

Public Instance Methods

brightness=(val) click to toggle source
# File lib/rpi_led_bargraph.rb, line 20
def brightness=(val)
  @brightness = val
  level(@level, blink: @blink)
end
inspect() click to toggle source
# File lib/rpi_led_bargraph.rb, line 39
def inspect()
  'done'
end
level(n, blink: false) click to toggle source

input % from 0-100

# File lib/rpi_led_bargraph.rb, line 45
def level(n, blink: false)    
  
  @blink = blink

  upper = (@pins.length / (100.0 / n) - 1).round
  upper = @pins.length - upper - 1 if @invert
  upper = 0 if upper < 1
  
  a = @dir == :ltr ? @pins : @pins.reverse    
  
  a.each(&:off)
  
  return if n == 0
  
  a[0..(upper).to_i].each {|x| x.on; x.brightness = @brightness}
 
  if @blink then
    sleep 0.3
    a[upper].brightness = @brightness
    a[upper].blink
  end
  
  @level = n
 
end
Also aliased as: progress
off() click to toggle source
# File lib/rpi_led_bargraph.rb, line 32
def off
  a = @dir == :ltr ? @pins.reverse : @pins
  a.each {|x| x.off; sleep @wait}
  @old_level = 0
  :off
end
on() click to toggle source
# File lib/rpi_led_bargraph.rb, line 25
def on
  a = @dir == :ltr ? @pins : @pins.reverse
  a.each {|x| x.on; sleep @wait}
  @old_level = 100
  :on
end
progress(n, blink: false)
Alias for: level