legOS's development cycle is a little clumsy. You write a program, compile it with the legOS source code, then download the whole thing to the RCX. It's the downloading that takes a long time.
Here are some tips to make things go smoother:
1. Always include code that terminates your own program. If your program can stop itself, control returns to legOS. When legOS has control, you can turn the RCX on and off and even reinitialize
the firmware, as described next.
Page 212
2. When legOS has control of the RCX, you can press and hold the Prgm button, then press the On-Off button. This blows away legOS (and your program) and returns control of the RCX to the
ROM. You'll need to do this before you can download a new set of firmware to the RCX.
3. If your program doesn't stop itself and give control back to legOS, you'll need to erase the firmware by removing a battery. If your program has a bug and does not terminate, you'll need to
remove a battery to reset the RCX.
4. Sometimes, through a bug in legOS or in your program, the RCX cannot be initialized by removing the batteries for just a few seconds. You will need to remove the batteries from your RCX
and wait for a minute or so before the firmware is erased. Some circuitry keeps the RCX's memory alive; in some cases, you need to wait for the circuitry to drain completely before the firmware
will be erased.
If the endless code-compile-download-reset cycle is getting you down, you might consider using an emulator. An emulator is a special program that runs on your development PC but acts like an
RCX. You can test your programs on the emulator much faster than you can test them on an actual RCX. Currently one legOS emulator exists; see the "Online Resources" section for details.
Debugging
The display is your best friend when it comes to debugging. legOS offers an impressive array of display functions. You can show words that indicate which part of your program is executing or
display the contents of variables. Of course, there's not a lot of space to work with, but you could easily display a series of values for a short time. You could even write debugging code that lets
you cycle through data by pressing a button.
Unexpectedly Static Variables
One of the craziest things about legOS development is that global variables retain their value from one time you run your program to the next. This is very important—it means that variables you
initialize at declaration time are initialized only once, when your program is first loaded on the RCX. Use the following program to convince yourself:
#include "conio.h"
int x = 44;
int main(void) {
lcd_int(x);
lcd_refresh();
Page 213
x++;
delay(1000);
return 0;
}
The value shown on the display will be 44 the first time the program is run, but it goes up by one each subsequent time, even when you turn the RCX off and on again. This interesting property
was the source of several bugs in the original LightSeeker.c program.
If you really want to initialize a variable each time your program is run, you should do it explicitly in the code somewhere, like this: