C# New Projects
The BrainPad is coded in C# using world-class Microsoft Visual Studio. This full-featured IDE is used by millions of professional developers to create mobile and PC applications. This page will show you how to start a Visual Studio project and deploy it to the BrainPad. Use these steps every time you want to create a new project.
A Windows PC must be setup with Microsoft Visual Studio and your BrainPad must be loaded with TinyCLR Operating System. Both steps are covered in the C# System Setup page. If you already know how to create a TinyCLR type project, skip this lesson and go to the C# Intro Lesson.
This lesson is more of a prep step and is not required to learn C#, but it is needed when using BrainPad with Visual Studio. A teacher can prep each BrainPad in advance so that learners can skip this step and go right to the C# Intro Lesson.
Create a Project
Assuming you have a computer that is already setup with Visual Studio and a BrainPad loaded with TinyCLR, you are almost ready to start coding. Remember that whenever you begin a new project, you will need to follow two main steps: Create a new project AND include the BrainPad Driver. Let’s go ahead and start Microsoft Visual Studio.
Create a new project: Select Create a new project from the Visual Studio side menu.
Select TinyCLR C#9 project: In the search window enter tinyclr. Several options will be available. We want to select TinyCLR Application C#9 and click Next.
Naming the Project: Now we’ll create a project name. You can call it what ever you want. We’ll call ours HelloProject, then click Create. Try using spaces and symbols and see what happens.
Delete All the Code: Since we’re using C#9, we can take advantage of something called Top-Level statements just to get us started. Later, we will learn about Main(). For now, select all the auto-generated code and delete it by highlighting the lines and clicking the delete key.
Now the BrainPad NuGet Library driver is ready to be included.
There are pre-made drivers you need to include, which allow access to all the commands available to the BrainPad. The driver library is called NuGet, which is a driver library package that Visual Studio can download from the internet.
BrainPad NuGet Library: To include this driver library, RIGHT-CLICK on our project name HelloProject in the Solution Explorer window on the right panel. Then select the Manage NuGet Packages... option.
The NuGet Package Manager will start. Click on the Browse tab at the top left of the new window. In the search bar, type BrainPad and a list of available options to install will appear. Select BrainPad.Drivers. Click on the Install button.
After clicking the Install button, a License Acceptance window will pop-up. Click I Accept.
The NuGet package will automatically install everything you need. And now you are finally ready to start coding!
Go back to “Program.cs” tab and type this code in.
using static BrainPad.Controller;
This is a USING STATEMENT. It is very important and MUST be added to every BrainPad project we create in C#. Lesson plans going forward won’t show the USING STATEMENT at the top, so just be aware it needs to be there.
Now we can start writing our first program. How about we print “Hello World” on the BrainPad’s screen?
With BrainPad connected to the computer, click on the Start button next to the GREEN arrow just below the top menu in the middle. This is how programs get deployed to the BrainPad.
The program will take a moment to compile and we’ll see it building in the Output window at the bottom of Visual Studio. Once deployed, we can observe our code running on the BrainPad. YEAH!!! You’ve created your first C# BrainPad program.
Note that the program is now loaded and will always be loaded on the BrainPad, until you modify the program. Go ahead and disconnect the BrainPad and reconnect it. You will see “Hello World” on the screen again. Even if you power up the BrainPad from a phone charger or a battery, the results are the same.
Once loaded with a program (application) the BrainPad will always run that program on power up. You can force the BrainPad to not run the program at power up using the B button on the front and the Reset button on the back. To do this, hold the B button down while simultaneously pressing and releasing the Reset button.
Now that you have a working project, we can start by learning some more C#. Check out the C# Intro Lesson to move to the next phase and start coding.
Your system is already setup to create PC and mobile applications. Do you dare to try? Start a new project and instead of TinyCLR project select Console Application, and from there select .NET 5.0. When the project is created, change the code to the following:
using System; Console.WriteLine("Hello World!");
Run the program and you will get a pop up window (your program running) with “Hello World!” printed on it. Compare the console code with the BrainPad code. You can see how on the BrainPad we are using the BrainPad controller, where on the PC we are using the System! And then the BrainPad has a Print() function to print to the screen, but the PC has a WriteLine to print to the Console. Later on when you learn more C#, you can always come back here and try the same code but running on the PC.