LeJos with Mirah

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

LeJos with Mirah

Postby pEhrlich » Mon Oct 15, 2012 6:28 am

Mirah is a project which also ruby-like syntax to be used in writing Java code. Today I spent some time setting it up, here's what I did. Hopefully you can see the beauty of having this powerful syntax an a lego environment.

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?
User avatar
pEhrlich
Advanced Member
 
Posts: 157
Joined: Fri Jan 04, 2008 1:38 am

Re: LeJos with Mirah

Postby skoehler » Mon Oct 15, 2012 8:23 am

pEhrlich wrote:Can be compiled to a linkable class file, or a java file. We'll do java first to see what gets made:
Code: Select all
   mirahc --classpath  /Users/peter/Downloads/leJOS_NXJ_0.9.1beta-3/lib/nxt/classes.jar  test.mirah


In the JRuby thread, you posted a link to a blog entry (or something like it), where a guy was using mirah with leJOS. That guy was using --bootclasspath instead of --classpath, which is the correct way of doing things. In other words: what you posted is wrong. It should be
Code: Select all
   mirahc --bootclasspath  /Users/peter/Downloads/leJOS_NXJ_0.9.1beta-3/lib/nxt/classes.jar  test.mirah


About the "can't convert Mirah::JVM::Types::TypeDefinition into String" you should probably ask the mirah developers. It doesn't sound like it's related to leJOS.
skoehler
leJOS Team Member
 
Posts: 1114
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff