1001ICT Introduction To Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 1 5 Hello, NXT! In this section we learn how to compile Making Stuff Happen (MaSH) (and Java) programs for the NXT, and how to upload them. 1001ICT Introduction To Programming – 2015-2 2 5.1 Procedures come from environments In our first MaSH program: import console; println("Hello, World!"); • We import the console environment, because the program will run on a PC and we want to print stuff on the console screen. • We use the println(String) procedure, because that is one of the procedures in the console environment that will output a string to a personal computer (PC)’s console. • Procedures are not built into or predefined in the language. • In a different environment, there will be different procedures. 1001ICT Introduction To Programming – 2015-2 3 5.2 A Hello program for the NXT Let’s write a program that puts a hello message on the screen of an NXT. Some considerations: • We must import the right environment: nxt. • A procedure in the nxt environment that puts text on the screen is drawString(String, int, int). Where int and int indicate we have to supply numbers, not just the string. The numbers are the column and row on the screen at which to start drawing the string. 1001ICT Introduction To Programming – 2015-2 4 • When the program ends, the screen reverts to the Lejos menus, so we need to get the program to pause until we have read the text. We can make the program wait until a touch sensor is pressed, before it ends. There’s a procedure for that: waitForPush(int). The number is the input port number that a touch sensor is plugged into. 1001ICT Introduction To Programming – 2015-2 5 • If a program uses a sensor, that sensor has to be set up. There’s a procedure for that: setUpSensor(int, int). The first number is the input port number. The second number is a number used to select the kind of sensor that is plugged into that port. To avoid having programs littered with numbers with special mean- ings that you would not remember and would need to look up, the nxt environment defines some symbolic constants: TOUCH, SOUND, ... • If we choose input port 4, this program will work on a Tricorder. 1001ICT Introduction To Programming – 2015-2 6 Putting it all together: import nxt; setUpSensor(4, TOUCH); drawString("Hello, NXT!", 0, 4); waitForPush(4); This is the program you compile and run in Lab 2. 1001ICT Introduction To Programming – 2015-2 7 5.3 Compiling and running a MaSH NXT program 5.3.1 Step 1: Create the program file Use your favourite text editor to create a text containing the text of the program. The file must have a name that ends with the extension .mash. The main part of file name must be a Java identifier, starting with an upper case letter. 5.3.2 Step 2: Compile with mashc Compile a MaSH program with a command like: $ mashc Program.mash 1001ICT Introduction To Programming – 2015-2 8 5.3.3 Step 3: Compile with nxjc Different! Compile the Java program with a command like: $ nxjc Program.java nxjc.bat is a batch file that makes javac use the Lejos NXJ libraries. 5.3.4 Step 4: Link with nxjlink Linking means bundling up all the compiled code into one file. Do this with a command like: $ nxjlink Program -o Program.nxj 1001ICT Introduction To Programming – 2015-2 9 5.3.5 Step 5: Upload to the NXT with nxjupload Do this with a command like: $ nxjupload Program 5.3.6 Step 4 and 5 shortcut The nxj command can be used to link and upload in one step. $ nxj Program 1001ICT Introduction To Programming – 2015-2 10 5.4 Convenience batch files for Windows This is getting to be a lot of steps! We have installed batch files in the lab PCs. 5.4.1 For console programs As mentioned before, command: > mash Program will compile a program with mashc then javac and then run it with java. (Don’t type any filename extension!) 1001ICT Introduction To Programming – 2015-2 11 5.4.2 For NXT programs The command: > mashn Program will compile a program with mashc and nxjc, then link and upload it with nxj. 5.4.3 For RCX programs The command: > mashl Program performs the equivalent for Robotic Command eXplorer (RCX) programs. 1001ICT Introduction To Programming – 2015-2 12 5.5 Convenience shell script for Macs and Linux For Unix (that is Mac OS X) and Linux, the previously mentioned bash shell script works for all MaSH environments. ] mc Program mc will compile a program with mashc then javac or nxjc and then run it with java or link and upload it with nxj. (Don’t type any extension other than .mash.) 1001ICT Introduction To Programming – 2015-2 13 5.6 Section summary This section covered: • an example program that uses the nxt environment; • how to compile, link, and upload an nxt program; and • the batch files for convenient compiling. 1001ICT Introduction To Programming – 2015-2 14 5.7 End of section feedback questions Send us your answers to these questions any time you like by clicking on them. • What was the most useful topic in this section? • What was the least useful topic in this section? • What was the least clear topic in this section? • What topic in this section would you like to know more about? • Did you find an error in this section? 1001ICT Introduction To Programming – 2015-2 15