Sha256: 91e42ecf122eb2fc19ca153763bcc17ff55334d6b7f5f170973488927e1faba6

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

require 'artoo/drivers/driver'

module Artoo
  module Drivers
    # The LED driver behaviors
    class Led < Driver

      COMMANDS = [:on, :off, :toggle, 
                  :brightness, 
                  :on?, :off?].freeze

      def initialize(params = {})
        @is_on = false
        super
      end

      # @return [Boolean] True if on
      def on?
        @is_on
      end

      # @return [Boolean] True if off
      def off?
        (not on?)
      end

      # Sets led to level HIGH
      def on
        change_state(pin, :high)
        @is_on = true
        true
      end

      # Sets led to level LOW
      def off
        change_state(pin, :low)
        @is_on = false
        true
      end

      # Toggle status
      # @example on > off, off > on
      def toggle
        on? ? off : on
      end

      # Change brightness level
      # @param [Integer] level
      def brightness(level=0)
        connection.pwm_write(pin, level)
      end

      private
      def change_state(pin, level)
        connection.digital_write(pin, level)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
artoo-gpio-0.4.1 lib/artoo/drivers/led.rb
artoo-gpio-0.4.0 lib/artoo/drivers/led.rb
artoo-gpio-0.3.0 lib/artoo/drivers/led.rb
artoo-gpio-0.2.1 lib/artoo/drivers/led.rb
artoo-gpio-0.2.0 lib/artoo/drivers/led.rb
artoo-gpio-0.1.0 lib/artoo/drivers/led.rb
artoo-gpio-0.0.1 lib/artoo/drivers/led.rb
artoo-arduino-1.1.0 lib/artoo/drivers/led.rb