 |
 |
|
 |
|
|
|
|
|
|
|
|
|
|
Programming Digi-Bee+ in Visual Basic |
|
|
|
|
|
|
|
|
|
|
|
| |
Although Digi-Bee+ comes with it's own
software (Logic-Lab Plus) to allow the beginner to start using it in home
automation projects very quickly, it also comes with a DLL interface to
allow the intermediate and advanced user to write their own programs for
it. The DLL provides a general purpose interface that greatly simplifies
the task of writing programs for a USB device. It can be tricky
manipulating the USB comms into sending and receiving messages to and from
a device which can easily be plugged and unplugged at any time. The DLL
eliminates all of these headaches by simplifying the task into three
library functions.
InitDgb( ) SetOutputs (outputs) and
ReadInputs(inputs)
InitDgb() is called somewhere near the start
of your program and takes care of all of the USB comms initialisation and
prepares the Digi-Bee+ for receiving messages.
SetOutputs(outputs) can then be called at any
time during your program to set the output pattern of on's and off's. The
parameter outputs is simply a 16 bit integer values. In outputs bit0
corresponds to output 1, bit 1 to output2, etc.... In each case a logic
value of 1 turns the output on and a value of 0 turns it off. For example
the statement below would turn on the first three outputs...
SetOutputs (7) since 7 in binary is 00000111
and the following would turn on outputs 1, 2, and 4
SetOutputs (11) since 11 in binary is
00001011
To read the current state of the digital inputs simply
pass an integer variable to the ReadInputs()
function and use it’s value after the call. For example if inputs 2 and 4
are on, then the call…
ReadInputs(inputval)
will result in the integer “inputval” containing the value 10. This
is 0000000000001010 in binary.
To read the current values of the 4 analogue inputs pass 4
variables by reference to the ReadAnalogueInputs function….
ReadAnalogueInputs(aip1, aip2, aip3, aip4)
… which will result in the digitised amplitude of the 4 voltage levels on
the 4 analogue inputs being returned in the supplied variables aip1 – 4.
If , for example, the voltages on the 4 analogue inputs happen to be 1.0v,
2.0v, 2.5v and 3.75v respectively, then the above function call will
result in the following values in the 4 variables…..
aip1 aip2 aip3 aip4
51, 102, 128, 196
This assumes a “normal” sensitivity setting
To set the sensitivity to other than 0-5v use the SetSensitivity function…
SetSensitivity(1) sets high sensitivity based
on current voltage reference
SetSensitivity(0) sets normal sensitivity
based on 0-5v
In the above example if the following function call had been made prior to
the ReadAnalogueInputs() function….
SetSensitivity(1);
Then the resulting values returned in a1 – a4 would be approximately
double (subject to the maximum of 255). This assumes that the on-board
voltage reference of 2.4v was used.
What could be simpler……………..
The only other thing that a VB program must do is to declare the functions
that it is going to use within the DLL and the name of the DLL itself.
This must be done at the start of your program or at least before any
references to the three functions are made. The following is a program
excerpt showing how this is done...
Declare Function InitDgb Lib "dgb.dll" () As Boolean
Declare Function SetOutputs Lib "dgb.dll" (ByVal outputs As Integer) As
Boolean
Declare Function ReadInputs Lib "dgb.dll" (ByRef inputs As Integer) As
Boolean
Declare Function ReadAnalogueInputs Lib "dgb.dll" (ByRef aip1 As Integer,
ByRef aip2 As Integer, ByRef aip3 As Integer, ByRef aip4 As Integer, ) As
Boolean
Declare Function SetSensitivity Lib "dgb.dll" (ByVal HighNorm As Integer)
As Boolean
The first declaration states that the function InitDgb has no
parameters, is found in dgb.dll and returns a boolean value. The second
states that SetOutputs has one integer parameter passed by value rather
than reference, is found in dgb.dll and also returns a boolean value. The
third state that ReadInputs has one integer parameter passed by reference,
is found in dgb.dll and, again, returns a boolean value. The fourth
passes 4 values by reference and the fifth by value etc....
It should be noted that the
Lib "dgb.dll"
lets the program know where to find the dgb.dll file. When written like
this it assumes, since there is no path information, that the dgb.dll file
can be found in the windows system directory ( c:\windows\system32 ) If
you like you can copy the file dgb.dll on the installation disk to the
system32 directory and the above statement will work perfectly.
Alternatively you can copy the file to some other location and give that
location in the declaration as in the example below...
Declare Function InitDgb Lib "c:\mylibrary\dgb.dll"
() As Boolean
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
To speed up your development of software for the Digi-Bee+ a complete
working example is available for download below. It is called VBdigi and
creates a very simple form based program that has individual buttons for
various functions such as initialising the Digi-Bee+ and setting various
patterns on the outputs. The main (and only) screen is shown on the right.
This has been written using Microsoft Visual Studio .net and the download
files contain the full workspace (solution) details to allow you to
immediately open and start editing or running this application.
Even if you don't have Visual Studio, the source code is virtually
self explanatory with the most relevant sections being in "Form1.vb" which
can even be opened in a simple text editor such as notepad. The files are
zipped for convenience and may be downloaded by right clicking on the link
below and choosing "save target as" to begin the download.
|
|
| |
|
|
|
|
|
|
|
|
|
| |
|
|
Download Files |
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
.
. |
|
| |
©
Copyright pc-control.co.uk 2008 |
|
| |
|
|