- Code: Select all
> rvm install jruby
> rvm use jruby
> gem install mirah
# https://github.com/ishikawa/sublime-text-2-mirah
First, a basic example program:
- Code: Select all
import lejos.nxt.Button
class Test
def self.main(args:String[]):void
System.out.println "Hello World from Mirah!"
Button.waitForAnyPress()
end
end
Can be compiled to a linkable class file, or a java file. We'll do java first to see what gets made (note: as of mirah 0.1.0, this is no longer available github mirah/mirah/pull/211):
- Code: Select all
mirahc --java --classpath /Users/peter/Downloads/leJOS_NXJ_0.9.1beta-3/lib/nxt/classes.jar test.mirah
- Code: Select all
// Generated from test.mirah
public class Test extends java.lang.Object {
public static void main(java.lang.String[] args) {
java.io.PrintStream temp$1 = java.lang.System.out;
temp$1.println("Hello World from Mirah!");
lejos.nxt.Button.waitForAnyPress();
}
}
A couple details are important for this to work:
- `self.` makes a static method
- args:String[] needs to be just so
- :void is important to make it recognized as a `#main` method in this case.
This works fine on the brick!
- Code: Select all
mirahc --classpath /Users/peter/Downloads/leJOS_NXJ_0.9.1beta-3/lib/nxt/classes.jar test.mirah
nxj -r Test
(a quick side note here is that I wisth `nxj Test.class` was smart enough to figure things out and drop the .class)
A few other pointers from my notes:
- Code: Select all
//Casting: use int:
degrees = gyroBoss.getDegrees
Motor.A.rotateTo int degrees
Here's my full program
- Code: Select all
class MyButtonListener
implements ButtonListener
def buttonPressed(b: Button):void
LCD.drawString("ESC pressed", 0, 6)
end
def buttonReleased(b: Button):void
LCD.drawString("ESC released", 0, 6)
end
end
class Test
def self.main(args:String[]):void
listener = MyButtonListener.new
Button.ESCAPE.addButtonListener(listener)
gyro = GyroSensor.new(SensorPort.S1)
gyroBoss = GyroDirectionFinder.new(gyro)
LCD.drawString 'calibrating gryo', 0, 3
gyro.recalibrateOffset
LCD.drawString 'done! ', 0, 3
time1 = System.currentTimeMillis()
while true
degrees = gyroBoss.getDegrees
LCD.drawString "angle: #{String.valueOf degrees}° ", 0, 3
Motor.A.rotateTo int degrees
time2 = System.currentTimeMillis()
LCD.drawString "refresh: #{String.valueOf (time2 - time1)}ms ", 0, 7
time1 = time2
end
end
end
Unfortunately it doesn't work perfectly -- compiling gives me an error, not sure what's going on.
- Code: Select all
Parsing...
test.mirah
Inferring types...
test.mirah:32: can't convert Mirah::JVM::Types::TypeDefinition into String
Button.ESCAPE.addButtonListener(listener)
Oh well. I got distracted for the rest of the day by and RC helicopter, and now the week begins again. Until next time!
Mirah:
http://studl.es/2012/05/mirah-on-nxt/
https://github.com/mirah/mirah/wiki/
Edit: See mirah groups thread, here (might be a bug in their stuff): https://groups.google.com/forum/?fromgr ... sI5N0kGq1M
PS anyone have experience building a basic bluetooth remote for iOS?
