Class: WallE::Assembler

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arduino) ⇒ Assembler

Returns a new instance of Assembler.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wall_e.rb', line 31

def initialize(arduino)
  @board = arduino
  @board.connect unless arduino.connected?
  @running = true
  @group = ThreadGroup.new

   Thread.new do
    loop do
      begin
        arduino.read_and_process
        sleep(0.5)
      rescue Exception => e
        puts e.message
        puts e.backtrace.inspect
        Thread.kill
      end
    end
  end
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



15
16
17
# File 'lib/wall_e.rb', line 15

def board
  @board
end

Class Method Details

.build(&block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/wall_e.rb', line 17

def self.build(&block)
  wall_e = create

  wall_e.instance_eval(&block) if block_given?

  Pry.start(wall_e, :prompt => [ proc { |obj, *| "wall_e > " },
                                 proc { |obj, *| "wall_e* "} ])
end

.createObject



26
27
28
29
# File 'lib/wall_e.rb', line 26

def self.create
  arduino = SerialSnoop.locate_ports
  new(arduino)
end

Instance Method Details

#Button(pin_number) ⇒ Object



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

def Button(pin_number)
  pin = Pin.new(pin_number, @board)
  (buttons << Button.new(pin)).last
end

#buttonsObject



95
96
97
# File 'lib/wall_e.rb', line 95

def buttons
  @buttons ||= []
end

#Claw(claw_pin_number, pan_pin_number) ⇒ Object



67
68
69
70
71
# File 'lib/wall_e.rb', line 67

def Claw(claw_pin_number, pan_pin_number)
  claw_servo = Servo(claw_pin_number, range: 60..144)
  pan_servo  = Servo(pan_pin_number)
  (claws << Claw.new(claw_servo, pan_servo)).last
end

#clawsObject



91
92
93
# File 'lib/wall_e.rb', line 91

def claws
  @claws ||= []
end

#delay(seconds) ⇒ Object



99
100
101
# File 'lib/wall_e.rb', line 99

def delay(seconds)
  @board.delay seconds
end

#Led(pin_number) ⇒ Object

TODO some metaprogramming sauce to reduce the component helper code.



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

def Led(pin_number)
  pin = Pin.new(pin_number, @board)
  (leds << Led.new(pin)).last
end

#ledsObject

TODO wrap up these collections in some metaprogramming sauce too.



79
80
81
# File 'lib/wall_e.rb', line 79

def leds
  @leds ||= []
end

#pauseObject



103
104
105
# File 'lib/wall_e.rb', line 103

def pause
  @running = false
end

#Piezo(pin_number) ⇒ Object



62
63
64
65
# File 'lib/wall_e.rb', line 62

def Piezo(pin_number)
  pin = Pin.new(pin_number, @board)
  (piezos << Piezo.new(pin)).last
end

#piezosObject



87
88
89
# File 'lib/wall_e.rb', line 87

def piezos
  @piezos ||= []
end

#repeat(&block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wall_e.rb', line 116

def repeat(&block)
  t = Thread.new do
    loop do
      Thread.stop unless @running
      begin
        block.call
      rescue Exception => e
        puts e.message
        puts e.backtrace.inspect
        Thread.kill
      end
    end
  end

  @group.add(t)
end

#resumeObject



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

def resume
  @group.list.each(&:wakeup)
  @running = true
end

#Servo(pin_number, options = {}) ⇒ Object



57
58
59
60
# File 'lib/wall_e.rb', line 57

def Servo(pin_number, options = {})
  pin = Pin.new(pin_number, @board)
  (servos << Servo.new(pin, options)).last
end

#servosObject



83
84
85
# File 'lib/wall_e.rb', line 83

def servos
  @servos ||= []
end

#shut_downObject



112
113
114
# File 'lib/wall_e.rb', line 112

def shut_down
  @group.list.each(&:kill)
end