NBC

/ 7 October 2006

Most of you may be wondering what in the world NBC is and what it has to do with Legos. Here’s a quite helpful description of what NBC is that may tell you how it ties to Legos: it’s the NXT’s assembly language, the layer just above the final compiling step. Earlier this week I found the compiler for this language (thanks to the Open-Source NXT) and already started programming in it. The compiler is available for immediate download at the site of it’s creator. I use TextWrangler to write the NBC code, though you can use any text-editing program (I’d rather TextWrangler because it is a text editor meant for computer programming). To get a hint at what NBC can do, you should download this RXE that another NBC NXT programmer wrote, and download the accompanying NBC editable file (you’ll likely need Stuffit to un-archive the file). Now, you’ll learn quite fast that the compiler requires you to execute a line or Unix commands to compile your NBC files, this is possible, and here are a few basic hints on how to do that.

In Terminal: cd {drag the folder where the NBC compiler is located} ../nbc -O={your prgm name}.rxe {your NVC file name}.nbc

That should compile the NBC file you just wrote into a RXE for your NXT, from there you can use the NXT-G software (the software that comes with the NXT) to download the RXE to your NXT via either USB or Bluetooth. If you’re using TextWrangler for the NBC writing, you can also make the compiling simpler by writing an Applescript with some of the following components in it.

			on posix_path(mac_path)
set mac_path to (mac_path as text)
set root to (offset of ":" in mac_path)
set rootdisk to (characters 1 thru (root - 1) of mac_path)
tell application "Finder"
	if (disk (rootdisk as string) is the startup disk) then
		set unixpath to "/" & (characters (root + 1) thru end of mac_path)
	else
		set unixpath to "/Volumes:" & mac_path
	end if
end tell
set chars to every character of unixpath
repeat with i from 2 to length of chars
	if item i of chars as text is equal to "/" then
		set item i of chars to ":"
	else if item i of chars as text is equal to ":" then
		set item i of chars to "/"
	else if item i of chars as text is equal to "'" then
		set item i of chars to "\'"
	else if item i of chars as text is equal to """ then
		set item i of chars to "\" & """
	else if item i of chars as text is equal to "*" then
		set item i of chars to "\*"
	else if item i of chars as text is equal to "?" then
		set item i of chars to "\?"
	else if item i of chars as text is equal to " " then
		set item i of chars to "\ "
	else if item i of chars as text is equal to "\" then
		set item i of chars to "\\"
	end if
end repeat
return every item of chars as string end posix_path

That long set of commands will set your computer up for Applescript to run Unix commands within itself. From here on out this set of code will be mentioned as “Unix Set”.

	on run
	set mynbc to "{where the compiler is, [path to]}"

From referred to as “Compiler Path”

” tell application “TextWrangler” set myfile to file of front window as text end tell

Tells TextWrangler to set the “myfile” to the frontmost file in TextWrangler, now referred to as “File Set”.

set mypath to posix_path(myfile)
set mycommand to mynbc & " -O=" & mypath & ".rxe " & mypath
set myresult to do shell script mycommand
set myresult to myresult & return & "Done!"
display dialog myresult
end run

The actual compiling, telling your mac to compile the NBC file to a RXE and to place that RXE in the same folder as the original NBC file, now referred to as “Compiling”.

You’d write your script like this: at the top, Compiler Path, below that would be, File Set, after that would be, Compiling, and a line of blank after that would be Unix Set.

That script would then be saved to /Users/{Your User}/Library/Application Support/TextWrangler/Scripts. And there you have it, a menu item that will automatically compile your NBC file to a RXE for your NXT.

Discussion