Created
October 16, 2010 19:32
-
-
Save ashmoran/630191 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArduinoInterpreter | |
| def initialize(setup_block, tick_block) | |
| @setup_block, @tick_block = setup_block, tick_block | |
| end | |
| def serial_begin | |
| puts "Initialising serial port..." | |
| end | |
| def serial_write(data) | |
| puts data | |
| end | |
| end | |
| class ArduinoProgram | |
| def setup(&setup_block) | |
| puts "Saving setup block..." | |
| @setup_block = setup_block | |
| end | |
| def tick(&tick_block) | |
| puts "Saving tick block..." | |
| @tick_block = tick_block | |
| end | |
| def run | |
| # @setup_block and @tick_block are ArduinoProgram vars passed into #setup and #tick | |
| ArduinoInterpreter.new(@setup_block, @tick_block).instance_eval do | |
| # instance_exec(&@ivar to find in an ArduinoInterpreter) | |
| instance_exec(&@setup_block) | |
| # This actually fails... @setup_block is found in the ArduinoInterpreter, but | |
| # the value of `self` reverts to the ArduinoProgram, so the block can't find the | |
| # method #serial_begin | |
| # | |
| # @setup_block.call | |
| loop do | |
| instance_exec(&@tick_block) | |
| sleep 1 | |
| end | |
| end | |
| end | |
| end | |
| def arduino_program(&block) | |
| program = ArduinoProgram.new | |
| program.instance_eval(&block) | |
| program | |
| end | |
| tick_tock = arduino_program do | |
| setup do | |
| serial_begin | |
| end | |
| tick do | |
| serial_write "tock" | |
| end | |
| end | |
| tick_tock.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArduinoProgram | |
| def setup(&setup_block) | |
| puts "Saving setup block..." | |
| @setup_block = setup_block | |
| end | |
| def tick(&tick_block) | |
| puts "Saving tick block..." | |
| @tick_block = tick_block | |
| end | |
| def run | |
| @setup_block.call | |
| loop do | |
| @tick_block.call | |
| sleep 1 | |
| end | |
| end | |
| private | |
| def serial_begin | |
| puts "Initialising serial port..." | |
| end | |
| def serial_write(data) | |
| puts data | |
| end | |
| end | |
| def arduino_program(&block) | |
| ArduinoProgram.new.tap do |program| | |
| program.instance_eval(&block) | |
| end | |
| end | |
| tick_tock = arduino_program do | |
| setup do | |
| serial_begin | |
| # Let's vandalise the application by calling a program DSL | |
| # method instead of an interpreter method... | |
| run | |
| end | |
| tick do | |
| serial_write "tock" | |
| end | |
| end | |
| tick_tock.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArduinoInterpreter | |
| def serial_begin | |
| puts "Initialising serial port..." | |
| end | |
| def serial_write(data) | |
| puts data | |
| end | |
| end | |
| class ArduinoProgram | |
| def setup(&setup_block) | |
| puts "Saving setup block..." | |
| @setup_block = setup_block | |
| end | |
| def tick(&tick_block) | |
| puts "Saving tick block..." | |
| @tick_block = tick_block | |
| end | |
| def run | |
| setup_block, tick_block = @setup_block, @tick_block | |
| interpreter = ArduinoInterpreter.new | |
| interpreter.instance_eval do | |
| setup_block.call | |
| loop do | |
| tick_block.call | |
| sleep 1 | |
| end | |
| end | |
| end | |
| end | |
| def arduino_program(&block) | |
| ArduinoProgram.new.tap do |program| | |
| program.instance_eval(&block) | |
| end | |
| end | |
| tick_tock = arduino_program do | |
| setup do | |
| serial_begin | |
| end | |
| tick do | |
| serial_write "tock" | |
| end | |
| end | |
| tick_tock.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment