Skip to content

Commit

Permalink
Replace more super calls with lifecycle callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Sep 27, 2024
1 parent 6767b57 commit e57164c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 49 deletions.
10 changes: 5 additions & 5 deletions lib/denko/analog_io/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class Output
include Behaviors::OutputPin
include Behaviors::Callbacks
include Behaviors::Threaded

include Behaviors::Lifecycle

interrupt_with :write

def initialize_pins(options={})
super(options)
self.mode = :output_dac

before_initialize do
params[:mode] = :output_dac
end

def write(value)
Expand Down
26 changes: 8 additions & 18 deletions lib/denko/behaviors/input_pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,24 @@ module Behaviors
module InputPin
include Component
include SinglePin
include Lifecycle

INPUT_MODES = [:input, :input_pulldown, :input_pullup]

before_initialize do
params[:mode] ||= :input
unless INPUT_MODES.include?(params[:mode])
raise "invalid input mode: #{params[:mode]} given. Should be one of #{INPUT_MODES.inspect}"
end
end

def _stop_listener
board.stop_listener(pin)
end

def debounce_time=(value)
board.set_pin_debounce(pin, value)
end

protected

def initialize_pins(options={})
super(options)

# Allow pull direction to be set with :mode, else default to :input.
if options[:mode]
initial_mode = options[:mode]
unless INPUT_MODES.include?(initial_mode)
raise "invalid input mode: #{initial_mode} given. Should be one of #{INPUT_MODES.inspect}"
end
else
initial_mode = :input
end

self.mode = initial_mode
end
end
end
end
20 changes: 5 additions & 15 deletions lib/denko/behaviors/output_pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@ module Behaviors
module OutputPin
include Component
include SinglePin
include Lifecycle

OUTPUT_MODES = [:output, :output_pwm, :output_dac, :output_open_drain, :output_open_source]

protected

def initialize_pins(options={})
super(options)

# Allow output type to be set with :mode, else default to :output.
if options[:mode]
initial_mode = options[:mode]
unless OUTPUT_MODES.include?(initial_mode)
raise "invalid input mode: #{initial_mode} given. Should be one of #{OUTPUT_MODES.inspect}"
end
else
initial_mode = :output
before_initialize do
params[:mode] ||= :output
unless OUTPUT_MODES.include?(params[:mode])
raise "invalid input mode: #{params[:mode]} given. Should be one of #{OUTPUT_MODES.inspect}"
end

self.mode = initial_mode
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/denko/behaviors/single_pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def convert_pins(options={})

def initialize_pins(options={})
@pin = options[:pin]
self.mode = params[:mode] if params[:mode]
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/denko/pulse_io/buzzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ module PulseIO
class Buzzer < PWMOutput
include Behaviors::Lifecycle

def initialize_pins(options={})
options[:mode] = :output_pwm
super(options)
before_initialize do
params[:mode] ||= :output_pwm
end

after_initialize do
low
board.no_tone(pin)
end

# Duration is in mills
Expand Down
6 changes: 3 additions & 3 deletions lib/denko/pulse_io/ir_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module Denko
module PulseIO
class IROutput
include Behaviors::OutputPin
include Behaviors::Lifecycle

def initialize_pins(options={})
options[:mode] = :output_pwm
super(options)
before_initialize do
params[:mode] = :output_pwm
end

def write(pulses=[], frequency: 38)
Expand Down
11 changes: 7 additions & 4 deletions test/pulse_io/buzzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ def part
end

def test_low_on_initialize
assert_equal part.state, board.low
mock = Minitest::Mock.new.expect :call, [part.pin]
board.stub(:no_tone, mock) do
part
end
end

def test_tone
mock = Minitest::Mock.new
mock.expect :call, nil, [part.pin, 60, nil]
Expand All @@ -23,7 +26,7 @@ def test_tone
end
mock.verify
end

def test_no_tone
part
mock = Minitest::Mock.new
Expand All @@ -33,7 +36,7 @@ def test_no_tone
end
mock.verify
end

def stop
mock = Minitest::Mock.new
mock.expect :call, nil
Expand Down

0 comments on commit e57164c

Please sign in to comment.