Introduction to Bricxcc and NXC Programming
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIX Introduction to BricxCC and NXC Programming This appendix is a tutorial about Bricx Command Center (BricxCC), the IDE (Integrated Development Environment) that is used to write, compile, and test the NXC programs in this book. This IDE is handy, because it includes many utilities for the LEGO programmable bricks and for the NXT in particular: a file browser, a sound converter to translate WAV files into RSO files, a panel to monitor the brick’s sensors and outputs, a screen-capture utility, and so on. The BricxCC program was originally written by Mark Overmars, and is now magnificently maintained and improved by John Hansen, who is also the creator of the NXC language. BricxCC was born as the IDE to write Not Quite C (NQC) programs for the RCX, so it inherits many fea- tures of the NQC language, created by Dave Baum. Through the years, many new functions have been built inside BricxCC, to let it manage the new NXC language and the NXT brick itself. I worked together with John Hansen to produce an NXC language tutorial that is now available on the Web. My tutorial, together with the well-done NXC programming guide by John Hansen, makes a comprehensive manual to learn this textual programming language for the NXT. I learned NXC using that guide, and I have also made contributions to fix some of the Bluetooth API functions. Many people worldwide have already started writing NXC programs successfully by reading my tutorial. I suggest that you read it too, to learn the basics of NXC. Given that this tutorial is not made to replace the BricxCC guide (to which you must refer for the doubts that could arise), let’s get started! You should download and install the latest release of BricxCC that now includes a built-in version of the NeXT Byte Codes (NBC) compiler. You can find the following information and software online: • All the software needed to get started at http://bricxcc.sourceforge.net/ • The NXC manual at http://bricxcc.sourceforge.net/nbc/nxcdoc/NXC_Guide.pdf • My tutorial at http://bricxcc.sourceforge.net/nbc/nxcdoc/NXC_tutorial.pdf First, I’ll show you how to write, compile, and download your first NXC program to the NXT, using BricxCC. Next, I’ll briefly analyze the BricxCC menu functions, followed by tracking the advanced NXT-dedicated utilities. 567 568 APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING Getting Connected with BricxCC Here, I assume you’ve already used the LEGO NXT-G programming software, so you know how to rename your NXT and how to make and download simple NXT-G programs. Also, I assume you have the NXT turned on and connected to the PC via USB (later I’ll explain how to use Bluetooth). Open BricxCC by double-clicking its desktop icon or by clicking the icon installed in the Start menu. At startup, the Find Brick form shows up (see Figure A-1). This dialog asks which kind of brick you’re going to use (notice that BricxCC can manage all kinds of programmable bricks produced by LEGO through the years), and how the brick should be connected. Figure A-1. The Find Brick form that shows up when you open BricxCC To use the NXT, you must first choose NXT from the drop-down list at the right, and Auto- matic from the left drop-down list. You don’t have to worry about the Firmware radio buttons, because these options are meant for the RCX. Click OK and the IDE should find the brick; if everything went fine, no error message appears and the toolbar icons are lit up with their nor- mal colors (otherwise they would look grayed—compare Figures A-2 and A-3). Figure A-2. The BricxCC IDE with buttons enabled, when the NXT is connected APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING 569 Figure A-3. The BricxCC IDE with buttons disabled, meaning no brick is connected Connecting the NXT via Bluetooth is a bit more complicated. Follow these steps to get connected without encountering any problems: 1. Be sure to have Bluetooth enabled on your laptop or a Bluetooth dongle connected and correctly installed on your PC. Also, make sure you have your NXT brick turned on, with Bluetooth on and visible. When choosing the settings in the Find Brick form (see Figure A-1 again), select Automatic as before, and check the Bluetooth check box. Then click OK. BricxCC starts searching for Bluetooth-enabled NXTs, but it will eventually hang. Don’t lose heart! Go on with the steps that, once done, let BricxCC find your NXT fast. ■Caution This step of the procedure might cause BricxCC to hang up. So, you need to force it to close by pressing Ctrl+Alt+Del, which terminates the process from the Task Manager. 2. You must introduce every NXT you’re going to use to your PC by using the Bluetooth Devices dialog (a feature present in Windows XP) once you have a Bluetooth dongle installed. To make them meet each other, open the Bluetooth Devices panel from the Control Panel or by double-clicking the icon you should have among the tray icons in the Windows application bar at the bottom of the screen, near the clock. In Figure A-4, you can see how my Bluetooth Devices dialog looks. You might have some items in yours, or none. 570 APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING Figure A-4. The Bluetooth Devices dialog in Windows XP 3. Click the Add button in the lower left of the panel and follow the instructions to make your NXT meet the PC and exchange greetings (see Figures A-5 and A-6). In those screens, click Next to proceed. Figure A-5. The first step of the Add Bluetooth Device Wizard APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING 571 Figure A-6. The Wizard found a new NXT device (named NXT). 4. You’re then asked for a passkey. Just type in 1234—the default for any NXT brick (see Figure A-7)—and click Next. Finally, click Finish in the last screen. At this point, your NXT (with the name you gave it) should be listed in the Bluetooth Devices (Devices tab). Figure A-7. In this step of the Add Bluetooth Device Wizard, you must type in the passkey. 572 APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING 5. Next, select the NXT name just added to the list (NXT in Figure A-8), and click the Prop- erties button in the lower right side of the panel. A new panel (like the one in Figure A-9) shows up. In the second line, you can read the Bluetooth address of the NXT. Figure A-8. The Bluetooth Devices dialog shows the newly connected NXT. Figure A-9. The NXT Properties, where you can copy its address APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING 573 6. Now, select and copy the address with Ctrl+C. Then, close this dialog by clicking OK. In the Bluetooth Devices dialog, select the COM Ports tab and make note of the COM port number of your NXT that’s marked as incoming (COM4 in Figure A-10). Figure A-10. The NXT device’s allotted COM ports 7. Now, open the file browser by pressing Windows+E, and browse to the BricxCC install folder (for example, C:\Program Files\BricxCC\). Here, you should find a file named nxt.dat; if not, create it. Open it with Notepad or any other text editor you prefer. Add the following line to this file: alias = BTH::nxt_name::bt_address::com_port You have to fill in the preceding fields like so: • alias: The name you desire for your NXT to be shown in the BricxCC Find the Brick drop-down list • nxt_name: The NXT brick name as shown in the Bluetooth Devices Peripherals tab • bt_address: The hexadecimal address you copied before from the Properties panel • com_port: The input COM port you noted before 8. Be careful to put the double colon :: in the right places. For example, my entry looks like this: NXT_BT = BTH::NXT::00:16:53:04:40:dc::4 574 APPENDIX ■ INTRODUCTION TO BRICXCC AND NXC PROGRAMMING 9. Save and close this file; also close the file browser and the Bluetooth Devices dialog. If you have BricxCC open, close it and reopen it. Now, you should find the newly connected NXT in the left drop-down list in the Find Brick form. Click OK and BricxCC will get connected to your NXT in just a few seconds. It was laborious, but your efforts will be repaid! This time it takes just a moment to con- nect your NXT via Bluetooth. You can repeat the whole procedure to add all the NXT devices you want. At this point, you are online with your brick. Let me show you what can be done now. Ready to Go The IDE’s main form should be empty and the toolbar buttons filled with their colors (mean- ing you are connected to the NXT). You can usually do every operation in three equivalent ways: using a keyboard shortcut, a menu item, or a toolbar button. You can create a new file in many ways: you can press Ctrl+N, you can select File ➤ New from the menu, or you can just click the “New file” button on the toolbar. An untitled file is then created; you can now double-click the file’s title bar to expand it. Now type in the following trivial NXC program: task main () { TextOut (10,LCD_LINE4,"Hello, World!"); Wait (SEC_2); } You can save the file by pressing Ctrl+S, or by selecting File ➤ Save, or by clicking the “Save file” icon on the toolbar (the third icon from the left—the one showing the floppy disk).