Your environment is now ready for your first program (or driver) !

For this tutorial, we will suppose that you have a BarGraph Click board and you want to code an application that is using it.
Furthermore it is assumed that you have completed all downloads and install on the First-time users page.

If it’s the fist time you’re using this board, then you may not have the corresponding driver. You should then go to the Downloads page and download it from the list of available drivers. The driver is zipped and contains complete commented out source code, an example application and a .chm help file.

Unzip this driver in a folder for future reference.

Open Visual Studio and create a new MicroFramework “Console application”.

Since the Bargraph driver is using PWM and SPI, you also have to add references to Microsoft.SPOT.Hardware and Microsoft.SPOT.Hardware.PWM :

You will of course need the MBN Core assembly, which is located in c:\Users\Public Documents\MBN (this was installed here by the setup program) :

Locate where you unzipped the BarGraph Click driver and “Add existing item” to your project :

Now, the solution should look like this :

So far, you have referenced the needed assemblies and added the driver’s source code to you project. So go to Program.cs source code. It should look like this :

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

usingSystem;

usingMicrosoft.SPOT;

namespaceMFConsoleApplication3

{

publicclassProgram

{

publicstaticvoidMain()

{

Debug.Print(Resources.GetString(Resources.StringResources.String1));

}

}

}

You can remove the Debug.Print() statement as we don’t need it in our example.

At this stage, we need to add some Using statements :

C#

1

2

3

usingSystem.Threading;

usingMBN;

usingMBN.Modules;

Then declare our Bargraph object :

C#

1

privatestaticBarGraphClick _bargraph;

In the main() method, we will now add the initialisation sequence and working code :

C#

1

2

3

_bargraph=newBarGraphClick(Hardware.SocketOne);

_bargraph.Bars(6);

Thread.Sleep(Timeout.Infinite);

The first line creates the Bargraph object, which will be plugged on Socket #1 on the mainboard. You can of course choose another socket if you have other Click boards.

It’s now almost done… The Bargraph is ready and only needs to be told how many leds it will light on 😉 Here, we tell it to display 6 bars with the 5 preceding leds being lit.

Should you want only the sixth led lit and not the others, simply use false as second parameter :

C#

1

_bargraph.Bars(6,false);

Last line is the standard way to terminate a NETMF program, telling it to sleep forever.

You’re done !

Now the complete source code should look like this :

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

usingSystem;

usingMicrosoft.SPOT;

usingSystem.Threading;

usingMBN;

usingMBN.Modules;

namespaceMFConsoleApplication3

{

publicclassProgram

{

privatestaticBarGraphClick _bargraph;

publicstaticvoidMain()

{

_bargraph=newBarGraphClick(Hardware.SocketOne);

_bargraph.Bars(6);

Thread.Sleep(Timeout.Infinite);

}

}

}

Now you will have to tell Visual Studio that the deployment and debugging will be done on the MBN board. To do this, go in the Project properties, select the “.NET Micro Framework” tab and choose “Transport : USB”. Your MBN board should then appear in the device dropdown box below.

This step needs to be done only once at the beginning of the debugging session.

You are now ready to : Compile and run !

If you want to add other Click boards, then you simply have to “Add existing item” to your project and select the corresponding driver’s source code. Yes, it’s that easy !

Tip :

If you want to easily use our drivers with modules connected on screw terminals, you may create a virtual MBN socket and use it in the driver constructor :

C#

1

2

3

4

5

6

7

8

9

10

11

12

varvds=newHardware.Socket

{

An=Pin.PC5,

Rst=Pin.PB0,

Cs=Pin.PE7,

Pwm=Pin.PE8,

Int=Pin.PE11,

Rx=Pin.PC4,

Tx=Pin.PE13,

Name="VirtualCharacterDisplaySocket"

};

lcd=newCharacterDisplay(vds);

Coding drivers for MBN is (almost) as easy as coding NETMF applications !

Since there are some constraints with pins mappings and different checks involved, you should start with one of the two drivers templates, depending on which kind of communication it’s using (I²C or any other).

In the following example, we will use the Bargraph Click board, which is by default a SPI board. Coding for an I²C driver is absolutely identical except for the template, which will of course be the I2C template.

At this point, you can see that the template has generated two source files : Driver.cs and DriverEvents.cs. If you don’t need to generate events in your driver, then you may safely delete the DriverEvents.cs file and remove the reference to the event at the beginning of Driver.cs.

Select the Driver.cs file. The code should look like this :

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

/*

* ExampleDriver driver skeleton generated on 11/10/2014 9:41:04 AM

*

* Initial revision coded by Christophe Gerbier

*

* References needed : (change according to your driver's needs)

* Microsoft.SPOT.Hardware

* Microsoft.SPOT.Native

* MikroBusNet

* mscorlib

*

* Copyright 2014 MBN

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,

* either express or implied. See the License for the specific language governing permissions and limitations under the License.

*

* Follow the TODO tasks to complete the driver code and remove this line

*/

//TODO: review the XML comments so that they are applicable to your driver

usingSystem;

usingSystem.Reflection;

usingMBN;

usingMBN.Enums;

usingMBN.Exceptions;

usingMBN.Extensions;

usingMicrosoft.SPOT.Hardware;

namespaceMBN.Modules

{

//TODO: Add other "Pins used" if needed

/// <summary>

/// Main class for the ExampleDriver driver

/// <para><b>Pins used :</b> Miso, Mosi, Cs, Sck</para>

/// </summary>

publicpartial classExampleDriver:IDriver

{

// This is a demo event fired by the driver. Implementation is in the BaF.Events namespace below.

/// <summary>

/// Occurs when a demo event is detected.

/// </summary>

publiceventDemoEventEventHandler DemoEventFired=delegate{};

privatereadonlySPI.Configuration _spiConfig;// SPI configuration

/// <summary>

/// Initializes a new instance of the <see cref="ExampleDriver"/> class.

/// </summary>

/// <param name="socket">The socket on which the ExampleDriver module is plugged on MikroBus.Net board</param>

/// <para>SOFT reset : generally by sending a software command to the chip</para><para>HARD reset : generally by activating a special chip's pin</para></param>

/// <returns></returns>

/// <exception cref="System.NotImplementedException">Thrown because this module has no reset feature.</exception>

publicboolReset(ResetModes resetMode)

{

thrownewNotImplementedException("Reset");

}

}

}

As you can see, the template also comes with XML comments ready. This will be useful for users as VS auto-completion will use this information. At the beginning of the class, we declare the DemoEventFired event that will be raised in the MethodWithEvent() method. If you don’t need events, you can delete this line and the code in the method.

In our example, since we won’t be using events, we will delete the related code and the SPIDriverEvents.cs file. Also, it’s now a good time to rename the SPIDriver.cs file to something more adapted to your driver. Here, we will call it BargraphClick.cs. This is not mandatory, of course, but using a generic name is not really useful.

Back to the driver itself…

You will notice that your driver needs to implement the IDriver interface. This interface is part of the MBN core and tells that the driver will expose some common methods and properties. The IDriver interface exposes the Reset() and PowerMode() methods as well as the DriverVersionproperty. Users can then check for the driver’s version to enable more advanced features, for example.

In the class constructor, the Hardware.CheckPins() method is used to see if there’s another driver that may already use the same pins as the Bargraph Click board. So it is very important to put the correct pins used, here.

The Bargraph Click board is using SPI pins by default, PWM pin to control brightness and the RST pin, so we have to modify the line accordingly :

Also, since it’s using PWM, we have to declare some (private) variables :

C#

1

2

privateDoublePWMLevel;// Brightness level

privatePWM Pwm;// Brightness control

PWM needs that a reference to Microsoft.SPOT.Hardware.PWM be added to the project. The PWMLevel variable will hold the current PWM value as we will use a property to get/set the leds brightness.

Just to show that the default constructor call can be modified, we will add a parameter for the initial brightness of the leds when the object is created (leds will have full brightness in the example). Now, let’s change the constructor to initialize SPI and PWMLevel as well :

The parameter NbBars is the number of bar to light on. Fill will indicate if the preceding leds will also be lit or not.

C#

1

2

3

4

5

//Example 1, light first 8 leds :

Bargraph.Leds(8);

//Example 2, light only the 8th led :

Bargraph.Leds(8,false);

Here you are ! The driver for the Bargraph Click board is done ! At least for basic features. You can download the complete source code of this driver on the Download page of this site.

Final note :

At this point, you may want to add more methods or properties, so that you have a very complete driver. Although this is understandable, be aware that this is a driver, and as such it’s not meant to contain a full application ! Temptation is huge to code an application in a driver, but think at users that don’t need the ExtraFeature(Boolean MyNice Param) ; method. It will consume memory while being useless in most cases. So, our advice would be to keep the driver as small as possible and to allow users to access the internals of the chip if needed. This can be done for example by making public the methods used to access internal registers. Or you can create a separate assembly that contains advanced features, based on the basic driver.

Virtual Sockets ®

Discover how Virtual Sockets can make your life easier when you want to use modules from different manufacturers.

Storage class

Short text about Storage class and a link to the full article.

Available Click boards

Drivers already coded72%

We need you !

MikroElektronika is building Click boards faster than we can code the drivers…
If you think you can help us in coding drivers (very easy), then you are only one click away 😉