RAWHID via HID-Project on Leonardo

I have connected a device to an Arduino Leonardo to experiment with RAW-HID.

I can send HID to the device fine, and it seems like I can receive it from the device fine too, as the LED blinks as it should in the program, but I don't seen to be getting anything out in the serial ports (TX-pin1 and RX-pin 0).

Should this program be outputting the received bytes to the TX port (pin 1)?

I am monitoring TX (pin 1) with a known functional serial to USB device which is also connected to GND and RX. TX from Arduino to RX on Serial->USB device of course.

Here is the program:

/*
  Copyright (c) 2014-2015 NicoHood
  See the readme for credit to other people.

  Advanced RawHID example

  Shows how to send bytes via RawHID.
  Press a button to send some example values.

  Every received data is mirrored to the host via Serial.

  See HID Project documentation for more information.
  https://github.com/NicoHood/HID/wiki/RawHID-API
*/

#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
const int pinButton = 2;
int LGR[] = {240,62,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};

// Buffer to hold RawHID data.
// If host tries to send more data than this,
// it will respond with an error.
// If the data is not read until the host sends the next data
// it will also respond with an error and the data will be lost.
uint8_t rawhidData[255];

void setup() {
  pinMode(pinLed, OUTPUT);
  pinMode(pinButton, INPUT_PULLUP);

  Serial.begin(9600);

  // Set the RawHID OUT report array.
  // Feature reports are also (parallel) possible, see the other example for this.
  RawHID.begin(rawhidData, sizeof(rawhidData));
}

void loop() {
  // Send data to the host
  if (!digitalRead(pinButton)) {
    digitalWrite(pinLed, HIGH);

    // Create buffer with numbers and send it
    uint8_t megabuff[64];
    for (uint8_t i = 0; i < sizeof(megabuff); i++) {
      megabuff[i] = LGR[i];
    }
    RawHID.write(megabuff, sizeof(megabuff));

    // Simple debounce
    delay(30);
    digitalWrite(pinLed, LOW);
  }


  // Check if there is new data from the RawHID device
  auto bytesAvailable = RawHID.available();
  if (bytesAvailable)
  {
    digitalWrite(pinLed, HIGH);
    delay(500);
    digitalWrite(pinLed, LOW);
    delay(500);
    digitalWrite(pinLed, HIGH);
        delay(500);
    digitalWrite(pinLed, LOW);
        delay(500);
    digitalWrite(pinLed, HIGH);

    // Mirror data via Serial
    while (bytesAvailable--) {
      Serial.write(RawHID.read());
      delay(20);
    }

    digitalWrite(pinLed, LOW);
  }
}

The light stays on for a little while, but the serial_>USB device doesn't show an RX LED nor does the PC Terminal program receive anything.

Should the serial data be being sent through TX?

That would be Serial1 on an Ardunio Leonardo.
Serial is routed via USB directly.

1 Like

It is!! Perfect. Thank you :-).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.