class OnionOmega::Stepper

Public Class Methods

new( range:, pins: [0, 1, 2, 3], half_stepping: false, persist_to_file: nil, gpio: GPIO.new ) click to toggle source
# File lib/onion_omega/stepper.rb, line 6
def initialize(
  range:,
  pins: [0, 1, 2, 3],
  half_stepping: false,
  persist_to_file: nil,
  gpio: GPIO.new
)
  @range = range
  @pins = pins
  @sequencer = StepperSequencer.new(
    number_of_pins: @pins.count,
    half_stepping:  half_stepping
  )
  @file = persist_to_file
  @current_step = (@file && File.exists?(@file)) ? File.read(@file).to_i : 0
  @gpio = gpio
  initialise_gpio
end

Public Instance Methods

backward(steps=1) click to toggle source
# File lib/onion_omega/stepper.rb, line 29
def backward(steps=1)
  steps.times { step(-1) }
end
forward(steps=1) click to toggle source
# File lib/onion_omega/stepper.rb, line 25
def forward(steps=1)
  steps.times { step(1) }
end
reset() click to toggle source
# File lib/onion_omega/stepper.rb, line 43
def reset
  set_percentage 0
end
set_percentage(percentage) click to toggle source
# File lib/onion_omega/stepper.rb, line 33
def set_percentage(percentage)
  desired_step = (percentage * @range).round
  steps = desired_step - @current_step
  if steps > 0
    steps.times { forward }
  elsif steps < 0
    steps.abs.times { backward }
  end
end

Private Instance Methods

initialise_gpio() click to toggle source
# File lib/onion_omega/stepper.rb, line 49
def initialise_gpio
  @pins.each do |pin|
    @gpio.set_output pin
  end
end
set_pins(pins) click to toggle source
# File lib/onion_omega/stepper.rb, line 61
def set_pins(pins)
  pins.each_with_index do |value, index|
    @gpio.set @pins[index], value
  end
end
step(increment = 1) click to toggle source
# File lib/onion_omega/stepper.rb, line 55
def step(increment = 1)
  @current_step += increment
  set_pins @sequencer.pins_for_step(@current_step)
  File.write @file, @current_step if @file
end