Class: BitWizard::Boards::FETs

Inherits:
BitWizard::Board show all
Defined in:
lib/bitwizard/nfets.rb

Instance Attribute Summary collapse

Attributes inherited from BitWizard::Board

#address, #bus, #features, #logger, #type, #version

Instance Method Summary collapse

Methods inherited from BitWizard::Board

detect, #read, #valid?, #write

Constructor Details

#initialize(options = {}) ⇒ FETs

Create an instance of a FET board

Parameters:

  • options (optional, Hash) (defaults to: {})

    A Hash of options.

Options Hash (options):

  • :num (Number)

    The number of FETs on the board (3 or 7)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#num_FETsObject (readonly)

Returns the value of attribute num_FETs.



6
7
8
# File 'lib/bitwizard/nfets.rb', line 6

def num_FETs
  @num_FETs
end

Instance Method Details

#[](port) ⇒ Number

Returns the PWM value on the specified port

Parameters:

  • port (Number)

    The port to read the value from

Returns:

  • (Number)

    The PWM value on the port (0..255)



154
155
156
157
158
# 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) ⇒ Object

Sets the PWM value on the specified port

Parameters:

  • port (Number)

    The port to set the value on

  • value (Number)

    The PWM value to set (0..255)



164
165
166
167
168
169
# 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) ⇒ Object

Disables Pulse Width Modulation on the specified port

Parameters:

  • port (Number|Array)

    The port/ports to disable PWM on



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

Enables Pulse Width Modulation on the specified port

Parameters:

  • port (Number|Array)

    The port/ports to enable PWM on



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Boolean

Checks if a port has PWM enabled

Parameters:

  • port (Number)

    The port to check

Returns:

  • (Boolean)

    Is the port enabled for PWM control



101
102
103
# File 'lib/bitwizard/nfets.rb', line 101

def pwm_enabled?(port)
	pwm_ports.include? port
end

#pwm_portsArray

Returns the ports that have PWM enabled

Returns:

  • (Array)

    An array containing the port numbers with PWM enabled



87
88
89
90
91
92
93
94
95
# 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_delayNumber

Read the step delay of the stepper motor

Returns:

  • (Number)

    The stepdelay in tenths of a millisecond



138
139
140
# File 'lib/bitwizard/nfets.rb', line 138

def stepper_delay
	read(0x43, 1)[0]
end

#stepper_delay=(delay) ⇒ Object

Set the step delay of the stepper motor

Parameters:

  • delay (Number)

    The new stepdelay, in tenths of a millisecond (maximum 255 - 25ms between steps)



144
145
146
147
148
# 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_positionNumber

Read the current position of the stepper motor

Returns:

  • (Number)

    The current stepper position



108
109
110
# File 'lib/bitwizard/nfets.rb', line 108

def stepper_position
	read(0x40, 4).pack("C*").unpack("l>")[0]
end

#stepper_position=(position) ⇒ Object

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

Parameters:

  • position (Number)

    The new position of the stepper motor



114
115
116
117
118
# 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_targetNumber

Read the target position of the stepper motor

Returns:

  • (Number)

    The target position of the stepper



123
124
125
# File 'lib/bitwizard/nfets.rb', line 123

def stepper_target
	read(0x41, 4).pack("C*").unpack("l>")[0]
end

#stepper_target=(position) ⇒ Object

Set the target position of the stepper motor

Parameters:

  • position (Number)

    The target position for the stepper motor



129
130
131
132
133
# 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