Sha256: d67a2a1ea99f3b4e6f73bd854cc7c6feeda5c13c0ef494a6c720f85c8cf9c852

Contents?: true

Size: 886 Bytes

Versions: 8

Compression:

Stored size: 886 Bytes

Contents

#frozen_string_literal: true

module FFI::WiringPi::SoftPwm
  extend FFI::Library

  ffi_lib 'wiringPi'

  attach_function :soft_pwm_create, :softPwmCreate, [:int, :int, :int], :int
  attach_function :soft_pwm_write, :softPwmWrite, [:int, :int], :void

  class Pin
    def initialize(pin, initial_state = 0, range = 100)
      raise ArgumentError, 'Range should be Integer > 0' unless range.is_a?(Integer) && range > 0
      raise ArgumentError, 'State should be within the range' unless initial_state.is_a?(Integer) && (0..range).cover?(initial_state)
      @pin = pin
      @range = range
      FFI::WiringPi::SoftPwm.soft_pwm_create pin, initial_state, range
    end

    def write(value)
      raise ArgumentError, 'Value should be within the range' unless value.is_a?(Integer) && (0..@range).cover?(value)
      FFI::WiringPi::SoftPwm.soft_pwm_write @pin, value
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ffi-wiring_pi-1.0.3 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-1.0.2 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-1.0.1 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-1.0.0 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-0.6.0 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-0.5.0 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-0.4.0 lib/ffi/wiring_pi/soft_pwm.rb
ffi-wiring_pi-0.3.0 lib/ffi/wiring_pi/soft_pwm.rb