Class: WallE::Assembler
- Inherits:
-
Object
- Object
- WallE::Assembler
- Defined in:
- lib/wall_e.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
Class Method Summary collapse
Instance Method Summary collapse
- #Button(pin_number) ⇒ Object
- #buttons ⇒ Object
- #Claw(claw_pin_number, pan_pin_number) ⇒ Object
- #claws ⇒ Object
- #delay(seconds) ⇒ Object
-
#initialize(arduino) ⇒ Assembler
constructor
A new instance of Assembler.
-
#Led(pin_number) ⇒ Object
TODO some metaprogramming sauce to reduce the component helper code.
-
#leds ⇒ Object
TODO wrap up these collections in some metaprogramming sauce too.
- #pause ⇒ Object
- #Piezo(pin_number) ⇒ Object
- #piezos ⇒ Object
- #repeat(&block) ⇒ Object
- #resume ⇒ Object
- #Servo(pin_number, options = {}) ⇒ Object
- #servos ⇒ Object
- #shut_down ⇒ Object
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. puts e.backtrace.inspect Thread.kill end end end end |
Instance Attribute Details
#board ⇒ Object (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 |
.create ⇒ Object
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) ( << Button.new(pin)).last end |
#buttons ⇒ Object
95 96 97 |
# File 'lib/wall_e.rb', line 95 def @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 |
#claws ⇒ Object
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 |
#leds ⇒ Object
TODO wrap up these collections in some metaprogramming sauce too.
79 80 81 |
# File 'lib/wall_e.rb', line 79 def leds @leds ||= [] end |
#pause ⇒ Object
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 |
#piezos ⇒ Object
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. puts e.backtrace.inspect Thread.kill end end end @group.add(t) end |
#resume ⇒ Object
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, = {}) pin = Pin.new(pin_number, @board) (servos << Servo.new(pin, )).last end |
#servos ⇒ Object
83 84 85 |
# File 'lib/wall_e.rb', line 83 def servos @servos ||= [] end |
#shut_down ⇒ Object
112 113 114 |
# File 'lib/wall_e.rb', line 112 def shut_down @group.list.each(&:kill) end |