class BitWizard::Boards::FETs

Attributes

num_FETs[R]

Public Class Methods

new(options={}) click to toggle source

Create an instance of a FET board

@param [optional, Hash] options A Hash of options.
@option options [Number] :num The number of FETs on the board (3 or 7)
Calls superclass method BitWizard::Board::new
# File lib/bitwizard/nfets.rb, line 12
def initialize(options={})
        options = {
                num: 3,
                bus: :spi
        }.merge(options)
        options = options.merge({
                type: "#{options[:bus]}_#{options[:num]}fets".to_sym,
        })

        raise ArgumentError.new "Number of FETs must be 3 or 7" unless options[:num] == 3 or options[:num] == 7

        super(options)

        @num_FETs = options[:num]
end

Public Instance Methods

[](port) click to toggle source

Returns the PWM value on the specified port

@param [Number] port The port to read the value from
@return [Number] The PWM value on the port (0..255)
# File lib/bitwizard/nfets.rb, line 154
def [](port)
        raise ArgumentError.new "Port must be an integer between 1 and #{@num_FETs}" unless port.is_a? Fixnum and (1..@num_FETs).include? port

        return read(0x50 + (port-1), 1)[0]
end
[]=(port, value) click to toggle source

Sets the PWM value on the specified port

@param [Number] port The port to set the value on
@param [Number] value The PWM value to set (0..255)
# File lib/bitwizard/nfets.rb, line 164
def []=(port, value)
        raise ArgumentError.new "Port must be an integer between 1 and #{@num_FETs}" unless port.is_a? Fixnum and (1..@num_FETs).include? port
        raise ArgumentError.new "Value must be an integer between 0 and 255" unless value.is_a? Fixnum and (0..255).include? value

        write(0x50 + (port-1), value)
end
pwm_disable(*port) click to toggle source

Disables Pulse Width Modulation on the specified port

@param [Number|Array] port The port/ports to disable PWM on
# File lib/bitwizard/nfets.rb, line 59
def pwm_disable(*port)
        case port.count
        when 0
                raise ArgumentError.new "wrong number of arguments"
        when 1
                port = port.first
        end

        if port.is_a? Array then
                port.each do |port|
                        pwm_disable port
                end
                return true
        end
        raise ArgumentError.new "Port must be an integer between 1 and #{@num_FETs}" unless port.is_a? Fixnum and (1..@num_FETs).include? port

        port = 2**(port-1)

        curPWM = read(0x5f, 1)[0]
        tgtPWM = curPWM & ~port
        write(0x5f, tgtPWM)

        true
end
pwm_enable(*port) click to toggle source

Enables Pulse Width Modulation on the specified port

@param [Number|Array] port The port/ports to enable PWM on
# File lib/bitwizard/nfets.rb, line 31
def pwm_enable(*port)
        case port.count
        when 0
                raise ArgumentError.new "wrong number of arguments"
        when 1
                port = port.first
        end

        if port.is_a? Array then
                port.each do |port|
                        pwm_enable port
                end
                return true
        end
        raise ArgumentError.new "Port must be an integer between 1 and #{@num_FETs}" unless port.is_a? Fixnum and (1..@num_FETs).include? port

        port = 2**(port-1)

        curPWM = read(0x5f, 1)[0]
        tgtPWM = curPWM | port
        write(0x5f, tgtPWM)

        true
end
pwm_enabled?(port) click to toggle source

Checks if a port has PWM enabled

@param [Number] port The port to check
@return [Boolean] Is the port enabled for PWM control
# File lib/bitwizard/nfets.rb, line 101
def pwm_enabled?(port)
        pwm_ports.include? port
end
pwm_ports() click to toggle source

Returns the ports that have PWM enabled

@return [Array] An array containing the port numbers with PWM enabled
# File lib/bitwizard/nfets.rb, line 87
def pwm_ports
        curPWM = read(0x5f, 1)[0]

        ret = []
        (1..@num_FETs).each do |port|
                ret << port if curPWM & 2**(port-1) > 0
        end
        ret
end
stepper_delay() click to toggle source

Read the step delay of the stepper motor

@return [Number] The stepdelay in tenths of a millisecond
# File lib/bitwizard/nfets.rb, line 138
def stepper_delay
        read(0x43, 1)[0]
end
stepper_delay=(delay) click to toggle source

Set the step delay of the stepper motor

@param [Number] delay The new stepdelay, in tenths of a millisecond (maximum 255 - 25ms between steps)
# File lib/bitwizard/nfets.rb, line 144
def stepper_delay=(delay)
        raise ArgumentError.new "Delay must be an integer between 0 and 255" unless delay.is_a? Fixnum and (0..255).include? delay

        write(0x43, delay)
end
stepper_position() click to toggle source

Read the current position of the stepper motor

@return [Number] The current stepper position
# File lib/bitwizard/nfets.rb, line 108
def stepper_position
        read(0x40, 4).pack("C*").unpack("l>")[0]
end
stepper_position=(position) click to toggle source

Set the current position of the stepper motor, without actually moving it

@param [Number] position The new position of the stepper motor
# File lib/bitwizard/nfets.rb, line 114
def stepper_position=(position)
        raise ArgumentError.new "Position must be an integer" unless position.is_a? Fixnum

        write(0x40, [position].pack("l>"))
end
stepper_target() click to toggle source

Read the target position of the stepper motor

@return [Number] The target position of the stepper
# File lib/bitwizard/nfets.rb, line 123
def stepper_target
        read(0x41, 4).pack("C*").unpack("l>")[0]
end
stepper_target=(position) click to toggle source

Set the target position of the stepper motor

@param [Number] position The target position for the stepper motor
# File lib/bitwizard/nfets.rb, line 129
def stepper_target=(position)
        raise ArgumentError.new "Position must be an integer" unless position.is_a? Fixnum

        write(0x41, [position].pack("l>"))
end