Module: Rubotz
- Defined in:
- lib/rubotz.rb,
lib/rubotz/generator.rb,
lib/rubotz/motor_generator.rb,
lib/rubotz/ultrasonic_sensor.rb
Defined Under Namespace
Classes: Generator, MotorGenerator, UltrasonicSensor
Constant Summary collapse
- SIMPLE_WHILE =
"#{File.dirname(__FILE__)}/../templates/simple_while.nxc.erb"
- CONFIG =
YAML.load_file("#{File.dirname(__FILE__)}/../config/nxt_home.yaml")
Class Method Summary collapse
-
.compile(options = {}) ⇒ Object
Rubotz.compile takes two keyword arguments - a filename and a Rubotz.program call, which takes a block.
-
.generate(&block) ⇒ Object
Rubotz.generate is an internal method.
-
.program(&block) ⇒ Object
Rubotz.program takes a block of Ruby code and evals it in the context of a Generator, which knows how to turn that code into NXC code.
Class Method Details
.compile(options = {}) ⇒ Object
Rubotz.compile takes two keyword arguments - a filename and a Rubotz.program call, which takes a block. Here’s the simplest possible instance:
Rubotz.compile(:filename => "f", :program => (Rubotz.program {true}))
Technically even simpler instances are possible, but would probably do bad things.
One bad thing to watch out for is that there is a hard limit on the number of characters you can use in a filename, and I’m not yet sure what that is. However, beyond a certain number of characters, Mindstorms won’t recognize the .rxe suffix on your generated executable, and when that happens, it assumes the file is data rather than a program. The workaround is obvious: use short file names.
One other caveat: compile saves your generated code, and will overwrite existing generated code if it was generated with the same :filename. Keep an eye on your nxt/code dir, or use unique filenames, or be bold and fearless. Whatever. It’s up to you.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rubotz.rb', line 29 def compile( = {}) filename = [:file].gsub(/ /, "_") File.open("#{Rubotz::CONFIG['nxt_home']}/code/#{filename}.nxc", "w") do |file| file.puts [:program] end system ("cd #{Rubotz::CONFIG['nxt_home']}; " + "./nbc " + "-O=bin/#{filename}.rxe " + "code/#{filename}.nxc") end |
.generate(&block) ⇒ Object
Rubotz.generate is an internal method. It’s not marked private because I don’t really care, I kind of have a laissez-faire, Perlish attitude to methods, if you shoot yourself in the foot, hey, it’s your foot. However, it’s really only useful if you’re writing new generator code.
43 44 45 |
# File 'lib/rubotz.rb', line 43 def generate(&block) Generator.new(&block).code end |
.program(&block) ⇒ Object
Rubotz.program takes a block of Ruby code and evals it in the context of a Generator, which knows how to turn that code into NXC code. It then runs the generated NXC code through ERb, to mesh with existing boilerplate stuff in NXC. Note however that it does not save a file, it simply returns the generated code in the ERb template as text.
50 51 52 53 54 55 56 |
# File 'lib/rubotz.rb', line 50 def program(&block) generator = Generator.new(&block) initialization_section = generator.initialization_section loop_body = generator.loop_body comment = generator.comment || "" ERB.new(File.read(SIMPLE_WHILE)).result(binding) end |