Most .NET Gadgeteer users are familiar with plugging additional modules into their creation and then calling the methods supplied by the installed driver. Whilst this is probably as far as many people go it is worth keeping in mind that most drivers are open source (.NET Gadgeteer Codeplex http://gadgeteer.codeplex.com/) and so if a feature is not implemented you can change the driver and implement it yourself. The drivers are implemented in C# and you can just add the project to your solution and edit/debug to your hearts content.

Even better… if there is not a .NET Gadgeteer module for the task you have in mind, don’t panic. It is very easy to create drivers for new hardware especially if they have standard interfaces such as I2C, serial, PWM etc. Just to let you know upfront, you only need to code the parts that make you module work, all the other goodness such as an installer, toolkit addition and version support are all taken care of in the templates.

I thought I would write up an example in the hope it may help others. I have a need for a long range transmitter and currently Gadgeteer does not have such a module. So the next stage was to build one, we have a few prototypes and transmission timing can be an issue. We then commissioned Sytech Designs http://sytechdesigns.com/ to build a module with the hardware we normally use but with better timings, smaller form factor and of course a .NET Gadgeteer socket. I plan to write more on the actual module hardware later. Suffice to say, we now have a module that is compatible with sockets U and I.

RTTY

So here are the steps that you will be required to complete to build a driver and installer that you can distribute with a module. Please note that there are full and proper instructions on Codeplex and I recommend you read these: http://gadgeteer.codeplex.com/releases/view/72208

Step 1 – Create driver project in VS.

NewModuleCreate

Step 2 – Select the versions of NetMF

You are then prompted to select which versions of NetMF you plan to support, i would recommend both as you will be able to use the same code for much of the projects. (It will also help with the uptake of any module you are building)

image

Step 3 – Read the ReadMe.txt

Ok seriously it does save time and will solve a lot of strange issues if you read and follow the readme.txt. At this stage you need to setup namespaces, dependencies and some other configurations. There is a fair amount of text in the readme, but it does not take long to go through. In my experience if you have issues later on it is because you missed a step here.

Step 4 – Extra marks for testing so add some from the start!

The default template will generate 3 projects, one for the installer ‘FSKRadio’ and one for each version of the NetMF that you selected to support. At this point I like to add a couple more. Unless you module is very simple, you will not want to code, compile, build installer, install and then test; it is just too long a loop. I like to generate a test project for each version of the NetMF versions. imageTo do this simply add a new project, and add a reference to the corresponding module driver you wish to test.

image

TIP: It is worth mentioning at this stage that for those of you, like me, who hit F5 in Visual Studio and sit back you will encounter issues. the problem is that VS/NetMf does not like multiple projects that produce DLL’s with the same name. So hitting F5 will run one of the Testing projects, but proabably not the one you expect. You will get an error complaining that the hardware has a DLL version mismatch. Before you go reflashing your hardware, rather than hitting F5 try right click project –> debug –> Start new instance, to be sure you are deploying the correct project.

Step 5 – Add the serial interface.

My FSK module communicates via serial so the first thing is to do is to setup the serial port. Since the module communications are oneway, there will never be any data received (RX). However for every message sent I have a ACK message containing ‘OK’ .

public FSKRadio(int socketNumber)
{
    //Get the socket used by the module
    socket = Socket.GetSocket(socketNumber, true, this, null);
    //Validate Socket choice
    socket.EnsureTypeIsSupported(new char[] { 'U' }, this);
    //Get the serial port
    m_serial = new Serial(socket, 38400, Serial.SerialParity.None, Serial.SerialStopBits.One, 8, Serial.HardwareFlowControl.NotRequired, this);
    //Default is \n - used for auto ACK messages
    m_serial.LineReceivedEventDelimiter = "\r";
    //Read all data - mainly just debug
    m_serial.AutoReadLineEnabled = true;
    //Handle any data on the serial line
    m_serial.LineReceived += new Serial.LineReceivedEventHandler(m_serial_LineReceived);

So for now I just auto read any data and output it to the debug. (In the future I plan to wait for the ACK from each message so that I don’t attempt to flood the module with data. It just sends at 300baud)

void m_serial_LineReceived(Serial sender, string line)
{            
    Debug.Print("RX :" + line);
}

(I hope this shows how easy it is to communicate with a serial device using .NET Gadgeteer)

Step 6 – Add the GPIO pins

That is not the full story, there are a couple of GPIO pins that are also used by the module. The first changes the module from setup mode to operational mode. When in setup mode the modules does not transmit, but has a series of options around changing the broadcast frequency and the frequency shift. The second enables or disables the transmit of the Radiometrix module. Each of the GPIO pins are digital (0/1) and do not support interrupts. these can simple be configured by a couple of lines of code:

m_mode = new DigitalOutput(socket, Socket.Pin.Six, true, this);
m_enableTX = new DigitalOutput(socket, Socket.Pin.Three, true, this);

and used by simply setting the properties to true/false:

public void EnableTransmit()
{
    //Turn transmitter on 
    m_enableTX.Write(true);
}

Step 7 – Compile and Install

I will obviously need to add a few more methods to my driver, but for now i just plan to use the serial port, by sending commands from my application. So now we are ready to build the installer. Since we do not want any debug features in the driver, simply change the VS build from Debug to Release and hit build.

At this point you will be pleasantly surprised to find that you are done! Check in the ‘bin\Release\Installer’ directory and you will find an MSI ready for distribution. This MSI will install your module in the VS toolbox and you will be able to drag and drop it into the designer.

(If you hit issues here check you completed the steps in the ReadMe.)