Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created September 16, 2014 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/0eb28046a4ea8c35cb5b to your computer and use it in GitHub Desktop.
Save cowboyd/0eb28046a4ea8c35cb5b to your computer and use it in GitHub Desktop.
hypothetical ruby DSL for brick pi robot operations
# config phase
OperationInstance = Struct.new(:start, :condition, :finish)
class OperationBuilder
def start(&block)
@start = block || proc {}
end
def condition(&block)
@condition = block || proc {}
end
def finish(&block)
@finish = block || proc {}
end
def operation
OperationInstance.new(@start, @condition, @finish)
end
end
class Bot
def operation
builder = OperationBuilder.new
yield builder
operation = builder.operation
schedule operation.start
schedule do
if operation.condition.call
schedule operation.condition
else
schedule operation.finish
end
end
end
def schedule(&block)
end
end
bot.run do |events|
speed = 60
events.on('keypress', 'w') do
bot.move_forward
end
def bot.move_forward(fractional_seconds = nil)
bot.operation do |op|
start_time = nil
op.start do
start_time = Time.now
bot.motor_1.spin 60
bot.motor_2.spin 60
end
op.condition do
if fractional_seconds
((Time.now - start_time) * 1000) < fractional_seconds
else
true
end
end
op.finish do
bot.motor_1.spin 0
bot.motor_1.spin 0
end
end
end
loop do
char = get_char
bot.trigger 'keypress', key: char
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment