lib/ffi/wiring_pi/gpio.rb in ffi-wiring_pi-0.1.6 vs lib/ffi/wiring_pi/gpio.rb in ffi-wiring_pi-0.2.0

- old
+ new

@@ -103,10 +103,48 @@ else raise "Unknown result: #{result.inspect}" end end + # @param pin [Integer] pin position (depends on setup mode) + # @param value [Integer] 0-1023 + attach_function :pwm_write, :pwmWrite, [:int, :int], :void + + # This writes the 8-bit byte supplied to the first 8 GPIO pins. + # It’s the fastest way to set all 8 bits at once to a particular value, + # although it still takes two write operations to the Pi’s GPIO hardware. + # @param pin [Integer] pin position (depends on setup mode) + # @param value [Integer] 0-1023 + attach_function :digital_write_byte, :digitalWriteByte, [:int], :void + + def self.batch_write(boolean_array) + digital_write_byte boolean_array.each_with_index.sum { |bit, i| (bit ? 1 : 0 ) << i } + end + + # The PWM generator can run in 2 modes – :balanced and :mark_space + # The :mark_space mode is traditional, however the default mode in the Pi is :balanced. + # @param value [Integer] PWM_MODE_BAL or PWM_MODE_MS + attach_function :pwm_set_mode, :pwmSetMode, [:int], :void + + def self.pwm_mode(mode = :balanced) + raise ArgumentError("mode is invalid: #{mode.inspect}") unless mode.in?([:balanced, :mark_space]) + pwm_set_mode mode == :balanced ? PWM_MODE_BAL : PWM_MODE_MS + end + + # This sets the range register in the PWM generator. The default is 1024. + # @param value [Integer] + attach_function :pwm_set_range, :pwmSetRange, [:uint], :void + + def self.pwm_range(range = 1024) + raise ArgumentError("range is invalid: #{range.inspect}") unless range.is_a?(Integer) && range >= 0 + pwm_set_range range + end + + # This sets the divisor for the PWM clock. + # @param value [Integer] + attach_function :pwm_set_clock, :pwmSetClock, [:int], :void + class Pin def initialize(position, mode) @position = position @mode = mode end @@ -117,8 +155,13 @@ end def down! raise ArgumentError('Can only set in OUTPUT mode') && return unless @mode == FFI::WiringPi::GPIO::OUTPUT FFI::WiringPi::GPIO.down(@position) + end + + def value + raise ArgumentError('Can only set in INPUT mode') && return unless @mode == FFI::WiringPi::GPIO::INPUT + FFI::WiringPi::GPIO.read(@position) end end end