Learning About Arduino IDE and Jython
Introduction
In relation to some ideas around regression testing I began looking at adapting the preprocessing/compilation portion of the Arduino IDE so that it could be run from the command line.
Under the assumption it would be easier (and more fun) to be using Python rather than Java but still wanting to leverage the existing Java code I began to explore using Jython.
Project Log
( 20 March 2009 )
- The pre-processor class: PdePreprocessor.java
- You will need to install Jython. I tried the standalone install first but had problems with it not finding the classes I wanted it to find. So, for the moment you will need to do a normal install.
- You will need to set up the class path to point to the Arduino .jar files, something like this (on OS X):
export CLASSPATH=/<path>/arduino-0013/Arduino\ 13.app/Contents/Resources/Java/Arduino.jar:/<path>/arduino-0013/Arduino\ 13.app/Contents/Resources/Java/oro.jar:/<path>/arduino-0013/Arduino\ 13.app/Contents/Resources/Java/antlr.jar
- Then have a script like this:
from processing.app.preproc import PdePreprocessor from processing.app import Target theTarget = Target("/<path>/arduino-0013/hardware/cores/", "arduino") thePreprocessor = PdePreprocessor() theProgram = """ void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(100); digitalWrite(13, LOW); delay(100); } """ thePreprocessor.write(theProgram, "/tmp/", "foo", [], theTarget)
- You should have a file /tmp/foo.cpp generated when you execute it (within the jython2.2.1 directory), something like:
./jython arduino_prep.py
- @@ TODO: Check this example actually works as documented.
- Unfortunately, when I moved on to trying to get the example compiled as well, I discovered the IDE is highly coupled—even between UI and non-UI-specific classes. The code also has a lot of code (sometimes commented out, sometimes not) that is only required for its original use in the Processing IDE. Many of the comments have not been changed from the Java-specific comments of the Processing IDE to the C++ ones of the Arduino IDE.
- The biggest issue is that you need to have Sketch instance and to create one you need to supply it an Editor instance—which pulls in GUI related code. The Editor class accesses Base and Preferences classes as well, which then brings in dependencies on preferences files, file paths and operating systems and it all blows up in a mess.
- The sketch shouldn't require an Editor instance in order to compile it. I tried supplying a Null but it barfs. I then tried sub-classing the Editor class in order to dummy out the routines causing problems, but there seems to be no way to stop the original/super constructor from being called:
If a Java superclass is not explicitly initialized, its empty constructor is called at the completion of a Jython subclass's __init__ method.
- So, it seems without extensive re-working getting the compilation stage to work independently of the IDE won't be a goer. IMO the two should be totally separated but I suspect it would be an uphill battle trying to change the IDE code if there's a desire to stay inline with the Processing IDE.
- At least with the preprocessor accessible it should be possible to modify the Makefile to work on a standard PDE file I think—although libraries might become an issue.
- Some links around making stand alone Jython code:
- If it turned out it was too difficult to have a standalone Jython script, the Java implementation would seem relatively straight forward.