Coding with MS Visual Studio

Tips for Editing Code in Microsoft Visual Studio

Arguably the most powerful coding IDE available, Microsoft Visual Studio 2010 can greatly enhance one’s code writing experience. This page will be dedicated to techniques for using Visual Studio as a code editing tool in conjunction with the online mBed compiler.

This project is currently in BETA 1. At this stage, the idea is to simply use Visual Studio for its powerful Intellisense and text editing capabilities. No compiling support is included, and some preprocessor commands may still need to be added.

The important point is that one can write their program logic in Visual Studio’s rich editor, and then copy-and-paste the code into the mBed online compiler.

Coding with Visual Studio for mBed

The following text outlines the steps necessary to get Visual Studio up and running with the mBed header files. As an alternative to walking through these steps, you can download a preconfigured project folder from here: http://cid-8357f4a651e4838b.office.live.com/self.aspx/mBed/mBedTemplate.zip

Make copies of this project each time you start a new mBed project, then simply modify and save over the provided files. (Note that MCU specific header files are not included in this project; download the ones you need from: http://mbed.org/projects/libraries/svn/mbed/trunk?rev=27)

Download Required Files

First, you will need a copy of Visual Studio 2010. Visual Studio Professional users and MSDN subscribers will already have the ability to create C++ projects. All others may download the free Visual Studio 2010 Express C++ Edition from here: http://www.microsoft.com/express/Downloads/#2010-Visual-CPP

Next, you will need a copy of each of the primary header files for mBed. These can each be downloaded from here: http://mbed.org/projects/libraries/svn/mbed/trunk?rev=27 by selecting each file, then clicking the “Plain Text” link at the bottom of each page. Save each of these files to a single folder where you can easily access them to make copies.

You may also wish to download the header files from the appropriate MCU subdirectory for your mBed board.

(Note that this may still represent an incomplete list of header files for full programming support)

Now you are ready to create a project to be used as a shell for writing your mBed code.

Create a New Project

Open Visual Studio and choose to create a new project. The type of project isn’t especially important at this stage as we will only be using the code editor, but a MakeFile or Blank project template should be fine.

After creating the project, right click the project name in the solution explorer and select open project folder. Copy all of the header files downloaded previously into this folder. Return to Visual Studio and click the Show All Files button in the Solution Explorer. Select each of the header files (they will have a red circle next to their names). Right click the selected group of items and select Include in Project. Click the Show All Files button again.

The Solution Explorer should now list all of the header files under the Header Files folder.

Right click the Source Files folder and select Add New Item. Choose a C++ File object and name it “main”. Add the following text to this file to get the basic “hello world” sample:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

Finally, from the main menu bar, click Project and select Rescan Solution.

You can now write code in your main.cpp file and get Intellisense dropdown lists on object members as well as the embedded help text in a tooltip! Not to mention the powerful find/replace, formatting, and other code editing features!

Compiling Your Code

You will need to create a project in the mBed online compiler with the same name as your Visual Studio program. After composing your code in Visual Studio, copy and paste it into the online compiler and then compile and download as normal.

For now, it is suggested that you not attempt to build or compile in Visual Studio. This may introduce a bunch of annoying warnings or errors since we have not really told VS about the target platform (it thinks we are targeting Windows.) You should be able to click Build -> Clean [Solution] from the main menu, should such warnings or errors arise in the IDE.

Additional Tips & Tricks

  • Press CTRL+Space at any time while coding to force an Intellisense lookup. Useful for looking up members by name (e.g. type "str" and then press CTRL+Space), or for browsing available code members (e.g. press CTRL+Space from the start of a new line).
  • Press F1 with the carret on standard C++ terms (or variables of standard types) to look them up automatically in the MSDN Library online help. Useful for those who are familiar with programming, but new to C++.

More to Come

This BETA1 template is a very simplistic implementation of this Visual Studio-enhanced editing concept. For now, it is a code editor-only tool requiring a manual synchronization of your code between VS and the mBed online compiler.

As the concept is explored, it may be found that more can be done with VS – it is an immensely powerful and extensible IDE so it is, in theory, possible to create an entire support package for mBed, right down to the compiler (though this would obviously require support from mBed developers directly as the workings of the compiler could most likely not be inferred well enough by the community to duplicate).


All wikipages