When you drag a module from the toolbox onto the designer it references the correct DLL’s and adds import statements to the program.cs.
Fig 1: Adding a compass module to the designer.
This way you are ready to just use the module in your code. This is the normal way to to things… However what if you want to make a change to the driver for a particular module, for example to:
- add an event, method or property,
- perhaps fix an annoying bug,
- add extra debug, or work out how the hardware works,
- load a different driver, such as the GPS/Bluetooth module drivers from Codeplex.com ,
- copy the driver for a similar piece of hardware,
- you want to compile a driver for a different version of NetMF (e.g. upgrade 4.1 –> 4.2)
- you cant be bothered to get WIX setup and working just to make a small change.
The correct way to change a driver is to get the source code VS solution, edit it and then rebuild. This will build an installer (msi) which can be installed and distributed with the changes intact. This process is fine for the final edits and distribution of a driver, but what if you just want to try a few changes? This is a long process as each change results in a build, reinstall , test.
This post is designed to show how to simply edit drivers or include different drivers. It is NOT a substitute for building the install files which should be distributed with each hardware module.
Example
Lets say we want to make an edit to the compass driver, perhaps it does not function as we expected. Here is a summary of what to do:
- Get the source code. (Either the existing source, or substitute with a different one)
- Remove the existing driver from your solution.
- Add the (new) source code to your solution.
- Add the module to your project.
- Edit the driver and test.
Get the source code.
There are multiple drivers for some modules, and the alternatives can often be better. (Well at least until the manufacturer incorporates the changes into the distributed driver.)
If you are looking for other drivers try: http://www.codeplex.com/site/search?query=.net%20gadgeteer
The source for the distributed drivers lives here: http://gadgeteer.codeplex.com/SourceControl/latest
I use SVN to get the latest source, but if you just want the source then click the download button. It will give you a zip, be sure to expand it to somewhere sensible, we will be using it later.
Remove the existing driver from your solution.
This step is important as it can drive you crazy later by swapping your new driver with the old one when you least suspect it.
Here is some background information first:
If we look a the code behind the designer we can see what happens when the compass module is added.
Click “Show all files” if you cannot see the “Program.gadgeteer.cs” file. This file is generated EVERY time you save the designer (the place you drag modules onto). Inside this file there is the following code. (For this example project)
- namespace CompassDriverExample {
- using Gadgeteer;
- using GTM = Gadgeteer.Modules;
-
-
- public partial class Program : Gadgeteer.Program {
-
- private Gadgeteer.Modules.Seeed.Compass compass;
-
- public static void Main() {
- // Important to initialize the Mainboard first
- Program.Mainboard = new LoveElectronics.Gadgeteer.ArgonR1();
- Program p = new Program();
- p.InitializeModules();
- p.ProgramStarted();
- // Starts Dispatcher
- p.Run();
- }
-
- private void InitializeModules() {
- this.compass = new GTM.Seeed.Compass(8);
- }
- }
- }
The first line of importance creates a compass variable of type Gadgeteer.Modules.Seeed.Compass.
- private Gadgeteer.Modules.Seeed.Compass compass;
The second line of importance is the one that creates an instance of the compass object and specifies the socket number.
- this.compass = new GTM.Seeed.Compass(8);
In order to create the compass object it needs to know about it. This is achieved automatically by adding a reference to the compiled DLL, when you dragged in a new module.
Here are the things you need to change to remove the old driver:
1) Delete the module from the designer.
- This will remove it from the generated code and will stop the auto generated code swapping back to the old driver when you save.
2) Check that the references on your project no longer reference the DLL for the old driver. (highlighted above)
or
You could just start with a blank project.
The reason I show all this is that later on you will need to instantiate the object yourself. If you don’t know what to write you can copy the auto generated code … more later.
Add the (new) source code to your solution.
At this point you have a blank project or one that does NOT reference/use the module that you plan to edit the driver for. First we need to add the source code, this is the code that you downloaded earlier or can be a different driver that you wish to try.
In Visual studio right click on the solution and add an existing project by navigating to the correct project. (This can be a little confusing as it tends to be a long way down the folder structure, and you need to be sure of the version you are using, v4.2)
Inside the Compass –> software folder you are looking for the compass.csproj file. (I would expect to see 2 versions, one for 4.1 and one for 4.2, but this may not be the case if an old driver template was used.)
A little note: In this particular case there is just a driver for the 4.1 version of NetMF, yet when you install the latest SDK the compass is available in 4.2. This means that the manufacturer has updated the driver but not uploaded the changes to Codeplex. Be sure to encourage manufacturers to keep the source updated. This could also be the case if the Codeplex source code has different methods/signatures. Since my project is v4.2 I will just choose to update the target framework in the project properties.(Good example of how to upgrade versions)
Add the module to your project
At this stage you should have multiple projects in your solution. Your project as well as the project for the module you are wanting to edit. So now we have all the source code in place. Lets use it.
First we need to let our project know about the new project by adding a reference, but instead of referencing the DLL we reference the project with the source code.
It is under projects not the .NET tab:
You will then see a reference to the Compass project, note that this is different to the reference show above when you let the designer import it.
Now we are ready to use the module. Remember all the automatically generated code that the designer does for you, well now you have to do it yourself. Instead of adding to code to the generated file (you will loose it , when it auto generates) we add it to the main program.cs file. (only 2 lines)
First create a private variable for the compass then in the program started create an instance, on socket 8. Be sure not to use the socket in the designer – you will get a warning if you do so fear not.
- using System;
- using System.Collections;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
-
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
-
- namespace CompassDriverExample
- {
- public partial class Program
- {
- private Gadgeteer.Modules.Seeed.Compass compass; //Declare the compass
-
- void ProgramStarted()
- {
- this.compass = new GTM.Seeed.Compass(8);// create an instance on socket 8
- Debug.Print("Program Started");
- }
- }
- }
Edit the driver and test.
If you have made it this far then you have a solution that builds a module from source and instantiates the module, ready for use. Have a go to see if it works as expected and all the methods are there.
Now down to the best bit, you can now just change the module source and deploy as you normally would. The compass project will be rebuild every time. For example you may want to know exactly what the ‘Gain’ value is and how it is set. The module has a setgain method which takes a Gain property. The property ranges from Gain1 to Gian8, if you hold your mouse over the property it will display a tooltip, but lets go look at the source.
If you right click on a method (and have the source available) you can select “Go to definition” and it will take you to the code – got to love Visual Studio.
The method source simply writes a value to a register, where the value is of type ‘Gain’ (Right click on ‘Gain’ and go to definition)
- public void SetGain(Gain gain)
- {
- Write(Register.CRB, (byte)gain);
- }
Here are the values :
- public enum Gain : byte
- {
- /// <summary>
- /// +/- 0.88 Ga
- /// </summary>
- Gain1 = 0×00,
-
- /// <summary>
- /// +/- 1.2 Ga (default)
- /// </summary>
- Gain2 = 0×20,
-
- /// <summary>
- /// +/- 1.9 Ga
- /// </summary>
- Gain3 = 0×40,
-
- /// <summary>
- /// +/- 2.5 Ga
- /// </summary>
- Gain4 = 0×60,
-
- /// <summary>
- /// +/- 4.0 Ga
- /// </summary>
- Gain5 = 0×80,
-
- /// <summary>
- /// +/- 4.7 Ga
- /// </summary>
- Gain6 = 0xA0,
-
- /// <summary>
- /// +/- 5.6 Ga
- /// </summary>
- Gain7 = 0xC0,
-
- /// <summary>
- /// +/- 8.1 Ga
- /// </summary>
- Gain8 = 0xE0,
-
- }
If you rename one of these values, it will have an immediate effect on your code. For example:
- public enum Gain : byte
- {
- /// <summary>
- /// +/- 0.88 Ga
- /// </summary>
- Gain1RenamedToSomethingBetter = 0×00,
-
- /// <summary>
Will immediately change in your program.cs

Job done, you are now able to edit the driver and have the changes take effect immediately. Compile is same as always and the solution will deploy your latest code to the hardware when you deploy. This way you can edit and change code until you are satisfied. Once that is done you can then go back to the Compass SOLUTION and build an installer…..
Advanced – where did these numbers come from?
But you have to wonder what these values are and what do they mean? Here is where we need the datasheet. A few folders up from the source code there is a ‘Hardware’ folder that has a datasheet. I you have a custom modules or there is not datasheet you can do very well by searching for the chip number. If stuck pester the manufacturer to add a link to the datasheet for the module.
This compass module is a HMC5883 and there is a data sheet in the Hardware folder (in the downloaded zip from codeplex) :
These can be hard to read, but we can use the bits we are interested in. For example searching the datasheet for ‘Gain’ shows that there is a gain control and that there are 3 bits dedicated to changing it.

Further on in the datasheet there is a section about ‘configuration register B’ this is for setting the gain. Remember that the gain method just writes a byte (8 bits) to a register (Register B), table 10 lets us know that the register is 1 byte (8 bits) – same as our code. So now we just need to check the values that are to be written. Table 10 on page 13 shows that bits 7,6,5 are for setting the gain and 4-0 must be set to zero. Table 12 shows the different gain values and shows how this changes the output.
For example the default gain is 1024 counts/Gauss and this is done by setting the 8 bits of the register to [0,0,1,0,0,0,0,0] but if we look at the code the values are written in hex not binary.
If you are not comfortable converting, believe it or not calculator is your friend! Change calculator into programmer mode:
Select binary and then input the binary you want to convert, in this case [0,0,1,0,0,0,0,0] (note you do not need to leading zeros)
If you then select hex, it will convert the value. (Answer 20)
Since the compiler would not know if we meant 20 (twenty) or 20 (thirty two in decimal,or binary [0,0,1,0,0,0,0,0]) there is a prefix added to a number to let it know that it is hex, the prefix is ‘0x’. And sure enough if we look at the code the default gain is ‘0×20’ which is the correct value according to the data sheet.
- public enum Gain : byte
- {
- /// <summary>
- /// +/- 0.88 Ga
- /// </summary>
- Gain1RenamedToSomethingBetter = 0×00,
-
- /// <summary>
- /// +/- 1.2 Ga (default)
- /// </summary>
- Gain2 = 0×20,
Have a look around the datasheet, perhaps there are hardware features that are not implemented in code, so you can improve the performance, enhance the driver, solve bugs or trouble shoot issues.
For example register A can be used to set the sensor rate (default 15Hz) as well as a bias for different axis, but if you right click on the register name and select ‘find all references’, it shows that the driver never uses it….
Happy Gadgeteering…. check www.gadgeteering.net for a complete list of hardware.
Tips
If you are using the express version of Visual Studio it only supports one language. So if you have a VB project and import a C# project, you wont get any debug and things will look a bit strange.
Remember that since you are compiling from source you can now add a break point inside the driver! (F9) This is a fantastic way to halt execution and inspect what is going on, with out having to write tonnes of output lines……