Can i use the HID-library with the uno r4 minima?

Hi im relativly new to arduino and wanted to know if i can use the HID-Project library (https://www.arduino.cc/reference/en/libraries/hid-project/) with my uno r4 minima because when i try to upload my code it says "Unsupported architecture"

So that will be a no then. There is probably a list in that library that checks for what sorts of processors are capable of running that library and it doesn't mention the r4.

It might me a matter of changing that list, but I can see from other posts that while the R4 is capable of doing this, no one has figured out how to do it yet.

2 Likes

ok thanks, do you have an idea how to make this code work then? i guess it wont work with the uno r3

#include <HID-Project.h>

const int joyXPin = A0;
const int joyYPin = A1;
const int buttonPin = 2;

void setup() {
  Joystick.begin();
  pinMode(buttonPin, INPUT);
}

void loop() {
  int joyXValue = analogRead(joyXPin);
  int joyYValue = analogRead(joyYPin);
  int buttonState = digitalRead(buttonPin);
  
  int xCoord = map(joyXValue, 0, 1023, -100, 100);
  int yCoord = map(joyYValue, 0, 1023, -100, 100);

  // Bewege die Maus basierend auf den Joystick-Koordinaten
  Joystick.X(xCoord);
  Joystick.Y(yCoord);

  // Senden eines Mausklicks, wenn der ButtonState 1 ist
  if (buttonState == 1) {
    Joystick.pressButton(1); // Linke Maustaste drücken
  } else {
    Joystick.releaseButton(1); // Linke Maustaste loslassen
  }

  // Kurze Pause zwischen den Schleifen, um die Bewegung flüssiger zu gestalten
  delay(10);
}

sorry that comments are in german.

Actually it says on the arduino website

  • HID support: The UNO R4 Minima comes with built-in HID (Human Interface Device) support, enabling it to simulate a mouse or keyboard when connected to a computer via a USB cable. This convenient feature makes it a breeze to send keystrokes and mouse movements to a computer, enhancing usability and functionality.

so i guess the library isn't needed, so there must be some other way of doing it. Is there anything in the board specific examples ?

btw i think this should be in a different sub-section of the forum about the new arduino boards

That library does more than just providing a HID. It e.g. can indicate on the Arduino that the CAPS lock key is active or not.

3 Likes

I agree so, I have moved it.

Shouldn't it be under the MINIMA instead of WIFI?

The title of the thread is can I use HID... with the uno r4 Minima?

I keep meaning to look, at the HID support. Does it support Joystick? That is what he is trying to use.

Or maybe I am just a little grumpy this morning :wink:

1 Like

I had a quick look, i think it does but i am no expert on the matter. There are however standard examples included in the core.
So since the specified library isn't working the logical place to start would be there.

There is some related discussion you might find interesting here:

It is about the UNO R4 WiFi, but I think it should be relevant for the UNO R4 Minima as well. A joystick library is provided at the end of that thread. I just gave it a quick test on both my UNO R4 WiFi and Minima boards and unfortunately it is not working for me. I used this simple sketch:

#include "JoystickWin.h"  // https://forum.arduino.cc/t/unojoy-on-r4-card/1162272/11

void setup() {
  JoystickWin.begin();
}

void loop() {
  JoystickWin.x(200);
  JoystickWin.press(1);
  JoystickWin.sendReport();
  delay(500);

  JoystickWin.x(0);
  JoystickWin.release(1);
  JoystickWin.sendReport();
  delay(500);
}

The board is recognized as a game controller after uploading the sketch, but I don't see the expected X axis movement and button press when I check using the Windows "Game Controller" test utility.

The following is the code I tested with after some clean up. I do not have access to my Windows PC right now.

#include "JoystickWin.h"

void setup() {
  Serial.begin(115200);
  while (!Serial && (millis() < 3000)) delay(10);
  Serial.print("sizeof JoystickWin_Report_t: ");
  Serial.println(sizeof(JoystickWin_Report_t));
  JoystickWin.begin();
}

void loop() {
  static uint8_t count = 0;
  if (count >= BUTTONS_MAX) {
    JoystickWin.releaseAll();
    count = 0;
  }
  JoystickWin.press(count);
  count++;
  Serial.print("button ");
  Serial.println(count);

  // Go through all hat switch directions
  static uint8_t dir = 0;
  JoystickWin.hat1(dir++);
  JoystickWin.hat2(dir++);
  if (dir > 7)
    dir = 0;

  // Move axes to random positions
  JoystickWin.x(random(AXIS_MAX + 1));
  JoystickWin.y(random(AXIS_MAX + 1));
  JoystickWin.z(random(AXIS_MAX + 1));
  JoystickWin.rx(random(AXIS_MAX + 1));
  JoystickWin.ry(random(AXIS_MAX + 1));
  JoystickWin.rz(random(AXIS_MAX + 1));
  JoystickWin.rudder(random(AXIS_MAX + 1));
  JoystickWin.throttle(random(AXIS_MAX + 1));
  JoystickWin.accelerator(random(AXIS_MAX + 1));
  JoystickWin.brake(random(AXIS_MAX + 1));
  JoystickWin.steering(random(AXIS_MAX + 1));

  // The above updates values in RAM. The following
  // sends the values to the USB host.
  JoystickWin.sendReport();

  delay(100);
}

@ptillisch
In the short program I found it necessary to add lines to setup() to open Serial. I have no idea why this is necessary. I was saved in my longer test program by my obsession to make sure C structs are the same correct size across AVR and ARM arches.

void setup() {
  Serial.begin(115200);
  while (!Serial && (millis() < 3000)) delay(10);
  JoystickWin.begin();
}

JoystickWin works on Micro (same processor as Pro Micro and Leonard), Due, Nano 33 IoT (SAMD21), and Uno R4 WiFi. It does not work on mbed boards such as Giga R1 because mbed USB works differently. It does not work on boards without an integrated USB controller such as Uno and Mega but that is what UnoJoy is for. I will post JoystickWin with a verified working test program on github soon.

2 Likes

Hello everyone! We created a public uno-r4-library-compatibility repository. @customcontroller I added JoystickWin here

Are you successfully using it on a Linux machine? I made an attempt to install that jstest utility from your screenshot (which looks very useful) on my Linux machine but I had to install flatpak/flathub for that purpose and for some reason it just completely hangs my machine when I try to do that so I finally gave up.

Thanks! My sketch also started working once I added that. I investigated further and found it was not actually about the Serial initialization but instead the fact that the code introduced a delay before the JoystickWin.begin() call. The sketch continues to work even if I replace the Serial code with this:

delay(500);

But not if I reduce the delay to 100 ms. I don't have any idea why the delay is necessary.

Thanks!

I will have to try it out.

I just cloned it to my Windows machine (github desktop).

And compiled the example:

Quick note: I am seeing several compiler warnings. Some of them in the library HID, but some of them in the new library.

Like:

C:\Users\kurte\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.4\cores\arduino/api/Common.h:43:74: note: in definition of macro 'constrain'
 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
                                                                          ^~~
c:\Users\kurte\Documents\Arduino\libraries\JoystickWin\JoystickWin.cpp: In member function 'void JoystickWin_::hat2(int)':
c:\Users\kurte\Documents\Arduino\libraries\JoystickWin\JoystickWin.cpp:133:22: warning: second operand of conditional expression has no effect [-Wunused-value]
   constrain(d, 0, 15);

I believe there is a problem in the usage of constrain.
That is I believe for example:

void JoystickWin_::hat2(int d) {
  constrain(d, 0, 15);
  _hid_report.hat2 = d;
}

Should be:

void JoystickWin_::hat2(int d) {
  d = constrain(d, 0, 15);
  _hid_report.hat2 = d;
}

That is the constrain macro returns the constrained value.

But looks like it works on my PC:

Will boot up my Ubuntu machine in a bit and see if it works. Been a long time since I have tried JSTest.

Update:

From my Ubuntu 22.04 machine.

Was able to run the command line version of jsttest
by doing:

sudo apt install joystick
jstest /dev/input/js0

Was able to run the graphical version:

sudo apt install jstest-gtk
jstest-gtk

Or launch it from the applications.

I see now this is answered in the library readme:

https://github.com/controllercustom/JoystickWin#readme

The following joystick class works when plugged into a computer running Windows 11 and a computer running Ubuntu Linux.

I got the impression it was only compatible with Windows due to the "Win" in the library name and the "for Windows" in the description. I'm glad if it can provide cross-platform support.

Wondering if it would be possible to be able to do something like:

while (!Joystick && millis() < 4000) {}

I am pretty sure that is not currently possible, as I did not see any operator ... defined in the class.

But in addition to that my quick look through it and the HID library, it is unclear to me if/how you could detect if the USB connected or not?

I wonder if it Is reasonably doable?

Lots of stuff, I still need to learn about the Arduino HID library. Like can I retrieve the LED states from the host? ...

Yes, I use jstest-gkt on a PC running Ubuntu 22.04. I installed using sudo apt install.

jstest-gtk/jammy,now 0.1.1~git20160825-4 amd64 [installed]
  joystick testing and configuration tool

I suspect the additional delay is needed to make sure USB enumeration is complete before writing HID reports.

Thanks for confirming it work on Uno R4 Minima. I do not have that board.