Class: BitWizard::Boards::FETs
- Inherits:
-
BitWizard::Board
- Object
- BitWizard::Board
- BitWizard::Boards::FETs
- Defined in:
- lib/bitwizard/nfets.rb
Instance Attribute Summary collapse
-
#num_FETs ⇒ Object
readonly
Returns the value of attribute num_FETs.
Attributes inherited from BitWizard::Board
#address, #bus, #features, #logger, #type, #version
Instance Method Summary collapse
-
#[](port) ⇒ Number
Returns the PWM value on the specified port.
-
#[]=(port, value) ⇒ Object
Sets the PWM value on the specified port.
-
#initialize(options = {}) ⇒ FETs
constructor
Create an instance of a FET board.
-
#pwm_disable(*port) ⇒ Object
Disables Pulse Width Modulation on the specified port.
-
#pwm_enable(*port) ⇒ Object
Enables Pulse Width Modulation on the specified port.
-
#pwm_enabled?(port) ⇒ Boolean
Checks if a port has PWM enabled.
-
#pwm_ports ⇒ Array
Returns the ports that have PWM enabled.
-
#stepper_delay ⇒ Number
Read the step delay of the stepper motor.
-
#stepper_delay=(delay) ⇒ Object
Set the step delay of the stepper motor.
-
#stepper_position ⇒ Number
Read the current position of the stepper motor.
-
#stepper_position=(position) ⇒ Object
Set the current position of the stepper motor, without actually moving it.
-
#stepper_target ⇒ Number
Read the target position of the stepper motor.
-
#stepper_target=(position) ⇒ Object
Set the target position of the stepper motor.
Methods inherited from BitWizard::Board
detect, #read, #valid?, #write
Constructor Details
#initialize(options = {}) ⇒ FETs
Create an instance of a FET board
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bitwizard/nfets.rb', line 12 def initialize(={}) = { num: 3, bus: :spi }.merge() = .merge({ type: "#{[:bus]}_#{[:num]}fets".to_sym, }) raise ArgumentError.new "Number of FETs must be 3 or 7" unless [:num] == 3 or [:num] == 7 super() @num_FETs = [:num] end |
Instance Attribute Details
#num_FETs ⇒ Object (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
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
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
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
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
101 102 103 |
# File 'lib/bitwizard/nfets.rb', line 101 def pwm_enabled?(port) pwm_ports.include? port end |
#pwm_ports ⇒ Array
Returns the ports that have 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_delay ⇒ Number
Read the step delay of the stepper motor
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
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_position ⇒ Number
Read the current position of the stepper motor
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
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_target ⇒ Number
Read the target position of the stepper motor
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
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 |