Lazarus

Programming => General => Topic started by: folkeu08 on April 08, 2020, 01:44:49 am

Title: USB HID
Post by: folkeu08 on April 08, 2020, 01:44:49 am
Hi,
I would like to interact with electronics in USB HID mode so as not to have to install drivers under Windows.
The only third party component found that allows this without additional dll is this: https://www.winsoft.sk/nathid.htm (https://www.winsoft.sk/nathid.htm)
I mounted the electronic board of this site https://www.roboticus.org/electronique/usb/12-carte-dinterface-sur-port-usb-protocole-hid-part-2.html (https://www.roboticus.org/electronique/usb/12-carte-dinterface-sur-port-usb-protocole-hid-part-2.html) to make my hand to the USB and installed the firmware in the PIC. The board is well recognized by the PC.
I try to reproduce the operation of the demo delphi7 software under Lazarus with third party components but I can't do anything good.
I am attaching my cleaned project file.
Thank you
François
Title: Re: USB HID
Post by: folkeu08 on April 09, 2020, 03:16:25 pm
Hi,
The component is buggy. It does not detect the board https://www.microchip.com/Developmenttools/ProductDetails/DM163025
I make my tests with this board.
The author is informed and tries to remedy the problem.
François
Title: Re: USB HID
Post by: DonAlfredo on April 09, 2020, 03:26:06 pm
You might try
https://github.com/LongDirtyAnimAlf/FPC-USB-HID
Title: Re: USB HID
Post by: folkeu08 on April 09, 2020, 05:54:40 pm
Hi DonAlfredo,
I also connected again to give the info because I just found it too.
I compiled and it works.
I must have a problem in my firmware of the pic 18f4550 because it still does not detect it.
Thanks
François
Title: Re: [SOLVED] USB HID
Post by: folkeu08 on April 09, 2020, 06:09:18 pm
Hi,
This post https://forum.lazarus.freepascal.org/index.php?topic=47371.0 (https://forum.lazarus.freepascal.org/index.php?topic=47371.0) indicates a memory problem but if I configure debugging as indicated in the post, the error message when closing the window is no longer present.
François
Title: Re: USB HID
Post by: folkeu08 on April 16, 2020, 02:39:00 pm
Hi,
Is there an example of code to communicate in USB with a Pic18f4550 which will have one:
 * VENDOR_ID = 0x1234
 * PRODUCT_ID = 0x0001
to transmit and receive digital data from 0 to 255?
Even if it's not quite that, I think I should be able to adapt.
Thank you
François
Title: Re: USB HID
Post by: DonAlfredo on April 16, 2020, 03:00:00 pm
I do not know if this is related to the FPC USB HID software, but in usb2.pas are two constants: vendor and product. You can adapt these two to suit your hardware.
Title: Re: USB HID
Post by: folkeu08 on April 17, 2020, 10:49:50 am
Hello,
For the syntax of the send and receive commands, can I rely on the JvHIDController examples from the JVCL library?
I read that your Lazarus component was a rewrite of the latter.
a PIC18f4550 from Microchip (www.microchip.com) is a processor that allows dialogue via USB. It is used a lot in amateur electronics
Thank you
Title: Re: USB HID
Post by: DonAlfredo on April 17, 2020, 02:01:26 pm
The whole library was designed to communicate with Microchip USB HID controllers.
And its in use 24/7 for a very long time now, with many MCU's.

Example use:

Code: Pascal  [Select][+][-]
  1. function TUSB.SetValue(board):boolean;
  2. var
  3.   error:boolean;
  4.   avalue:word;
  5. begin
  6.   avalue:=0;
  7.  
  8.   with AUSBList.Items[board] do
  9.   begin
  10.       FillChar(LocalData, SizeOf(LocalData), 0);
  11.       LocalData.Data[0] := byte(CMD_set_value);
  12.       LocalData.Data[1] := 12345;
  13.     end;
  14.  
  15.     error:=HidReadWrite(AUSBList.Items[board],False);
  16.  
  17.     if (NOT error) then with AUSBList.Items[board].LocalData do
  18.     begin
  19.       if ( data[0]=byte(CMD_set_value) ) then
  20.       begin
  21.         avalue:=(data[3]+data[4]*256);
  22.       end else error:=True;
  23.  
  24.   end;
  25.  
  26.   result:=error;
  27. end;
Title: Re: USB HID
Post by: folkeu08 on June 01, 2020, 07:58:02 pm
Hi,
I found an example in Delphi 7 which uses the TJvHidDevice Controller component. The project is in attached files
In public statements, "MyHidDev: TJvHidDevice;" is indicated.
At the compilation Lazarus said he did not know TJvHidDevice.
What to replace it with ?
It is use un this prosedure :
Code: Pascal  [Select][+][-]
  1. procedure TfmMain.HidCtl1DeviceChange(Sender: TObject);
  2. begin
  3.   if (MyHidDev <> nil) and (not MyHidDev.IsPluggedIn) then
  4.     HidCtl1.CheckIn(MyHidDev);
  5.   if (MyHidDev = nil) then
  6.     HidCtl1.CheckOutById(MyHidDev, Sonelec_VID, Sonelec_PID);
  7.   //bHIDDevConnected := (MyHidDev <> nil);
  8.   MyHidDev := nil;
  9.   Hid_Reset(false);
  10. end;
  11.  


Thanks
Title: Re: USB HID
Post by: DonAlfredo on June 01, 2020, 10:10:43 pm
This is the uses-section of your main form:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  3.   Dialogs, ButtonGroup, StdCtrls, ComCtrls, JvComponentBase,
  4.   JvHidControllerClass, ExtCtrls, rmSwitch, VrControls, VrLeds,
  5.   VrCheckLed, ActnList, Menus, AdvEdit, advlued;

This is definitely not Lazarus. And I am not able to run or test your project with Lazarus.
If you want (my) help, please upload a Lazarus project.
Title: Re: USB HID
Post by: MarkMLl on June 01, 2020, 10:38:50 pm
I am concerned that OP has not taken on board that whatever software he eventually gets running on a PC he will also need matching firmware to run on the PIC.

Telling us that the PIC is 1234:0001 and expecting everything to magically come together is not a good start.

MarkMLl
Title: Re: USB HID
Post by: folkeu08 on June 01, 2020, 11:15:01 pm
Hi,
I started with the Microchip "MCHPUSB" demo project.
I mounted a small hardware card with the pic firmware. The card works because it is detected by the executable also provided by Microchip and I manage to control it in both directions.
I try to reproduce this operation in Lazarus without using a third party DLL with your source.
I'm not used to Lazarus.
The first step is to mount the electronic card in the software and I go around in circles.
I am looking to have the interface detected by the software and to have the state of the InterfaceLabel changed from "Intercave not connected" to "Interface connected".
The  VENDOR_ID = 1240 and PRODUCT_ID = 0005;
The project is attached
Thanks
Title: Re: USB HID
Post by: DonAlfredo on June 02, 2020, 09:41:10 am
The included project (your project with some changes) will show you HID devices when you press connect.
Title: Re: USB HID
Post by: folkeu08 on June 02, 2020, 09:10:49 pm
Hello DonAlfredo,

I managed to detect my interface from your example by going to modify the value of Vendor and Product.
I certainly explained it wrong.
I would like the USB interface to be detected or not detected by the software when the USB plug is inserted or removed from the PC and the information "Connected" or "Not Connected" within the "InterfaceLabel".
Then I should be able to read and send data because the syntax "Data-Read" and "Data_Write" exists.
I don't just want to list the USB interfaces connected to the PC.
Thanks
Title: Re: USB HID
Post by: folkeu08 on June 07, 2020, 01:57:10 pm
Hello,
Is what I am trying to do achievable at least?
I'm still looking and can't find the solution.
Thanks
Title: Re: USB HID
Post by: DonAlfredo on June 07, 2020, 02:24:04 pm
Ok. Look into usb2.pas.

Code: Pascal  [Select][+][-]
  1.   Vendor                        = $04d8;
  2.   Product                       = $000b;
Changes these to suit your hardware.

procedure TUSB.DeviceChange(Sender:TObject);

This procedure gets called when HID hardware is added or removed.
You can see inside this procedure that HID-Device Vendor and Product are checked against the above values.
If match, then DeviceArrival(HidDev) will be called.

procedure TUSB.DeviceArrival(HidDev: TJvHidDevice);

This procedure add a new controller:
      NewUSBController := TUSBController.Create(HidDev);
And  this controller gets added into the lists of USB devices:
      AUSBList.Items[newboard]:=NewUSBController;

function TUSB.HidReadWrite(Ctrl: TUSBController; ReadOnly:boolean):boolean;

Use this function to read and/or write towards your USB HID Device.
(in this example, I do send some calibration data towards the HID MicroChip controller.)

Code: Pascal  [Select][+][-]
  1.     with AUSBList.Items[board] do
  2.     begin
  3.  
  4.       FillChar(LocalData, SizeOf(LocalData), 0);
  5.  
  6.       LocalData.Data[0] := byte(CMD_set_cal);
  7.       LocalData.Data[1] := position-1;
  8.       LocalData.Data[2] := 7;
  9.       LocalData.Data[3] := whattocal;
  10.  
  11.       temp:=calval;
  12.       LocalData.Data[4] :=(temp MOD 256);
  13.       temp:=(temp DIV 256);
  14.       LocalData.Data[5] :=(temp MOD 256);
  15.  
  16.       LocalData.Data[6] := DayOfTheMonth(Now);
  17.       LocalData.Data[7] := MonthOfTheYear(Now);
  18.       LocalData.Data[8] := (YearOf(Now) DIV 256);
  19.       LocalData.Data[9] := (YearOf(Now) MOD 256);
  20.  
  21.     end;
  22.  
  23.     error:=HidReadWrite(AUSBList.Items[board],False);
  24.  
  25.     if (NOT error) then with AUSBList.Items[board].LocalData do
  26.     begin
  27.       if ( data[0]=byte(CMD_set_cal) ) AND ( data[1]=(position-1) ) then
  28.       begin
  29.         //do something with the data received
  30.       end else error:=True;
  31.     end;
  32.  

My setup to show you why this is done like described above:
I have 20+MicroChip USB MCU connected to a single PC through USB.
These are all added into the list. And always have the same [board]number due to their unique HID serial number.
Each of these MCU connect through I2C towards another 20 MCU measuring devices. Totalling 400 connected MCU by USB onto a single PC. Running 24/7 for 6 months.
IMHO, USB is very good and stable and can be used just like a RS485 network on steroids.
Title: Re: USB HID
Post by: folkeu08 on June 07, 2020, 11:59:17 pm
Good evening,
I'm looking at this this week or the next W-E.
With this help and the next novices like me, I will create a tutorial that you can add to the component.
Proteus (https://www.labcenter.com/usb/) allows a 100% virtual simulation with the examples in the demo version of the software. With that, I will be able to create this tutorial for beginners who do not have a PCB at pic18f4550.
I went to the wrong directions because I was trying with TUSB and MyHID.
François
Title: Re: USB HID
Post by: folkeu08 on August 17, 2020, 08:57:49 am
Hi all,
I suppose it is this part of code in the USB2 unit that identifies whether the hard USB is connected or not
Code: Pascal  [Select][+][-]
  1. if HidDev.IsPluggedIn AND NOT HidDev.IsCheckedOut then
  2.       begin
  3.         AddInfo('New device that has not been checked out.');
  4.         DeviceArrival(HidDev);
  5.       end;
  6.       if NOT HidDev.IsPluggedIn AND HidDev.IsCheckedOut then
  7.       begin
  8.         AddInfo('Checkedout device that has been unplugged.');
  9.         DeviceRemoval(HidDev);
  10.       end;                                      
  11.  

I took this piece of code to insert it in my Main_Form by modifying it as follows :
Code: Pascal  [Select][+][-]
  1. if HidDev.IsPluggedIn AND NOT HidDev.IsCheckedOut then
  2.       begin
  3.        Detection_Label.Caption := Detected';
  4.      end;
  5.      if NOT HidDev.IsPluggedIn AND HidDev.IsCheckedOut then
  6.      begin
  7.       Detection_Label.Caption := 'Not Detected';
  8.      end;                                      
  9.  

At compilation, I was told that the HidDev identifier was not found.  >:(
@+
Title: Re: USB HID
Post by: folkeu08 on August 19, 2020, 06:27:30 pm
Hi all,
I am basically an electronics engineer and not a computer scientist. I would like to be able to dialogue with a PC by the usb.
I have been looking for how to do it since April and I am blocked by the lack of knowledge and my inexperience. I do not despair of getting there.
I do not know if I am doing it well but I can already start the application to modify the label 'Detection_label' if the card is connected or not. For that I added in the procedure procedure (in USB2) "TUSB.DeviceChange (Sender: TObject);" a variable whose string is different if the hard is present or not.
My difficulty is to modify the label if the card is added or removed while the software is running.
does anyone have a lead?
I have set a timer but even that does nothing.

My project is in link because too big to be attached to the post : http://www.club.ac-news.fr/download/USBHID_Test.rar (http://www.club.ac-news.fr/download/USBHID_Test.rar)
My third-party components are attached to the post
Thanks
François
Title: Re: USB HID
Post by: avra on August 19, 2020, 07:58:54 pm
I am basically an electronics engineer and not a computer scientist. I would like to be able to dialogue with a PC by the usb.
I have been looking for how to do it since April and I am blocked by the lack of knowledge and my inexperience.
Is there a project requirement to use USB HID? If not, and you just want a 2-way communication between microcontroller and pc then you can save your self a lot of trouble if you use simple serial communication like explained here:
https://wiki.freepascal.org/Arduino#Serial_communication

Right now I am in the middle of the STM32 project with composite USB CDC, HID MOUSE, HID KEYBOARD and HID JOYSTICK, and it is many, many times more complex then the link above, so I would not recommend it to a newbie. Above example can get you started in half and hour if you already have a working environment.

You might also want to take a look at this blog:
https://bigdanzblog.wordpress.com/?s=arduino+lazarus
Title: Re: USB HID
Post by: MarkMLl on August 19, 2020, 11:01:13 pm
Is there a project requirement to use USB HID? If not, and you just want a 2-way communication between microcontroller and pc then you can save your self a lot of trouble if you use simple serial communication

I agree.

When I did /my/ electronics degree we were expected to be able to understand arbitrary technical documentation.

MarkMLl
Title: Re: USB HID
Post by: folkeu08 on August 19, 2020, 11:42:33 pm
Hi,
I specialize in analog (audio). I do my hand with the PICs to control audio switching by software.
Hence my difficulties due to a lack of digital experience
Title: Re: USB HID
Post by: MarkMLl on August 21, 2020, 06:08:16 pm
Well, good analogue guys are hard to find.

Have you investigated DonAlfredo's suggestion of June 7th?

Do you have a pointer to a source of this development kit (or whatever it is) that you're using, so we can see what it is?

The only PIC I've interfaced to was a Velleman kit, but World+dog uses Arduino-style devices in the way suggested by Avra.

Do you appreciate that HID is quite a complex standard sitting on top of USB (etc.), and that one of the things it does is generate a descriptor that gives the OS etc. some of its functional details? Not sure how you'd get at that on Windows... on Linux it's saved in the /sys tree.

MarkMLl
Title: Re: USB HID
Post by: avra on August 21, 2020, 09:18:41 pm
I do my hand with the PICs to control audio switching by software.
So why don't you try simple serial with PIC like here:
https://simple-circuit.com/mplab-xc8-uart-example-pic-mcu/
and try to adapt it to work synapse serial example I showed?

You just need FT232 or similar serial to usb module as shown in the link (if your PIC model does not already come with USB interface and implement CDC).

If you insist on HID then take a look at these:
https://github.com/JoshyFun/VUSBRelayPascal
https://github.com/Zaaphod/HIDAPI.pas
https://github.com/Zaaphod/pas-libusb
https://github.com/Zaaphod/libusbxhid
https://mcuprogramming.blogspot.com/2010/12/example-in-lazarus-for-libusb.html
https://mcuprogramming.blogspot.com/2010/12/libusb-10-wrapper-for-fpc.html?q=usb
Title: Re: USB HID
Post by: Lulu on August 26, 2020, 06:54:46 pm
Hi, until now I use PIC micro-controller + FT232 circuit like Avra says. But FT232 is very expensive compared to PIC16F1454.
I am interested by the PIC16F1454 and after some research I found this:
https://github.com/jgeisler0303/PIC16F1454_USB2Serial (https://github.com/jgeisler0303/PIC16F1454_USB2Serial)
I have never used this project before so I can't tell you if it work and how to modify it.
Title: Re: USB HID
Post by: MarkMLl on August 26, 2020, 07:27:57 pm
In that case use something cheaper like the CH340. Or learn how to interpret the existing datasheets for the PIC etc., and the relevant libraries.

It occurs to me that since OP basically wants something that allows him to ackle a few GPIO bits under USB control, it might be worth looking for a chip that does that off the shelf rather than messing about with a PIC or whatever. Apart from anything else, that would presumably mean that the rest of us would be able to source exactly the device that he was using, and that there could be other people who'd find it useful which would give them an incentive to help out.

The article referenced below is oriented towards Linux, I think that I recall OP implying that he used Windows and I'd expect somebody to have an appropriate driver.

https://hackaday.com/2018/02/21/linux-adds-ch341-gpio/

MarkMLl
Title: Re: USB HID
Post by: MarkMLl on September 26, 2020, 10:20:40 pm
I thought I'd post an update in case it was useful to anybody.

I've now got a CH341 board as sold as item 401298217576 on eBay, it cost me £7.34 but the basic chip is much cheaper. This has a jumper which controls whether it appears as a serial port (supported by the Linux kernel) or an I2C+GPIO+etc. port, that has the effect of changing the USB PID but discussion elsewhere suggests that that is the only change (i.e. within limits a suitable driver might be able to "mix'n'match" the capabilities it wanted).

I can't speak for Windows, but on Linux the driver from https://github.com/gschorcht/i2c-ch341-usb compiles and loads without problem, resulting in individually-controllable GPIO bits in /sys/class/gpio. I've not explored the chip's I2C/SPI capabilities, but I've used the Linux kernel to access those from Pascal code on an RPi without problems in the past.

At present the driver is "out of tree", but on Debian derivatives it is apparently possible to integrate it using DKMS... I've not tried this yet but have found the mechanism a "no brainer" in the past for things like ZFS.

For somebody who simply needs to access a few control bits, I'd suggest that this is a viable alternative to an Arduino or some other microcontroller since it doesn't rely on a program that has to be maintained and downloaded onto the slave device.

MarkMLl




Title: Re: USB HID
Post by: folkeu08 on September 26, 2020, 11:10:00 pm
@MarkMLl

I have taken note of your map for future developments.
My hardware is already developed and it only remains for me this problem of USB dialogue to be resolved with Lazarus and the card.
My project is to make an FM transmitter controllable by a USB port.
The information is displayed by 6 7-segment displays. The following version will be displayed by an LCD, then by a graphic touchscreen LCD.
Your card has too few ports for this project.
In addition, I do not wish to limit myself to software under Linux.
I continue my research how to make this dialogue
Francois
Title: Re: USB HID
Post by: folkeu08 on October 10, 2020, 02:25:07 pm
Hi,
I have some free time today, I will try new things.
@DonAlfredo: If I put a timer and every second I run a refresh, it will allow me to display in a label if the hard usb interface is connected or not ?
Thanks
Title: Re: USB HID
Post by: DonAlfredo on October 10, 2020, 05:29:58 pm
For sure you should be able to check the connection status. The Hid- controller and Hid-dev have suitable properties.
TinyPortal © 2005-2018