Class: Sparky

Inherits:
Object
  • Object
show all
Defined in:
lib/sparky.rb

Constant Summary collapse

@@run_pins =
[[Switchy::PINS::C4, Switchy::PINS::C2], [Switchy::PINS::C5, Switchy::PINS::D0 ]]
@@fail_pins =
[[Switchy::PINS::C6, Switchy::PINS::D1], [Switchy::PINS::C7, Switchy::PINS::D2]]
@@pending_pins =
[[Switchy::PINS::B7, Switchy::PINS::D3], [Switchy::PINS::B6, Switchy::PINS::D4]]
@@pass_pins =
[[Switchy::PINS::B3, Switchy::PINS::D7, Switchy::PINS::B5, Switchy::PINS::D5],
[Switchy::PINS::B2, Switchy::PINS::B0, Switchy::PINS::B4, Switchy::PINS::D6]]
@@reset_pins =
[[Switchy::PINS::C4, Switchy::PINS::C2,
Switchy::PINS::C6, Switchy::PINS::D1,
Switchy::PINS::B7, Switchy::PINS::D3, 
Switchy::PINS::B5, Switchy::PINS::B3, 
Switchy::PINS::D7, Switchy::PINS::D5]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSparky

Returns a new instance of Sparky.



20
21
22
23
# File 'lib/sparky.rb', line 20

def initialize
  @passed, @failed, @pending = -1, -1, -1
  @switchy = Switchy.new
end

Instance Attribute Details

#failedObject

Returns the value of attribute failed.



7
8
9
# File 'lib/sparky.rb', line 7

def failed
  @failed
end

#passedObject

Returns the value of attribute passed.



7
8
9
# File 'lib/sparky.rb', line 7

def passed
  @passed
end

#pendingObject

Returns the value of attribute pending.



7
8
9
# File 'lib/sparky.rb', line 7

def pending
  @pending
end

Instance Method Details

#clear_failedObject



61
62
63
# File 'lib/sparky.rb', line 61

def clear_failed
  clear_pins @@fail_pins
end

#clear_passObject



53
54
55
# File 'lib/sparky.rb', line 53

def clear_pass
  clear_pins @@pass_pins
end

#clear_pendingObject



69
70
71
# File 'lib/sparky.rb', line 69

def clear_pending
  clear_pins @@pending_pins
end

#clear_pin(pin) ⇒ Object



39
40
41
# File 'lib/sparky.rb', line 39

def clear_pin(pin)
  @switchy.set_pin pin.first, 0
end

#clear_pins(pins) ⇒ Object



43
44
45
46
47
# File 'lib/sparky.rb', line 43

def clear_pins(pins)
  pins.first.each do |pin|
    @switchy.set_pin pin, 0
  end
end

#clear_runObject



77
78
79
# File 'lib/sparky.rb', line 77

def clear_run
  clear_pins @@run_pins
end

#example_failedObject



107
108
109
110
111
# File 'lib/sparky.rb', line 107

def example_failed
  clear_pin pin(:failed, @failed % 2) if @failed >= 0
  @failed += 1
  set_pin pin(:failed, @failed % 2)
end

#example_passedObject



101
102
103
104
105
# File 'lib/sparky.rb', line 101

def example_passed
  clear_pin pin(:passed, @passed % 4) if @passed >= 0
  @passed += 1
  set_pin pin(:passed, @passed % 4)
end

#example_pendingObject



113
114
115
116
117
# File 'lib/sparky.rb', line 113

def example_pending
  clear_pin pin(:pending, @pending % 2) if @pending >= 0
  @pending += 1
  set_pin pin(:pending, @pending % 2)
end

#finish_runObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/sparky.rb', line 90

def finish_run
  reset
  if @failed > -1
    failed
  elsif @pending > -1
    pending
  else
    pass
  end
end

#passObject



49
50
51
# File 'lib/sparky.rb', line 49

def pass
  set_pins @@pass_pins
end

#pin(set, idx) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/sparky.rb', line 119

def pin(set, idx)
  pins = case set
         when :passed then @@pass_pins
         when :failed then @@fail_pins
         when :pending then @@pending_pins
         end
  [ pins.first[idx], pins.last[idx] ]
end

#resetObject



81
82
83
# File 'lib/sparky.rb', line 81

def reset
  clear_pins @@reset_pins
end

#runObject



73
74
75
# File 'lib/sparky.rb', line 73

def run
  set_pins @@run_pins
end

#set_pin(pin) ⇒ Object



34
35
36
37
# File 'lib/sparky.rb', line 34

def set_pin(pin)
  @switchy.set_pin pin.first, 1
  @switchy.set_pin pin.last, 0
end

#set_pins(pins) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/sparky.rb', line 25

def set_pins(pins)
  pins.first.each do |pin|
    @switchy.set_pin pin, 1
  end
  pins.last.each do |pin|
    @switchy.set_pin pin, 0
  end
end

#start_runObject



85
86
87
88
# File 'lib/sparky.rb', line 85

def start_run
  reset
  run
end