Sha256: 3456bada7523d19509af928055e805a13dfe8eb8250d90e79fb65bccc37d34e3

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'ostruct'
require 'thread'
Thread.abort_on_exception = true

require_relative './led_transition'
require_relative './transition_queue'

module Frankenpins
  class LED

    attr_reader :default_duration
    attr_writer :default_duration

    def initialize(options={})
      options[:direction] = :out
      @pin = Frankenpins::Pin.new(options)

      @using_pwm = false
      @pwm_max   = 100

      @is_on = false
      @brightness = 0

      # TODO: Should be 0?
      @default_duration = nil

      @queue = TransitionQueue.new
      @queue.start!
    end

    def on(opts={})
      brightness(100, opts)
      @is_on = true
    end

    def off(opts={})
      brightness(0, opts)
      @is_on = false
    end

    def brightness(value, opts={})

      duration = opts[:duration] || @default_duration

      if value != 100 || value != 0 || duration
        use_pwm!
      end

      props = { :pin => @pin }

      if duration
        props[:type]     = :pwm
        props[:from]     = @brightness
        props[:to]       = value
        props[:duration] = duration
      elsif @using_pwm
        props[:type]  = :pwm
        props[:value] = value
      else
        props[:type]  = :digital
        props[:value] = false if @brightness == 0
        props[:value] = true  if @brightness == 100
      end

      @queue.push(LEDTransition.new(props))
      @brightness = value
    end

    private
    def use_pwm!
      if @using_pwm == false
        @pin.io.soft_pwm_create(@pin.wiring_pin, 0, @pwm_max)
        @using_pwm = true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frankenpins-0.3.0 lib/frankenpins/led.rb