Learn How to Write Applications for the NanoComputer in One Really Long Day - Chapter 4

From NanoComputerWiki

Jump to: navigation, search

Contents

Chapter 4 -- Writing Your First Application (Part 1)

Introduction

We are now ready to write our first application for the NanoComputer which we will call MyHelloWorld.
Writing our first application has been divided into two parts. The first part will explain how to create a new application, how to compile the application, how to install the application, and how to run the application. The second part will explain how to make the application do something.
Don't be discouraged if part one seems a bit long and tedious with very little viewable results. Everything after part one moves very quickly with viewable results.
Our first application will be a variant of the traditional "Hello World!" program. The "Hello World!" program will clear the LCD screen of the NanoComputer and print the words "Hello World!" on the screen.


Start the Windows XP Command Prompt

Most of the steps in the rest of this chapter and in the following chapter will be done inside the Windows XP Command Prompt.
  • Please start the Windows XP Command Prompt now.
If you have trouble starting the Windows XP Command Prompt, please refer back to Chapter 3 - Starting the Windows XP Command Prompt.
Image:MyHelloWorld_CommandPrompt.png


Create a Directory for the MyHelloWorld Application

We will now create a directory named C:\MyHelloWorld to hold the files which will make up the project for our MyHelloWorld Application.
  • To create the directory issue the following commands inside the Windows XP Command Prompt.

C:
cd\
mkdir \MyHelloWorld

After entering the commands into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The commands that have just been issued inside the Windows XP Command Prompt are shown highlighted with a yellow background.
Image:MyHelloWorld_CreateDirectory.png
We can verify that the directory was created correctly by issuing the DIR command inside the Windows XP Command Prompt. The DIR command will show a listing of files and directories in the current directory. We can then verify that the newly created directory is shown in the list.
  • To show a list of files and directories in the current directory issue the following command inside the Windows XP Command Prompt.

dir

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The newly created directory MyHelloWorld is also shown highlighted with a yellow background in the list of files and directories returned by the DIR command.
Image:MyHelloWorld_CreateDirectory_Dir.png


Change the Current Directory to C:\MyHelloWorld

We will now change the current directory used by the Windows XP Command Prompt to C:\MyHelloWorld.
  • To change the current directory to C:\MyHelloWorld issue the following commands inside the Windows XP Command Prompt.

cd \MyHelloWorld

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The current directory of the Windows XP Command Prompt is shown to the left of the > sign on the last line of text inside the window. The correct current directory of C:\MyHelloWorld is shown highlighted with a yellow background in the screenshot below.
Image:MyHelloWorld_ChangeDirectory.png


Creating the Project for MyHelloWorld

We will now create a project for our MyHelloWorld application. A project is a collection files used to generate the MyHelloWorld application. An analogy is that a loaf of bread is a collection of slices of bread. The function of each file in the project will be explained later, but for now we will just explain how to create the files in the project.
  • To create the files needed for the MyHelloWorld application issue the following command inside the Windows XP Command Prompt.

NC_CreateProject MyHelloWorld

After entering the commands into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background.
Image:MyHelloWorld_CreateProject.png
The NC_CreateProject command will create two new files in the current directory as shown by the status messages returned by the NC_CreateProject command in the screenshot above. The purposes of these two files will be explained later.
We can verify that the project was created correctly by issuing the DIR command and viewing the file and directory listing. If you have trouble issuing the DIR command, please consult #Create a Directory for the MyHelloWorld Application.
After issuing the DIR command inside the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The DIR command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The newly created files MyHelloWorld.c and makefile are also shown highlighted with a yellow background in the list of files and directories returned by the DIR command.
Image:MyHelloWorld_CreateProject_Dir.png


Viewing the Source Code File

The MyHelloWorld.c source code file contains instructions for the NanoComputer to follow when executing the MyHelloWorld application. We will be modifying this file in later steps to make the MyHelloWorld application do interesting things, but for now, we will just be viewing this file to become familiarized with it's contents.
The MyHelloWorld.c can be viewed and edited in any text editor, but for this exercise we will be using the Notepad text editor which comes with Windows XP.
  • To view the MyHelloWorld.c source code file in Notepad issue the following command in the Windows XP Command Prompt.

notepad MyHelloWorld.c

After entering the commands into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The commands that have just been issued inside the Windows XP Command Prompt are shown highlighted with a yellow background.
Image:MyHelloWorld_Notepad.png
The source code file should now be displayed inside the Notepad application in a new window and should look similar to the screenshot below.
Image:MyHelloWorld_Notepad_SourceCode.png
While the source code file may look complicated, the only portion that is of interest to us is the contents of the void main() function between the open curly brace '{' and the close curly brace '}' . The contents of the void main() function in a newly generated source code file are two lines of text as shown below:
// Clear the LCD Screen using BLUE color

        LCD_ClearDisplay(BLUE);
Lines in the source code file which begin with a double slash // are comments and do not actually instruct the NanoComputer to do anything, they are there solely to provide information to human beings editing the source code file.
Let's examine the two lines that are currently in the void main() function. The first line is a comment because it starts with a double slash //. The comment is there to tell a human being editing the source code file the purpose of the line of code following the comment. In this case, the comment states that the next line will clear the LCD screen with a blue color. The second line inside the void main() function instructs the NanoComputer to clear the LCD display with the blue color. We will explain in more detail how the LCD_ClearDisplay() function works later.
  • After you have finished viewing the source code, close the Notepad application by clicking on the big red X in the upper right hand corner of the Notepad application.
Image:MyHelloWorld Notepad Close.png


Compiling the MyHelloWorld Application

The NanoComputer cannnot directly understand the information inside the source code file, the information in the source code file must be translated into a format that the NanoComputer can understand. This translation process is called compiling. An analogy would be that the source code file is a piece of paper with instructions on it written in English, yet the NanoComputer only understands Japanese. The piece of paper will be fed into the compiler and the compiler will print out a piece of paper with the instructions translated into Japanese. The piece of paper with the Japanese instructions on it will then be given to the NanoComputer to execute. It's actually a bit more complicated then that, but the analogy is good enough for us to understand the process to write a NanoComputer application.
  • To compile the MyHelloWorld.c source code file into the MyHelloWorld.ihx file which the NanoComputer can understand, issue the following command inside the Windows XP Command Prompt

make

If the source code file is compiled successfully, make will not display anything on the screen, but make will create a new directory named output containing new generated files.. While this seems strange that make would not return any kind of indication that it succeeded, this is the historical standard for certain type of application derived from UNIX of which the make application is one of.
After entering the commands into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The commands that have just been issued inside the Windows XP Command Prompt are shown highlighted with a yellow background.
Image:MyHelloWorld_Make.png
We can verify that the make command generated the correct output by issuing the DIR command and viewing the file and directory listing. If you have trouble issuing the DIR command, please consult #Create a Directory for the MyHelloWorld Application.
After issuing the DIR command inside the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The DIR command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The newly created output directory is also shown highlighted with a yellow background in the list of files and directories returned by the DIR command.
Image:MyHelloWorld_Make_Dir.png
We can then further verify that the files in the newly generated output file are correct by issuing the DIR output command inside the Windows XP Command Prompt. The DIR output command will show a listing of files and directories in the newly created output directory.
  • To show a list of files and directories in the newly generated output directory issue the following command inside the Windows XP Command Prompt.

dir output

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The newly created MyHelloWorld.ihx file is also shown highlighted with a yellow background in the list of files and directories returned by the DIR output command.
Image:MyHelloWorld_Make_Dir_Output.png


Installing the MyHelloWorld Application

We have now successfully compiled the MyHelloWorld application from a source code file into the MyHelloWorld.ihx file which the NanoComputer can understand. In order for the NanoComputer to run the application, the application must first be installed onto the NanoComputer. This involves transfering the MyHelloWorld.ihx file from the PC to the NanoComputer. Once the MyHelloWorld application has been installed on the NanoComputer, it will be automatically stored on the NanoComputer as MyHelloWorld.app instead of MyHelloWorld.ihx.
  • To install the MyHelloWorld application onto the NanoComputer, issue the following command inside the Windows XP Command Prompt. The NC_CMD --install will display a status bar as it transfers the MyHelloWorld.ihx file to the NanoComputer and the NC_CMD --install command will display a final OK if the transfer is succesful.
If the NC_CMD command is not successful for any reason, try unplugging the NanoComputer from the USB port, waiting 10 seconds, plugging the NanoComputer back into the USB port, waiting 10 more seconds, then rerunning the command.

NC_CMD --install output\MyHelloWorld

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The final OK returned by the NC_CMD --install after a succesful installation is also shown highlighted with a yellow background in the screenshot below.
Image:MyHelloWorld_Install.png
We can verify that the NC_CMD --install correctly installed the MyHelloWorld application by issuing the NC_CMD --dir command. The NC_CMD --dir command will show a listing of files currently stored on the NanoComputer. Note: The NC_CMD --dir shows the files on the NanoComputer, which is different then the DIR command which shows the files and directories in the current directory on the PC
  • To show a list of files currently stored on the NanoComputer, issue the following command inside the Windows XP Command Prompt.

NC_CMD --dir

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background. The newly installed MyHelloWorld application is also shown highlighted with a yellow background in the list of files and directories returned by the NC_CMD --dir command.
Image:MyHelloWorld_Install_Dir.png


Running the MyHelloWorld Application

We can now run our MyHelloWorld application since it has been succesfully compiled and installed onto the NanoComputer.
Before running the MyHelloWorld application, the NanoComputer LCD should display something similar to the image below.
Image:MyHelloWorld_NanoComputer_1.png
  • To run the MyHelloWorld application, issue the following command inside the Windows XP Command Prompt.

NC_CMD --run MyHelloWorld

After entering the command into the Windows XP Command Prompt, the Windows XP Command Prompt Window should look similar to the screenshot below. The command that has just been issued inside the Windows XP Command Prompt is shown highlighted with a yellow background.
Image:MyHelloWorld_Run.png
After running the MyHelloWorld application, the LCD screen of the NanoComputer should clear the LCD screen with a blue color as instructed by the source code file we viewed earlier and look similar to the image below. We will make the application print "Hello World!" in the next chapter.
Image:MyHelloWorld_NanoComputer_2.png


Conclusion

  • Congratulations, you have now learned how to create a new project for a new application, compile the application, install the application, and run the application. In the next chapter we will add code to the source code file to make the application print "Hello World!".
  • Continue onto Chapter 5 - Writing Your First Application (Part 2)


Navigation

Click on any of the links below to navigate to a new page