HIDmaker FS's generated Delphi code contains several files that define the objects that are used by the HIDmaker Software Framework.

File OneHdVar.pas defines the object that represents one HIDmaker USB transfer variable. Each of these objects gets the name that you gave the data item in HIDmaker's Visual Data Designer. If you defined a data item called Pot1, so your PC could read a pot on your board, then your PIC peripheral code will have a simple variable called Pot1, and your Delphi code will have an object called Pot1.

The file that defines the object that represents your whole device has a name that changes with each new HIDmaker project, but its naming convention is <project name>_L.pas . In the case of our Varty16 project, this file is named Varty16_L.pas. You supply a name for this class on the second page of the HIDmaker FS wizard. In the generated Delphi code for our Varty16 project, the class name for the whole device is TVariety1.

The file for the main module or main window of the project also has a different name for each project, but its naming convention is <project name>_M.pas . For our Varty16 project, this file is named Varty16_M.pas . The main module defines an array of objects named Variety1. It has an array, so that you can connect more than one of your devices to the same PC at the same time. (We know of no other USB solution that has this automatic capability.)

The HIDagent object does not change from one project to the next. You install it into your Delphi IDE before you load your first HIDmaker FS project.

 

Comment block showing what's in each report

At the top of the file that represents your whole device, Varty16_L.pas in this case, there is a custom generated comment block that contains a convenient summary of the HID data Reports used in this particular project. Here is what it looks like for the Varty16 project:

 

//*****************************************************************************
//
// The following constants are generated automatically from the design data
// that was created in HIDmaker(R). These constants describe the data items
// (variables), and the HID reports that will contain them, in a way that
// makes it possible for this object to automatically BIND them to properties
// and functions that are a lot easier to use in your programs than the normal
// HID system calls.
//
// This HID has the following Reports, which contain the following variables:
//
// InputRptA :
// In8bit - 8 bit simple variable
// In9bit - 9 bit simple variable
// In16bitArray - array variable, 15 elements, each 16 bits long
// In13bit - 13 bit simple variable
// In5bit - 5 bit simple variable
// In8bitArray - array variable, 63 elements, each 8 bits long
// You may read simple variables by first calling function ReadInputRptA to get
// the report from the device, then accessing properties like In8bit.UnscaledValue etc.
// For array variables, see the special instructions below.
//
// OutputRptB :
// Out8bitArray - array variable, 6 elements, each 8 bits long
// Out7bit - 7 bit simple variable
// You may write these variables by first setting the properties In8bit etc.,
// then calling function WriteOutputRptB to send the data to the device.
// For array variables, see the special instructions below.
//
//
// This class also provides methods like ReadAllInputRpts and
// WriteAllOutputRpts to simultaneously update all reports of each time
// with a single function call.
//
//*****************************************************************************

 

 

Data item declarations

The HIDmaker USB transfer variables that you have defined for this project when you ran HIDmaker's Visual Page Designer are declared as public (but not published) properties of the object that represents your whole device.

Here is what those declarations look like in file Varty16_L.pas:

 

// Properties which will be bound to data items in this device
property In9bit : TOneHidVar read FIn9bit write FIn9bit;
property In16bitArray : TOneHidVar read FIn16bitArray write FIn16bitArray;
property In13bit : TOneHidVar read FIn13bit write FIn13bit;
property In8bit : TOneHidVar read FIn8bit write FIn8bit;
property In5bit : TOneHidVar read FIn5bit write FIn5bit;
property Out8bitArray : TOneHidVar read FOut8bitArray write FOut8bitArray;
property In8bitArray : TOneHidVar read FIn8bitArray write FIn8bitArray;
property Out7bit : TOneHidVar read FOut7bit write FOut7bit;

 

 

Routines for reading and writing Reports

File Varty16_L.pas defines several routines (methods) for reading and writing Reports. Here are the declarations for these methods"

 

{ Reads all Input and Feature reports of this HID }
function ReadAllReports : Boolean;
{ Project dependent: Writes all Output and Feature reports of this HID }
function WriteAllReports : Boolean;
{ Reads an Input report } function ReadInputRptA : Boolean;
{ Writes an Output report } function WriteOutputRptB : Boolean;

 

In a Composite Device project that contains more than one USB Interface, there may be more ReadInputRptX() and WriteOutputRptX() functions, and there may be additional functions for reading and writing Feature Reports if they are used in your project.

Whatever Report types your project may contain, ReadAllReports( ) will read all the Input and Feature reports that are present. Similarly, WriteAllReports( ) will write all Output and Feature reports that are present in your project, using whatever values have already been set in all the data variables.

 

Event Handlers for Device Connection and Disconnection

The main module of your HIDmaker FS PC source code, Varty16_M.pas in the case of the Delphi code for our Varty16 project, also contains event handlers that notify the program when any USB HID device is connected, opened, closed, or disconnected. These are used to handle surprise connections and disconnections of your device.

 

 

SendRptsBtnClick( ) handler for Output Data

There are two main routines, located at the end of the main module source code, that show you how to send and receive data from your PC program to your device. These two routines are the main parts of the generated code that you will want to modify first.

The SendRptsBtnClick( ) button is called when you click the "Send All Reports" button. As the routine was generated, it sends test values to your device, though you can uncomment some lines to cause it to send out random, changing values each time instead.

Here is what the SendRptsBtnClick( ) routine looks like in the Delphi code for our Varty16 project:

 

procedure TMainForm.SendRptsBtnClick(Sender: TObject);

var

AStr : String;

DevNum, i : Integer;

MaxVal : Integer;

begin

for DevNum := 0 to MyDevList.Count - 1 do

begin

AddToResults(nil,'');

AStr := 'Device #' + IntToStr(DevNum) + ':';

AddToResults(nil,AStr);

 

for i := 0 to 5 do

begin

Variety1[DevNum].Out8bitArray_Array[i] := i;

// Activate next 2 lines to send random values

//MaxVal := 127;

//Variety1[DevNum].Out8bitArray_Array[i] := Trunc(Random(MaxVal+1));

AStr := Format(' Writing to device: Out8bitArray_Array[%d] = 0x%x',[i, Variety1[DevNum].Out8bitArray_Array[i]]);

AddToResults(nil,AStr);

end;

 

 

Variety1[DevNum].Out7bit.UnScaledValue := 19;

// Activate next 2 lines to send random values

//MaxVal := 63;

//Variety1[DevNum].Out7bit.UnScaledValue := Trunc(Random(MaxVal+1));

AStr := Format(' Wrote to device: Out7bit = 0x%x',[Variety1[DevNum].Out7bit.UnscaledValue]);

AddToResults(nil,AStr);

if Variety1[DevNum].WriteAllReports then

begin

// Success

AddToResults(nil,' Successfully wrote to device');

end

else

begin

// Failure

Beep;

AStr := '+ + + Unable to send to device'+ IntToStr(DevNum) + '! + + +';

AddToResults(nil,AStr);

end;

end;

end;

 

 

You will want to customize the generated PC code to let your users change data they want to send to your device, by clicking, dragging, or typing on Windows controls on your main window. HIDmaker's generated code is designed to make it easy for you to do that, as long as you are able to do a liitle bit of PC programming with the modern "visual" compilers that we support.

 

Example: How to light an LED

 

 

 

ReadRptsBtnClick( ) handler for Input Data

The other of the two routines you will want to modify first reads all Input and Feature Reports that may be present in your device, and displays the values in simple text format.

Here is what the SendRptsBtnClick( ) routine looks like in the Delphi code for our Varty16 project:

 

procedure TMainForm.ReadRptsBtnClick(Sender: TObject);

var

AStr : String;

DevNum, i : Integer;

begin

ClearBtnClick(nil);

for DevNum := 0 to MyDevList.Count - 1 do

begin

//

AddToResults(nil,'');

AStr := 'Device #' + IntToStr(DevNum) + ':';

AddToResults(nil,AStr);

if Variety1[DevNum].ReadAllReports then

begin

AStr := ' In9bit = ' + IntToStr(Variety1[DevNum].In9bit.UnScaledValue);

AddToResults(nil,AStr);

 

// Read array of values Variety1[DevNum].In16bitArray_Array[15]

for i := 0 to 14 do

begin

AStr := ' In16bitArray_Array[' + IntToStr(i) +'] = ' + IntToStr(Variety1[DevNum].In16bitArray_Array[i]);

AddToResults(nil,AStr);

end;

 

AStr := ' In13bit = ' + IntToStr(Variety1[DevNum].In13bit.UnScaledValue);

AddToResults(nil,AStr);

 

AStr := ' In8bit = ' + IntToStr(Variety1[DevNum].In8bit.UnScaledValue);

AddToResults(nil,AStr);

 

AStr := ' In5bit = ' + IntToStr(Variety1[DevNum].In5bit.UnScaledValue);

AddToResults(nil,AStr);

 

// Read array of values Variety1[DevNum].In8bitArray_Array[63]

for i := 0 to 62 do

begin

AStr := ' In8bitArray_Array[' + IntToStr(i) +'] = ' + IntToStr(Variety1[DevNum].In8bitArray_Array[i]);

AddToResults(nil,AStr);

end;

 

end

else

begin

Beep;

AStr := '+ + + Unable to read from device '+ IntToStr(DevNum) + '! + + +';

AddToResults(nil,AStr);

end;

end;

end;

 

You will want to customize the generated PC code to display or store the data that you read from your device. HIDmaker's generated code is designed to make it easy for you to do that, as long as you are able to do a liitle bit of PC programming with the modern "visual" compilers that we support.

 

 

Example: How to read a pot and a button

 

 

 

What you do differently for Feature Report Items

In a word: "Nothing." If your project contains any Feature Reports, then the custom generated object that represents your whole device will contain a few new routines, perhaps called ReadFeatureRptC( ) and SendFeatureRptD( ), for reading and writing these feature Reports.

If you want to call these Feature Report routines individually, you can use the same sort of code as you would for Input and Output reports, examples of which are shown above.

But you don't even have to do that much if you don't want to. Just calling ReadAllReports( ) as the code we showed above does, will automatically call your new ReadFeatureRptC( ) routine in addition to calling ReadInputRptA( ). In the same way, just continuing to call SendAllReports( ) as shown above will make sure that your new feature reports get sent as well.