Sha256: 9e31494d6fa1696d6e14a30ce51eadf09adf44f597104d27bfd40960dfcb3cb0

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module WallE
  class Servo
    OutOfBoundsError = Class.new(StandardError)

    # Public: Initialize a Servo
    #
    # pin     - the Pin the servo is attached to.
    # options - the Hash options to configure the servo (default: {}):
    #           :range - the Range to restrict movement (default 0..180).
    def initialize(pin, options = {})
      @pin = pin
      @pin.set_mode(Pin::SERVO)
      @range = options.fetch(:range) { 0..180 }
    end

    # Public: Move the servo the the minimum degree.
    #
    # Returns nothing.
    def min
      move_to @range.min
    end

    # Public: Move the servo to the maximum degree.
    #
    # Returns nothing.
    def max
      move_to @range.max
    end

    # Public: Move the servo to the center degree.
    #
    # Returns nothing.
    def center
      move_to ((@range.min + @range.max) / 2).abs
    end

    # Public: Move the servo.
    #
    # degrees - the Integer degrees to move to.
    #
    # Returns nothing.
    # Raises OutOfBoundsError if the servo cannot move to the degree.
    def move_to(degrees)
      raise OutOfBoundsError unless @range.include?(degrees)
      @pin.servo_write(degrees)
    end

    def maxed?
      position == @range.max
    end

    def position
      @pin.value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wall_e-0.1.0 lib/wall_e/components/servo.rb
wall_e-0.0.4 lib/wall_e/components/servo.rb
wall_e-0.0.3 lib/wall_e/components/servo.rb
wall_e-0.0.2 lib/wall_e/components/servo.rb